Nested macros for defining DSL keywords

This commit is contained in:
Michael Miller 2020-09-25 21:44:17 -06:00
parent 9103bfde0f
commit 6363436afa
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
2 changed files with 22 additions and 41 deletions

View file

@ -18,35 +18,11 @@ module Spectator::DSL
end end
end end
define_example(:example) define_example :example
macro it(what = nil, *, _source_file = __FILE__, _source_line = __LINE__, &block) define_example :it
{% puts "#{_source_file}:#{_source_line}" %}
def %test
{{yield}}
end
%source = ::Spectator::Source.new(__FILE__, __LINE__) define_example :specify
::Spectator::DSL::Builder.add_example(
{{what.is_a?(StringLiteral | NilLiteral) ? what : what.stringify}},
%source,
{{@type.name}}.new
) { |example, context| context.as({{@type.name}}).%test }
end
macro specify(what = nil, *, _source_file = __FILE__, _source_line = __LINE__, &block)
{% puts "#{_source_file}:#{_source_line}" %}
def %test
{{yield}}
end
%source = ::Spectator::Source.new(__FILE__, __LINE__)
::Spectator::DSL::Builder.add_example(
{{what.is_a?(StringLiteral | NilLiteral) ? what : what.stringify}},
%source,
{{@type.name}}.new
) { |example, context| context.as({{@type.name}}).%test }
end
end end
macro it(description = nil, _source_file = __FILE__, _source_line = __LINE__, &block) macro it(description = nil, _source_file = __FILE__, _source_line = __LINE__, &block)
{% if block.is_a?(Nop) %} {% if block.is_a?(Nop) %}

View file

@ -5,25 +5,30 @@ module Spectator::DSL
# DSL methods and macros for creating example groups. # DSL methods and macros for creating example groups.
# This module should be included as a mix-in. # This module should be included as a mix-in.
module Groups module Groups
# Defines a new example group. macro define_example_group(name)
# The *what* argument is a name or description of the group. # Defines a new example group.
# If it isn't a string literal, then it is symbolized for `ExampleNode#name`. # The *what* argument is a name or description of the group.
# If it isn't a string literal, then it is symbolized for `ExampleNode#name`.
macro {{name.id}}(what, &block)
class Group%group < \{{@type.id}}; _spectator_group_subject(\{{what}})
# TODO: Handle string interpolation in examples and groups.
::Spectator::DSL::Builder.start_group(
\{{what.is_a?(StringLiteral) ? what : what.stringify}},
::Spectator::Source.new(__FILE__, __LINE__)
)
\{{block.body}}
# Example group: {{what.stringify}} ::Spectator::DSL::Builder.end_group
# Source: {{_source_file}}:{{_source_line}} end
macro example_group(what, *, _source_file = __FILE__, _source_line = __LINE__, &block) end
class Group%group < {{@type.id}}; _spectator_group_subject({{what}}); ::Spectator::DSL::Builder.start_group({{what.is_a?(StringLiteral) ? what : what.stringify}}, ::Spectator::Source.new(__FILE__, __LINE__)); {{block.body}}; ::Spectator::DSL::Builder.end_group; end
{% debug(false) %}
end end
macro describe(what, *, _source_file = __FILE__, _source_line = __LINE__, &block) define_example_group :example_group
example_group({{what}}, _source_file: {{_source_file}}, _source_line: {{_source_line}}) {{block}}
end
macro context(what, *, _source_file = __FILE__, _source_line = __LINE__, &block) define_example_group :describe
example_group({{what}}, _source_file: {{_source_file}}, _source_line: {{_source_line}}) {{block}}
end define_example_group :context
# Defines the implicit subject for the test context. # Defines the implicit subject for the test context.
# If *what* is a type, then the `described_class` method will be defined. # If *what* is a type, then the `described_class` method will be defined.