From e07a45b72ffa0a20c71b4081fad0e7dc2b9c1a13 Mon Sep 17 00:00:00 2001 From: Sherin Kurian Date: Sun, 11 Aug 2019 09:03:27 -0700 Subject: [PATCH 1/2] cleaner support for comparing datetimes in json --- lib/rspec/json_expectations/json_traverser.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/rspec/json_expectations/json_traverser.rb b/lib/rspec/json_expectations/json_traverser.rb index 563c8fb..afa97f5 100644 --- a/lib/rspec/json_expectations/json_traverser.rb +++ b/lib/rspec/json_expectations/json_traverser.rb @@ -83,8 +83,12 @@ def match_size_of_collection(errors, expected, actual, prefix, options) def handle_value(errors, expected, actual, negate=false, prefix=[]) return nil unless handled_by_simple_value?(expected) + actual_as_json = actual.class.method_defined?(:as_json) ? actual.as_json : nil + expected_as_json = expected.class.method_defined?(:as_json) ? expected.as_json : nil if conditionally_negate(actual == expected, negate) true + elsif actual_as_json && expected_as_json && conditionally_negate(actual_as_json == expected_as_json, negate) + true else errors[prefix.join("/")] = { actual: actual, @@ -95,7 +99,11 @@ def handle_value(errors, expected, actual, negate=false, prefix=[]) end def handled_by_simple_value?(expected) - HANDLED_BY_SIMPLE_VALUE_HANDLER.any? { |type| type === expected } + HANDLED_BY_SIMPLE_VALUE_HANDLER.any? { |type| type === expected } || + expected.class.method_defined?(:as_json) + end + + def handled_by_casting_to_simple_value?(expected) end def handle_regex(errors, expected, actual, negate=false, prefix=[]) @@ -129,10 +137,11 @@ def handle_rspec_matcher(errors, expected, actual, negate=false, prefix=[]) end def handle_unsupported(expected) - unless SUPPORTED_VALUES.any? { |type| expected.is_a?(type) } - raise NotImplementedError, - "#{expected} expectation is not supported" - end + return nil if SUPPORTED_VALUES.any? { |type| expected.is_a?(type) } + return nil if handled_by_simple_value?(expected) + + raise NotImplementedError, + "#{expected} expectation is not supported" end def has_key?(actual, key) From 7925d1e6ed85466830ec214bfff595702b5171be Mon Sep 17 00:00:00 2001 From: Sherin Kurian Date: Sun, 11 Aug 2019 09:13:04 -0700 Subject: [PATCH 2/2] cleanup --- lib/rspec/json_expectations/json_traverser.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/rspec/json_expectations/json_traverser.rb b/lib/rspec/json_expectations/json_traverser.rb index afa97f5..061c302 100644 --- a/lib/rspec/json_expectations/json_traverser.rb +++ b/lib/rspec/json_expectations/json_traverser.rb @@ -83,11 +83,11 @@ def match_size_of_collection(errors, expected, actual, prefix, options) def handle_value(errors, expected, actual, negate=false, prefix=[]) return nil unless handled_by_simple_value?(expected) - actual_as_json = actual.class.method_defined?(:as_json) ? actual.as_json : nil - expected_as_json = expected.class.method_defined?(:as_json) ? expected.as_json : nil + actual_as_json = actual.class.method_defined?(:as_json) ? actual.as_json : actual + expected_as_json = expected.class.method_defined?(:as_json) ? expected.as_json : expected if conditionally_negate(actual == expected, negate) true - elsif actual_as_json && expected_as_json && conditionally_negate(actual_as_json == expected_as_json, negate) + elsif conditionally_negate(actual_as_json == expected_as_json, negate) true else errors[prefix.join("/")] = { @@ -103,9 +103,6 @@ def handled_by_simple_value?(expected) expected.class.method_defined?(:as_json) end - def handled_by_casting_to_simple_value?(expected) - end - def handle_regex(errors, expected, actual, negate=false, prefix=[]) return nil unless expected.is_a?(Regexp)