mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Handle have_
prefix for matcher
This commit is contained in:
parent
45f0f7f6d1
commit
16bcce59ae
1 changed files with 24 additions and 15 deletions
|
@ -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,26 +570,35 @@ 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.
|
||||||
descriptor = { {{method_name}}: Tuple.new({{call.args.splat}}) }
|
{% method_name = call.name[3..-1] %}
|
||||||
label = String::Builder.new({{method_name.stringify}})
|
{% elsif call.name.starts_with?("have_") %}
|
||||||
{% unless call.args.empty? %}
|
# Swap `have_` with `has_`.
|
||||||
label << '('
|
{% method_name = ("has_" + call.name[5..-1].stringify).id %}
|
||||||
{% for arg, index in call.args %}
|
|
||||||
label << {{arg}}
|
|
||||||
{% if index < call.args.size - 1 %}
|
|
||||||
label << ", "
|
|
||||||
{% end %}
|
|
||||||
{% end %}
|
|
||||||
label << ')'
|
|
||||||
{% end %}
|
|
||||||
::Spectator::Matchers::PredicateMatcher.new(descriptor, label.to_s)
|
|
||||||
{% else %}
|
{% else %}
|
||||||
{% raise "Undefined local variable or method '#{call}'" %}
|
{% raise "Undefined local variable or method '#{call}'" %}
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
|
descriptor = { {{method_name}}: Tuple.new({{call.args.splat}}) }
|
||||||
|
label = String::Builder.new({{method_name.stringify}})
|
||||||
|
{% unless call.args.empty? %}
|
||||||
|
label << '('
|
||||||
|
{% for arg, index in call.args %}
|
||||||
|
label << {{arg}}
|
||||||
|
{% if index < call.args.size - 1 %}
|
||||||
|
label << ", "
|
||||||
|
{% end %}
|
||||||
|
{% end %}
|
||||||
|
label << ')'
|
||||||
|
{% end %}
|
||||||
|
::Spectator::Matchers::PredicateMatcher.new(descriptor, label.to_s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue