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

@ -12,11 +12,11 @@ module Spectator::Matchers
"matches #{expected.label}" "matches #{expected.label}"
end end
def failure_message(actual) private def failure_message(actual)
"#{actual.label} did not match #{expected.label}" "#{actual.label} does not match #{expected.label}"
end end
def failure_message_when_negated(actual) private def failure_message_when_negated(actual)
"#{actual.label} matched #{expected.label}" "#{actual.label} matched #{expected.label}"
end end
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. # 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. # The values are checked with the `includes?` method.
struct ContainMatcher(ExpectedType) < ValueMatcher(ExpectedType) struct ContainMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
private def match?(actual) private def match?(actual)
expected.all? do |item| expected.value.all? do |item|
actual.includes?(item) actual.value.includes?(item)
end end
end end
# Determines whether the matcher is satisfied with the partial given to it. def description
def match(partial, negated = false) "contains #{expected.label}"
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end end
# Match data specific to this matcher. private def failure_message(actual)
private struct MatchData(ExpectedType, ActualType) < MatchData "#{actual.label} does not match #{expected.label}"
# Creates the match data.
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
super(matched)
end end
# Information about the match. private def failure_message_when_negated(actual)
def named_tuple "#{actual.label} contains #{expected.label}"
{
subset: NegatableMatchDataValue.new(@values.expected),
superset: @values.actual,
}
end end
# Describes the condition that satisfies the matcher. private def values(actual) : Array(LabeledValue)
# This is informational and displayed to the end-user. [
def message LabeledValue.new(expected.value.to_s, "subset"),
"#{@values.actual_label} contains #{@values.expected_label}" LabeledValue.new(actual.value.to_s, "superset"),
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
end end
end end
end end

View file

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

View file

@ -4,43 +4,20 @@ module Spectator::Matchers
# Common matcher that tests whether two values equal each other. # Common matcher that tests whether two values equal each other.
# The values are compared with the == operator. # The values are compared with the == operator.
struct EqualityMatcher(ExpectedType) < ValueMatcher(ExpectedType) struct EqualityMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
private def match?(actual) private def match?(actual)
actual == expected expected.value == actual.value
end end
# Determines whether the matcher is satisfied with the partial given to it. def description
def match(partial, negated = false) "equals #{expected.label}"
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end end
# Match data specific to this matcher. private def failure_message(actual)
private struct MatchData(ExpectedType, ActualType) < MatchData "#{actual.label} does not equal #{expected.label}"
# Creates the match data.
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
super(matched)
end end
# Information about the match. private def failure_message_when_negated(actual)
def named_tuple "#{actual.label} equals #{expected.label}"
{
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
end end
end end
end end