Abstract away locals to SampleValues

This commit is contained in:
Michael Miller 2018-09-23 17:04:06 -06:00
parent cddfe5591d
commit cf0c438a5d
7 changed files with 42 additions and 15 deletions

View file

@ -58,7 +58,7 @@ module Spectator
end
protected def build : ExampleGroup
root_group.build(nil, {} of Symbol => Internals::ValueWrapper)
root_group.build(nil, Internals::SampleValues.empty)
end
end
end

View file

@ -4,8 +4,8 @@ module Spectator
def initialize(@example_type : Example.class)
end
def build(group : ExampleGroup, locals : Hash(Symbol, Internals::ValueWrapper)) : Example
@example_type.new(group, locals)
def build(group : ExampleGroup, sample_values : Internals::SampleValues) : Example
@example_type.new(group, sample_values)
end
end
end

View file

@ -37,10 +37,10 @@ module Spectator
@around_each_hooks << block
end
def build(parent : ExampleGroup?, locals : Hash(Symbol, Internals::ValueWrapper)) : ExampleGroup
def build(parent : ExampleGroup?, sample_values : Internals::SampleValues) : ExampleGroup
ExampleGroup.new(@what, parent, build_hooks).tap do |group|
children = @children.map do |child|
child.build(group, locals).as(ExampleGroup::Child)
child.build(group, sample_values).as(ExampleGroup::Child)
end
group.children = children
end

View file

@ -8,13 +8,13 @@ module Spectator
super(what)
end
def build(parent : ExampleGroup?, locals : Hash(Symbol, Internals::ValueWrapper)) : ExampleGroup
def build(parent : ExampleGroup?, sample_values : Internals::SampleValues) : ExampleGroup
ExampleGroup.new(@what, parent, build_hooks).tap do |group|
children = [] of ExampleGroup::Child
@collection.each do |value|
iter_locals = locals.merge({@symbol => value})
iter_values = sample_values.add(@symbol, @symbol.to_s, value)
iter_children = @children.map do |child|
child.build(group, iter_locals)
child.build(group, iter_values)
end
children.concat(iter_children)
end

View file

@ -4,7 +4,7 @@ module Spectator
module DSL
module StructureDSL
def initialize(locals : Hash(Symbol, ::Spectator::Internals::ValueWrapper))
def initialize(sample_values : Internals::SampleValues)
end
macro describe(what, &block)
@ -43,9 +43,9 @@ module Spectator
@%wrapper.as(::Spectator::Internals::TypedValueWrapper(typeof(%collection.first))).value
end
def initialize(locals : Hash(Symbol, ::Spectator::Internals::ValueWrapper))
def initialize(sample_values : ::Spectator::Internals::SampleValues)
super
@%wrapper = locals[:%group]
@%wrapper = sample_values.get_wrapper(:%group)
end
_spectator_given_collection Collection%collection, %to_a, %collection
@ -173,7 +173,7 @@ module Spectator
include ::Spectator::DSL::ExampleDSL
include {{@type.id}}
def initialize(locals : Hash(Symbol, ::Spectator::Internals::ValueWrapper))
def initialize(sample_values : ::Spectator::Internals::SampleValues)
super
end
@ -185,9 +185,9 @@ module Spectator
private macro _spectator_example(example_class_name, wrapper_class_name, base_class, description, &block)
class {{example_class_name.id}} < {{base_class.id}}
def initialize(group : ::Spectator::ExampleGroup, locals : Hash(Symbol, ::Spectator::Internals::ValueWrapper))
def initialize(group : ::Spectator::ExampleGroup, sample_values : ::Spectator::Internals::SampleValues)
super
@instance = {{wrapper_class_name.id}}.new(locals)
@instance = {{wrapper_class_name.id}}.new(sample_values)
end
{% if block.is_a?(Block) %}

View file

@ -6,7 +6,7 @@ module Spectator
abstract def run : Result
abstract def description : String
def initialize(@group, @locals = {} of Symbol => Internals::ValueWrapper)
def initialize(@group, sample_values : Internals::SampleValues)
end
private getter locals

View file

@ -0,0 +1,27 @@
require "./value_wrapper"
module Spectator
module Internals
struct SampleValues
private record Entry, name : String, wrapper : ValueWrapper
def self.empty
new({} of Symbol => Entry)
end
protected def initialize(@values = {} of Symbol => Entry)
end
def add(id : Symbol, name : String, value : T) : SampleValues forall T
wrapper = TypedValueWrapper(T).new(value)
SampleValues.new(@values.merge({
id => Entry.new(name, wrapper)
}))
end
def get_wrapper(id : Symbol)
@values[id].wrapper
end
end
end
end