Create example context variants

This commit is contained in:
Michael Miller 2020-09-06 10:31:23 -06:00
parent 3f7e0d7882
commit b866bc7e08
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
4 changed files with 34 additions and 3 deletions

View file

@ -1,7 +1,7 @@
require "./context"
module Spectator
# Encapsulates a method in a context.
# Encapsulates a method in a test context.
# This could be used to invoke a test case or hook method.
# The context is passed as an argument.
# The proc should downcast the context instance to the desired type

View file

@ -1,4 +1,4 @@
require "./context_delegate"
require "./example_context_delegate"
require "./example_group"
require "./example_node"
require "./result"
@ -19,7 +19,7 @@ module Spectator
# It can be a `Symbol` to describe a type.
# The *source* tracks where the example exists in source code.
# The example will be assigned to *group* if it is provided.
def initialize(@delegate : ContextDelegate,
def initialize(@delegate : ExampleContextDelegate,
name : String | Symbol? = nil, source : Source? = nil, group : ExampleGroup? = nil)
super(name, source, group)
end

View file

@ -0,0 +1,20 @@
require "./context"
require "./example_context_method"
module Spectator
# Stores a test context and a method to call within it.
# This is a variant of `ContextDelegate` that accepts the current running example.
struct ExampleContextDelegate
# 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 : ExampleContextMethod)
end
# Invokes a method in the test context.
# The *example* is the current running example.
def call(example : Example)
@method.call(example, @context)
end
end
end

View file

@ -0,0 +1,11 @@
require "./context"
module Spectator
# Encapsulates a method in a test context.
# This could be used to invoke a test case or hook method.
# The context is passed as an argument.
# The proc should downcast the context instance to the desired type
# and call a method on that context.
# The current example is also passed as an argument.
alias ExampleContextMethod = Example, Context ->
end