mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Refactor AttributesMatcher to use new style
This commit is contained in:
parent
6a317b7a21
commit
ec96bf2de2
1 changed files with 40 additions and 48 deletions
|
@ -1,71 +1,63 @@
|
||||||
require "./value_matcher"
|
require "../test_value"
|
||||||
|
require "./failed_match_data"
|
||||||
|
require "./matcher"
|
||||||
|
require "./successful_match_data"
|
||||||
|
|
||||||
module Spectator::Matchers
|
module Spectator::Matchers
|
||||||
# Matcher that tests that multiple attributes match specified conditions.
|
# Matcher that tests that multiple attributes match specified conditions.
|
||||||
# The attributes are tested with the === operator.
|
# The attributes are tested with the === operator.
|
||||||
# The `ExpectedType` type param should be a `NamedTuple`.
|
# The `ExpectedType` type param should be a `NamedTuple`.
|
||||||
struct AttributesMatcher(ExpectedType) < ValueMatcher(ExpectedType)
|
# Each key in the tuple is the attribute/method name,
|
||||||
# Determines whether the matcher is satisfied with the value given to it.
|
# and the corresponding value is the expected value to match against.
|
||||||
private def match?(actual)
|
struct AttributesMatcher(ExpectedType) < Matcher
|
||||||
# Test each attribute and immediately return false if a comparison fails.
|
private getter expected
|
||||||
{% for attribute in ExpectedType.keys %}
|
|
||||||
return false unless expected[{{attribute.symbolize}}] === actual[{{attribute.symbolize}}]
|
|
||||||
{% end %}
|
|
||||||
|
|
||||||
# All checks passed if this point is reached.
|
def initialize(@expected : TestValue(ExpectedType))
|
||||||
true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Determines whether the matcher is satisfied with the partial given to it.
|
def description
|
||||||
def match(partial, negated = false)
|
"has attributes #{expected.label}"
|
||||||
actual_values = snapshot_values(partial.actual)
|
end
|
||||||
values = ExpectedActual.new(expected, label, actual_values, partial.label)
|
|
||||||
MatchData.new(match?(actual_values), values)
|
def match(actual)
|
||||||
|
snapshot = snapshot_values(actual.value)
|
||||||
|
if matched?(snapshot)
|
||||||
|
SuccessfulMatchData.new
|
||||||
|
else
|
||||||
|
FailedMatchData.new("#{actual.label} does not have attributes #{expected.label}", **values(snapshot))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def negated_match(actual)
|
||||||
|
snapshot = snapshot_values(actual.value)
|
||||||
|
if matched?(snapshot)
|
||||||
|
FailedMatchData.new("#{actual.label} has attributes #{expected.label}", **values(snapshot))
|
||||||
|
else
|
||||||
|
SuccessfulMatchData.new
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Captures all of the actual values.
|
# Captures all of the actual values.
|
||||||
# A `NamedTuple` is returned,
|
# A `NamedTuple` is returned, with each key being the attribute.
|
||||||
# with each key being the attribute.
|
private def snapshot_values(object)
|
||||||
private def snapshot_values(actual)
|
|
||||||
{% begin %}
|
{% begin %}
|
||||||
{
|
{
|
||||||
{% for attribute in ExpectedType.keys %}
|
{% for attribute in ExpectedType.keys %}
|
||||||
{{attribute}}: actual.{{attribute}},
|
{{attribute}}: object.{{attribute}},
|
||||||
{% end %}
|
{% end %}
|
||||||
}
|
}
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Match data specific to this matcher.
|
private def values(snapshot)
|
||||||
private struct MatchData(ExpectedType, ActualType) < MatchData
|
{% begin %}
|
||||||
# Creates the match data.
|
{
|
||||||
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
|
{% for attribute in ExpectedType.keys %}
|
||||||
super(matched)
|
{{"expected " + attribute.stringify}}: expected.value[{{attribute.symbolize}}]),
|
||||||
end
|
{{"actual " + attribute.stringify}}: snapshot[{{attribute.symbolize}}],
|
||||||
|
|
||||||
# Information about the match.
|
|
||||||
def named_tuple
|
|
||||||
{% begin %}
|
|
||||||
{
|
|
||||||
{% for attribute in ExpectedType.keys %}
|
|
||||||
{{"expected " + attribute.stringify}}: NegatableMatchDataValue.new(@values.expected[{{attribute.symbolize}}]),
|
|
||||||
{{"actual " + attribute.stringify}}: @values.actual[{{attribute.symbolize}}],
|
|
||||||
{% end %}
|
|
||||||
}
|
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
}
|
||||||
|
{% end %}
|
||||||
# Describes the condition that satisfies the matcher.
|
|
||||||
# This is informational and displayed to the end-user.
|
|
||||||
def message
|
|
||||||
"#{@values.actual_label} has attributes #{@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 attributes #{@values.expected_label}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue