Fix some more matcher errors

This commit is contained in:
Michael Miller 2019-08-09 15:14:00 -06:00
parent 8fc3af075c
commit 85b7412436
3 changed files with 20 additions and 10 deletions

View file

@ -14,7 +14,7 @@ module Spectator::DSL
# expect(1 + 2).to eq(3) # expect(1 + 2).to eq(3)
# ``` # ```
macro eq(expected) macro eq(expected)
::Spectator::Matchers::EqualityMatcher.new(::Spectator::TestValue.new({{expected}}, {{expected.stringify}}))) ::Spectator::Matchers::EqualityMatcher.new(::Spectator::TestValue.new({{expected}}, {{expected.stringify}}))
end end
# Indicates that some value should not equal another. # Indicates that some value should not equal another.

View file

@ -22,12 +22,12 @@ module Spectator::Matchers
index = compare_arrays(expected_elements, actual_elements) index = compare_arrays(expected_elements, actual_elements)
case index case index
when Int # Content differs.
failed_content_mismatch(expected_elements, actual_elements, index, actual.label)
when true # Contents are identical. when true # Contents are identical.
SuccessfulMatchData.new SuccessfulMatchData.new
when false # Size differs. else # Size differs.
failed_size_mismatch(expected_elements, actual_elements, actual.label) failed_size_mismatch(expected_elements, actual_elements, actual.label)
else # Content differs.
failed_content_mismatch(expected_elements, actual_elements, index, actual.label)
end end
end end
@ -36,11 +36,11 @@ module Spectator::Matchers
expected_elements = expected.value.to_a expected_elements = expected.value.to_a
case compare_arrays(expected_elements, actual_elements) case compare_arrays(expected_elements, actual_elements)
when Int # Contents differ.
SuccessfulMatchData.new
when true # Contents are identical. when true # Contents are identical.
failed_content_identical(expected_elements, actual_elements, actual.label) failed_content_identical(expected_elements, actual_elements, actual.label)
when false # Size differs. else # Size differs.
SuccessfulMatchData.new
else # Contents differ.
SuccessfulMatchData.new SuccessfulMatchData.new
end end
end end
@ -84,7 +84,7 @@ module Spectator::Matchers
end end
private def failed_content_identical(expected_elements, actual_elements, actual_label) 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}", expected: "Not #{expected_elements.inspect}",
actual: actual_elements.inspect actual: actual_elements.inspect
) )

View file

@ -21,14 +21,14 @@ module Spectator::Matchers
if match?(snapshot) if match?(snapshot)
SuccessfulMatchData.new SuccessfulMatchData.new
else else
FailedMatchData.new("#{actual.label} does not have #{expected.label}", **snapshot) FailedMatchData.new("#{actual.label} does not have #{expected.label}", **values(snapshot))
end end
end end
def negated_match(actual) def negated_match(actual)
snapshot = snapshot_values(actual.value) snapshot = snapshot_values(actual.value)
if match?(snapshot) if match?(snapshot)
FailedMatchData.new("#{actual.label} has #{expected.label}", **snapshot) FailedMatchData.new("#{actual.label} has #{expected.label}", **values(snapshot))
else else
SuccessfulMatchData.new SuccessfulMatchData.new
end end
@ -54,6 +54,16 @@ module Spectator::Matchers
{% end %} {% end %}
end end
private def values(snapshot)
{% begin %}
{
{% for attribute in ExpectedType.keys %}
{{attribute}}: snapshot[{{attribute.symbolize}}].inspect,
{% end %}
}
{% end %}
end
private def match?(snapshot) private def match?(snapshot)
# Test each predicate and immediately return false if one is false. # Test each predicate and immediately return false if one is false.
{% for attribute in ExpectedType.keys %} {% for attribute in ExpectedType.keys %}