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)
{% if call.name.starts_with?("be_") %}
{% 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 %}
{% raise "Undefined local variable or method '#{call}'" %}
{% end %}

View file

@ -5,6 +5,12 @@ module Spectator::Matchers
# The `ExpectedType` type param should be a `NamedTuple`.
# Each key in the tuple is a predicate (without the `?`) to test.
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.
# True is returned if the match was successful, false otherwise.
def match?(partial)