spec/support/deep_eql.rb in yaks-0.9.0 vs spec/support/deep_eql.rb in yaks-0.10.0
- old
+ new
@@ -23,11 +23,13 @@
def description
'be deeply equal'
end
def recurse(target, expectation)
- @result &&= DeepEql.new(expectation, stack, diffs).matches?(target)
+ # leave this in two lines so it doesn't short circuit
+ result = DeepEql.new(expectation, stack, diffs).matches?(target)
+ @result &&= result
end
def stack_as_jsonpath
'$' + stack.map do |item|
case item
@@ -49,39 +51,38 @@
if target[key] != expectation[key]
if [Hash, Array].any?{|klz| target[key].is_a? klz }
recurse(target[key], expectation[key])
else
add_failure_message begin
- if expectation[key].class == target[key].class
- "expected #{expectation[key].inspect}, got #{target[key].inspect}"
- else
- "expected #{expectation[key].class}: #{expectation[key].inspect}, got #{target[key].class}: #{target[key].inspect}"
- end
- rescue Encoding::CompatibilityError
- "expected #{expectation[key].encoding}, got #{target[key].encoding}"
- end
+ if expectation[key].class == target[key].class
+ "expected #{expectation[key].inspect}, got #{target[key].inspect}"
+ else
+ "expected #{expectation[key].class}: #{expectation[key].inspect}, got #{target[key].class}: #{target[key].inspect}"
+ end
+ rescue Encoding::CompatibilityError
+ "expected #{expectation[key].encoding}, got #{target[key].encoding}"
+ end
end
end
pop
end
def matches?(target)
@target = target
-
case expectation
when Hash
if target.is_a?(Hash)
if target.class != expectation.class # e.g. HashWithIndifferentAccess
add_failure_message("expected #{expectation.class}, got #{target.class}")
end
(expectation.keys - target.keys).each do |key|
- add_failure_message "Expected key #{key}"
+ add_failure_message "Expected key #{key.inspect} => #{expectation[key].inspect}"
end
(target.keys - expectation.keys).each do |key|
- add_failure_message "Unexpected key #{key}"
+ add_failure_message "Unexpected key #{key.inspect} => #{target[key].inspect}"
end
- (target.keys | expectation.keys).each do |key|
+ (target.keys & expectation.keys).each do |key|
compare key
end
else
add_failure_message("expected Hash got #{@target.inspect}")
end
@@ -105,19 +106,21 @@
end
def failure_message_for_should
diffs.join("\n")
end
- alias failure_message failure_message_for_should
+ alias_method :failure_message, :failure_message_for_should
def failure_message_for_should_not
"expected #{@target.inspect} not to be in deep_eql with #{@expectation.inspect}"
end
- alias failure_message_when_negated failure_message_for_should_not
+ alias_method :failure_message_when_negated, :failure_message_for_should_not
end
end
-module RSpec::Matchers
- def deep_eql(exp)
- Matchers::DeepEql.new(exp)
+module RSpec
+ module Matchers
+ def deep_eql(exp)
+ ::Matchers::DeepEql.new(exp)
+ end
end
end