Define test context types

This commit is contained in:
Michael Miller 2020-09-05 14:55:49 -06:00
parent d31b8f4093
commit 9c6502234b
No known key found for this signature in database
GPG Key ID: FB9F12F7C646A4AD
4 changed files with 43 additions and 0 deletions

8
src/spectator/context.cr Normal file
View File

@ -0,0 +1,8 @@
require "../spectator_context"
module Spectator
# Base class that all test cases run in.
# This type is used to store all test case contexts as a single type.
# The instance must be downcast to the correct type before calling a context method.
alias Context = ::SpectatorContext
end

View File

@ -0,0 +1,18 @@
require "./context"
require "./context_method"
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
# Invokes a method in the test context.
def call
@method.call(@context)
end
end
end

View File

@ -0,0 +1,10 @@
require "./context"
module Spectator
# Encapsulates a method in a 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.
alias ContextMethod = Context ->
end

View File

@ -0,0 +1,7 @@
# Base class that all test cases run in.
# This type is used to store all test case contexts as a single type.
# The instance must be downcast to the correct type before calling a context method.
# This type is intentionally outside the `Spectator` module.
# The reason for this is to prevent name collision when using the DSL to define a spec.
abstract class SpectatorContext
end