Handle `have_` prefix for matcher

This commit is contained in:
Michael Miller 2019-06-01 23:14:58 -06:00
parent 45f0f7f6d1
commit 16bcce59ae
1 changed files with 24 additions and 15 deletions

View File

@ -561,7 +561,7 @@ module Spectator::DSL
end end
# Used to create predicate matchers. # Used to create predicate matchers.
# Any missing method that starts with 'be_' will be handled. # Any missing method that starts with 'be_' or 'have_' will be handled.
# All other method names will be ignored and raise a compile-time error. # All other method names will be ignored and raise a compile-time error.
# #
# This can be used to simply check a predicate method that ends in '?'. # This can be used to simply check a predicate method that ends in '?'.
@ -570,10 +570,22 @@ module Spectator::DSL
# expect("foobar").to be_ascii_only # expect("foobar").to be_ascii_only
# # Is equivalent to: # # Is equivalent to:
# expect("foobar".ascii_only?).to be_true # expect("foobar".ascii_only?).to be_true
#
# expect("foobar").to_not have_back_references
# # Is equivalent to:
# expect("foobar".has_back_references?).to_not be_true
# ``` # ```
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. # Remove `be_` prefix.
{% method_name = call.name[3..-1] %}
{% elsif call.name.starts_with?("have_") %}
# Swap `have_` with `has_`.
{% method_name = ("has_" + call.name[5..-1].stringify).id %}
{% else %}
{% raise "Undefined local variable or method '#{call}'" %}
{% end %}
descriptor = { {{method_name}}: Tuple.new({{call.args.splat}}) } descriptor = { {{method_name}}: Tuple.new({{call.args.splat}}) }
label = String::Builder.new({{method_name.stringify}}) label = String::Builder.new({{method_name.stringify}})
{% unless call.args.empty? %} {% unless call.args.empty? %}
@ -587,9 +599,6 @@ module Spectator::DSL
label << ')' label << ')'
{% end %} {% end %}
::Spectator::Matchers::PredicateMatcher.new(descriptor, label.to_s) ::Spectator::Matchers::PredicateMatcher.new(descriptor, label.to_s)
{% else %}
{% raise "Undefined local variable or method '#{call}'" %}
{% end %}
end end
end end
end end