2020-11-08 22:06:49 +00:00
|
|
|
require "./events"
|
2021-01-16 23:28:33 +00:00
|
|
|
require "./example_procsy_hook"
|
2021-05-08 02:09:33 +00:00
|
|
|
require "./node"
|
2020-09-06 01:55:46 +00:00
|
|
|
|
2020-09-05 22:47:40 +00:00
|
|
|
module Spectator
|
|
|
|
# Collection of examples and sub-groups.
|
2021-05-08 02:09:33 +00:00
|
|
|
class ExampleGroup < Node
|
|
|
|
include Enumerable(Node)
|
2020-11-08 22:06:49 +00:00
|
|
|
include Events
|
2021-05-08 02:09:33 +00:00
|
|
|
include Iterable(Node)
|
2020-09-05 22:47:40 +00:00
|
|
|
|
2021-05-08 02:09:33 +00:00
|
|
|
@nodes = [] of Node
|
2021-01-09 19:48:53 +00:00
|
|
|
|
2021-05-08 03:04:17 +00:00
|
|
|
# Parent group this group belongs to.
|
|
|
|
getter! group : ExampleGroup
|
|
|
|
|
|
|
|
# Assigns this group to the specified *group*.
|
|
|
|
# This is an internal method and should only be called from `ExampleGroup`.
|
|
|
|
# `ExampleGroup` manages the association of nodes to groups.
|
|
|
|
protected setter group : ExampleGroup?
|
|
|
|
|
2021-05-08 18:00:29 +00:00
|
|
|
# Calls all hooks from the parent group if there is a parent.
|
|
|
|
# The *hook* is the method name of the group hook to invoke.
|
|
|
|
private macro call_parent_hooks(hook)
|
|
|
|
if (parent = @group)
|
|
|
|
parent.{{hook.id}}
|
|
|
|
end
|
|
|
|
end
|
2020-11-15 18:22:52 +00:00
|
|
|
|
2021-05-08 18:00:29 +00:00
|
|
|
# Calls all hooks from the parent group if there is a parent.
|
|
|
|
# The *hook* is the method name of the example hook to invoke.
|
|
|
|
# The current *example* must be provided.
|
|
|
|
private macro call_parent_hooks(hook, example)
|
|
|
|
if (parent = @group)
|
|
|
|
parent.{{hook.id}}({{example}})
|
2020-11-15 18:22:52 +00:00
|
|
|
end
|
2021-05-08 18:00:29 +00:00
|
|
|
end
|
2020-11-15 18:22:52 +00:00
|
|
|
|
2021-05-08 18:00:29 +00:00
|
|
|
# Calls group hooks of the current group.
|
|
|
|
private def call_hooks(hooks)
|
2021-01-09 18:14:27 +00:00
|
|
|
hooks.each do |hook|
|
|
|
|
Log.trace { "Invoking hook #{hook}" }
|
|
|
|
hook.call
|
|
|
|
end
|
2020-11-15 18:22:52 +00:00
|
|
|
end
|
|
|
|
|
2021-05-08 18:00:29 +00:00
|
|
|
# Calls example hooks of the current group.
|
|
|
|
# Requires the current *example*.
|
|
|
|
private def call_hooks(hooks, example)
|
2021-01-09 18:14:27 +00:00
|
|
|
hooks.each do |hook|
|
|
|
|
Log.trace { "Invoking hook #{hook}" }
|
2021-05-08 18:00:29 +00:00
|
|
|
hook.call(example)
|
2021-01-09 18:14:27 +00:00
|
|
|
end
|
2021-05-08 18:00:29 +00:00
|
|
|
end
|
2020-11-15 18:22:52 +00:00
|
|
|
|
2021-05-08 18:00:29 +00:00
|
|
|
group_event before_all do |hooks|
|
|
|
|
Log.trace { "Processing before_all hooks for #{self}" }
|
|
|
|
|
|
|
|
call_parent_hooks(:call_once_before_all)
|
|
|
|
call_hooks(hooks)
|
|
|
|
end
|
|
|
|
|
|
|
|
group_event after_all do |hooks|
|
|
|
|
Log.trace { "Processing after_all hooks for #{self}" }
|
|
|
|
|
|
|
|
call_hooks(hooks)
|
2021-06-06 04:44:34 +00:00
|
|
|
call_parent_hooks(:call_once_after_all) if @group.try(&.finished?)
|
2020-11-15 18:22:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
example_event before_each do |hooks, example|
|
2021-01-09 18:33:29 +00:00
|
|
|
Log.trace { "Processing before_each hooks for #{self}" }
|
2020-11-15 18:22:52 +00:00
|
|
|
|
2021-05-08 18:00:29 +00:00
|
|
|
call_parent_hooks(:call_before_each, example)
|
|
|
|
call_hooks(hooks, example)
|
2020-11-15 18:22:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
example_event after_each do |hooks, example|
|
2021-01-09 18:33:29 +00:00
|
|
|
Log.trace { "Processing after_each hooks for #{self}" }
|
2020-11-15 18:22:52 +00:00
|
|
|
|
2021-05-08 18:00:29 +00:00
|
|
|
call_hooks(hooks, example)
|
|
|
|
call_parent_hooks(:call_after_each, example)
|
2020-11-15 18:22:52 +00:00
|
|
|
end
|
2020-11-08 22:06:49 +00:00
|
|
|
|
2021-05-08 03:04:17 +00:00
|
|
|
# Creates the example group.
|
|
|
|
# The *name* describes the purpose of the group.
|
|
|
|
# It can be a `Symbol` to describe a type.
|
|
|
|
# The *location* tracks where the group exists in source code.
|
|
|
|
# This group will be assigned to the parent *group* if it is provided.
|
2021-06-12 22:45:45 +00:00
|
|
|
# A set of *metadata* can be used for filtering and modifying example behavior.
|
2021-05-08 03:04:17 +00:00
|
|
|
def initialize(@name : Label = nil, @location : Location? = nil,
|
2021-06-12 22:45:45 +00:00
|
|
|
@group : ExampleGroup? = nil, @metadata : Metadata = Metadata.new)
|
2021-05-08 03:04:17 +00:00
|
|
|
# Ensure group is linked.
|
|
|
|
group << self if group
|
|
|
|
end
|
|
|
|
|
2020-09-05 22:47:40 +00:00
|
|
|
# Removes the specified *node* from the group.
|
|
|
|
# The node will be unassigned from this group.
|
2021-05-08 02:09:33 +00:00
|
|
|
def delete(node : Node)
|
2020-09-05 22:47:40 +00:00
|
|
|
# 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
|
|
|
|
|
2021-05-08 03:04:17 +00:00
|
|
|
# Constructs the full name or description of the example group.
|
|
|
|
# This prepends names of groups this group is part of.
|
|
|
|
def to_s(io)
|
2021-05-17 01:37:45 +00:00
|
|
|
# Prefix with group's full name if the node belongs to a group.
|
|
|
|
return unless parent = @group
|
|
|
|
|
|
|
|
parent.to_s(io)
|
2021-05-08 03:04:17 +00:00
|
|
|
name = @name
|
|
|
|
|
2021-05-17 01:37:45 +00:00
|
|
|
# Add padding between the node names
|
|
|
|
# only if the names don't appear to be symbolic.
|
|
|
|
# Skip blank group names (like the root group).
|
|
|
|
io << ' ' unless !parent.name? || # ameba:disable Style/NegatedConditionsInUnless
|
|
|
|
(parent.name?.is_a?(Symbol) && name.is_a?(String) &&
|
|
|
|
(name.starts_with?('#') || name.starts_with?('.')))
|
2021-05-08 03:04:17 +00:00
|
|
|
|
|
|
|
super
|
|
|
|
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.
|
2021-05-08 02:09:33 +00:00
|
|
|
def <<(node : Node)
|
2020-09-05 22:47:40 +00:00
|
|
|
# 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
|
2021-01-16 23:28:33 +00:00
|
|
|
|
|
|
|
@around_hooks = [] of ExampleProcsyHook
|
|
|
|
|
2021-01-17 00:16:31 +00:00
|
|
|
# Adds a hook to be invoked when the *around_each* event occurs.
|
2021-01-16 23:28:33 +00:00
|
|
|
def add_around_each_hook(hook : ExampleProcsyHook) : Nil
|
|
|
|
@around_hooks << hook
|
|
|
|
end
|
|
|
|
|
|
|
|
# Defines a hook for the *around_each* event.
|
|
|
|
# The block of code given to this method is invoked when the event occurs.
|
|
|
|
# The current example is provided as a block argument.
|
2021-05-08 18:43:41 +00:00
|
|
|
def around_each(&block) : Nil
|
2021-01-16 23:28:33 +00:00
|
|
|
hook = ExampleProcsyHook.new(label: "around_each", &block)
|
|
|
|
add_around_each_hook(hook)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Signals that the *around_each* event has occurred.
|
|
|
|
# All hooks associated with the event will be called.
|
2021-05-08 18:43:41 +00:00
|
|
|
def call_around_each(example, &block : -> _) : Nil
|
2021-01-16 23:28:33 +00:00
|
|
|
# Avoid overhead if there's no hooks.
|
|
|
|
return yield if @around_hooks.empty?
|
|
|
|
|
|
|
|
# Start with a procsy that wraps the original code.
|
2021-05-08 18:43:41 +00:00
|
|
|
procsy = example.procsy(&block)
|
2021-01-16 23:28:33 +00:00
|
|
|
procsy = wrap_around_each(procsy)
|
|
|
|
procsy.call
|
|
|
|
end
|
|
|
|
|
|
|
|
# Wraps a procsy with the *around_each* hooks from this example group.
|
|
|
|
# The returned procsy will call each hook then *procsy*.
|
2021-05-08 18:43:41 +00:00
|
|
|
protected def wrap_around_each(procsy)
|
2021-01-16 23:28:33 +00:00
|
|
|
# Avoid overhead if there's no hooks.
|
|
|
|
return procsy if @around_hooks.empty?
|
|
|
|
|
|
|
|
# Wrap each hook with the next.
|
|
|
|
outer = procsy
|
2021-01-31 04:15:20 +00:00
|
|
|
@around_hooks.reverse_each do |hook|
|
2021-01-16 23:28:33 +00:00
|
|
|
outer = hook.wrap(outer)
|
|
|
|
end
|
|
|
|
|
|
|
|
# If there's a parent, wrap the procsy with its hooks.
|
|
|
|
# Otherwise, return the outermost procsy.
|
|
|
|
return outer unless (parent = group?)
|
|
|
|
|
|
|
|
parent.wrap_around_each(outer)
|
|
|
|
end
|
2020-09-05 22:47:40 +00:00
|
|
|
end
|
|
|
|
end
|