mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add have_attributes
matcher
This one is kind of magical.
This commit is contained in:
parent
c504c945a7
commit
1c9cb41fa2
2 changed files with 48 additions and 0 deletions
|
@ -394,5 +394,19 @@ module Spectator::DSL
|
||||||
macro have(*expected)
|
macro have(*expected)
|
||||||
::Spectator::Matchers::HaveMatcher.new({{expected.splat.stringify}}, {{expected}})
|
::Spectator::Matchers::HaveMatcher.new({{expected.splat.stringify}}, {{expected}})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Indicates that some value should have a set of attributes matching some conditions.
|
||||||
|
# A list of named arguments are expected.
|
||||||
|
# The names correspond to the attributes in the instance to check.
|
||||||
|
# The values are conditions to check with the `===` operator against the attribute's value.
|
||||||
|
#
|
||||||
|
# Examples:
|
||||||
|
# ```
|
||||||
|
# expect("foobar").to have_attributes(size: 6, upcase: "FOOBAR")
|
||||||
|
# expect(%i[a b c]).to have_attributes(size: 1..5, first: Symbol)
|
||||||
|
# ```
|
||||||
|
macro have_attributes(**expected)
|
||||||
|
::Spectator::Matchers::AttributesMatcher.new({{expected.double_splat.stringify}}, {{expected}})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
34
src/spectator/matchers/attributes_matcher.cr
Normal file
34
src/spectator/matchers/attributes_matcher.cr
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
require "./value_matcher"
|
||||||
|
|
||||||
|
module Spectator::Matchers
|
||||||
|
# Matcher that tests that multiple attributes match specified conditions.
|
||||||
|
# The attributes are tested with the `===` operator.
|
||||||
|
# The `ExpectedType` type param should be a `NamedTuple`.
|
||||||
|
struct AttributesMatcher(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.
|
||||||
|
def match?(partial)
|
||||||
|
actual = partial.actual
|
||||||
|
|
||||||
|
# Test each attribute and immediately return false if a comparison fails.
|
||||||
|
{% for attribute in ExpectedType.keys %}
|
||||||
|
return false unless expected[{{attribute.symbolize}}] === actual.{{attribute}}
|
||||||
|
{% end %}
|
||||||
|
|
||||||
|
# All checks passed if this point is reached.
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
# Describes the condition that satisfies the matcher.
|
||||||
|
# This is informational and displayed to the end-user.
|
||||||
|
def message(partial)
|
||||||
|
"Expected #{partial.label} to have #{label}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Describes the condition that won't satsify the matcher.
|
||||||
|
# This is informational and displayed to the end-user.
|
||||||
|
def negated_message(partial)
|
||||||
|
"Expected #{partial.label} to not have #{label}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue