From 002c1d892b991af5259c014d56696788c7fcb265 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Wed, 20 Feb 2019 21:00:22 -0700 Subject: [PATCH] Adjust how Result#call works --- src/spectator/errored_result.cr | 11 +++++++++-- src/spectator/failed_result.cr | 11 +++++++++-- src/spectator/pending_result.cr | 11 +++++++++-- src/spectator/result.cr | 7 +++++++ src/spectator/successful_result.cr | 11 +++++++++-- 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/spectator/errored_result.cr b/src/spectator/errored_result.cr index 790c242..fd06e67 100644 --- a/src/spectator/errored_result.cr +++ b/src/spectator/errored_result.cr @@ -6,9 +6,16 @@ module Spectator # This is different from a "failed" result # in that the error was not from a failed expectation. class ErroredResult < FailedResult - # Calls the `error` method on *interface* and passes self. + # Calls the `error` method on *interface*. def call(interface) - interface.error(self) + interface.error + end + + # Calls the `error` method on *interface* + # and passes the yielded value. + def call(interface) + value = yield self + interface.error(value) end end end diff --git a/src/spectator/failed_result.cr b/src/spectator/failed_result.cr index f1d3526..7364c62 100644 --- a/src/spectator/failed_result.cr +++ b/src/spectator/failed_result.cr @@ -16,9 +16,16 @@ module Spectator super(example, elapsed, expectations) end - # Calls the `failure` method on *interface* and passes self. + # Calls the `failure` method on *interface*. def call(interface) - interface.failure(self) + interface.failure + end + + # Calls the `failure` method on *interface* + # and passes the yielded value. + def call(interface) + value = yield self + interface.failure(value) end end end diff --git a/src/spectator/pending_result.cr b/src/spectator/pending_result.cr index 6643dff..7e930c5 100644 --- a/src/spectator/pending_result.cr +++ b/src/spectator/pending_result.cr @@ -5,9 +5,16 @@ module Spectator # A pending result means the example is not ready to run yet. # This can happen when the functionality to be tested is not implemented yet. class PendingResult < Result - # Calls the `pending` method on *interface* and passes self. + # Calls the `pending` method on *interface*. def call(interface) - interface.pending(self) + interface.pending + end + + # Calls the `pending` method on *interface* + # and passes the yielded value. + def call(interface) + value = yield self + interface.pending(value) end end end diff --git a/src/spectator/result.cr b/src/spectator/result.cr index b24cbda..5b016ea 100644 --- a/src/spectator/result.cr +++ b/src/spectator/result.cr @@ -15,5 +15,12 @@ module Spectator # This is used to avoid placing if or case-statements everywhere based on type. # Each sub-class implements this method by calling the correct method on *interface*. abstract def call(interface) + + # Calls the corresponding method for the type of result. + # This is used to avoid placing if or case-statements everywhere based on type. + # Each sub-class implements this method by calling the correct method on *interface*. + # This variation takes a block, which is passed the result. + # The value returned from the block will be returned by this method. + abstract def call(interface, &block : Result -> _) end end diff --git a/src/spectator/successful_result.cr b/src/spectator/successful_result.cr index 23bad8b..f590812 100644 --- a/src/spectator/successful_result.cr +++ b/src/spectator/successful_result.cr @@ -3,9 +3,16 @@ require "./finished_result" module Spectator # Outcome that indicates running an example was successful. class SuccessfulResult < FinishedResult - # Calls the `success` method on *interface* and passes self. + # Calls the `success` method on *interface*. def call(interface) - interface.success(self) + interface.success + end + + # Calls the `success` method on *interface* + # and passes the yielded value. + def call(interface) + value = yield self + interface.success(value) end end end