mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Playing around with new macro style
This commit is contained in:
parent
74905b82bd
commit
ed40e995d2
3 changed files with 36 additions and 21 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
7
src/spectator/examples.cr
Normal file
7
src/spectator/examples.cr
Normal file
|
@ -0,0 +1,7 @@
|
|||
require "./dsl"
|
||||
|
||||
module Spectator
|
||||
module Examples
|
||||
include ::Spectator::DSL
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue