2020-11-08 22:06:49 +00:00
|
|
|
require "./events"
|
2020-09-06 01:55:46 +00:00
|
|
|
require "./example_node"
|
|
|
|
|
2020-09-05 22:47:40 +00:00
|
|
|
module Spectator
|
|
|
|
# Collection of examples and sub-groups.
|
|
|
|
class ExampleGroup < ExampleNode
|
|
|
|
include Enumerable(ExampleNode)
|
2020-11-08 22:06:49 +00:00
|
|
|
include Events
|
2020-10-17 17:23:51 +00:00
|
|
|
include Iterable(ExampleNode)
|
2020-09-05 22:47:40 +00:00
|
|
|
|
2020-11-08 22:06:49 +00:00
|
|
|
group_event before_all
|
|
|
|
group_event after_all
|
|
|
|
example_event before_each
|
|
|
|
example_event after_each
|
|
|
|
|
2020-09-05 22:47:40 +00:00
|
|
|
@nodes = [] of ExampleNode
|
|
|
|
|
|
|
|
# Removes the specified *node* from the group.
|
|
|
|
# The node will be unassigned from this group.
|
|
|
|
def delete(node : ExampleNode)
|
|
|
|
# Only remove from the group if it is associated with this group.
|
|
|
|
return unless node.group == self
|
|
|
|
|
|
|
|
node.group = nil
|
|
|
|
@nodes.delete(node)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Yields each node (example and sub-group).
|
|
|
|
def each
|
|
|
|
@nodes.each { |node| yield node }
|
|
|
|
end
|
|
|
|
|
2020-10-17 17:23:51 +00:00
|
|
|
# Returns an iterator for each (example and sub-group).
|
|
|
|
def each
|
|
|
|
@nodes.each
|
|
|
|
end
|
|
|
|
|
2020-09-12 22:02:11 +00:00
|
|
|
# Checks if all examples and sub-groups have finished.
|
|
|
|
def finished? : Bool
|
|
|
|
@nodes.all?(&.finished?)
|
|
|
|
end
|
|
|
|
|
2020-09-05 22:47:40 +00:00
|
|
|
# Adds the specified *node* to the group.
|
|
|
|
# Assigns the node to this group.
|
|
|
|
# If the node already belongs to a group,
|
|
|
|
# it will be removed from the previous group before adding it to this group.
|
|
|
|
def <<(node : ExampleNode)
|
|
|
|
# Remove from existing group if the node is part of one.
|
|
|
|
if (previous = node.group?)
|
|
|
|
previous.delete(node)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add the node to this group and associate with it.
|
|
|
|
@nodes << node
|
|
|
|
node.group = self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|