Update HaveMatcher to use MatchData

This commit is contained in:
Michael Miller 2019-03-06 10:29:00 -07:00
parent a78b80ed1f
commit 6ab2aad532
2 changed files with 576 additions and 482 deletions

File diff suppressed because it is too large Load diff

View file

@ -7,8 +7,7 @@ module Spectator::Matchers
struct HaveMatcher(ExpectedType) < ValueMatcher(ExpectedType) struct HaveMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it. # Determines whether the matcher is satisfied with the value given to it.
# True is returned if the match was successful, false otherwise. # True is returned if the match was successful, false otherwise.
def match?(partial) private def match?(actual)
actual = partial.actual
if actual.is_a?(String) if actual.is_a?(String)
match_string?(actual) match_string?(actual)
else else
@ -16,12 +15,6 @@ module Spectator::Matchers
end end
end 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) : MatchData
raise NotImplementedError.new("#match")
end
# Checks if a `String` matches the expected values. # Checks if a `String` matches the expected values.
# The `includes?` method is used for this check. # The `includes?` method is used for this check.
private def match_string?(actual) private def match_string?(actual)
@ -41,16 +34,39 @@ module Spectator::Matchers
end end
end end
# Describes the condition that satisfies the matcher. # Determines whether the matcher is satisfied with the partial given to it.
# This is informational and displayed to the end-user. # `MatchData` is returned that contains information about the match.
def message(partial) def match(partial)
"Expected #{partial.label} to include #{label}" values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end end
# Describes the condition that won't satsify the matcher. # Match data specific to this matcher.
# This is informational and displayed to the end-user. private struct MatchData(ExpectedType, ActualType) < MatchData
def negated_message(partial) # Creates the match data.
"Expected #{partial.label} to not include #{label}" def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
super(matched)
end
# Information about the match.
def values
{
subset: @values.expected,
superset: @values.actual,
}
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@values.actual_label} includes #{@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} does not include #{@values.expected_label}"
end
end end
end end
end end