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
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

View file

@ -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

View file

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