shard-spectator/src/spectator/matchers/range_matcher.cr

111 lines
3.9 KiB
Crystal
Raw Normal View History

2019-01-25 20:00:19 +00:00
require "./value_matcher"
module Spectator::Matchers
# Matcher that tests whether a value is in a given range.
# The `Range#includes?` method is used for this check.
2019-01-25 20:00:19 +00:00
struct RangeMatcher(ExpectedType) < ValueMatcher(ExpectedType)
2019-08-10 16:50:48 +00:00
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
def description : String
2019-08-08 22:47:17 +00:00
"is in #{expected.label}"
2019-01-25 20:20:17 +00:00
end
# Returns a new matcher, with the same bounds, but uses an inclusive range.
def inclusive
label = expected.label
2020-02-21 12:23:33 +00:00
new_range = Range.new(range.begin, range.end, exclusive: false)
expected = Value.new(new_range, label)
RangeMatcher.new(expected)
end
# Returns a new matcher, with the same bounds, but uses an exclusive range.
def exclusive
label = expected.label
new_range = Range.new(range.begin, range.end, exclusive: true)
expected = Value.new(new_range, label)
RangeMatcher.new(expected)
end
2021-11-28 22:45:17 +00:00
# Checks whether the matcher is satisfied with the expression given to it.
private def match?(actual : Expression(T)) : Bool forall T
actual_value = actual.value
expected_value = expected.value
if expected_value.is_a?(Range) && actual_value.is_a?(Comparable)
return match_impl?(expected_value, actual_value)
end
return false unless actual_value.is_a?(Comparable(typeof(expected_value.begin)))
expected_value.includes?(actual_value)
end
private def match_impl?(expected_value : Range(B, E), actual_value : Comparable(B)) : Bool forall B, E
expected_value.includes?(actual_value)
end
private def match_impl?(expected_value : Range(B, E), actual_value : T) : Bool forall B, E, T
return false unless actual_value.is_a?(B) || actual_value.is_a?(Comparable(B))
expected_value.includes?(actual_value)
end
private def match_impl?(expected_value : Range(Number, Number), actual_value : Number) : Bool
expected_value.includes?(actual_value)
end
2021-11-28 22:45:17 +00:00
# Message displayed when the matcher isn't satisfied.
2019-08-10 16:50:48 +00:00
#
# This is only called when `#match?` returns false.
#
# The message should typically only contain the test expression labels.
# Actual values should be returned by `#values`.
private def failure_message(actual) : String
2019-08-08 22:47:17 +00:00
"#{actual.label} is not in #{expected.label} (#{exclusivity})"
2019-01-25 20:20:17 +00:00
end
2021-11-28 22:45:17 +00:00
# Message displayed when the matcher isn't satisfied and is negated.
2019-08-10 16:50:48 +00:00
# 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
2019-08-08 22:47:17 +00:00
"#{actual.label} is in #{expected.label} (#{exclusivity})"
end
2019-08-10 16:50:48 +00:00
# Additional information about the match failure.
# The return value is a NamedTuple with Strings for each value.
2019-08-08 22:47:17 +00:00
private def values(actual)
{
lower: ">= #{range.begin.inspect}",
upper: "#{exclusive? ? "<" : "<="} #{range.end.inspect}",
actual: actual.value.inspect,
}
end
2019-08-10 16:50:48 +00:00
# Additional information about the match failure when negated.
# The return value is a NamedTuple with Strings for each value.
2019-08-08 22:47:17 +00:00
private def negated_values(actual)
{
lower: "< #{range.begin.inspect}",
upper: "#{exclusive? ? ">=" : ">"} #{range.end.inspect}",
actual: actual.value.inspect,
}
end
2019-08-08 22:47:17 +00:00
# Gets the expected range.
private def range
expected.value
end
2019-08-08 22:47:17 +00:00
# Indicates whether the range is inclusive or exclusive.
private def exclusive?
range.exclusive?
end
2019-08-08 22:47:17 +00:00
# Produces a string "inclusive" or "exclusive" based on the range.
private def exclusivity
exclusive? ? "exclusive" : "inclusive"
end
2019-01-25 20:00:19 +00:00
end
end