2021-01-09 18:06:59 +00:00
|
|
|
require "./label"
|
|
|
|
require "./source"
|
|
|
|
|
|
|
|
module Spectator
|
2021-01-09 19:04:27 +00:00
|
|
|
# Information about a hook tied to an example group and a proc to invoke it.
|
2021-01-09 18:06:59 +00:00
|
|
|
class ExampleGroupHook
|
|
|
|
# Location of the hook in source code.
|
|
|
|
getter! source : Source
|
|
|
|
|
|
|
|
# User-defined description of the hook.
|
|
|
|
getter! label : Label
|
|
|
|
|
2021-01-09 19:04:27 +00:00
|
|
|
@proc : ->
|
|
|
|
|
|
|
|
# Creates the hook with a proc.
|
|
|
|
# The *proc* will be called when the hook is invoked.
|
2021-01-09 18:06:59 +00:00
|
|
|
# A *source* and *label* can be provided for debugging.
|
2021-01-09 19:04:27 +00:00
|
|
|
def initialize(@proc : (->), *, @source : Source? = nil, @label : Label = nil)
|
2021-01-09 18:06:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Creates the hook with a block.
|
|
|
|
# The block will be executed when the hook is invoked.
|
|
|
|
# A *source* and *label* can be provided for debugging.
|
|
|
|
def initialize(*, @source : Source? = nil, @label : Label = nil, &block : -> _)
|
2021-01-09 19:04:27 +00:00
|
|
|
@proc = block
|
2021-01-09 18:06:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Invokes the hook.
|
|
|
|
def call : Nil
|
2021-01-09 19:04:27 +00:00
|
|
|
@proc.call
|
2021-01-09 18:06:59 +00:00
|
|
|
end
|
2021-01-09 18:14:27 +00:00
|
|
|
|
|
|
|
# Produces the string representation of the hook.
|
|
|
|
# Includes the source and label if they're not nil.
|
|
|
|
def to_s(io)
|
|
|
|
io << "example group hook"
|
|
|
|
|
|
|
|
if (label = @label)
|
|
|
|
io << ' '
|
|
|
|
io << label
|
|
|
|
end
|
|
|
|
|
|
|
|
if (source = @source)
|
|
|
|
io << " @ "
|
|
|
|
io << source
|
|
|
|
end
|
|
|
|
end
|
2021-01-09 18:06:59 +00:00
|
|
|
end
|
|
|
|
end
|