Split part of RangeMatcher off as CollectionMatcher

Use CollectionMatcher for `be_within`.
The `#of` method creates a RangeMatcher.
This commit is contained in:
Michael Miller 2019-05-16 18:20:08 -06:00
parent fa5c13df0e
commit 77307f6eb1
5 changed files with 609 additions and 384 deletions

View file

@ -242,7 +242,7 @@ module Spectator::DSL
# ```
#
# NOTE: The of suffix must be used
# if the *expected* argument does not implement an includes? method.
# if the *expected* argument does not implement an `includes?` method.
#
# Additionally, for this second usage,
# an "inclusive" or "exclusive" suffix can be added.
@ -258,7 +258,7 @@ module Spectator::DSL
# NOTE: Do not attempt to mix the two use cases.
# It likely won't work and will result in a compilation error.
macro be_within(expected)
::Spectator::Matchers::RangeMatcher.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::CollectionMatcher.new({{expected}}, {{expected.stringify}})
end
# Indicates that some value should be between a lower and upper-bound.

View file

@ -0,0 +1,67 @@
require "./value_matcher"
module Spectator::Matchers
# Matcher for checking that a value is in a collection of other values.
struct CollectionMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
private def match?(actual)
expected.includes?(actual)
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
actual = partial.actual
matched = match?(actual)
MatchData.new(matched, ExpectedActual.new(partial, self))
end
# Creates a new range matcher with bounds based off of *center*.
#
# This method expects that the original matcher was created with a "difference" value.
# That is:
# ```
# RangeMatcher.new(diff).of(center)
# ```
# This implies that the `#match` method would not work on the original matcher.
#
# The new range will be centered at *center*
# and have upper and lower bounds equal to *center* plus and minus diff.
# The range will be inclusive.
def of(center)
diff = @expected
lower = center - diff
upper = center + diff
range = Range.new(lower, upper)
RangeMatcher.new(range, "#{center} +/- #{label}")
end
# 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

View file

@ -1,10 +1,8 @@
require "./value_matcher"
module Spectator::Matchers
# Matcher that tests whether a value is in a given range or set of values.
# The `includes?` method is used for this check.
# Typically this matcher uses a `Range`,
# but any type that implements the `includes?` method is supported.
# Matcher that tests whether a value is in a given range.
# The `Range#includes?` method is used for this check.
struct RangeMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
private def match?(actual)
@ -17,31 +15,7 @@ module Spectator::Matchers
actual = partial.actual
matched = match?(actual)
expected_value = @expected
if expected_value.is_a?(Range)
RangeMatchData.new(matched, ExpectedActual.new(expected_value, label, actual, partial.label))
else
SetMatchData.new(matched, ExpectedActual.new(partial, self))
end
end
# Creates a new range matcher with bounds based off of *center*.
#
# This method expects that the original matcher was created with a "difference" value.
# That is:
# ```
# RangeMatcher.new(diff).of(center)
# ```
# This implies that the `#match` method would not work on the original matcher.
#
# The new range will be centered at *center*
# and have upper and lower bounds equal to *center* plus and minus diff.
# The range will be inclusive.
def of(center)
diff = @expected
lower = center - diff
upper = center + diff
range = Range.new(lower, upper)
RangeMatcher.new(range, "#{center} +/- #{label}")
MatchData.new(matched, ExpectedActual.new(expected_value, label, actual, partial.label))
end
# Returns a new matcher, with the same bounds, but uses an inclusive range.
@ -58,7 +32,7 @@ module Spectator::Matchers
# Match data specific to this matcher.
# This is used when the expected type is a `Range`.
private struct RangeMatchData(B, E, ActualType) < MatchData
private struct MatchData(B, E, ActualType) < MatchData
# Creates the match data.
def initialize(matched, @values : ExpectedActual(Range(B, E), ActualType))
super(matched)
@ -100,34 +74,5 @@ module Spectator::Matchers
exclusive? ? "exclusive" : "inclusive"
end
end
# Match data specific to this matcher.
# This is used when the expected type is not a `Range`.
private struct SetMatchData(ExpectedType, ActualType) < MatchData
# Creates the match data.
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
super(matched)
end
# Information about the match.
def named_tuple
{
set: 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