Refactor HaveMatcher

This commit is contained in:
Michael Miller 2019-08-08 15:26:30 -06:00
parent f1ebce7739
commit 17aa472d92

View file

@ -5,67 +5,51 @@ module Spectator::Matchers
# For a `String`, the `includes?` method is used. # For a `String`, the `includes?` method is used.
# Otherwise, it expects an `Enumerable` and iterates over each item until === is true. # Otherwise, it expects an `Enumerable` and iterates over each item until === is true.
struct HaveMatcher(ExpectedType) < ValueMatcher(ExpectedType) struct HaveMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
# True is returned if the match was successful, false otherwise.
private def match?(actual) private def match?(actual)
if actual.is_a?(String) actual_value = actual.value
match_string?(actual) if actual_value.is_a?(String)
match_string(actual_value)
else else
match_enumerable?(actual) match_enumerable(actual_value)
end end
end 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_value)
expected.all? do |item| expected.value.all? do |item|
actual.includes?(item) actual_value.includes?(item)
end end
end end
# Checks if an `Enumerable` matches the expected values. # Checks if an `Enumerable` matches the expected values.
# The `===` operator is used on every item. # The `===` operator is used on every item.
private def match_enumerable?(actual) private def match_enumerable?(actual_value)
array = actual.to_a array = actual_value.to_a
expected.all? do |item| expected.value.all? do |item|
array.any? do |elem| array.any? do |element|
item === elem item === element
end end
end end
end end
# Determines whether the matcher is satisfied with the partial given to it. def description
def match(partial, negated = false) "includes #{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 include #{expected.label}"
# Creates the match data. end
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
super(matched)
end
# Information about the match. private def failure_message_when_negated(actual)
def named_tuple "#{actual.label} includes #{expected.label}"
{ end
subset: NegatableMatchDataValue.new(@values.expected),
superset: @values.actual,
}
end
# Describes the condition that satisfies the matcher. private def values(actual)
# This is informational and displayed to the end-user. {
def message subset: expected.value.inspect,
"#{@values.actual_label} includes #{@values.expected_label}" superset: actual.value.inspect,
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