From b4e74444d173c516eaa195d98abf57272c3c26b0 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sun, 8 Nov 2020 22:21:52 -0700 Subject: [PATCH] Some work on hooks in DSL --- src/spectator/dsl.cr | 1 + src/spectator/dsl/hooks.cr | 85 +++-------------------------------- src/spectator_test_context.cr | 1 + 3 files changed, 8 insertions(+), 79 deletions(-) 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.