Playing around with new macro style

This commit is contained in:
Michael Miller 2018-08-23 16:24:03 -06:00
parent 74905b82bd
commit ed40e995d2
3 changed files with 36 additions and 21 deletions

View file

@ -4,19 +4,18 @@ require "./spectator/*"
module Spectator module Spectator
VERSION = "0.1.0" VERSION = "0.1.0"
@@top_level_groups = [] of ExampleGroup FOO = [] of MacroId
def self.describe(type : T.class) : Nil forall T macro describe(what, source_file = __FILE__, source_line = __LINE__, &block)
group = DSL.new(type.to_s) module Spectator
with group yield module Examples
@@top_level_groups << group._spec_build {{block.body}}
end
end
{% debug %}
end end
at_exit do at_exit do
@@top_level_groups.each do |group| # TODO
group.examples.each do |example|
example.run
end
end
end end
end end

View file

@ -5,23 +5,32 @@ private macro _spec_add_example(example)
end end
module Spectator module Spectator
class DSL module DSL
@examples = [] of Spectator::Example private macro nest(type, what, &block)
{% safe_name = what.id.stringify.gsub(/\W+/, "_") %}
protected def initialize(@type : String) {% module_name = (type.id + safe_name.camelcase).id %}
module {{module_name.id}}
include ::Spectator::DSL
{{block.body}}
end
end end
def describe macro describe(what, &block)
raise NotImplementedError.new("Spectator::DSL#describe") nest("Describe", {{what}}) {{block}}
end end
def context macro context(what, &block)
raise NotImplementedError.new("Spectator::DSL#context") nest("Context", {{what}}) {{block}}
end end
def it(description : String, &block) : Nil macro it(description, &block)
example = Spectator::Example.new(description, block) {% safe_name = description.id.stringify.gsub(/\W+/, "_") %}
_spec_add_example(example) {% class_name = (safe_name.camelcase + "Example").id %}
class {{class_name.id}} < ::Spectator::Example
def run
{{block.body}}
end
end
end end
def it_behaves_like def it_behaves_like

View file

@ -0,0 +1,7 @@
require "./dsl"
module Spectator
module Examples
include ::Spectator::DSL
end
end