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.
This commit is contained in:
Michael Miller 2018-09-21 14:03:09 -06:00
parent cba0abc7f1
commit a9d4acda36
5 changed files with 34 additions and 47 deletions

View file

@ -38,44 +38,27 @@ module Spectator
%collection.first %collection.first
end end
def %group @%wrapper : ValueWrapper
::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?
def {{block.args.empty? ? "value".id : block.args.first}} def {{block.args.empty? ? "value".id : block.args.first}}
if (wrapper = @%wrapper) @%wrapper.as(TypedValueWrapper(typeof(%first))).value
wrapper.unsafe_as(TypedValueWrapper(typeof(%value))).value end
else
%dup.tap do |value| def initialize(locals : Hash(Symbol, ValueWrapper))
@%wrapper = TypedValueWrapper(typeof(%value)).new(value) super
end @%wrapper = locals[:%given]
end
end end
_given_collection Collection%collection, %to_a do _given_collection Collection%collection, %to_a do
{{collection}} {{collection}}
end end
%collection = Collection%collection.new.%to_a %to_a = Collection%collection.new.%to_a
::Spectator::Definitions::GROUPS[\{{@type.symbolize}}] = ::Spectator::Definitions::GROUPS[\{{@type.symbolize}}] =
GivenExampleGroup(typeof(%collection.first)).new( GivenExampleGroup(typeof(%to_a.first)).new(
{{collection.stringify}}, {{collection.stringify}},
%collection, %to_a,
:%given,
::Spectator::Definitions::GROUPS[{{@type.symbolize}}] ::Spectator::Definitions::GROUPS[{{@type.symbolize}}]
) )
@ -160,6 +143,10 @@ module Spectator
include ::Spectator::DSL::ExampleDSL include ::Spectator::DSL::ExampleDSL
include {{@type.id}} include {{@type.id}}
def initialize(locals : Hash(Symbol, ValueWrapper))
super
end
def %run def %run
{{block.body}} {{block.body}}
end end
@ -167,7 +154,7 @@ module Spectator
class Example%example < ::Spectator::RunnableExample class Example%example < ::Spectator::RunnableExample
protected def run_instance protected def run_instance
Wrapper%example.new.%run Wrapper%example.new(locals).%run
end end
def description def description
@ -180,8 +167,8 @@ module Spectator
end end
class Factory%example < ::Spectator::ExampleFactory class Factory%example < ::Spectator::ExampleFactory
def build def build(locals = {} of Symbol => ValueWrapper)
Example%example.new Example%example.new(locals)
end end
end end

View file

@ -5,5 +5,10 @@ module Spectator
abstract def run : Result abstract def run : Result
abstract def description : String abstract def description : String
abstract def group : ExampleGroup abstract def group : ExampleGroup
def initialize(@locals = {} of Symbol => ValueWrapper)
end
private getter locals
end end
end end

View file

@ -36,11 +36,11 @@ module Spectator
end end
end end
def all_examples def all_examples(locals = {} of Symbol => ValueWrapper)
Array(Example).new(example_count).tap do |array| Array(Example).new(example_count).tap do |array|
@children.each do |child| @children.each do |child|
if child.is_a?(ExampleFactory) if child.is_a?(ExampleFactory)
array << child.build array << child.build(locals)
else else
array.concat(child.all_examples) array.concat(child.all_examples)
end end

View file

@ -12,5 +12,8 @@ module Spectator
children: [] of Object children: [] of Object
} %} } %}
::Spectator::Definitions::GROUPS[{{@type.symbolize}}] = ::Spectator::ExampleGroup::ROOT ::Spectator::Definitions::GROUPS[{{@type.symbolize}}] = ::Spectator::ExampleGroup::ROOT
def initialize(locals = {} of Symbol => ValueWrapper)
end
end end
end end

View file

@ -2,9 +2,7 @@ require "./example_group"
module Spectator module Spectator
class GivenExampleGroup(T) < ExampleGroup class GivenExampleGroup(T) < ExampleGroup
@mapping = {} of Example => T def initialize(what, @collection : Array(T), @symbol : Symbol, parent = nil)
def initialize(what, @collection : Array(T), parent = nil)
super(what, parent) super(what, parent)
end end
@ -12,20 +10,14 @@ module Spectator
super * @collection.size super * @collection.size
end end
def all_examples def all_examples(locals = {} of Symbol => ValueWrapper)
Array(Example).new(example_count).tap do |array| Array(Example).new(example_count).tap do |array|
examples = super @collection.each do |local|
@collection.each do |value| wrapper = TypedValueWrapper(T).new(local)
examples.each do |example| iter_locals = locals.merge({@symbol => wrapper})
@mapping[example] = value array.concat(super(iter_locals))
end
array.concat(examples)
end end
end end
end end
def value_for(example : Example) : T
@mapping[example]
end
end end
end end