Simplify more matchers that are easy targets

This commit is contained in:
Michael Miller 2019-08-01 16:35:02 -06:00
parent 16a2204a2d
commit 11600a2f8d
4 changed files with 39 additions and 98 deletions

View file

@ -7,16 +7,16 @@ module Spectator::Matchers
private def match?(actual)
expected.value === actual.value
end
def description
"matches #{expected.label}"
end
def failure_message(actual)
"#{actual.label} did not match #{expected.label}"
private def failure_message(actual)
"#{actual.label} does not match #{expected.label}"
end
def failure_message_when_negated(actual)
private def failure_message_when_negated(actual)
"#{actual.label} matched #{expected.label}"
end
end

View file

@ -4,45 +4,29 @@ module Spectator::Matchers
# Matcher that tests whether a value, such as a `String` or `Array`, contains one or more values.
# The values are checked with the `includes?` method.
struct ContainMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
private def match?(actual)
expected.all? do |item|
actual.includes?(item)
expected.value.all? do |item|
actual.value.includes?(item)
end
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 description
"contains #{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
private def failure_message(actual)
"#{actual.label} does not match #{expected.label}"
end
# Information about the match.
def named_tuple
{
subset: NegatableMatchDataValue.new(@values.expected),
superset: @values.actual,
}
end
private def failure_message_when_negated(actual)
"#{actual.label} contains #{expected.label}"
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@values.actual_label} contains #{@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 contain #{@values.expected_label}"
end
private def values(actual) : Array(LabeledValue)
[
LabeledValue.new(expected.value.to_s, "subset"),
LabeledValue.new(actual.value.to_s, "superset"),
]
end
end
end

View file

@ -4,44 +4,24 @@ module Spectator::Matchers
# Matcher that tests whether a collection is empty.
# The values are checked with the `empty?` method.
struct EmptyMatcher < Matcher
# Textual representation of what the matcher expects.
def label
"empty?"
private def match?(actual)
actual.value.empty?
end
# Determines whether the matcher is satisfied with the partial given to it.
def match(partial, negated = false)
actual = partial.actual
matched = actual.empty?
MatchData.new(matched, actual, partial.label)
def description
"is empty"
end
# Match data specific to this matcher.
private struct MatchData(T) < MatchData
# Creates the match data.
def initialize(matched, @actual : T, @actual_label : String)
super(matched)
end
private def failure_message(actual)
"#{actual.label} is not empty"
end
# Information about the match.
def named_tuple
{
expected: NegatableMatchDataValue.new([] of Nil),
actual: @actual,
}
end
private def failure_message_when_negated(actual)
"#{actual.label} is empty"
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@actual_label} is empty"
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 empty"
end
private def values(actual) : Array(LabeledValue)
[LabeledValue.new(actual.value.to_s, "actual")]
end
end
end

View file

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