From 85b741243634ee1318e8c4184f72dfe3a1a3d0cc Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 9 Aug 2019 15:14:00 -0600 Subject: [PATCH] Fix some more matcher errors --- src/spectator/dsl/matcher_dsl.cr | 2 +- src/spectator/matchers/array_matcher.cr | 14 +++++++------- src/spectator/matchers/have_predicate_matcher.cr | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/spectator/dsl/matcher_dsl.cr b/src/spectator/dsl/matcher_dsl.cr index 30fc916..18b30ab 100644 --- a/src/spectator/dsl/matcher_dsl.cr +++ b/src/spectator/dsl/matcher_dsl.cr @@ -14,7 +14,7 @@ module Spectator::DSL # expect(1 + 2).to eq(3) # ``` macro eq(expected) - ::Spectator::Matchers::EqualityMatcher.new(::Spectator::TestValue.new({{expected}}, {{expected.stringify}}))) + ::Spectator::Matchers::EqualityMatcher.new(::Spectator::TestValue.new({{expected}}, {{expected.stringify}})) end # Indicates that some value should not equal another. diff --git a/src/spectator/matchers/array_matcher.cr b/src/spectator/matchers/array_matcher.cr index 66594a9..28080ea 100644 --- a/src/spectator/matchers/array_matcher.cr +++ b/src/spectator/matchers/array_matcher.cr @@ -22,12 +22,12 @@ module Spectator::Matchers index = compare_arrays(expected_elements, actual_elements) case index + when Int # Content differs. + failed_content_mismatch(expected_elements, actual_elements, index, actual.label) when true # Contents are identical. SuccessfulMatchData.new - when false # Size differs. + else # Size differs. failed_size_mismatch(expected_elements, actual_elements, actual.label) - else # Content differs. - failed_content_mismatch(expected_elements, actual_elements, index, actual.label) end end @@ -36,11 +36,11 @@ module Spectator::Matchers expected_elements = expected.value.to_a case compare_arrays(expected_elements, actual_elements) + when Int # Contents differ. + SuccessfulMatchData.new when true # Contents are identical. failed_content_identical(expected_elements, actual_elements, actual.label) - when false # Size differs. - SuccessfulMatchData.new - else # Contents differ. + else # Size differs. SuccessfulMatchData.new end end @@ -84,7 +84,7 @@ module Spectator::Matchers end private def failed_content_identical(expected_elements, actual_elements, actual_label) - FailedMatchData.new("#{actual.label} contains exactly #{expected.label}", + FailedMatchData.new("#{actual_label} contains exactly #{expected.label}", expected: "Not #{expected_elements.inspect}", actual: actual_elements.inspect ) diff --git a/src/spectator/matchers/have_predicate_matcher.cr b/src/spectator/matchers/have_predicate_matcher.cr index 96145d5..3bb881a 100644 --- a/src/spectator/matchers/have_predicate_matcher.cr +++ b/src/spectator/matchers/have_predicate_matcher.cr @@ -21,14 +21,14 @@ module Spectator::Matchers if match?(snapshot) SuccessfulMatchData.new else - FailedMatchData.new("#{actual.label} does not have #{expected.label}", **snapshot) + FailedMatchData.new("#{actual.label} does not have #{expected.label}", **values(snapshot)) end end def negated_match(actual) snapshot = snapshot_values(actual.value) if match?(snapshot) - FailedMatchData.new("#{actual.label} has #{expected.label}", **snapshot) + FailedMatchData.new("#{actual.label} has #{expected.label}", **values(snapshot)) else SuccessfulMatchData.new end @@ -54,6 +54,16 @@ module Spectator::Matchers {% end %} end + private def values(snapshot) + {% begin %} + { + {% for attribute in ExpectedType.keys %} + {{attribute}}: snapshot[{{attribute.symbolize}}].inspect, + {% end %} + } + {% end %} + end + private def match?(snapshot) # Test each predicate and immediately return false if one is false. {% for attribute in ExpectedType.keys %}