Delay example creation by using factories

The #given block functionality specifically needs this to create 
multiple examples.
This commit is contained in:
Michael Miller 2018-09-19 21:58:32 -06:00
parent bb7f036a82
commit 4d601281f6
3 changed files with 17 additions and 6 deletions

View file

@ -146,7 +146,13 @@ module Spectator
end end
end end
::Spectator::Definitions::GROUPS[\{{@type.symbolize}}].children << Example%example.new class Factory%example < ::Spectator::ExampleFactory
def build
Example%example.new
end
end
::Spectator::Definitions::GROUPS[\{{@type.symbolize}}].children << Factory%example.new
end end
macro pending(description, &block) macro pending(description, &block)

View file

@ -0,0 +1,5 @@
module Spectator
abstract class ExampleFactory
abstract def build : Example
end
end

View file

@ -11,7 +11,7 @@ module Spectator
getter after_all_hooks = [] of -> getter after_all_hooks = [] of ->
getter after_each_hooks = [] of -> getter after_each_hooks = [] of ->
getter around_each_hooks = [] of Proc(Nil) -> getter around_each_hooks = [] of Proc(Nil) ->
getter children = [] of Example | ExampleGroup getter children = [] of ExampleFactory | ExampleGroup
@before_all_hooks_run = false @before_all_hooks_run = false
@after_all_hooks_run = false @after_all_hooks_run = false
@ -22,8 +22,8 @@ module Spectator
end end
end end
def examples : Enumerable(Example) def examples : Enumerable(ExampleFactory)
@children.select { |child| child.is_a?(Example) }.map { |child| child.unsafe_as(Example) } @children.select { |child| child.is_a?(ExampleFactory) }.map { |child| child.unsafe_as(ExampleFactory) }
end end
def groups : Enumerable(ExampleGroup) def groups : Enumerable(ExampleGroup)
@ -97,8 +97,8 @@ module Spectator
protected def add_examples(array : Array(Example)) protected def add_examples(array : Array(Example))
@children.each do |child| @children.each do |child|
if child.is_a?(Example) if child.is_a?(ExampleFactory)
array << child array << child.build
else else
child.add_examples(array) child.add_examples(array)
end end