mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
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:
parent
cba0abc7f1
commit
a9d4acda36
5 changed files with 34 additions and 47 deletions
|
@ -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
|
||||
@%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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue