Trying to get exaples to map to their given values

This commit is contained in:
Michael Miller 2018-09-19 23:00:17 -06:00
parent aaf973b8cf
commit 8b8981494d
2 changed files with 46 additions and 3 deletions

View file

@ -30,8 +30,41 @@ module Spectator
module Given%given module Given%given
include {{@type.id}} include {{@type.id}}
def %collection
{{collection}}
end
def %first
%collection.first
end
def %group
::Spectator::Definitions::GROUPS[\{{@type.symbolize}}].as(
GivenExampleGroup(typeof(%first)))
end
def %value
%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}}
nil # TODO if (wrapper = @%wrapper)
wrapper.unsafe_as(TypedValueWrapper(typeof(%value))).value
else
%dup.tap do |value|
@%wrapper = TypedValueWrapper(typeof(%value)).new(value)
end
end
end end
_given_collection Collection%collection, %to_a do _given_collection Collection%collection, %to_a do

View file

@ -2,14 +2,24 @@ 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), parent = nil) def initialize(what, @collection : Array(T), parent = nil)
super(what, parent) super(what, parent)
end end
protected def add_examples(array : Array(Example)) protected def add_examples(array : Array(Example))
@collection.each do |item| @collection.each do |value|
super(array) examples = super.all_examples
examples.each do |example|
@mapping[example] = value
end end
array.concat(examples)
end
end
def value_for(example : Example) : T
@mapping[example]
end end
end end
end end