mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Refactor predicate and respond matchers
Add missing match? method to AttributesMatcher.
This commit is contained in:
parent
520c738f6a
commit
3ae16c6ec1
4 changed files with 108 additions and 124 deletions
|
@ -21,7 +21,7 @@ module Spectator::Matchers
|
||||||
|
|
||||||
def match(actual)
|
def match(actual)
|
||||||
snapshot = snapshot_values(actual.value)
|
snapshot = snapshot_values(actual.value)
|
||||||
if matched?(snapshot)
|
if match?(snapshot)
|
||||||
SuccessfulMatchData.new
|
SuccessfulMatchData.new
|
||||||
else
|
else
|
||||||
FailedMatchData.new("#{actual.label} does not have attributes #{expected.label}", **values(snapshot))
|
FailedMatchData.new("#{actual.label} does not have attributes #{expected.label}", **values(snapshot))
|
||||||
|
@ -30,7 +30,7 @@ module Spectator::Matchers
|
||||||
|
|
||||||
def negated_match(actual)
|
def negated_match(actual)
|
||||||
snapshot = snapshot_values(actual.value)
|
snapshot = snapshot_values(actual.value)
|
||||||
if matched?(snapshot)
|
if match?(snapshot)
|
||||||
FailedMatchData.new("#{actual.label} has attributes #{expected.label}", **values(snapshot))
|
FailedMatchData.new("#{actual.label} has attributes #{expected.label}", **values(snapshot))
|
||||||
else
|
else
|
||||||
SuccessfulMatchData.new
|
SuccessfulMatchData.new
|
||||||
|
@ -49,6 +49,16 @@ module Spectator::Matchers
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def match?(snapshot)
|
||||||
|
# Test that every attribute has the expected value.
|
||||||
|
{% for attribute in ExpectedType.keys %}
|
||||||
|
return false unless expected.value[{{attribute.symbolize}}] === snapshot[{{attribute.symbolize}}]
|
||||||
|
{% end %}
|
||||||
|
|
||||||
|
# At this point, none of the checks failed, so the match was successful.
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
private def values(snapshot)
|
private def values(snapshot)
|
||||||
{% begin %}
|
{% begin %}
|
||||||
{
|
{
|
||||||
|
|
|
@ -7,57 +7,53 @@ module Spectator::Matchers
|
||||||
# Each key in the tuple is a predicate (without the '?' and 'has_' prefix) to test.
|
# Each key in the tuple is a predicate (without the '?' and 'has_' prefix) to test.
|
||||||
# Each value is a a `Tuple` of arguments to pass to the predicate method.
|
# Each value is a a `Tuple` of arguments to pass to the predicate method.
|
||||||
struct HavePredicateMatcher(ExpectedType) < ValueMatcher(ExpectedType)
|
struct HavePredicateMatcher(ExpectedType) < ValueMatcher(ExpectedType)
|
||||||
# Determines whether the matcher is satisfied with the value given to it.
|
private getter expected
|
||||||
private def match?(values)
|
|
||||||
# Test each predicate and immediately return false if one is false.
|
|
||||||
{% for attribute in ExpectedType.keys %}
|
|
||||||
return false unless values[{{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 #{expected.label}"
|
||||||
values = snapshot_values(partial.actual)
|
end
|
||||||
MatchData.new(match?(values), values, partial.label, label)
|
|
||||||
|
def match(actual)
|
||||||
|
snapshot = snapshot_values(actual.value)
|
||||||
|
if match?(snapshot)
|
||||||
|
SuccessfulMatchData.new
|
||||||
|
else
|
||||||
|
FailedMatchData.new("#{actual.label} does not have #{expected.label}", **snapshot)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def negated_match(actual)
|
||||||
|
snapshot = snapshot_values(actual.value)
|
||||||
|
if match?(snapshot)
|
||||||
|
FailedMatchData.new("#{actual.label} has #{expected.label}", **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.has_{{attribute}}?(*@expected[{{attribute.symbolize}}]),
|
{{attribute}}: object.has_{{attribute}}?(*@expected[{{attribute.symbolize}}]),
|
||||||
{% end %}
|
{% end %}
|
||||||
}
|
}
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Match data specific to this matcher.
|
private def match?(snapshot)
|
||||||
private struct MatchData(ActualType) < MatchData
|
# Test each predicate and immediately return false if one is false.
|
||||||
# Creates the match data.
|
{% for attribute in ExpectedType.keys %}
|
||||||
def initialize(matched, @named_tuple : ActualType, @actual_label : String, @expected_label : String)
|
return false unless snapshot[{{attribute.symbolize}}]
|
||||||
super(matched)
|
{% end %}
|
||||||
end
|
|
||||||
|
|
||||||
# Information about the match.
|
# All checks passed if this point is reached.
|
||||||
getter named_tuple
|
true
|
||||||
|
|
||||||
# Describes the condition that satisfies the matcher.
|
|
||||||
# This is informational and displayed to the end-user.
|
|
||||||
def message
|
|
||||||
"#{@actual_label} has #{@expected_label}"
|
|
||||||
end
|
|
||||||
|
|
||||||
# Describes the condition that won't satsify the matcher.
|
|
||||||
# This is informational and displayed to the end-user.
|
|
||||||
def negated_message
|
|
||||||
"#{@actual_label} does not have #{@expected_label}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,58 +5,54 @@ module Spectator::Matchers
|
||||||
# The `ExpectedType` type param should be a `NamedTuple`.
|
# The `ExpectedType` type param should be a `NamedTuple`.
|
||||||
# Each key in the tuple is a predicate (without the '?') to test.
|
# Each key in the tuple is a predicate (without the '?') to test.
|
||||||
# Each value is a a `Tuple` of arguments to pass to the predicate method.
|
# Each value is a a `Tuple` of arguments to pass to the predicate method.
|
||||||
struct PredicateMatcher(ExpectedType) < ValueMatcher(ExpectedType)
|
struct PredicateMatcher(ExpectedType) < Matcher(ExpectedType)
|
||||||
# Determines whether the matcher is satisfied with the value given to it.
|
private getter expected
|
||||||
private def match?(values)
|
|
||||||
# Test each predicate and immediately return false if one is false.
|
|
||||||
{% for attribute in ExpectedType.keys %}
|
|
||||||
return false unless values[{{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)
|
"is #{expected.label}"
|
||||||
values = snapshot_values(partial.actual)
|
end
|
||||||
MatchData.new(match?(values), values, partial.label, label)
|
|
||||||
|
def match(actual)
|
||||||
|
snapshot = snapshot_values(actual.value)
|
||||||
|
if match?(snapshot)
|
||||||
|
SuccessfulMatchData.new
|
||||||
|
else
|
||||||
|
FailedMatchData.new("#{actual.label} is not #{expected.label}", **snapshot)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def negated_match(actual)
|
||||||
|
snapshot = snapshot_values(actual.value)
|
||||||
|
if match?(snapshot)
|
||||||
|
FailedMatchData.new("#{actual.label} is #{expected.label}", **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}}?(*@expected[{{attribute.symbolize}}]),
|
{{attribute}}: object.{{attribute}}?(*@expected[{{attribute.symbolize}}]),
|
||||||
{% end %}
|
{% end %}
|
||||||
}
|
}
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Match data specific to this matcher.
|
private def match?(snapshot)
|
||||||
private struct MatchData(ActualType) < MatchData
|
# Test each predicate and immediately return false if one is false.
|
||||||
# Creates the match data.
|
{% for attribute in ExpectedType.keys %}
|
||||||
def initialize(matched, @named_tuple : ActualType, @actual_label : String, @expected_label : String)
|
return false unless snapshot[{{attribute.symbolize}}]
|
||||||
super(matched)
|
{% end %}
|
||||||
end
|
|
||||||
|
|
||||||
# Information about the match.
|
# All checks passed if this point is reached.
|
||||||
getter named_tuple
|
true
|
||||||
|
|
||||||
# Describes the condition that satisfies the matcher.
|
|
||||||
# This is informational and displayed to the end-user.
|
|
||||||
def message
|
|
||||||
"#{@actual_label} is #{@expected_label}"
|
|
||||||
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 #{@expected_label}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,67 +6,49 @@ module Spectator::Matchers
|
||||||
# The `ExpectedType` type param should be a `NamedTuple`,
|
# The `ExpectedType` type param should be a `NamedTuple`,
|
||||||
# with each key being the method to check and the value is ignored.
|
# with each key being the method to check and the value is ignored.
|
||||||
struct RespondMatcher(ExpectedType) < Matcher
|
struct RespondMatcher(ExpectedType) < Matcher
|
||||||
# Determines whether the matcher is satisfied with the value given to it.
|
def description
|
||||||
private def match?(actual)
|
"responds to #{label}"
|
||||||
# The snapshot did the hard work.
|
|
||||||
# Here just check if all values are true.
|
|
||||||
actual.values.all?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Determines whether the matcher is satisfied with the partial given to it.
|
private def label
|
||||||
def match(partial, negated = false)
|
# Prefix every method name with # and join them with commas.
|
||||||
values = snapshot_values(partial.actual)
|
{{ExpectedType.keys.map { |e| "##{e}".id }.splat.stringify}}
|
||||||
MatchData.new(match?(values), values, partial.label, label)
|
end
|
||||||
|
|
||||||
|
def match(actual)
|
||||||
|
snapshot = snapshot_values(actual.value)
|
||||||
|
if match?(snapshot)
|
||||||
|
SuccessfulMatchData.new
|
||||||
|
else
|
||||||
|
FailedMatchData.new("#{actual.label} does not respond to #{label}", **snapshot)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def negated_match(actual)
|
||||||
|
snapshot = snapshot_values(actual.value)
|
||||||
|
if match?(snapshot)
|
||||||
|
FailedMatchData.new("#{actual.label} responds to #{label}", **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 method in ExpectedType.keys %}
|
{% for attribute in ExpectedType.keys %}
|
||||||
{{method.stringify}}: actual.responds_to?({{method.symbolize}}),
|
{{attribute}}: object.responds_to?({{attribute.symbolize}}),
|
||||||
{% end %}
|
{% end %}
|
||||||
}
|
}
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Textual representation of what the matcher expects.
|
private def match?(snapshot)
|
||||||
def label
|
# The snapshot did the hard work.
|
||||||
# Prefix every method name with # and join them with commas.
|
# Here just check if all values are true.
|
||||||
{{ExpectedType.keys.map { |e| "##{e}".id }.splat.stringify}}
|
snapshot.values.all?
|
||||||
end
|
|
||||||
|
|
||||||
# Match data specific to this matcher.
|
|
||||||
private struct MatchData(ActualType) < MatchData
|
|
||||||
# Creates the match data.
|
|
||||||
def initialize(matched, @actual : ActualType, @actual_label : String, @expected_label : String)
|
|
||||||
super(matched)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Information about the match.
|
|
||||||
def named_tuple
|
|
||||||
{% begin %}
|
|
||||||
{
|
|
||||||
{% for method in ActualType.keys %}
|
|
||||||
{{"responds to #" + method.stringify}}: @actual[{{method.symbolize}}],
|
|
||||||
{% end %}
|
|
||||||
}
|
|
||||||
{% end %}
|
|
||||||
end
|
|
||||||
|
|
||||||
# Describes the condition that satisfies the matcher.
|
|
||||||
# This is informational and displayed to the end-user.
|
|
||||||
def message
|
|
||||||
"#{@actual_label} responds to #{@expected_label}"
|
|
||||||
end
|
|
||||||
|
|
||||||
# Describes the condition that won't satsify the matcher.
|
|
||||||
# This is informational and displayed to the end-user.
|
|
||||||
def negated_message
|
|
||||||
"#{@actual_label} does not respond to #{@expected_label}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue