2018-10-14 23:10:12 +00:00
|
|
|
require "./example_group"
|
|
|
|
|
|
|
|
module Spectator
|
2018-11-20 21:04:14 +00:00
|
|
|
# A collection of examples and other example groups.
|
|
|
|
# This group can be nested under other groups.
|
2018-10-14 23:10:12 +00:00
|
|
|
class NestedExampleGroup < ExampleGroup
|
2018-11-20 21:04:14 +00:00
|
|
|
# Description from the user of the group's contents.
|
2018-10-14 23:10:12 +00:00
|
|
|
getter what : String
|
|
|
|
|
2018-11-20 21:04:14 +00:00
|
|
|
# Group that this is nested in.
|
2018-10-15 00:00:55 +00:00
|
|
|
getter parent : ExampleGroup
|
2018-10-14 23:10:12 +00:00
|
|
|
|
2018-11-20 21:04:14 +00:00
|
|
|
# Creates a new example group.
|
|
|
|
# The `what` argument is a description from the user.
|
|
|
|
# The `parent` should contain this group.
|
|
|
|
# After creating this group, the parent's children should be updated.
|
|
|
|
# The parent's children must contain this group,
|
|
|
|
# otherwise there may be unexpected behavior.
|
|
|
|
# The `hooks` are stored to be triggered later.
|
2018-10-14 23:10:12 +00:00
|
|
|
def initialize(@what, @parent, hooks : ExampleHooks)
|
|
|
|
super(hooks)
|
|
|
|
end
|
|
|
|
|
2018-11-20 21:04:14 +00:00
|
|
|
# Runs all of the `before_all` hooks.
|
|
|
|
# This should run prior to any examples in the group.
|
|
|
|
# The hooks will be run only once.
|
|
|
|
# Subsequent calls to this method will do nothing.
|
|
|
|
# Parent `before_all` hooks will be run first.
|
2018-12-06 02:57:14 +00:00
|
|
|
protected def run_before_all_hooks : Nil
|
2018-10-15 00:00:55 +00:00
|
|
|
parent.run_before_all_hooks
|
2018-10-14 23:10:12 +00:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2018-11-20 21:04:14 +00:00
|
|
|
# Runs all of the `before_each` hooks.
|
|
|
|
# This method should run prior to every example in the group.
|
|
|
|
# Parent `before_each` hooks will be run first.
|
2018-12-06 02:57:14 +00:00
|
|
|
protected def run_before_each_hooks : Nil
|
2018-10-15 00:00:55 +00:00
|
|
|
parent.run_before_each_hooks
|
2018-10-14 23:10:12 +00:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2018-11-20 21:04:14 +00:00
|
|
|
# Runs all of the `after_all` hooks.
|
|
|
|
# This should run following all examples in the group.
|
|
|
|
# The hooks will be run only once,
|
|
|
|
# and only after all examples in the group have finished.
|
|
|
|
# Subsequent calls after the hooks have been run will do nothing.
|
|
|
|
# Parent `after_all` hooks will be run last.
|
2018-12-06 02:57:14 +00:00
|
|
|
protected def run_after_all_hooks : Nil
|
2018-10-14 23:10:12 +00:00
|
|
|
super
|
2018-10-15 00:00:55 +00:00
|
|
|
parent.run_after_all_hooks
|
2018-10-14 23:10:12 +00:00
|
|
|
end
|
|
|
|
|
2018-11-20 21:04:14 +00:00
|
|
|
# Runs all of the `after_each` hooks.
|
|
|
|
# This method should run following every example in the group.
|
|
|
|
# Parent `after_each` hooks will be run last.
|
2018-12-06 02:57:14 +00:00
|
|
|
protected def run_after_each_hooks : Nil
|
2018-10-14 23:10:12 +00:00
|
|
|
super
|
2018-10-15 00:00:55 +00:00
|
|
|
parent.run_after_each_hooks
|
2018-10-14 23:10:12 +00:00
|
|
|
end
|
|
|
|
|
2018-11-20 21:04:14 +00:00
|
|
|
# Creates a proc that runs the `around_each` hooks
|
|
|
|
# in addition to a block passed to this method.
|
|
|
|
# To call the block and all `around_each` hooks,
|
|
|
|
# just invoke `Proc#call` on the returned proc.
|
|
|
|
# Parent `around_each` hooks will be in the outermost wrappings.
|
2018-10-14 23:10:12 +00:00
|
|
|
def wrap_around_each_hooks(&block : ->) : ->
|
2018-10-15 00:00:55 +00:00
|
|
|
wrapper = super(&block)
|
|
|
|
parent.wrap_around_each_hooks(&wrapper)
|
2018-10-14 23:10:12 +00:00
|
|
|
end
|
|
|
|
|
2018-11-20 21:04:14 +00:00
|
|
|
# Creates a string representation of the group.
|
|
|
|
# The string consists of `#what` appended to the parent.
|
|
|
|
# This results in a string like:
|
|
|
|
# `Foo #bar does something`
|
|
|
|
# for the following structure:
|
|
|
|
# ```
|
|
|
|
# describe Foo do
|
|
|
|
# describe "#bar" do
|
|
|
|
# it "does something" do
|
|
|
|
# # ...
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# ```
|
2018-10-14 23:10:12 +00:00
|
|
|
def to_s(io)
|
2018-10-15 00:00:55 +00:00
|
|
|
parent.to_s(io)
|
|
|
|
io << ' '
|
2018-10-14 23:10:12 +00:00
|
|
|
io << what
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|