shard-spectator/src/spectator/example_hooks.cr

51 lines
922 B
Crystal
Raw Normal View History

2018-09-23 21:26:19 +00:00
module Spectator
class ExampleHooks
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
@before_all.each &.call
2018-09-23 21:26:19 +00:00
end
def run_before_each
@before_each.each &.call
2018-09-23 21:26:19 +00:00
end
def run_after_all
@after_all.each &.call
2018-09-23 21:26:19 +00:00
end
def run_after_each
@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