mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Show missing values in error output
This commit is contained in:
parent
f465df48d4
commit
875ca587f3
1 changed files with 42 additions and 39 deletions
|
@ -1,9 +1,16 @@
|
||||||
require "./value_matcher"
|
require "./matcher"
|
||||||
|
|
||||||
module Spectator::Matchers
|
module Spectator::Matchers
|
||||||
# Matcher that tests whether a value, such as a `String` or `Array`, contains one or more values.
|
# Matcher that tests whether a value, such as a `String` or `Array`, contains one or more values.
|
||||||
# The values are checked with the `includes?` method.
|
# The values are checked with the `includes?` method.
|
||||||
struct ContainMatcher(ExpectedType) < ValueMatcher(ExpectedType)
|
struct ContainMatcher(ExpectedType) < Matcher
|
||||||
|
# Expected value and label.
|
||||||
|
private getter expected
|
||||||
|
|
||||||
|
# Creates the matcher with an expected value.
|
||||||
|
def initialize(@expected : TestValue(Array(ExpectedType)))
|
||||||
|
end
|
||||||
|
|
||||||
# Short text about the matcher's purpose.
|
# Short text about the matcher's purpose.
|
||||||
# This explains what condition satisfies the matcher.
|
# This explains what condition satisfies the matcher.
|
||||||
# The description is used when the one-liner syntax is used.
|
# The description is used when the one-liner syntax is used.
|
||||||
|
@ -11,54 +18,50 @@ module Spectator::Matchers
|
||||||
"contains #{expected.label}"
|
"contains #{expected.label}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Checks whether the matcher is satisifed with the expression given to it.
|
# Actually performs the test against the expression.
|
||||||
private def match?(actual : TestExpression(T)) : Bool forall T
|
def match(actual : TestExpression(T)) : MatchData forall T
|
||||||
actual_value = actual.value
|
actual_value = actual.value
|
||||||
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:includes?)
|
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:includes?)
|
||||||
|
|
||||||
expected.value.all? do |item|
|
actual_elements = actual_value.to_a
|
||||||
actual_value.includes?(item)
|
missing = expected.value.reject do |item|
|
||||||
|
actual_elements.includes?(item)
|
||||||
|
end
|
||||||
|
|
||||||
|
if missing.empty?
|
||||||
|
# Contents are present.
|
||||||
|
SuccessfulMatchData.new(description)
|
||||||
|
else
|
||||||
|
# Content is missing.
|
||||||
|
FailedMatchData.new(description, "#{actual.label} does not contain #{expected.label}",
|
||||||
|
expected: expected.value.inspect,
|
||||||
|
actual: actual_elements.inspect,
|
||||||
|
missing: missing.inspect,
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# If the expectation is negated, then this method is called instead of `#match?`.
|
# Performs the test against the expression, but inverted.
|
||||||
private def does_not_match?(actual : TestExpression(T)) : Bool forall T
|
# A successful match with `#match` should normally fail for this method, and vice-versa.
|
||||||
|
def negated_match(actual : TestExpression(T)) : MatchData forall T
|
||||||
actual_value = actual.value
|
actual_value = actual.value
|
||||||
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:includes?)
|
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:includes?)
|
||||||
|
|
||||||
!expected.value.any? do |item|
|
actual_elements = actual_value.to_a
|
||||||
actual_value.includes?(item)
|
missing = expected.value.reject do |item|
|
||||||
|
actual_elements.includes?(item)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
# Message displayed when the matcher isn't satisifed.
|
if missing.empty?
|
||||||
#
|
# Contents are identical.
|
||||||
# This is only called when `#match?` returns false.
|
FailedMatchData.new(description, "#{actual.label} contains #{expected.label}",
|
||||||
#
|
expected: "Not #{expected_elements.inspect}",
|
||||||
# The message should typically only contain the test expression labels.
|
actual: actual_elements.inspect
|
||||||
# Actual values should be returned by `#values`.
|
)
|
||||||
private def failure_message(actual) : String
|
else
|
||||||
"#{actual.label} does not contain #{expected.label}"
|
# Content differs.
|
||||||
end
|
SuccessfulMatchData.new(description)
|
||||||
|
end
|
||||||
# Message displayed when the matcher isn't satisifed and is negated.
|
|
||||||
# This is essentially what would satisfy the matcher if it wasn't negated.
|
|
||||||
#
|
|
||||||
# This is only called when `#does_not_match?` returns false.
|
|
||||||
#
|
|
||||||
# The message should typically only contain the test expression labels.
|
|
||||||
# Actual values should be returned by `#values`.
|
|
||||||
private def failure_message_when_negated(actual) : String
|
|
||||||
"#{actual.label} contains #{expected.label}"
|
|
||||||
end
|
|
||||||
|
|
||||||
# Additional information about the match failure.
|
|
||||||
# The return value is a NamedTuple with Strings for each value.
|
|
||||||
private def values(actual)
|
|
||||||
{
|
|
||||||
subset: expected.value.inspect,
|
|
||||||
superset: actual.value.inspect,
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private def unexpected(value, label)
|
private def unexpected(value, label)
|
||||||
|
|
Loading…
Reference in a new issue