From 44ade24fb734272640afb62cf1231b1ce3fa581f Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 19 Jun 2021 11:33:26 -0600 Subject: [PATCH] Generate context one or more times This is necessary for iterative (sample) groups so they don't share a context. --- src/spectator/dsl/examples.cr | 2 +- src/spectator/spec/builder.cr | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/spectator/dsl/examples.cr b/src/spectator/dsl/examples.cr index ddfb51c..38c62f0 100644 --- a/src/spectator/dsl/examples.cr +++ b/src/spectator/dsl/examples.cr @@ -52,7 +52,7 @@ module Spectator::DSL ::Spectator::DSL::Builder.add_example( _spectator_example_name(\{{what}}), ::Spectator::Location.new(\{{block.filename}}, \{{block.line_number}}, \{{block.end_line_number}}), - new.as(::Spectator::Context), + -> { new.as(::Spectator::Context) }, \%metadata ) do |example| example.with_context(\{{@type.name}}) do diff --git a/src/spectator/spec/builder.cr b/src/spectator/spec/builder.cr index f6ecee9..fe4eacd 100644 --- a/src/spectator/spec/builder.cr +++ b/src/spectator/spec/builder.cr @@ -109,7 +109,7 @@ module Spectator # # The *location* optionally defined where the example originates in source code. # - # The *context* is an instance of the context the test code should run in. + # The *context_builder* is a proc that creates a context the test code should run in. # See `Context` for more information. # # A set of *metadata* can be used for filtering and modifying example behavior. @@ -119,9 +119,10 @@ module Spectator # It will be yielded two arguments - the example created by this method, and the *context* argument. # The return value of the block is ignored. # It is expected that the test code runs when the block is called. - def add_example(name, location, context, metadata = Metadata.new, &block : Example -> _) + def add_example(name, location, context_builder, metadata = Metadata.new, &block : Example -> _) Log.trace { "Add example: #{name} @ #{location}; metadata: #{metadata}" } current_group.create_child do |group| + context = context_builder.call Example.new(context, block, name, location, group, metadata) end end