From a36982d6d6bdf2e7c9d81f86e04077915afb395c Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 15 May 2021 19:43:13 -0600 Subject: [PATCH] Use visitor pattern --- src/spectator/spec/events.cr | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/spectator/spec/events.cr b/src/spectator/spec/events.cr index b22eee0..1accae3 100644 --- a/src/spectator/spec/events.cr +++ b/src/spectator/spec/events.cr @@ -24,13 +24,9 @@ module Spectator # See `Formatting::Formatter#example_finished` private def example_finished(example) notification = Formatting::ExampleNotification.new(example) + visitor = ResultVisitor.new(formatter, notification) formatter.example_started(notification) - - case example.result - when .fail? then formatter.example_failed(notification) - when .pass? then formatter.example_passed(notification) - else formatter.example_pending(notification) - end + example.result.accept(visitor) end # Triggers the 'stop' event. @@ -44,6 +40,34 @@ module Spectator private def close formatter.close(nil) end + + # Provides methods for the various result types. + private struct ResultVisitor + # Creates the visitor. + # Requires the *formatter* to notify and the *notification* to send it. + def initialize(@formatter : Formatting::Formatter, @notification : Formatting::ExampleNotification) + end + + # Invokes the example passed method. + def pass + @formatter.example_passed(@notification) + end + + # Invokes the example failed method. + def fail + @formatter.example_failed(@notification) + end + + # Invokes the example failed method. + def error + @formatter.example_failed(@notification) + end + + # Invokes the example pending method. + def pending + @formatter.example_pending(@notification) + end + end end end end