Implement building groups and examples

This commit is contained in:
Michael Miller 2018-09-23 14:34:42 -06:00
parent 5b79e76b44
commit 94e2f54713
4 changed files with 42 additions and 24 deletions

View file

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

View file

@ -1,7 +1,9 @@
module Spectator module Spectator
module DSL module DSL
class ExampleGroupBuilder class ExampleGroupBuilder
@children = [] of ExampleFactory | ExampleGroupBuilder alias Child = ExampleFactory | ExampleGroupBuilder
@children = [] of Child
@before_all_hooks = [] of -> @before_all_hooks = [] of ->
@before_each_hooks = [] of -> @before_each_hooks = [] of ->
@after_all_hooks = [] of -> @after_all_hooks = [] of ->
@ -11,7 +13,7 @@ module Spectator
def initialize(@what : String) def initialize(@what : String)
end end
def add_child(child : ExampleFactory | ExampleGroupBuilder) : Nil def add_child(child : Child) : Nil
@children << child @children << child
end end
@ -35,8 +37,13 @@ module Spectator
@around_each_hooks << block @around_each_hooks << block
end end
def build(locals : Hash(Symbol, ValueWrapper)) : ExampleGroup def build(parent : ExampleGroup?, locals : Hash(Symbol, ValueWrapper)) : ExampleGroup
raise NotImplementedError ExampleGroup.new(@what, parent).tap do |group|
children = @children.map do |child|
child.build(group, locals).as(ExampleGroup::Child)
end
group.children = children
end
end end
end end
end end

View file

@ -8,8 +8,18 @@ module Spectator
super(what) super(what)
end end
def build(locals : Hash(Symbol, ValueWrapper)) : ExampleGroup def build(parent : ExampleGroup?, locals : Hash(Symbol, ValueWrapper)) : ExampleGroup
raise NotImplementedError ExampleGroup.new(@what, parent).tap do |group|
children = [] of ExampleGroup::Child
@collection.each do |value|
iter_locals = locals.merge({:TODO => value})
iter_children = @children.map do |child|
child.build(group, iter_locals)
end
children.concat(iter_children)
end
group.children = children
end
end end
end end
end end

View file

@ -2,47 +2,48 @@ require "./example"
module Spectator module Spectator
class ExampleGroup class ExampleGroup
ROOT = ExampleGroup.new("ROOT") alias Child = Example | ExampleGroup
getter what : String getter what : String
getter parent : ExampleGroup?
getter! parent : ExampleGroup
private getter! children : Array(Child)
setter children
getter before_all_hooks = [] of -> getter before_all_hooks = [] of ->
getter before_each_hooks = [] of -> getter before_each_hooks = [] of ->
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 ExampleFactory | ExampleGroup
@before_all_hooks_run = false @before_all_hooks_run = false
@after_all_hooks_run = false @after_all_hooks_run = false
def initialize(@what, @parent = nil) def initialize(@what, @parent)
if (parent = @parent)
parent.children << self
end
end end
def examples : Enumerable(ExampleFactory) def examples : Enumerable(Example)
@children.select { |child| child.is_a?(ExampleFactory) }.map { |child| child.unsafe_as(ExampleFactory) } children.select { |child| child.is_a?(Example) }.map { |child| child.unsafe_as(Example) }
end end
def groups : Enumerable(ExampleGroup) def groups : Enumerable(ExampleGroup)
@children.select { |child| child.is_a?(ExampleGroup) }.map { |child| child.unsafe_as(ExampleGroup) } children.select { |child| child.is_a?(ExampleGroup) }.map { |child| child.unsafe_as(ExampleGroup) }
end end
def example_count def example_count
@children.sum do |child| children.sum do |child|
child.is_a?(ExampleFactory) ? 1 : child.example_count child.is_a?(Example) ? 1 : child.example_count
end end
end end
def all_examples(locals = {} of Symbol => ValueWrapper) def all_examples
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?(Example)
array << child.build(locals) array << child
else else
array.concat(child.all_examples(locals)) array.concat(child.all_examples)
end end
end end
end end