Change MatchData to Expectation

After including negation into matcher logic, these types are equivalent.
This commit is contained in:
Michael Miller 2019-07-30 16:11:55 -06:00
parent 27f844ce25
commit a06247bb1e
2 changed files with 34 additions and 75 deletions

View file

@ -1,52 +1,52 @@
module Spectator::Expectations module Spectator::Expectations
# Ties together a partial, matcher, and their outcome. # Result of evaluating a matcher on an expectation partial.
class Expectation #
# Populates the base portiion of the expectation with values. # Procs are used to "store references" to the information.
# The *negated* flag should be true if the expectation is inverted. # Most of the time, the information in this struct is unused.
# The *match_data* is the value returned by `Spectator::Matcher#match` # To reduce unecessary overhead and memory usage,
# when the expectation was evaluated. # the values are constructed lazily, on-demand when needed.
# The *negated* flag and `MatchData#matched?` flag struct Expectation
# are mutually-exclusive in this context. # Indicates whether the matcher was satisified with the expectation partial.
def initialize(@match_data : Spectator::Matchers::MatchData, @negated : Bool) getter? matched : Bool
end
# Indicates whether the expectation was satisifed. # ditto
# This is true if:
# - The matcher was satisified and the expectation is not negated.
# - The matcher wasn't satisfied and the expectation is negated.
def satisfied? def satisfied?
@match_data.matched? ^ @negated matched?
end end
# Information about the match. # Extra information about the match that is shown in the result output.
# Returned value and type will something that has key-value pairs (like a `NamedTuple`). getter values : Array(MatchDataLabeledValue)
def values
@match_data.values.tap do |labeled_values| # Creates the expectation (match data).
if @negated #
labeled_values.each do |labeled_value| # The *matched* argument indicates
value = labeled_value.value # whether the matcher was satisified with the expectation partial.
value.negate # The *expected* and *actual* procs are what was expected to happen
end # and what actually happened.
end # They should write a string to the given IO.
end # The *values* are extra information about the match,
# that is shown in the result output.
def initialize(@matched, @expected : IO -> , @actual : IO -> , @values)
end end
# Text that indicates the condition that must be met for the expectation to be satisifed. # Description of what should have happened and would satisfy the matcher.
def expected_message # This is informational and displayed to the end-user.
@negated ? @match_data.negated_message : @match_data.message def expected
@expected.call
end end
# Text that indicates what the outcome was. # Description of what actually happened.
def actual_message # This is informational and displayed to the end-user.
@match_data.matched? ? @match_data.message : @match_data.negated_message def actual
@actual.call
end end
# Creates the JSON representation of the expectation. # Creates the JSON representation of the expectation.
def to_json(json : ::JSON::Builder) def to_json(json : ::JSON::Builder)
json.object do json.object do
json.field("satisfied", satisfied?) json.field("satisfied", satisfied?)
json.field("expected", expected_message) json.field("expected", expected)
json.field("actual", actual_message) json.field("actual", actual)
json.field("values") do json.field("values") do
json.object do json.object do
values.each do |labeled_value| values.each do |labeled_value|

View file

@ -1,41 +0,0 @@
require "./match_data_labeled_value"
module Spectator::Matchers
# Result of evaluating a matcher on an expectation partial.
#
# Procs are used to "store references" to the information.
# Most of the time, the information in this struct is unused.
# To reduce unecessary overhead and memory usage,
# the values are constructed lazily, on-demand when needed.
struct MatchData
# Indicates whether the matcher was satisified with the expectation partial.
getter? matched : Bool
# Extra information about the match that is shown in the result output.
getter values : Array(MatchDataLabeledValue)
# Creates the match data.
#
# The *matched* argument indicates
# whether the matcher was satisified with the expectation partial.
# The *expected* and *actual* procs are what was expected to happen
# and what actually happened.
# They should write a string to the given IO.
# The *values* are extra information about the match,
# that is shown in the result output.
def initialize(@matched, @expected : IO -> , @actual : IO -> , @values)
end
# Description of what should have happened and would satisfy the matcher.
# This is informational and displayed to the end-user.
def expected
@expected.call
end
# Description of what actually happened.
# This is informational and displayed to the end-user.
def actual
@actual.call
end
end
end