Don't need delegates stored in hooks

Use proc instead. The context can be retrieved from the example 
instance.
This commit is contained in:
Michael Miller 2021-01-09 12:04:27 -07:00
parent c4289b82da
commit 65dba9f317
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
2 changed files with 16 additions and 14 deletions

View file

@ -1,9 +1,8 @@
require "./context_delegate"
require "./label" require "./label"
require "./source" require "./source"
module Spectator module Spectator
# Information about a hook tied to an example group and a delegate to invoke it. # Information about a hook tied to an example group and a proc to invoke it.
class ExampleGroupHook class ExampleGroupHook
# Location of the hook in source code. # Location of the hook in source code.
getter! source : Source getter! source : Source
@ -11,22 +10,24 @@ module Spectator
# User-defined description of the hook. # User-defined description of the hook.
getter! label : Label getter! label : Label
# Creates the hook with a context delegate. @proc : ->
# The *delegate* will be called when the hook is invoked.
# Creates the hook with a proc.
# The *proc* will be called when the hook is invoked.
# A *source* and *label* can be provided for debugging. # A *source* and *label* can be provided for debugging.
def initialize(@delegate : ContextDelegate, *, @source : Source? = nil, @label : Label = nil) def initialize(@proc : (->), *, @source : Source? = nil, @label : Label = nil)
end end
# Creates the hook with a block. # Creates the hook with a block.
# The block will be executed when the hook is invoked. # The block will be executed when the hook is invoked.
# A *source* and *label* can be provided for debugging. # A *source* and *label* can be provided for debugging.
def initialize(*, @source : Source? = nil, @label : Label = nil, &block : -> _) def initialize(*, @source : Source? = nil, @label : Label = nil, &block : -> _)
@delegate = ContextDelegate.null(&block) @proc = block
end end
# Invokes the hook. # Invokes the hook.
def call : Nil def call : Nil
@delegate.call @proc.call
end end
# Produces the string representation of the hook. # Produces the string representation of the hook.

View file

@ -1,9 +1,8 @@
require "./example_context_delegate"
require "./label" require "./label"
require "./source" require "./source"
module Spectator module Spectator
# Information about a hook tied to an example and a delegate to invoke it. # Information about a hook tied to an example and a proc to invoke it.
class ExampleHook class ExampleHook
# Location of the hook in source code. # Location of the hook in source code.
getter! source : Source getter! source : Source
@ -11,10 +10,12 @@ module Spectator
# User-defined description of the hook. # User-defined description of the hook.
getter! label : Label getter! label : Label
# Creates the hook with an example context delegate. @proc : Example ->
# The *delegate* will be called when the hook is invoked.
# Creates the hook with a proc.
# The *proc* will be called when the hook is invoked.
# A *source* and *label* can be provided for debugging. # A *source* and *label* can be provided for debugging.
def initialize(@delegate : ExampleContextDelegate, *, @source : Source? = nil, @label : Label = nil) def initialize(@proc : (Example ->), *, @source : Source? = nil, @label : Label = nil)
end end
# Creates the hook with a block. # Creates the hook with a block.
@ -22,13 +23,13 @@ module Spectator
# The block will be executed when the hook is invoked. # The block will be executed when the hook is invoked.
# A *source* and *label* can be provided for debugging. # A *source* and *label* can be provided for debugging.
def initialize(*, @source : Source? = nil, @label : Label = nil, &block : Example -> _) def initialize(*, @source : Source? = nil, @label : Label = nil, &block : Example -> _)
@delegate = ExampleContextDelegate.null(&block) @proc = block
end end
# Invokes the hook. # Invokes the hook.
# The *example* refers to the current example. # The *example* refers to the current example.
def call(example : Example) : Nil def call(example : Example) : Nil
@delegate.call(example) @proc.call(example)
end end
# Produces the string representation of the hook. # Produces the string representation of the hook.