shard-spectator/src/spectator/example_group.cr

59 lines
1.5 KiB
Crystal
Raw Normal View History

require "./events"
2020-09-06 01:55:46 +00:00
require "./example_node"
module Spectator
# Collection of examples and sub-groups.
class ExampleGroup < ExampleNode
include Enumerable(ExampleNode)
include Events
2020-10-17 17:23:51 +00:00
include Iterable(ExampleNode)
group_event before_all
group_event after_all
example_event before_each
example_event after_each
@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
# 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