shard-spectator/src/spectator/context_delegate.cr

28 lines
796 B
Crystal
Raw Normal View History

2020-09-05 20:55:49 +00:00
require "./context"
require "./context_method"
2021-01-09 17:56:33 +00:00
require "./null_context"
2020-09-05 20:55:49 +00:00
module Spectator
# Stores a test context and a method to call within it.
struct ContextDelegate
# Creates the delegate.
# The *context* is the instance of the test context.
# The *method* is proc that downcasts *context* and calls a method on it.
def initialize(@context : Context, @method : ContextMethod)
end
2021-01-09 17:56:33 +00:00
# Creates a delegate with a null context.
# The context will be ignored and the block will be executed in its original scope.
def self.null(&block : -> _)
context = NullContext.new
method = ContextMethod.new { block.call }
new(context, method)
end
2020-09-05 20:55:49 +00:00
# Invokes a method in the test context.
def call
@method.call(@context)
end
end
end