Pass current example as block argument

This commit is contained in:
Michael Miller 2021-01-09 13:34:15 -07:00
parent 009ca4776a
commit 7451769a29
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
2 changed files with 22 additions and 4 deletions

View file

@ -24,8 +24,9 @@ module Spectator::DSL
macro {{name.id}}(what = nil, &block)
\{% raise "Cannot use '{{name.id}}' inside of a test block" if @def %}
\{% raise "A description or block must be provided. Cannot use '{{name.id}}' alone." unless what || block %}
\{% raise "Block argument count '{{name.id}}' hook must be 0..1" if block.args.size > 1 %}
def \%test : Nil # TODO: Pass example instance.
def \%test(\{{block.args.splat}}) : Nil
\{{block.body}}
end
@ -33,7 +34,15 @@ module Spectator::DSL
_spectator_example_name(\{{what}}),
::Spectator::Source.new(\{{block.filename}}, \{{block.line_number}}),
\{{@type.name}}.new.as(::Spectator::Context)
) { |example| example.with_context(\{{@type.name}}) { \%test } }
) do |example|
example.with_context(\{{@type.name}}) do
\{% if block.args.empty? %}
\%test
\{% else %}
\%test(example)
\{% end %}
end
end
end
end

View file

@ -26,15 +26,24 @@ module Spectator::DSL
macro define_example_hook(type)
macro {{type.id}}(&block)
\{% raise "Missing block for '{{type.id}}' hook" unless block %}
\{% raise "Block argument count '{{type.id}}' hook must be 0..1" if block.args.size > 1 %}
\{% raise "Cannot use '{{type.id}}' inside of a test block" if @def %}
def \%hook : Nil # TODO: Pass example instance.
def \%hook(\{{block.args.splat}}) : Nil
\{{block.body}}
end
::Spectator::DSL::Builder.{{type.id}}(
::Spectator::Source.new(\{{block.filename}}, \{{block.line_number}})
) { |example| example.with_context(\{{@type.name}}) { \%hook } }
) do |example|
example.with_context(\{{@type.name}}) do
\{% if block.args.empty? %}
\%hook
\{% else %}
\%hook(example)
\{% end %}
end
end
end
end