mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Refactor RangeMatcher
This commit is contained in:
parent
cbaa9a4c43
commit
520c738f6a
1 changed files with 44 additions and 53 deletions
|
@ -4,74 +4,65 @@ module Spectator::Matchers
|
||||||
# Matcher that tests whether a value is in a given range.
|
# Matcher that tests whether a value is in a given range.
|
||||||
# The `Range#includes?` method is used for this check.
|
# The `Range#includes?` method is used for this check.
|
||||||
struct RangeMatcher(ExpectedType) < ValueMatcher(ExpectedType)
|
struct RangeMatcher(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)
|
|
||||||
expected_value = @expected
|
private def failure_message(actual)
|
||||||
MatchData.new(matched, ExpectedActual.new(expected_value, label, actual, partial.label))
|
"#{actual.label} is not in #{expected.label} (#{exclusivity})"
|
||||||
|
end
|
||||||
|
|
||||||
|
private def failure_message_when_negated(actual)
|
||||||
|
"#{actual.label} is in #{expected.label} (#{exclusivity})"
|
||||||
|
end
|
||||||
|
|
||||||
|
private def values(actual)
|
||||||
|
{
|
||||||
|
lower: ">= #{range.begin.inspect}",
|
||||||
|
upper: "#{exclusive? ? "<" : "<="} #{range.end.inspect}",
|
||||||
|
actual: actual.value.inspect,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
private def negated_values(actual)
|
||||||
|
{
|
||||||
|
lower: "< #{range.begin.inspect}",
|
||||||
|
upper: "#{exclusive? ? ">=" : ">"} #{range.end.inspect}",
|
||||||
|
actual: actual.value.inspect,
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a new matcher, with the same bounds, but uses an inclusive range.
|
# Returns a new matcher, with the same bounds, but uses an inclusive range.
|
||||||
def inclusive
|
def inclusive
|
||||||
range = Range.new(@expected.begin, @expected.end, exclusive: false)
|
new_range = Range.new(range.begin, range.end, exclusive: false)
|
||||||
RangeMatcher.new(range, label)
|
expected = TestValue.new(new_range, label)
|
||||||
|
RangeMatcher.new(expected)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a new matcher, with the same bounds, but uses an exclusive range.
|
# Returns a new matcher, with the same bounds, but uses an exclusive range.
|
||||||
def exclusive
|
def exclusive
|
||||||
range = Range.new(@expected.begin, @expected.end, exclusive: true)
|
new_range = Range.new(range.begin, range.end, exclusive: true)
|
||||||
RangeMatcher.new(range, label)
|
expected = TestValue.new(new_range, label)
|
||||||
|
RangeMatcher.new(expected)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Match data specific to this matcher.
|
# Gets the expected range.
|
||||||
# This is used when the expected type is a `Range`.
|
private def range
|
||||||
private struct MatchData(B, E, ActualType) < MatchData
|
expected.value
|
||||||
# Creates the match data.
|
end
|
||||||
def initialize(matched, @values : ExpectedActual(Range(B, E), ActualType))
|
|
||||||
super(matched)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Information about the match.
|
# Indicates whether the range is inclusive or exclusive.
|
||||||
def named_tuple
|
private def exclusive?
|
||||||
{
|
range.exclusive?
|
||||||
lower: NegatablePrefixedMatchDataValue.new(">=", "<", range.begin),
|
end
|
||||||
upper: NegatablePrefixedMatchDataValue.new(exclusive? ? "<" : "<=", exclusive? ? ">=" : ">", range.end),
|
|
||||||
actual: @values.actual,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
# Describes the condition that satisfies the matcher.
|
# Produces a string "inclusive" or "exclusive" based on the range.
|
||||||
# This is informational and displayed to the end-user.
|
private def exclusivity
|
||||||
def message
|
exclusive? ? "exclusive" : "inclusive"
|
||||||
"#{@values.actual_label} is in #{@values.expected_label} (#{exclusivity})"
|
|
||||||
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} (#{exclusivity})"
|
|
||||||
end
|
|
||||||
|
|
||||||
# Gets the expected range.
|
|
||||||
private def range
|
|
||||||
@values.expected
|
|
||||||
end
|
|
||||||
|
|
||||||
# Indicates whether the range is inclusive or exclusive.
|
|
||||||
private def exclusive?
|
|
||||||
range.exclusive?
|
|
||||||
end
|
|
||||||
|
|
||||||
# Produces a string "inclusive" or "exclusive" based on the range.
|
|
||||||
private def exclusivity
|
|
||||||
exclusive? ? "exclusive" : "inclusive"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue