From bc602d9b62ba9ae5f4ba9e5ebbf6e7a7607b0ba8 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 14 Sep 2020 13:55:07 -0600 Subject: [PATCH] Working example creation from DSL --- src/spectator/dsl.cr | 1 + src/spectator/dsl/builder.cr | 4 ++++ src/spectator/dsl/examples.cr | 28 ++++++++++++++++++++++++---- src/spectator/spec/builder.cr | 11 +++++++++++ src/spectator_test_context.cr | 1 + 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/spectator/dsl.cr b/src/spectator/dsl.cr index 94d748a..f4cd763 100644 --- a/src/spectator/dsl.cr +++ b/src/spectator/dsl.cr @@ -1,5 +1,6 @@ # require "./dsl/*" require "./dsl/builder" +require "./dsl/examples" require "./dsl/groups" module Spectator diff --git a/src/spectator/dsl/builder.cr b/src/spectator/dsl/builder.cr index de91e76..13284a8 100644 --- a/src/spectator/dsl/builder.cr +++ b/src/spectator/dsl/builder.cr @@ -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 diff --git a/src/spectator/dsl/examples.cr b/src/spectator/dsl/examples.cr index b356ea5..ba39adb 100644 --- a/src/spectator/dsl/examples.cr +++ b/src/spectator/dsl/examples.cr @@ -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 diff --git a/src/spectator/spec/builder.cr b/src/spectator/spec/builder.cr index 22e9324..27a97b9 100644 --- a/src/spectator/spec/builder.cr +++ b/src/spectator/spec/builder.cr @@ -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 diff --git a/src/spectator_test_context.cr b/src/spectator_test_context.cr index 208f5cb..2031005 100644 --- a/src/spectator_test_context.cr +++ b/src/spectator_test_context.cr @@ -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.