mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add docs for ExampleHooks
This commit is contained in:
parent
05c3a75683
commit
d52be1ec50
1 changed files with 20 additions and 0 deletions
|
@ -1,5 +1,9 @@
|
||||||
module Spectator
|
module Spectator
|
||||||
|
# Collection of hooks that run at various times throughout testing.
|
||||||
|
# A hook is just a `Proc` (code block) that runs at a specified time.
|
||||||
class ExampleHooks
|
class ExampleHooks
|
||||||
|
# Creates an empty set of hooks.
|
||||||
|
# This will effectively run nothing extra while running a test.
|
||||||
def self.empty
|
def self.empty
|
||||||
new(
|
new(
|
||||||
[] of ->,
|
[] of ->,
|
||||||
|
@ -10,6 +14,7 @@ module Spectator
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Creates a new set of hooks.
|
||||||
def initialize(
|
def initialize(
|
||||||
@before_all : Array(->),
|
@before_all : Array(->),
|
||||||
@before_each : Array(->),
|
@before_each : Array(->),
|
||||||
|
@ -19,30 +24,45 @@ module Spectator
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Runs all `before_all` hooks.
|
||||||
|
# These hooks should be run once before all examples in the group start.
|
||||||
def run_before_all
|
def run_before_all
|
||||||
@before_all.each &.call
|
@before_all.each &.call
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Runs all `before_each` hooks.
|
||||||
|
# These hooks should be run every time before each example in a group.
|
||||||
def run_before_each
|
def run_before_each
|
||||||
@before_each.each &.call
|
@before_each.each &.call
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Runs all `after_all` hooks.
|
||||||
|
# These hooks should be run once after all examples in group finish.
|
||||||
def run_after_all
|
def run_after_all
|
||||||
@after_all.each &.call
|
@after_all.each &.call
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Runs all `after_each` hooks.
|
||||||
|
# These hooks should be run every time after each example in a group.
|
||||||
def run_after_each
|
def run_after_each
|
||||||
@after_each.each &.call
|
@after_each.each &.call
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 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.
|
||||||
def wrap_around_each(&block : ->)
|
def wrap_around_each(&block : ->)
|
||||||
wrapper = block
|
wrapper = block
|
||||||
|
# Must wrap in reverse order,
|
||||||
|
# otherwise hooks will run in the wrong order.
|
||||||
@around_each.reverse_each do |hook|
|
@around_each.reverse_each do |hook|
|
||||||
wrapper = wrap_proc(hook, wrapper)
|
wrapper = wrap_proc(hook, wrapper)
|
||||||
end
|
end
|
||||||
wrapper
|
wrapper
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Utility method for wrapping one proc with another.
|
||||||
private def wrap_proc(inner : Proc(Nil) ->, wrapper : ->)
|
private def wrap_proc(inner : Proc(Nil) ->, wrapper : ->)
|
||||||
->{ inner.call(wrapper) }
|
->{ inner.call(wrapper) }
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue