Refactor HaveKeyMatcher and HaveValueMatcher

This commit is contained in:
Michael Miller 2019-08-08 15:25:32 -06:00
parent 7cca43029e
commit f1ebce7739
2 changed files with 32 additions and 62 deletions

View file

@ -4,44 +4,29 @@ module Spectator::Matchers
# Matcher that tests whether a `Hash` (or similar type) has a given key. # Matcher that tests whether a `Hash` (or similar type) has a given key.
# The set is checked with the `has_key?` method. # The set is checked with the `has_key?` method.
struct HaveKeyMatcher(ExpectedType) < ValueMatcher(ExpectedType) struct HaveKeyMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
private def match?(actual) private def match?(actual)
actual.has_key?(expected) actual.value.has_key?(expected.value)
end end
# Determines whether the matcher is satisfied with the partial given to it. def description
def match(partial, negated = false) "has key #{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 have key #{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} has key #{expected.label}"
actual = @values.actual end
private def values(actual)
actual_value = actual.value
set = actual_value.responds_to?(:keys) ? actual_value.keys : actual_value
{ {
key: NegatableMatchDataValue.new(@values.expected), key: expected.value.inspect,
actual: actual.responds_to?(:keys) ? actual.keys : actual, actual: set.inspect,
} }
end end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@values.actual_label} has key #{@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 have key #{@values.expected_label}"
end
end
end end
end end

View file

@ -4,44 +4,29 @@ module Spectator::Matchers
# Matcher that tests whether a `Hash` (or similar type) has a given value. # Matcher that tests whether a `Hash` (or similar type) has a given value.
# The set is checked with the `has_value?` method. # The set is checked with the `has_value?` method.
struct HaveValueMatcher(ExpectedType) < ValueMatcher(ExpectedType) struct HaveValueMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
private def match?(actual) private def match?(actual)
actual.has_value?(expected) actual.value.has_value?(expected.value)
end end
# Determines whether the matcher is satisfied with the partial given to it. def description
def match(partial, negated = false) "has value #{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 have value #{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} has value #{expected.label}"
actual = @values.actual end
private def values(actual)
actual_value = actual.value
set = actual_value.responds_to?(:values) ? actual_value.values : actual_value
{ {
value: NegatableMatchDataValue.new(@values.expected), value: expected.value.inspect,
actual: actual.responds_to?(:values) ? actual.values : actual, actual: set.inspect,
} }
end end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@values.actual_label} has value #{@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 have value #{@values.expected_label}"
end
end
end end
end end