Update PredicateMatcher to use MatchData

This commit is contained in:
Michael Miller 2019-03-06 11:05:13 -07:00
parent 6ab2aad532
commit 81e20b13ec
2 changed files with 112 additions and 58 deletions

View file

@ -12,13 +12,10 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the value given to it.
# True is returned if the match was successful, false otherwise.
def match?(partial)
actual = partial.actual
private def match?(values)
# Test each predicate and immediately return false if one is false.
{% for attribute in ExpectedType.keys %}
return false unless actual.{{attribute}}?
return false unless values[{{attribute.symbolize}}]
{% end %}
# All checks passed if this point is reached.
@ -28,19 +25,44 @@ module Spectator::Matchers
# 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")
values = snapshot_values(partial.actual)
MatchData.new(match?(values), values, partial.label)
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message(partial)
"Expected #{partial.label} to be #{label}"
# Captures all of the actual values.
# A `NamedTuple` is returned,
# with each key being the attribute.
private def snapshot_values(actual)
{% begin %}
{
{% for attribute in ExpectedType.keys %}
{{attribute}}: actual.{{attribute}}?,
{% end %}
}
{% end %}
end
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
def negated_message(partial)
"Expected #{partial.label} to not be #{label}"
# Match data specific to this matcher.
private struct MatchData(ActualType) < MatchData
# Creates the match data.
def initialize(matched, @values : ActualType, @actual_label : String)
super(matched)
end
# Information about the match.
getter values
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@actual_label} is " + {{ActualType.keys.splat.stringify}}
end
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
def negated_message
"#{@actual_label} is not " + {{ActualType.keys.splat.stringify}}
end
end
end
end