From b866bc7e08b7d99b4df2ae9e1b431ba6badf0ebb Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sun, 6 Sep 2020 10:31:23 -0600 Subject: [PATCH] Create example context variants --- src/spectator/context_method.cr | 2 +- src/spectator/example.cr | 4 ++-- src/spectator/example_context_delegate.cr | 20 ++++++++++++++++++++ src/spectator/example_context_method.cr | 11 +++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/spectator/example_context_delegate.cr create mode 100644 src/spectator/example_context_method.cr diff --git a/src/spectator/context_method.cr b/src/spectator/context_method.cr index e1c9aa8..30cce6d 100644 --- a/src/spectator/context_method.cr +++ b/src/spectator/context_method.cr @@ -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 diff --git a/src/spectator/example.cr b/src/spectator/example.cr index 6f810ab..c392fab 100644 --- a/src/spectator/example.cr +++ b/src/spectator/example.cr @@ -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 diff --git a/src/spectator/example_context_delegate.cr b/src/spectator/example_context_delegate.cr new file mode 100644 index 0000000..bd5aa9c --- /dev/null +++ b/src/spectator/example_context_delegate.cr @@ -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 diff --git a/src/spectator/example_context_method.cr b/src/spectator/example_context_method.cr new file mode 100644 index 0000000..a3e3dfc --- /dev/null +++ b/src/spectator/example_context_method.cr @@ -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