2021-01-16 23:28:33 +00:00
|
|
|
require "./label"
|
2021-02-13 05:46:22 +00:00
|
|
|
require "./location"
|
2021-01-16 23:28:33 +00:00
|
|
|
|
|
|
|
module Spectator
|
|
|
|
# Information about a hook tied to an example and a proc to invoke it.
|
|
|
|
class ExampleProcsyHook
|
|
|
|
# Location of the hook in source code.
|
2021-02-13 05:46:22 +00:00
|
|
|
getter! location : Location
|
2021-01-16 23:28:33 +00:00
|
|
|
|
|
|
|
# User-defined description of the hook.
|
|
|
|
getter! label : Label
|
|
|
|
|
|
|
|
@proc : Example::Procsy ->
|
|
|
|
|
|
|
|
# Creates the hook with a proc.
|
|
|
|
# The *proc* will be called when the hook is invoked.
|
2021-02-13 05:46:22 +00:00
|
|
|
# A *location* and *label* can be provided for debugging.
|
|
|
|
def initialize(@proc : (Example::Procsy ->), *, @location : Location? = nil, @label : Label = nil)
|
2021-01-16 23:28:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Creates the hook with a block.
|
|
|
|
# The block must take a single argument - the current example wrapped in a procsy.
|
|
|
|
# The block will be executed when the hook is invoked.
|
2021-02-13 05:46:22 +00:00
|
|
|
# A *location* and *label* can be provided for debugging.
|
|
|
|
def initialize(*, @location : Location? = nil, @label : Label = nil, &block : Example::Procsy -> _)
|
2021-01-16 23:28:33 +00:00
|
|
|
@proc = block
|
|
|
|
end
|
|
|
|
|
|
|
|
# Invokes the hook.
|
|
|
|
# The *example* refers to the current example.
|
|
|
|
def call(procsy : Example::Procsy) : Nil
|
|
|
|
@proc.call(procsy)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates an example procsy that invokes this hook.
|
|
|
|
def wrap(procsy : Example::Procsy) : Example::Procsy
|
|
|
|
procsy.wrap { call(procsy) }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Produces the string representation of the hook.
|
2021-02-13 05:46:22 +00:00
|
|
|
# Includes the location and label if they're not nil.
|
2022-11-05 02:56:02 +00:00
|
|
|
def to_s(io : IO) : Nil
|
2021-01-16 23:28:33 +00:00
|
|
|
io << "example hook"
|
|
|
|
|
|
|
|
if (label = @label)
|
2021-05-30 20:21:42 +00:00
|
|
|
io << ' ' << label
|
2021-01-16 23:28:33 +00:00
|
|
|
end
|
|
|
|
|
2021-02-13 05:46:22 +00:00
|
|
|
if (location = @location)
|
2021-05-30 20:21:42 +00:00
|
|
|
io << " @ " << location
|
2021-01-16 23:28:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|