From 9c6502234ba93c79aa2354dff12297c010024c76 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 5 Sep 2020 14:55:49 -0600 Subject: [PATCH] Define test context types --- src/spectator/context.cr | 8 ++++++++ src/spectator/context_delegate.cr | 18 ++++++++++++++++++ src/spectator/context_method.cr | 10 ++++++++++ src/spectator_contnext.cr | 7 +++++++ 4 files changed, 43 insertions(+) create mode 100644 src/spectator/context.cr create mode 100644 src/spectator/context_delegate.cr create mode 100644 src/spectator/context_method.cr create mode 100644 src/spectator_contnext.cr diff --git a/src/spectator/context.cr b/src/spectator/context.cr new file mode 100644 index 0000000..d2c5d55 --- /dev/null +++ b/src/spectator/context.cr @@ -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 diff --git a/src/spectator/context_delegate.cr b/src/spectator/context_delegate.cr new file mode 100644 index 0000000..1beef83 --- /dev/null +++ b/src/spectator/context_delegate.cr @@ -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 diff --git a/src/spectator/context_method.cr b/src/spectator/context_method.cr new file mode 100644 index 0000000..e1c9aa8 --- /dev/null +++ b/src/spectator/context_method.cr @@ -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 diff --git a/src/spectator_contnext.cr b/src/spectator_contnext.cr new file mode 100644 index 0000000..36ff503 --- /dev/null +++ b/src/spectator_contnext.cr @@ -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