Allow passing arguments to predicate matcher

This commit is contained in:
Michael Miller 2019-06-01 22:46:06 -06:00
parent d969b0557a
commit 7e73ec2fe1
2 changed files with 4 additions and 3 deletions

View file

@ -574,7 +574,7 @@ module Spectator::DSL
macro method_missing(call) macro method_missing(call)
{% if call.name.starts_with?("be_") %} {% if call.name.starts_with?("be_") %}
{% method_name = call.name[3..-1] %} # Remove be_ prefix. {% method_name = call.name[3..-1] %} # Remove be_ prefix.
::Spectator::Matchers::PredicateMatcher(NamedTuple({{method_name}}: Nil)).new ::Spectator::Matchers::PredicateMatcher.new({ {{method_name}}: Tuple.new({{call.args.splat}}) }, {{method_name.stringify}})
{% else %} {% else %}
{% raise "Undefined local variable or method '#{call}'" %} {% raise "Undefined local variable or method '#{call}'" %}
{% end %} {% end %}

View file

@ -4,7 +4,8 @@ module Spectator::Matchers
# Matcher that tests one or more predicates (methods ending in '?'). # Matcher that tests one or more predicates (methods ending in '?').
# 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.
struct PredicateMatcher(ExpectedType) < Matcher # Each value is a a `Tuple` of arguments to pass to the predicate method.
struct PredicateMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Textual representation of what the matcher expects. # Textual representation of what the matcher expects.
# Constructs the label from the type parameters. # Constructs the label from the type parameters.
def label def label
@ -36,7 +37,7 @@ module Spectator::Matchers
{% begin %} {% begin %}
{ {
{% for attribute in ExpectedType.keys %} {% for attribute in ExpectedType.keys %}
{{attribute}}: actual.{{attribute}}?, {{attribute}}: actual.{{attribute}}?(*@expected[{{attribute.symbolize}}]),
{% end %} {% end %}
} }
{% end %} {% end %}