lib/wildcard_matchers.rb in wildcard_matchers-0.0.4 vs lib/wildcard_matchers.rb in wildcard_matchers-0.1.0
- old
+ new
@@ -1,72 +1,20 @@
-require "wildcard_matchers/helpers"
-require "wildcard_matchers/matchers"
-
module WildcardMatchers
+ autoload :Helpers, "wildcard_matchers/helpers"
+ autoload :Matchers, "wildcard_matchers/matchers"
+ autoload :WildcardMatcher, "wildcard_matchers/wildcard_matcher"
+
include Helpers
include Matchers
def wildcard_match?(actual, expected, &on_failure)
- on_failure = proc { return false } unless block_given?
+ errors = WildcardMatcher.check_errors(actual, expected)
- recursive_match(actual, expected, &on_failure)
- end
-
- protected
- def recursive_match(actual, expected, position = ".", &on_failure)
- # "case expected" should omit Array or Hash
- # "case actual" should omit Proc
- case expected
- when Class
- # when expected is Array or Hash (Class) comes here and do nothing
- when Array
- return check_array(actual, expected, position, &on_failure)
- when Hash
- return check_hash(actual, expected, position, &on_failure)
- end
-
- unless expected === actual
- yield("#{position}: expect #{actual.inspect} to #{expected.inspect}")
- false
- else
+ if errors.empty? #matcher.errors.empty?
true
- end
- end
-
- # TODO: class ArrayMatcher ?
- def check_array(actual, expected, position, &on_failure)
- return false unless actual.is_a?(Array)
-
- if expected.size == actual.size
- actual.zip(expected).map.with_index do |(a, e), index|
- recursive_match(a, e, position + "[#{index}]", &on_failure)
- end.all?
else
- yield <<_MESSAGE_
-#{position}: expect Array size #{actual.size} to #{expected.size}
- actual: #{actual.inspect}
- expect: #{expected.inspect}
-_MESSAGE_
+ on_failure.call(errors) if block_given?
false
end
end
-
- # TODO: class HashMatcher ?
- def check_hash(actual, expected, position, &on_failure)
- return false unless actual.is_a?(Hash)
-
- if (actual.keys - expected.keys).size == 0 && (expected.keys - actual.keys).size == 0
- expected.map do |key, value|
- recursive_match(actual[key], value, position + "[#{key.inspect}]", &on_failure)
- end.all?
- else
- yield <<_MESSAGE_
-#{position}: expect Hash keys #{actual.size} to #{expected.size}
- +keys: #{actual.keys - expected.keys}
- -keys: #{expected.keys - actual.keys }
-_MESSAGE_
- false
- end
- end
-
module_function *self.instance_methods
end