2018-09-23 21:26:19 +00:00
|
|
|
module Spectator
|
2018-11-16 20:56:43 +00:00
|
|
|
# Collection of hooks that run at various times throughout testing.
|
|
|
|
# A hook is just a `Proc` (code block) that runs at a specified time.
|
2018-09-23 21:26:19 +00:00
|
|
|
class ExampleHooks
|
2018-11-16 20:56:43 +00:00
|
|
|
# Creates an empty set of hooks.
|
|
|
|
# This will effectively run nothing extra while running a test.
|
2018-09-24 06:25:15 +00:00
|
|
|
def self.empty
|
|
|
|
new(
|
|
|
|
[] of ->,
|
|
|
|
[] of ->,
|
2019-01-02 00:48:07 +00:00
|
|
|
[] of ->,
|
|
|
|
[] of ->,
|
2018-09-24 06:25:15 +00:00
|
|
|
[] of Proc(Nil) ->
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-11-16 20:56:43 +00:00
|
|
|
# Creates a new set of hooks.
|
2018-09-23 21:26:19 +00:00
|
|
|
def initialize(
|
|
|
|
@before_all : Array(->),
|
2019-01-02 00:48:07 +00:00
|
|
|
@before_each : Array(->),
|
2018-09-23 21:26:19 +00:00
|
|
|
@after_all : Array(->),
|
2019-01-02 00:48:07 +00:00
|
|
|
@after_each : Array(->),
|
2018-09-27 23:50:50 +00:00
|
|
|
@around_each : Array(Proc(Nil) ->)
|
|
|
|
)
|
2018-09-23 21:26:19 +00:00
|
|
|
end
|
|
|
|
|
2018-11-16 20:56:43 +00:00
|
|
|
# Runs all `before_all` hooks.
|
|
|
|
# These hooks should be run once before all examples in the group start.
|
2018-09-23 21:26:19 +00:00
|
|
|
def run_before_all
|
2018-09-28 00:49:01 +00:00
|
|
|
@before_all.each &.call
|
2018-09-23 21:26:19 +00:00
|
|
|
end
|
|
|
|
|
2018-11-16 20:56:43 +00:00
|
|
|
# Runs all `before_each` hooks.
|
|
|
|
# These hooks should be run every time before each example in a group.
|
2019-01-02 00:48:07 +00:00
|
|
|
def run_before_each
|
|
|
|
@before_each.each &.call
|
2018-09-23 21:26:19 +00:00
|
|
|
end
|
|
|
|
|
2018-11-16 20:56:43 +00:00
|
|
|
# Runs all `after_all` hooks.
|
|
|
|
# These hooks should be run once after all examples in group finish.
|
2018-09-23 21:26:19 +00:00
|
|
|
def run_after_all
|
2018-09-28 00:49:01 +00:00
|
|
|
@after_all.each &.call
|
2018-09-23 21:26:19 +00:00
|
|
|
end
|
|
|
|
|
2018-11-16 20:56:43 +00:00
|
|
|
# Runs all `after_each` hooks.
|
|
|
|
# These hooks should be run every time after each example in a group.
|
2019-01-02 00:48:07 +00:00
|
|
|
def run_after_each
|
|
|
|
@after_each.each &.call
|
2018-09-23 21:26:19 +00:00
|
|
|
end
|
|
|
|
|
2018-11-16 20:56:43 +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.
|
2018-09-23 21:26:19 +00:00
|
|
|
def wrap_around_each(&block : ->)
|
|
|
|
wrapper = block
|
2018-11-16 20:56:43 +00:00
|
|
|
# Must wrap in reverse order,
|
|
|
|
# otherwise hooks will run in the wrong order.
|
2018-09-23 21:26:19 +00:00
|
|
|
@around_each.reverse_each do |hook|
|
|
|
|
wrapper = wrap_proc(hook, wrapper)
|
|
|
|
end
|
|
|
|
wrapper
|
|
|
|
end
|
|
|
|
|
2018-11-16 20:56:43 +00:00
|
|
|
# Utility method for wrapping one proc with another.
|
2018-09-23 21:26:19 +00:00
|
|
|
private def wrap_proc(inner : Proc(Nil) ->, wrapper : ->)
|
2018-09-27 23:50:50 +00:00
|
|
|
->{ inner.call(wrapper) }
|
2018-09-23 21:26:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|