Workaround context scope not used in method delegation

This commit is contained in:
Michael Miller 2021-01-16 16:52:16 -07:00
parent 153933b044
commit 13061cfb38
No known key found for this signature in database
GPG Key ID: FB9F12F7C646A4AD
1 changed files with 31 additions and 1 deletions

View File

@ -99,12 +99,28 @@ module Spectator
# An error will be raised if *klass* doesn't match the test context's type.
# The block given to this method will be executed within the test context.
#
# The context casted to an instance of *klass* is provided as a block argument.
#
# TODO: Benchmark compiler performance using this method versus client-side casting in a proc.
def with_context(klass)
protected def with_context(klass)
context = klass.cast(@context)
with context yield
end
# Casts the example's test context to a specific type.
# This is an advanced method intended for internal usage only.
#
# The *klass* defines the type of the test context.
# This is typically only known by the code constructing the example.
# An error will be raised if *klass* doesn't match the test context's type.
#
# The context casted to an instance of *klass* is returned.
#
# TODO: Benchmark compiler performance using this method versus client-side casting in a proc.
protected def cast_context(klass)
klass.cast(@context)
end
# Constructs the full name or description of the example.
# This prepends names of groups this example is part of.
def to_s(io)
@ -159,6 +175,20 @@ module Spectator
self.class.new(@example, &block)
end
# Executes code within the example's test context.
# This is an advanced method intended for internal usage only.
#
# The *klass* defines the type of the test context.
# This is typically only known by the code constructing the example.
# An error will be raised if *klass* doesn't match the test context's type.
# The block given to this method will be executed within the test context.
#
# TODO: Benchmark compiler performance using this method versus client-side casting in a proc.
protected def with_context(klass)
context = @example.cast_context(klass)
with context yield
end
# Allow instance to behave like an example.
forward_missing_to @example
end