require File.expand_path(File.dirname(__FILE__)) + '/helpers.rb' context "a resource set" do setup do Module.new do extend Ray::ResourceSet def self.missing_pattern(*args) args end add_set(/^string:(.+)$/) do |str| str end end end asserts "uses the block it was passed to create objects" do topic["string:test"] == "test" end asserts "always returns the same object" do topic["string:my string"].equal? topic["string:my string"] end asserts "uses the missing_pattern method when a string isn't matched" do topic["Hello world"] == ["Hello world"] end asserts("using it with another arguments") { topic["foo", 3] }.raises(ArgumentError) asserts "can select objects from cache" do first, sec = topic["string:first"], topic["string:sec"] topic.select! { |key, val| key == "string:first" } first.equal?(topic["string:first"]) && !sec.equal?(topic["string:sec"]) end asserts "can reject objects from cache" do first, sec = topic["string:first"], topic["string:sec"] topic.reject! { |key, val| key == "string:first" } !first.equal?(topic["string:first"]) && sec.equal?(topic["string:sec"]) end end context "a resource set without missing_pattern" do setup do Module.new { extend Ray::ResourceSet } end asserts("using an unmatched string") { topic["anything"] }.raises(Ray::NoPatternError) end context "a resource set with more arugments" do setup do Module.new do extend Ray::ResourceSet need_argument_count 1 def self.missing_pattern(*args) args end add_set(/^string:(.+)$/) do |str, times| str * times end end end denies("using it with more arguments") { topic["string:foo", 3] }.raises(StandardError) asserts "provides user arguments after captures" do res = [] topic.add_set(/my_(.)/) do |char, times| res << char res << times true end topic["my_a", 3] res == ["a", 3] end asserts "passes all the arguments to missing_pattern" do topic["a", 3] == ["a", 3] end end run_tests if __FILE__ == $0