From a9d4acda36af8c4f7083b5aabe99331ddcaf8c4f Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 21 Sep 2018 14:03:09 -0600 Subject: [PATCH] Pass locals to example and test wrapper initializer The #initialize method is defined in each context/given/module that needs to add a local. This is an elegant (although possibly confusing) solution. --- src/spectator/dsl/structure_dsl.cr | 49 ++++++++++------------------ src/spectator/example.cr | 5 +++ src/spectator/example_group.cr | 4 +-- src/spectator/examples.cr | 3 ++ src/spectator/given_example_group.cr | 20 ++++-------- 5 files changed, 34 insertions(+), 47 deletions(-) diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index 71d7114..f864825 100644 --- a/src/spectator/dsl/structure_dsl.cr +++ b/src/spectator/dsl/structure_dsl.cr @@ -38,44 +38,27 @@ module Spectator %collection.first end - def %group - ::Spectator::Definitions::GROUPS[\{{@type.symbolize}}].as( - GivenExampleGroup(typeof(%first))) - end - - def %value - nil # TODO: %group.value_for(self) - end - - def %dup - if (value = %value).responds_to?(:clone) - value.clone - else - value.dup - end - end - - @%wrapper : ValueWrapper? + @%wrapper : ValueWrapper def {{block.args.empty? ? "value".id : block.args.first}} - if (wrapper = @%wrapper) - wrapper.unsafe_as(TypedValueWrapper(typeof(%value))).value - else - %dup.tap do |value| - @%wrapper = TypedValueWrapper(typeof(%value)).new(value) - end - end + @%wrapper.as(TypedValueWrapper(typeof(%first))).value + end + + def initialize(locals : Hash(Symbol, ValueWrapper)) + super + @%wrapper = locals[:%given] end _given_collection Collection%collection, %to_a do {{collection}} end - %collection = Collection%collection.new.%to_a + %to_a = Collection%collection.new.%to_a ::Spectator::Definitions::GROUPS[\{{@type.symbolize}}] = - GivenExampleGroup(typeof(%collection.first)).new( + GivenExampleGroup(typeof(%to_a.first)).new( {{collection.stringify}}, - %collection, + %to_a, + :%given, ::Spectator::Definitions::GROUPS[{{@type.symbolize}}] ) @@ -160,6 +143,10 @@ module Spectator include ::Spectator::DSL::ExampleDSL include {{@type.id}} + def initialize(locals : Hash(Symbol, ValueWrapper)) + super + end + def %run {{block.body}} end @@ -167,7 +154,7 @@ module Spectator class Example%example < ::Spectator::RunnableExample protected def run_instance - Wrapper%example.new.%run + Wrapper%example.new(locals).%run end def description @@ -180,8 +167,8 @@ module Spectator end class Factory%example < ::Spectator::ExampleFactory - def build - Example%example.new + def build(locals = {} of Symbol => ValueWrapper) + Example%example.new(locals) end end diff --git a/src/spectator/example.cr b/src/spectator/example.cr index d39b857..ae657be 100644 --- a/src/spectator/example.cr +++ b/src/spectator/example.cr @@ -5,5 +5,10 @@ module Spectator abstract def run : Result abstract def description : String abstract def group : ExampleGroup + + def initialize(@locals = {} of Symbol => ValueWrapper) + end + + private getter locals end end diff --git a/src/spectator/example_group.cr b/src/spectator/example_group.cr index d48be08..b7370fb 100644 --- a/src/spectator/example_group.cr +++ b/src/spectator/example_group.cr @@ -36,11 +36,11 @@ module Spectator end end - def all_examples + def all_examples(locals = {} of Symbol => ValueWrapper) Array(Example).new(example_count).tap do |array| @children.each do |child| if child.is_a?(ExampleFactory) - array << child.build + array << child.build(locals) else array.concat(child.all_examples) end diff --git a/src/spectator/examples.cr b/src/spectator/examples.cr index 196bb9a..cac73f4 100644 --- a/src/spectator/examples.cr +++ b/src/spectator/examples.cr @@ -12,5 +12,8 @@ module Spectator children: [] of Object } %} ::Spectator::Definitions::GROUPS[{{@type.symbolize}}] = ::Spectator::ExampleGroup::ROOT + + def initialize(locals = {} of Symbol => ValueWrapper) + end end end diff --git a/src/spectator/given_example_group.cr b/src/spectator/given_example_group.cr index d08b11f..3b98f8e 100644 --- a/src/spectator/given_example_group.cr +++ b/src/spectator/given_example_group.cr @@ -2,9 +2,7 @@ require "./example_group" module Spectator class GivenExampleGroup(T) < ExampleGroup - @mapping = {} of Example => T - - def initialize(what, @collection : Array(T), parent = nil) + def initialize(what, @collection : Array(T), @symbol : Symbol, parent = nil) super(what, parent) end @@ -12,20 +10,14 @@ module Spectator super * @collection.size end - def all_examples + def all_examples(locals = {} of Symbol => ValueWrapper) Array(Example).new(example_count).tap do |array| - examples = super - @collection.each do |value| - examples.each do |example| - @mapping[example] = value - end - array.concat(examples) + @collection.each do |local| + wrapper = TypedValueWrapper(T).new(local) + iter_locals = locals.merge({@symbol => wrapper}) + array.concat(super(iter_locals)) end end end - - def value_for(example : Example) : T - @mapping[example] - end end end