diff --git a/src/spectator/example_group.cr b/src/spectator/example_group.cr index 23e0f75..01f0d0e 100644 --- a/src/spectator/example_group.cr +++ b/src/spectator/example_group.cr @@ -93,6 +93,13 @@ module Spectator group << self if group end + # Creates a child that is attched to the group. + # Yields zero or more times to create the child. + # The group the child should be attached to is provided as a block argument. + def create_child + yield self + end + # Removes the specified *node* from the group. # The node will be unassigned from this group. def delete(node : Node) diff --git a/src/spectator/iterative_example_group.cr b/src/spectator/iterative_example_group.cr index cf384a0..ca050b9 100644 --- a/src/spectator/iterative_example_group.cr +++ b/src/spectator/iterative_example_group.cr @@ -19,6 +19,13 @@ module Spectator end end + # Creates a child that is attched to the group. + # Yields zero or more times to create the child. + # The group the child should be attached to is provided as a block argument. + def create_child + @nodes.each { |child| yield child.as(Iteration(T)) } + end + # Adds the specified *node* to the group. # Assigns the node to this group. # If the node already belongs to a group, diff --git a/src/spectator/spec/builder.cr b/src/spectator/spec/builder.cr index 8bee550..f6ecee9 100644 --- a/src/spectator/spec/builder.cr +++ b/src/spectator/spec/builder.cr @@ -119,12 +119,11 @@ module Spectator # It will be yielded two arguments - the example created by this method, and the *context* argument. # The return value of the block is ignored. # It is expected that the test code runs when the block is called. - # - # The newly created example is returned. - def add_example(name, location, context, metadata = Metadata.new, &block : Example -> _) : Example + def add_example(name, location, context, metadata = Metadata.new, &block : Example -> _) Log.trace { "Add example: #{name} @ #{location}; metadata: #{metadata}" } - Example.new(context, block, name, location, current_group, metadata) - # The example is added to the current group by `Example` initializer. + current_group.create_child do |group| + Example.new(context, block, name, location, group, metadata) + end end # Defines a new pending example. @@ -143,8 +142,9 @@ module Spectator # The newly created example is returned. def add_pending_example(name, location, metadata = Metadata.new, reason = nil) : Example Log.trace { "Add pending example: #{name} @ #{location}; metadata: #{metadata}" } - Example.pending(name, location, current_group, metadata, reason) - # The example is added to the current group by `Example` initializer. + current_group.create_child do |group| + Example.pending(name, location, group, metadata, reason) + end end # Attaches a hook to be invoked before any and all examples in the current group.