2021-07-17 20:01:27 +00:00
|
|
|
require "./example_group"
|
|
|
|
require "./example_group_hook"
|
|
|
|
require "./example_hook"
|
|
|
|
require "./example_procsy_hook"
|
2021-08-08 03:45:49 +00:00
|
|
|
require "./hooks"
|
2021-07-17 20:01:27 +00:00
|
|
|
require "./label"
|
|
|
|
require "./location"
|
|
|
|
require "./metadata"
|
2021-07-17 17:10:44 +00:00
|
|
|
require "./node_builder"
|
|
|
|
|
|
|
|
module Spectator
|
2021-07-17 20:01:27 +00:00
|
|
|
# Progressively constructs an example group.
|
|
|
|
# Hooks and builders for child nodes can be added over time to this builder.
|
|
|
|
# When done, call `#build` to produce an `ExampleGroup`.
|
2021-07-17 17:10:44 +00:00
|
|
|
class ExampleGroupBuilder < NodeBuilder
|
2021-08-08 03:45:49 +00:00
|
|
|
include Hooks
|
|
|
|
|
|
|
|
define_hook before_all : ExampleGroupHook
|
|
|
|
define_hook after_all : ExampleGroupHook, :prepend
|
|
|
|
define_hook before_each : ExampleHook
|
|
|
|
define_hook after_each : ExampleHook, :prepend
|
|
|
|
define_hook around_each : ExampleProcsyHook
|
|
|
|
|
2021-07-17 17:10:44 +00:00
|
|
|
@children = [] of NodeBuilder
|
|
|
|
|
2021-07-17 20:01:27 +00:00
|
|
|
# Creates the builder.
|
|
|
|
# Initially, the builder will have no children and no hooks.
|
|
|
|
# The *name*, *location*, and *metadata* will be applied to the `ExampleGroup` produced by `#build`.
|
2021-07-17 17:10:44 +00:00
|
|
|
def initialize(@name : Label = nil, @location : Location? = nil, @metadata : Metadata = Metadata.new)
|
|
|
|
end
|
|
|
|
|
2021-07-17 20:01:27 +00:00
|
|
|
# Constructs an example group with previously defined attributes, children, and hooks.
|
|
|
|
# The *parent* is an already constructed example group to nest the new example group under.
|
|
|
|
# It can be nil if the new example group won't have a parent.
|
2021-07-17 17:10:44 +00:00
|
|
|
def build(parent = nil)
|
|
|
|
ExampleGroup.new(@name, @location, parent, @metadata).tap do |group|
|
2021-07-17 20:01:27 +00:00
|
|
|
apply_hooks(group)
|
2021-07-17 17:10:44 +00:00
|
|
|
@children.each(&.build(group))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-17 20:01:27 +00:00
|
|
|
# Adds a child builder to the group.
|
|
|
|
# The *builder* will have `NodeBuilder#build` called on it from within `#build`.
|
|
|
|
# The new example group will be passed to it.
|
2021-07-17 17:10:44 +00:00
|
|
|
def <<(builder)
|
|
|
|
@children << builder
|
|
|
|
end
|
2021-07-17 20:01:27 +00:00
|
|
|
|
|
|
|
# Adds all previously configured hooks to an example group.
|
|
|
|
private def apply_hooks(group)
|
2021-08-08 03:45:49 +00:00
|
|
|
before_all_hooks.each { |hook| group.before_all(hook) }
|
|
|
|
before_each_hooks.each { |hook| group.before_each(hook) }
|
|
|
|
after_all_hooks.each { |hook| group.after_all(hook) }
|
|
|
|
after_each_hooks.each { |hook| group.after_each(hook) }
|
|
|
|
around_each_hooks.each { |hook| group.around_each(hook) }
|
2021-07-17 20:01:27 +00:00
|
|
|
end
|
2021-07-17 17:10:44 +00:00
|
|
|
end
|
|
|
|
end
|