mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add have_size_of matcher
This commit is contained in:
parent
bb2b834662
commit
efde29c90e
3 changed files with 175 additions and 0 deletions
|
@ -480,6 +480,16 @@ module Spectator::DSL
|
|||
::Spectator::Matchers::SizeMatcher.new({{expected}}, {{expected.stringify}})
|
||||
end
|
||||
|
||||
# Indicates that some set should have the same size (number of elements) as another set.
|
||||
#
|
||||
# Example:
|
||||
# ```
|
||||
# expect([1, 2, 3]).to have_size_of(%i[x y z])
|
||||
# ```
|
||||
macro have_size_of(expected)
|
||||
::Spectator::Matchers::SizeOfMatcher.new(({{expected}}), {{expected.stringify}})
|
||||
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.
|
||||
|
|
44
src/spectator/matchers/size_of_matcher.cr
Normal file
44
src/spectator/matchers/size_of_matcher.cr
Normal file
|
@ -0,0 +1,44 @@
|
|||
require "./value_matcher"
|
||||
|
||||
module Spectator::Matchers
|
||||
# Matcher that tests whether a set has the same number of elements as another set.
|
||||
# The set's `#size` method is used for this check.
|
||||
struct SizeOfMatcher(ExpectedType) < ValueMatcher(ExpectedType)
|
||||
# Determines whether the matcher is satisfied with the partial given to it.
|
||||
# `MatchData` is returned that contains information about the match.
|
||||
def match(partial)
|
||||
actual = partial.actual.size
|
||||
size = expected.size
|
||||
values = ExpectedActual.new(size, label, actual, partial.label)
|
||||
MatchData.new(actual == size, values)
|
||||
end
|
||||
|
||||
# Match data specific to this matcher.
|
||||
private struct MatchData(ExpectedType, ActualType) < MatchData
|
||||
# Creates the match data.
|
||||
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
|
||||
super(matched)
|
||||
end
|
||||
|
||||
# Information about the match.
|
||||
def named_tuple
|
||||
{
|
||||
expected: NegatableMatchDataValue.new(@values.expected),
|
||||
actual: @values.actual,
|
||||
}
|
||||
end
|
||||
|
||||
# Describes the condition that satisfies the matcher.
|
||||
# This is informational and displayed to the end-user.
|
||||
def message
|
||||
"#{@values.actual_label} has the same number of elements as #{@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} has a different number of elements than #{@values.expected_label}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue