mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Cleanup match methods
This commit is contained in:
parent
13fad5081b
commit
ab147d00fb
1 changed files with 55 additions and 35 deletions
|
@ -28,24 +28,47 @@ 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
|
||||||
|
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
|
if expected_elements.size == actual_elements.size
|
||||||
index = 0
|
index = 0
|
||||||
expected_elements.zip(actual_elements) do |expected_element, actual_element|
|
expected_elements.zip(actual_elements) do |expected_element, actual_element|
|
||||||
unless expected_element == actual_element
|
return index 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
|
|
||||||
index += 1
|
index += 1
|
||||||
end
|
end
|
||||||
# Success.
|
true
|
||||||
SuccessfulMatchData.new
|
|
||||||
else
|
else
|
||||||
# Size mismatch.
|
false
|
||||||
FailedMatchData.new("#{actual.label} does not contain exactly #{expected.label} (size mismatch)",
|
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(expected_elements.inspect, "expected"),
|
||||||
LabeledValue.new(actual_elements.inspect, "actual"),
|
LabeledValue.new(actual_elements.inspect, "actual"),
|
||||||
|
@ -53,25 +76,22 @@ module Spectator::Matchers
|
||||||
LabeledValue.new(actual_elements.size, "actual size"),
|
LabeledValue.new(actual_elements.size, "actual size"),
|
||||||
])
|
])
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def negated_match(actual)
|
def failed_content_identical(expected_elements, actual_elements, actual_label)
|
||||||
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
|
|
||||||
FailedMatchData.new("#{actual.label} contains exactly #{expected.label}",
|
FailedMatchData.new("#{actual.label} contains exactly #{expected.label}",
|
||||||
[
|
[
|
||||||
LabeledValue.new("Not #{expected_elements.inspect}", "expected"),
|
LabeledValue.new("Not #{expected_elements.inspect}", "expected"),
|
||||||
LabeledValue.new(actual_elements.inspect, "actual"),
|
LabeledValue.new(actual_elements.inspect, "actual"),
|
||||||
])
|
])
|
||||||
else
|
|
||||||
SuccessfulMatchData.new
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue