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,24 +28,47 @@ module Spectator::Matchers
def match(actual)
actual_elements = actual.value.to_a
expected_elements = expected.value.to_a
index = compare_arrays(expected_elements, actual_elements)
case index
when true # Contents are identical.
SuccessfulMatchData.new
when false # 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
def negated_match(actual)
actual_elements = actual.value.to_a
expected_elements = expected.value.to_a
case compare_arrays(expected_elements, actual_elements)
when true # Contents are identical.
failed_content_identical(expected_elements, actual_elements, actual.label)
when false # Size differs.
SuccessfulMatchData.new
else # Contents differ.
SuccessfulMatchData.new
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|
unless expected_element == actual_element
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
return index unless expected_element == actual_element
index += 1
end
# Success.
SuccessfulMatchData.new
true
else
# Size mismatch.
FailedMatchData.new("#{actual.label} does not contain exactly #{expected.label} (size mismatch)",
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"),
@ -53,25 +76,22 @@ module Spectator::Matchers
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 negated_match(actual)
actual_elements = actual.value.to_a
expected_elements = expected.value.to_a
if expected_elements.size == actual_elements.size
index = 0
expected_elements.zip(actual_elements) do |expected_element, actual_element|
return SuccessfulMatchData.new unless expected_element == actual_element
index += 1
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"),
])
else
SuccessfulMatchData.new
end
end
end
end