Change Expectation to store the result

This removes Expectation::Result and uses Expectation and sub-types
instead.
Having two "Result" types is confusing.
This commit is contained in:
Michael Miller 2018-11-13 14:08:27 -07:00
parent e38747eafd
commit 06ced9f799
2 changed files with 40 additions and 63 deletions

View file

@ -1,57 +1,35 @@
module Spectator::Expectations module Spectator::Expectations
# Min-in for all expectation types. abstract class Expectation
# Classes that include this must implement # Populates the base portiion of the expectation with values.
# the `#satisfied?`, `#message`, and `#negated_message` methods. # The `matched` flag should be true if the matcher is satisfied with the partial.
# Typically, expectation classes/structs store an `ExpectationPartial` # The `negated` flag should be true if the expectation is inverted.
# and a `Matchers::Matcher` and then proxy calls to those instances. # These options are mutually-exclusive in this context.
module Expectation # Don't flip the value of `matched` because `negated` is true.
# Checks whether the expectation is met. def initialize(@matched : Bool, @negated : Bool)
abstract def satisfied? : Bool
# Describes the condition that must be met for the expectation to be satisifed.
abstract def message : String
# Describes the condition under which the expectation won't be satisifed.
abstract def negated_message : String
# Evaulates the expectation and produces a result.
# The `negated` flag should be set to true to invert the result.
def eval(negated = false) : Result
success = satisfied? ^ negated
Result.new(success, negated, self)
end end
# Information regarding the outcome of an expectation. # Indicates whether the expectation was satisifed.
class Result # This is true if:
# Indicates whether the expectation was satisifed or not. # - The matcher was satisified and the expectation is not negated.
getter? successful : Bool # - The matcher wasn't satisfied and the expectation is negated.
def satisfied?
# Indicates whether the expectation failed. @matched ^ @negated
def failure? : Bool
!@successful
end end
# Creates the result. # Text that indicates the condition that must be met for the expectation to be satisifed.
# The expectation is stored so that information from it may be lazy-loaded.
protected def initialize(@successful, @negated : Bool, @expectation : Expectation)
end
# Description of the condition that satisfies, or meets, the expectation.
def expected_message def expected_message
message(@negated) @negated ? negated_message : message
end end
# Description of what actually happened when the expectation was evaluated. # Text that indicates what the outcome was.
def actual_message def actual_message
message(!@successful) @matched ? message : negated_message
end end
# Retrieves the message or negated message from an expectation. # Describes the condition that must be met for the matcher to be satisifed.
# Set `negated` to true to get the negated message, private abstract def message : String
# or to false to get the regular message.
private def message(negated) # Describes the condition under which the matcher won't be satisifed.
negated ? @expectation.negated_message : @expectation.message private abstract def negated_message : String
end
end
end end
end end

View file

@ -5,27 +5,26 @@ module Spectator::Expectations
# There are two values - the actual and expected. # There are two values - the actual and expected.
# The actual value is what the SUT returned. # The actual value is what the SUT returned.
# The expected value is what the test wants to see. # The expected value is what the test wants to see.
struct ValueExpectation(ActualType, ExpectedType) class ValueExpectation(ActualType, ExpectedType) < Expectation
include Expectation
# Creates the expectation. # Creates the expectation.
# This simply takes in the expectation partial and the matcher. # The `matched` flag should be true if the matcher is satisfied with the partial.
def initialize(@partial : ValueExpectationPartial(ActualType), # The `negated` flag should be true if the expectation is inverted.
# See `Expectation#initialize` for details on these two arguments.
# The `partial` and the `matcher` arguments should reference
# the actual and expected value with matcher respectively.
def initialize(matched, negated,
@partial : ValueExpectationPartial(ActualType),
@matcher : Matchers::ValueMatcher(ExpectedType)) @matcher : Matchers::ValueMatcher(ExpectedType))
end super(matched, negated)
# Checks whether the expectation is met.
def satisfied? : Bool
@matcher.match?(@partial)
end end
# Describes the condition that must be met for the expectation to be satisifed. # Describes the condition that must be met for the expectation to be satisifed.
def message : String private def message : String
@matcher.message(@partial) @matcher.message(@partial)
end end
# Describes the condition under which the expectation won't be satisifed. # Describes the condition under which the expectation won't be satisifed.
def negated_message : String private def negated_message : String
@matcher.negated_message(@partial) @matcher.negated_message(@partial)
end end
end end