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
define_example(:example)
define_example :example
macro it(what = nil, *, _source_file = __FILE__, _source_line = __LINE__, &block)
{% puts "#{_source_file}:#{_source_line}" %}
def %test
{{yield}}
end
define_example :it
%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
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
define_example :specify
end
macro it(description = nil, _source_file = __FILE__, _source_line = __LINE__, &block)
{% if block.is_a?(Nop) %}

View file

@ -5,25 +5,30 @@ module Spectator::DSL
# DSL methods and macros for creating example groups.
# This module should be included as a mix-in.
module Groups
# Defines a new example group.
# 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 define_example_group(name)
# Defines a new example group.
# 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}}
# Source: {{_source_file}}:{{_source_line}}
macro example_group(what, *, _source_file = __FILE__, _source_line = __LINE__, &block)
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) %}
::Spectator::DSL::Builder.end_group
end
end
end
macro describe(what, *, _source_file = __FILE__, _source_line = __LINE__, &block)
example_group({{what}}, _source_file: {{_source_file}}, _source_line: {{_source_line}}) {{block}}
end
define_example_group :example_group
macro context(what, *, _source_file = __FILE__, _source_line = __LINE__, &block)
example_group({{what}}, _source_file: {{_source_file}}, _source_line: {{_source_line}}) {{block}}
end
define_example_group :describe
define_example_group :context
# Defines the implicit subject for the test context.
# If *what* is a type, then the `described_class` method will be defined.