Simplify CaseMatcher by using new matcher refactoring

This commit is contained in:
Michael Miller 2019-08-01 16:17:39 -06:00
parent c75fba8076
commit 16a2204a2d
1 changed files with 9 additions and 32 deletions

View File

@ -4,43 +4,20 @@ module Spectator::Matchers
# Common matcher that tests whether two values semantically equal each other.
# The values are compared with the === operator.
struct CaseMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
private def match?(actual)
expected === actual
expected.value === actual.value
end
def description
"matches #{expected.label}"
end
# Determines whether the matcher is satisfied with the partial given to it.
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
def failure_message(actual)
"#{actual.label} did not match #{expected.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
{
expected: 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} matches #{@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 match #{@values.expected_label}"
end
def failure_message_when_negated(actual)
"#{actual.label} matched #{expected.label}"
end
end
end