Cleanup match methods

This commit is contained in:
Michael Miller 2019-08-06 22:18:05 -06:00
parent 13fad5081b
commit ab147d00fb

View file

@ -28,50 +28,70 @@ module Spectator::Matchers
def match(actual) def match(actual)
actual_elements = actual.value.to_a actual_elements = actual.value.to_a
expected_elements = expected.value.to_a expected_elements = expected.value.to_a
if expected_elements.size == actual_elements.size index = compare_arrays(expected_elements, actual_elements)
index = 0
expected_elements.zip(actual_elements) do |expected_element, actual_element| case index
unless expected_element == actual_element when true # Contents are identical.
return FailedMatchData.new("#{actual.label} does not contain exactly #{expected.label} (element mismatch)",
[
LabeledValue.new(expected_element.inspect, "expected"),
LabeledValue.new(actual_element.inspect, "actual"),
LabeledValue.new(index.to_s, "index"),
])
end
index += 1
end
# Success.
SuccessfulMatchData.new SuccessfulMatchData.new
else when false # Size differs.
# Size mismatch. failed_size_mismatch(expected_elements, actual_elements, actual.label)
FailedMatchData.new("#{actual.label} does not contain exactly #{expected.label} (size mismatch)", else # Content differs.
[ failed_content_mismatch(expected_elements, actual_elements, index, actual.label)
LabeledValue.new(expected_elements.inspect, "expected"),
LabeledValue.new(actual_elements.inspect, "actual"),
LabeledValue.new(expected_elements.size, "expected size"),
LabeledValue.new(actual_elements.size, "actual size"),
])
end end
end end
def negated_match(actual) def negated_match(actual)
actual_elements = actual.value.to_a actual_elements = actual.value.to_a
expected_elements = expected.value.to_a expected_elements = expected.value.to_a
if expected_elements.size == actual_elements.size
index = 0 case compare_arrays(expected_elements, actual_elements)
expected_elements.zip(actual_elements) do |expected_element, actual_element| when true # Contents are identical.
return SuccessfulMatchData.new unless expected_element == actual_element failed_content_identical(expected_elements, actual_elements, actual.label)
index += 1 when false # Size differs.
end SuccessfulMatchData.new
FailedMatchData.new("#{actual.label} contains exactly #{expected.label}", else # Contents differ.
[
LabeledValue.new("Not #{expected_elements.inspect}", "expected"),
LabeledValue.new(actual_elements.inspect, "actual"),
])
else
SuccessfulMatchData.new SuccessfulMatchData.new
end end
end end
private def compare_arrays(expected_elements, actual_elements)
if expected_elements.size == actual_elements.size
index = 0
expected_elements.zip(actual_elements) do |expected_element, actual_element|
return index unless expected_element == actual_element
index += 1
end
true
else
false
end
end
def failed_size_mismatch(expected_elements, actual_elements, actual_label)
FailedMatchData.new("#{actual_label} does not contain exactly #{expected.label} (size mismatch)",
[
LabeledValue.new(expected_elements.inspect, "expected"),
LabeledValue.new(actual_elements.inspect, "actual"),
LabeledValue.new(expected_elements.size, "expected size"),
LabeledValue.new(actual_elements.size, "actual size"),
])
end
def failed_content_mismatch(expected_elements, actual_elements, index, actual_label)
FailedMatchData.new("#{actual_label} does not contain exactly #{expected.label} (element mismatch)",
[
LabeledValue.new(expected_elements[index].inspect, "expected"),
LabeledValue.new(actual_elements[index].inspect, "actual"),
LabeledValue.new(index.to_s, "index"),
])
end
def failed_content_identical(expected_elements, actual_elements, actual_label)
FailedMatchData.new("#{actual.label} contains exactly #{expected.label}",
[
LabeledValue.new("Not #{expected_elements.inspect}", "expected"),
LabeledValue.new(actual_elements.inspect, "actual"),
])
end
end end
end end