From e6fd7165865e8f6df8b5453782a0b814054090af Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Tue, 1 Jan 2019 17:58:54 -0700 Subject: [PATCH] Run around_each hooks in same context as example code --- src/spectator/dsl/structure_dsl.cr | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index 83904ed..c06150b 100644 --- a/src/spectator/dsl/structure_dsl.cr +++ b/src/spectator/dsl/structure_dsl.cr @@ -852,6 +852,17 @@ module Spectator::DSL # end # ``` # + # The hook can use values and methods in the group like examples can. + # It is called in the same scope as the example code. + # ``` + # let(array) { [1, 2, 3] } + # around_each do |proc| + # array << 4 + # proc.call + # array.pop + # end + # ``` + # # If multiple `#around_each` blocks are specified, # then they are run in the order they were defined. # ``` @@ -885,7 +896,18 @@ module Spectator::DSL # # See also: `#before_all`, `#before_each`, `#after_all`, and `#after_each`. macro around_each(&block) - ::Spectator::DSL::Builder.add_around_each_hook {{block}} + # Around each hook. + # Defined as a method so that it can access the same scope as the example code. + def %hook({{block.args.splat}}) : Nil + {{block.body}} + end + + ::Spectator::DSL::Builder.add_around_each_hook do |proc| + # Get the wrapper instance and cast to current group type. + example = ::Spectator::Internals::Harness.current.example + instance = example.instance.as({{@type.id}}) + instance.%hook(proc) + end end # Creates an example, or a test case.