shard-spectator/src/spectator/example_hooks.cr

40 lines
776 B
Crystal
Raw Normal View History

2018-09-23 21:26:19 +00:00
module Spectator
class ExampleHooks
def initialize(
@before_all : Array(->),
@before_each : Array(->),
@after_all : Array(->),
@after_each : Array(->),
@around_each : Array(Proc(Nil) ->))
end
def run_before_all
@before_all.each(&.call)
end
def run_before_each
@before_each.each(&.call)
end
def run_after_all
@after_all.each(&.call)
end
def run_after_each
@after_each.each(&.call)
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 : ->)
-> { inner.call(wrapper) }
end
end
end