From 347e1a84e5ecc8d25133c40e28d5e14873bfc52e Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 7 Nov 2020 13:47:31 -0700 Subject: [PATCH] Dedicated example runner type --- src/spectator/example.cr | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/spectator/example.cr b/src/spectator/example.cr index bcfd96e..4b8c7e7 100644 --- a/src/spectator/example.cr +++ b/src/spectator/example.cr @@ -30,12 +30,10 @@ module Spectator # Returns the result of the execution. # The result will also be stored in `#result`. def run : Result - Log.debug { "Running example #{self}" } - elapsed = Time.measure do - @delegate.call(self) - end + runner = Runner.new(self, @delegate) + @result = runner.run + ensure @finished = true - @result = PassResult.new(elapsed) end # Exposes information about the example useful for debugging. @@ -53,5 +51,23 @@ module Spectator io << result end + + # Responsible for executing example code and reporting results. + private struct Runner + # Creates the runner. + # *example* is the example being tested. + # The *delegate* is the entrypoint of the example's test code. + def initialize(@example : Example, @delegate : ExampleContextDelegate) + end + + # Executes the example's test code and produces a result. + def run : Result + Log.debug { "Running example #{@example}" } + elapsed = Time.measure do + @delegate.call(@example) + end + PassResult.new(elapsed) + end + end end end