Add #call method to each result

This feels like a good middle-ground.
I didn't want this to be a factory method, or return an instance.
That seemed like overkill for formatting.
But I don't want to place if and case-statements everywhere in the 
formatters.
I hope this doesn't violate single-responsibility principle or any other 
guidelines.
This commit is contained in:
Michael Miller 2019-02-17 14:24:02 -07:00
parent 0b06e72f7e
commit 75f9a5838b
5 changed files with 23 additions and 0 deletions

View file

@ -6,5 +6,9 @@ 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`.
def call(interface)
interface.error(self)
end
end
end

View file

@ -18,5 +18,10 @@ module Spectator
def initialize(example, elapsed, @expectations, @error)
super(example, elapsed)
end
# Calls the `failure` method on `interface` and passes `self`.
def call(interface)
interface.failure(self)
end
end
end

View file

@ -5,5 +5,9 @@ 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`.
def call(interface)
interface.pending(self)
end
end
end

View file

@ -10,5 +10,10 @@ module Spectator
# and that this result is for.
def initialize(@example)
end
# 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`.
abstract def call(interface)
end
end

View file

@ -14,5 +14,10 @@ module Spectator
def initialize(example, elapsed, @expectations)
super(example, elapsed)
end
# Calls the `success` method on `interface` and passes `self`.
def call(interface)
interface.success(self)
end
end
end