Working example creation from DSL

This commit is contained in:
Michael Miller 2020-09-14 13:55:07 -06:00
parent 7c44cba667
commit bc602d9b62
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
5 changed files with 41 additions and 4 deletions

View file

@ -1,5 +1,6 @@
# require "./dsl/*"
require "./dsl/builder"
require "./dsl/examples"
require "./dsl/groups"
module Spectator

View file

@ -13,5 +13,9 @@ module Spectator::DSL
def end_group(*args)
@@builder.end_group(*args)
end
def add_example(*args, &block : Example, Context ->)
@@builder.add_example(*args, &block)
end
end
end

View file

@ -1,8 +1,29 @@
require "../source"
require "../spec_builder"
require "./builder"
module Spectator
module DSL
module Spectator::DSL
module Examples
macro example(what = nil, *, _source_file = __FILE__, _source_line = __LINE__, &block)
def %test
{{block.body}}
end
%source = ::Spectator::Source.new({{_source_file}}, {{_source_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 it(what = nil, *, _source_file = __FILE__, _source_line = __LINE__, &block)
example({{what}}, _source_file: {{_source_file}}, _source_line: {{_source_line}}) {{block}}
end
macro specify(what = nil, *, _source_file = __FILE__, _source_line = __LINE__, &block)
example({{what}}, _source_file: {{_source_file}}, _source_line: {{_source_line}}) {{block}}
end
end
macro it(description = nil, _source_file = __FILE__, _source_line = __LINE__, &block)
{% if block.is_a?(Nop) %}
{% if description.is_a?(Call) %}
@ -60,5 +81,4 @@ module Spectator
macro xit(description = nil, &block)
pending({{description}}) {{block}}
end
end
end

View file

@ -1,4 +1,5 @@
require "../example"
require "../example_context_method"
require "../example_group"
module Spectator
@ -38,6 +39,16 @@ module Spectator
@group_stack.pop
end
def add_example(name, source, context, &block : Example, Context ->)
{% if flag?(:spectator_debug) %}
puts "Add example: #{name} @ #{source}"
puts "Context: #{context}"
{% end %}
delegate = ExampleContextDelegate.new(context, block)
Example.new(delegate, name, source, current_group)
# The example is added to the current group by `Example` initializer.
end
def build
raise NotImplementedError.new("#build")
end

View file

@ -2,6 +2,7 @@ require "./spectator_context"
require "./spectator/dsl"
class SpectatorTestContext < SpectatorContext
include ::Spectator::DSL::Examples
include ::Spectator::DSL::Groups
# Initial implicit subject for tests.