Add null constructor

This commit is contained in:
Michael Miller 2021-01-09 10:56:33 -07:00
parent 7d0ba752e9
commit cf422eca02
No known key found for this signature in database
GPG Key ID: FB9F12F7C646A4AD
1 changed files with 9 additions and 0 deletions

View File

@ -1,5 +1,6 @@
require "./context"
require "./context_method"
require "./null_context"
module Spectator
# Stores a test context and a method to call within it.
@ -10,6 +11,14 @@ module Spectator
def initialize(@context : Context, @method : ContextMethod)
end
# 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
# Invokes a method in the test context.
def call
@method.call(@context)