diff --git a/src/spectator/dsl.cr b/src/spectator/dsl.cr index 1e46330..e76bd7f 100644 --- a/src/spectator/dsl.cr +++ b/src/spectator/dsl.cr @@ -2,6 +2,7 @@ require "./dsl/builder" require "./dsl/examples" require "./dsl/groups" +require "./dsl/hooks" require "./dsl/top" module Spectator diff --git a/src/spectator/dsl/hooks.cr b/src/spectator/dsl/hooks.cr index e495004..88e513f 100644 --- a/src/spectator/dsl/hooks.cr +++ b/src/spectator/dsl/hooks.cr @@ -1,3 +1,5 @@ +require "./builder" + module Spectator::DSL # DSL methods for adding custom logic to key times of the spec execution. module Hooks @@ -9,7 +11,7 @@ module Spectator::DSL {{block.body}} end - ::Spectator::DSL::Builder.before_all { {{@type.name}.%hook } + ::Spectator::DSL::Builder.before_all { {{@type.name}}.%hook } end macro before_each(&block) @@ -19,7 +21,9 @@ module Spectator::DSL {{block.body}} end - ::Spectator::DSL::Builder.before_each { |example| example.with_context({{@type.name}) { %hook } } + ::Spectator::DSL::Builder.before_each do |example| + example.with_context({{@type.name}}) { %hook } + end end macro after_all(&block) @@ -28,81 +32,4 @@ module Spectator::DSL macro after_each(&block) end end - - macro before_each(&block) - def %hook({{block.args.splat}}) : Nil - {{block.body}} - end - - ::Spectator::SpecBuilder.add_before_each_hook do |test, example| - cast_test = test.as({{@type.id}}) - {% if block.args.empty? %} - cast_test.%hook - {% else %} - cast_test.%hook(example) - {% end %} - end - end - - macro after_each(&block) - def %hook({{block.args.splat}}) : Nil - {{block.body}} - end - - ::Spectator::SpecBuilder.add_after_each_hook do |test, example| - cast_test = test.as({{@type.id}}) - {% if block.args.empty? %} - cast_test.%hook - {% else %} - cast_test.%hook(example) - {% end %} - end - end - - macro before_all(&block) - ::Spectator::SpecBuilder.add_before_all_hook {{block}} - end - - macro after_all(&block) - ::Spectator::SpecBuilder.add_after_all_hook {{block}} - end - - macro around_each(&block) - def %hook({{block.args.first || :example.id}}) : Nil - {{block.body}} - end - - ::Spectator::SpecBuilder.add_around_each_hook { |test, proc| test.as({{@type.id}}).%hook(proc) } - end - - macro pre_condition(&block) - def %hook({{block.args.splat}}) : Nil - {{block.body}} - end - - ::Spectator::SpecBuilder.add_pre_condition do |test, example| - cast_test = test.as({{@type.id}}) - {% if block.args.empty? %} - cast_test.%hook - {% else %} - cast_test.%hook(example) - {% end %} - end - end - - macro post_condition(&block) - def %hook({{block.args.splat}}) : Nil - {{block.body}} - end - - ::Spectator::SpecBuilder.add_post_condition do |test, example| - cast_test = test.as({{@type.id}}) - {% if block.args.empty? %} - cast_test.%hook - {% else %} - cast_test.%hook(example) - {% end %} - end - end - end end diff --git a/src/spectator_test_context.cr b/src/spectator_test_context.cr index 2031005..0e8fb2b 100644 --- a/src/spectator_test_context.cr +++ b/src/spectator_test_context.cr @@ -4,6 +4,7 @@ require "./spectator/dsl" class SpectatorTestContext < SpectatorContext include ::Spectator::DSL::Examples include ::Spectator::DSL::Groups + include ::Spectator::DSL::Hooks # Initial implicit subject for tests. # This method should be overridden by example groups when an object is described.