mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Refactor collection matcher
This commit is contained in:
parent
ec96bf2de2
commit
f2f46418a3
1 changed files with 17 additions and 38 deletions
|
@ -1,18 +1,24 @@
|
||||||
|
require "../test_value"
|
||||||
|
require "./range_matcher"
|
||||||
require "./value_matcher"
|
require "./value_matcher"
|
||||||
|
|
||||||
module Spectator::Matchers
|
module Spectator::Matchers
|
||||||
# Matcher for checking that a value is in a collection of other values.
|
# Matcher for checking that a value is in a collection of other values.
|
||||||
struct CollectionMatcher(ExpectedType) < ValueMatcher(ExpectedType)
|
struct CollectionMatcher(ExpectedType) < ValueMatcher(ExpectedType)
|
||||||
# Determines whether the matcher is satisfied with the value given to it.
|
|
||||||
private def match?(actual)
|
private def match?(actual)
|
||||||
expected.includes?(actual)
|
expected.value.includes?(actual.value)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Determines whether the matcher is satisfied with the partial given to it.
|
def description
|
||||||
def match(partial, negated = false)
|
"is in #{expected.label}"
|
||||||
actual = partial.actual
|
end
|
||||||
matched = match?(actual)
|
|
||||||
MatchData.new(matched, ExpectedActual.new(partial, self))
|
private def failure_message(actual)
|
||||||
|
"#{actual.label} is not in #{expected.label}"
|
||||||
|
end
|
||||||
|
|
||||||
|
private def failure_message_when_negated(actual)
|
||||||
|
"#{actual.label} is in #{expected.label}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates a new range matcher with bounds based off of *center*.
|
# Creates a new range matcher with bounds based off of *center*.
|
||||||
|
@ -20,7 +26,7 @@ module Spectator::Matchers
|
||||||
# This method expects that the original matcher was created with a "difference" value.
|
# This method expects that the original matcher was created with a "difference" value.
|
||||||
# That is:
|
# That is:
|
||||||
# ```
|
# ```
|
||||||
# RangeMatcher.new(diff).of(center)
|
# CollectionMatcher.new(diff).of(center)
|
||||||
# ```
|
# ```
|
||||||
# This implies that the `#match` method would not work on the original matcher.
|
# This implies that the `#match` method would not work on the original matcher.
|
||||||
#
|
#
|
||||||
|
@ -28,39 +34,12 @@ module Spectator::Matchers
|
||||||
# and have upper and lower bounds equal to *center* plus and minus diff.
|
# and have upper and lower bounds equal to *center* plus and minus diff.
|
||||||
# The range will be inclusive.
|
# The range will be inclusive.
|
||||||
def of(center)
|
def of(center)
|
||||||
diff = @expected
|
diff = @expected.value
|
||||||
lower = center - diff
|
lower = center - diff
|
||||||
upper = center + diff
|
upper = center + diff
|
||||||
range = Range.new(lower, upper)
|
range = Range.new(lower, upper)
|
||||||
RangeMatcher.new(range, "#{center} +/- #{label}")
|
test_value = TestValue.new(range, "#{center} +/- #{expected.label}")
|
||||||
end
|
RangeMatcher.new(test_value)
|
||||||
|
|
||||||
# Match data specific to this matcher.
|
|
||||||
private struct MatchData(ExpectedType, ActualType) < MatchData
|
|
||||||
# Creates the match data.
|
|
||||||
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
|
|
||||||
super(matched)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Information about the match.
|
|
||||||
def named_tuple
|
|
||||||
{
|
|
||||||
collection: NegatableMatchDataValue.new(@values.expected),
|
|
||||||
actual: @values.actual,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
# Describes the condition that satisfies the matcher.
|
|
||||||
# This is informational and displayed to the end-user.
|
|
||||||
def message
|
|
||||||
"#{@values.actual_label} is in #{@values.expected_label}"
|
|
||||||
end
|
|
||||||
|
|
||||||
# Describes the condition that won't satsify the matcher.
|
|
||||||
# This is informational and displayed to the end-user.
|
|
||||||
def negated_message
|
|
||||||
"#{@values.actual_label} is not in #{@values.expected_label}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue