2018-09-23 21:26:19 +00:00
|
|
|
module Spectator
|
|
|
|
class ExampleHooks
|
2018-09-24 06:25:15 +00:00
|
|
|
def self.empty
|
|
|
|
new(
|
|
|
|
[] of ->,
|
|
|
|
[] of ->,
|
|
|
|
[] of ->,
|
|
|
|
[] of ->,
|
|
|
|
[] of Proc(Nil) ->
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-09-23 21:26:19 +00:00
|
|
|
def initialize(
|
|
|
|
@before_all : Array(->),
|
|
|
|
@before_each : Array(->),
|
|
|
|
@after_all : Array(->),
|
|
|
|
@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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def run_before_each
|
2018-09-28 00:49:01 +00:00
|
|
|
@before_each.each &.call
|
2018-09-23 21:26:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def run_after_each
|
2018-09-28 00:49:01 +00:00
|
|
|
@after_each.each &.call
|
2018-09-23 21:26:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def wrap_around_each(&block : ->)
|
|
|
|
wrapper = block
|
|
|
|
@around_each.reverse_each do |hook|
|
|
|
|
wrapper = wrap_proc(hook, wrapper)
|
|
|
|
end
|
|
|
|
wrapper
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|