Initial change to MatchData

This commit is contained in:
Michael Miller 2019-02-23 21:52:10 -07:00
parent 87ca825845
commit 1e1503331e
5 changed files with 66 additions and 40 deletions

View file

@ -6,10 +6,9 @@ module Spectator::Expectations
# The *negated* flag should be true if the expectation is inverted. # The *negated* flag should be true if the expectation is inverted.
# These options are mutually-exclusive in this context. # These options are mutually-exclusive in this context.
# Don't flip the value of *matched* because *negated* is true. # Don't flip the value of *matched* because *negated* is true.
# The *partial* and the *matcher* arguments should reference # The *match_data* is the value returned by `Spectator::Matcher#match`
# the actual and expected result respectively. # when the expectation was evaluated.
def initialize(@matched : Bool, @negated : Bool, def initialize(@matched : Bool, @negated : Bool, @match_data : MatchData)
@partial : ExpectationPartial, @matcher : Matchers::Matcher)
end end
# Indicates whether the expectation was satisifed. # Indicates whether the expectation was satisifed.
@ -22,22 +21,12 @@ module Spectator::Expectations
# Text that indicates the condition that must be met for the expectation to be satisifed. # Text that indicates the condition that must be met for the expectation to be satisifed.
def expected_message def expected_message
@negated ? negated_message : message @negated ? @match_data.negated_message : @match_data.message
end end
# Text that indicates what the outcome was. # Text that indicates what the outcome was.
def actual_message def actual_message
satisfied? ? message : negated_message satisfied? ? @match_data.message : @match_data.negated_message
end
# Describes the condition that must be met for the expectation to be satisifed.
private def message
@matcher.message(@partial)
end
# Describes the condition under which the expectation won't be satisifed.
private def negated_message
@matcher.negated_message(@partial)
end end
end end
end end

View file

@ -44,8 +44,8 @@ module Spectator::Expectations
# Evaluates the expectation and returns it. # Evaluates the expectation and returns it.
private def eval(matcher, negated = false) private def eval(matcher, negated = false)
matched = matcher.match?(self) match_data = matcher.match(self)
Expectation.new(matched, negated, self, matcher) Expectation.new(matched, negated, match_data)
end end
# Reports an expectation to the current harness. # Reports an expectation to the current harness.

View file

@ -0,0 +1,27 @@
module Spectator::Matchers
# Information regarding a expectation parial and matcher.
# `Matcher#match` will return a sub-type of this.
abstract struct MatchData
# Indicates whether the matcher was satisified with the expectation partial.
getter? matched : Bool
# Creates the base of the match data.
# The *matched* argument indicates
# whether the matcher was satisified with the expectation partial.
def initialize(@matched)
end
# Information about the match.
# Returned value and type will differ by sub-type,
# but all will return a set of key-value pairs.
abstract def values
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
abstract def message : String
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
abstract def negated_message : String
end
end

View file

@ -14,14 +14,6 @@ module Spectator::Matchers
# Determines whether the matcher is satisfied with the value given to it. # Determines whether the matcher is satisfied with the value given to it.
# True is returned if the match was successful, false otherwise. # True is returned if the match was successful, false otherwise.
abstract def match?(partial) : Bool abstract def match(partial) : MatchData
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
abstract def message(partial) : String
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
abstract def negated_message(partial) : String
end end
end end

View file

@ -9,22 +9,40 @@ module Spectator::Matchers
super("nil?") super("nil?")
end end
# Determines whether the matcher is satisfied with the value given to it. # Determines whether the matcher is satisfied with the partial given to it.
# True is returned if the match was successful, false otherwise. # `MatchData` is returned that contains information about the match.
def match?(partial) def match(partial)
partial.actual.nil? actual = partial.actual
matched = actual.nil?
MatchData.new(matched, actual, partial.label)
end
# Match data specific to this matcher.
private struct MatchData(T) < MatchData
# Creates the match data.
def initialize(matched, @actual : T, @expected_label : String)
super(matched)
end
# Information about the match.
def values
{
expected: nil,
actual: @actual,
}
end end
# Describes the condition that satisfies the matcher. # Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user. # This is informational and displayed to the end-user.
def message(partial) def message
"Expected #{partial.label} to be nil" "#{@expected_label} is nil"
end end
# Describes the condition that won't satsify the matcher. # Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user. # This is informational and displayed to the end-user.
def negated_message(partial) def negated_message
"Expected #{partial.label} to not be nil" "#{@expected_label} is not nil"
end
end end
end end
end end