mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Refactor HaveKeyMatcher and HaveValueMatcher
This commit is contained in:
parent
7cca43029e
commit
f1ebce7739
2 changed files with 32 additions and 62 deletions
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue