diff --git a/src/spectator/core/context.cr b/src/spectator/core/context.cr index d7c577f1..53c5a168 100644 --- a/src/spectator/core/context.cr +++ b/src/spectator/core/context.cr @@ -1,20 +1,15 @@ +require "./dsl" require "./hooks" -require "./example" +require "./memoization" +require "./methods" module Spectator::Core module Context + include DSL include Hooks - - abstract def context(description, &) - - def describe(description, &) - context(description) { with self yield self } - end - - abstract def specify(description, &block : Example ->) : Example - - def it(description, &block : Example ->) : Example - specify(description, &block) - end + include Memoization + include Methods end end + +Spectator.def_example_alias(:it) diff --git a/src/spectator/core/dsl.cr b/src/spectator/core/dsl.cr new file mode 100644 index 00000000..279c4617 --- /dev/null +++ b/src/spectator/core/dsl.cr @@ -0,0 +1,23 @@ +module Spectator + EXAMPLE_ALIASES = [] of Nil + + macro def_example_alias(name) + {% EXAMPLE_ALIASES << name %} + end + + module Core + module DSL + macro finished + {% for example_alias in EXAMPLE_ALIASES %} + macro {{example_alias.id}}(description, &block) + {% verbatim do %} + specify({{description}}) do {% if !block.args.empty? %} |{{block.args.splat}}| {% end %} + {{yield}} + end + {% end %} + end + {% end %} + end + end + end +end diff --git a/src/spectator/core/memoization.cr b/src/spectator/core/memoization.cr new file mode 100644 index 00000000..60d45cf9 --- /dev/null +++ b/src/spectator/core/memoization.cr @@ -0,0 +1,21 @@ +module Spectator::Core + def self.null_value(type : T.class) : T forall T + value = uninitialized T + end + + module Memoization + macro let(name, &block) + %block = -> do + {{yield}} + end + + {{name.id}} = ::Spectator::Core.null_value(typeof(%block.call)) + + before_each do # TODO: Ensure hook is called before all others. + {{name.id}} = %block.call + end + + {% debug %} + end + end +end diff --git a/src/spectator/core/methods.cr b/src/spectator/core/methods.cr index 94e956df..2c53f560 100644 --- a/src/spectator/core/methods.cr +++ b/src/spectator/core/methods.cr @@ -1,26 +1,23 @@ +require "./example" + module Spectator::Core module Methods - def context(description : String, &) - example_group = ExampleGroup.new(description) - Spectator.current_example_group.push(example_group) do - with self yield - end - end + abstract def context(description, &) - def describe(description : String, &) - context(description) { yield } + def describe(description, &) + context(description) { with self yield self } end - def it(description : String) - end + abstract def specify(description, &block : Example ->) : Example - def it(description : String, & : Example ->) - end + # def it(description, &block : Example ->) : Example + # specify(description, &block) + # end - def pending(description : String) - end - - def pending(description : String, & : Example ->) - end + # macro it(description, &block) + # specify({{description}}) do {% if !block.args.empty? %} |{{block.args.splat}}| {% end %} + # {{yield}} + # end + # end end end