diff --git a/src/spectator.cr b/src/spectator.cr index e229e45..52c09ad 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -4,19 +4,18 @@ require "./spectator/*" module Spectator VERSION = "0.1.0" - @@top_level_groups = [] of ExampleGroup + FOO = [] of MacroId - def self.describe(type : T.class) : Nil forall T - group = DSL.new(type.to_s) - with group yield - @@top_level_groups << group._spec_build + macro describe(what, source_file = __FILE__, source_line = __LINE__, &block) + module Spectator + module Examples + {{block.body}} + end + end + {% debug %} end at_exit do - @@top_level_groups.each do |group| - group.examples.each do |example| - example.run - end - end + # TODO end end diff --git a/src/spectator/dsl.cr b/src/spectator/dsl.cr index 2f2089f..cf40edd 100644 --- a/src/spectator/dsl.cr +++ b/src/spectator/dsl.cr @@ -5,23 +5,32 @@ private macro _spec_add_example(example) end module Spectator - class DSL - @examples = [] of Spectator::Example - - protected def initialize(@type : String) + module DSL + private macro nest(type, what, &block) + {% safe_name = what.id.stringify.gsub(/\W+/, "_") %} + {% module_name = (type.id + safe_name.camelcase).id %} + module {{module_name.id}} + include ::Spectator::DSL + {{block.body}} + end end - def describe - raise NotImplementedError.new("Spectator::DSL#describe") + macro describe(what, &block) + nest("Describe", {{what}}) {{block}} end - def context - raise NotImplementedError.new("Spectator::DSL#context") + macro context(what, &block) + nest("Context", {{what}}) {{block}} end - def it(description : String, &block) : Nil - example = Spectator::Example.new(description, block) - _spec_add_example(example) + macro it(description, &block) + {% safe_name = description.id.stringify.gsub(/\W+/, "_") %} + {% class_name = (safe_name.camelcase + "Example").id %} + class {{class_name.id}} < ::Spectator::Example + def run + {{block.body}} + end + end end def it_behaves_like diff --git a/src/spectator/examples.cr b/src/spectator/examples.cr new file mode 100644 index 0000000..4882526 --- /dev/null +++ b/src/spectator/examples.cr @@ -0,0 +1,7 @@ +require "./dsl" + +module Spectator + module Examples + include ::Spectator::DSL + end +end