Parameterless initializer that generates the label

This commit is contained in:
Michael Miller 2019-02-05 10:36:37 -07:00
parent 0e0b8f4ae2
commit 6bfd15b7f9
2 changed files with 7 additions and 1 deletions

View file

@ -423,7 +423,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({{method_name.stringify}}) ::Spectator::Matchers::PredicateMatcher(NamedTuple({{method_name}}: Nil)).new
{% else %} {% else %}
{% raise "Undefined local variable or method '#{call}'" %} {% raise "Undefined local variable or method '#{call}'" %}
{% end %} {% end %}

View file

@ -5,6 +5,12 @@ 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.
struct PredicateMatcher(ExpectedType) < Matcher struct PredicateMatcher(ExpectedType) < Matcher
# Creates the matcher.
# Constructs the label from the type parameters.
def initialize
super({{ExpectedType.keys.splat.stringify}})
end
# Determines whether the matcher is satisfied with the value given to it. # Determines whether the matcher is satisfied with the value given to it.
# True is returned if the match was successful, false otherwise. # True is returned if the match was successful, false otherwise.
def match?(partial) def match?(partial)