Pass negation flag to matcher

Some matchers need to know if they're negated to work properly and can't
be simply negated afterwards.
This commit is contained in:
Michael Miller 2019-07-30 16:19:27 -06:00
parent ab1c0269e0
commit e95c81f46e
31 changed files with 33 additions and 69 deletions

View file

@ -27,13 +27,13 @@ module Spectator::Expectations
# Asserts that some criteria defined by the matcher is satisfied.
def to(matcher) : Nil
report(eval(matcher))
report(matcher.match(self, false))
end
# Asserts that some criteria defined by the matcher is not satisfied.
# This is effectively the opposite of `#to`.
def to_not(matcher) : Nil
report(eval(matcher, true))
report(matcher.match(self, true))
end
# ditto
@ -42,12 +42,6 @@ module Spectator::Expectations
to_not(matcher)
end
# Evaluates the expectation and returns it.
private def eval(matcher, negated = false)
match_data = matcher.match(self)
Expectation.new(match_data, negated)
end
# Reports an expectation to the current harness.
private def report(expectation : Expectation)
Internals::Harness.current.report_expectation(expectation)

View file

@ -6,8 +6,7 @@ module Spectator::Matchers
# has the exact same contents as another and in the same order.
struct ArrayMatcher(ExpectedType) < ValueMatcher(Enumerable(ExpectedType))
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
actual = partial.actual.to_a
expected_elements = expected.to_a
values = ExpectedActual.new(expected_elements, label, actual, partial.label)

View file

@ -17,8 +17,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
actual_values = snapshot_values(partial.actual)
values = ExpectedActual.new(expected, label, actual_values, partial.label)
MatchData.new(match?(actual_values), values)

View file

@ -10,8 +10,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end

View file

@ -9,8 +9,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
actual = partial.actual
matched = match?(actual)
MatchData.new(matched, ExpectedActual.new(partial, self))

View file

@ -12,8 +12,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end

View file

@ -10,8 +10,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
actual = partial.actual
matched = actual.empty?
MatchData.new(matched, actual, partial.label)

View file

@ -16,8 +16,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
actual = values.actual
if actual.responds_to?(:ends_with?)

View file

@ -10,8 +10,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end

View file

@ -21,8 +21,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
exception = capture_exception { partial.actual }
matched = match?(exception)
if exception.nil?

View file

@ -10,8 +10,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end

View file

@ -10,8 +10,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end

View file

@ -10,8 +10,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end

View file

@ -35,8 +35,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end

View file

@ -19,8 +19,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial) : MatchData
def match(partial, negated = false)
values = snapshot_values(partial.actual)
MatchData.new(match?(values), values, partial.label, label)
end

View file

@ -10,8 +10,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end

View file

@ -10,8 +10,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end

View file

@ -10,8 +10,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end

View file

@ -10,8 +10,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end

View file

@ -10,8 +10,7 @@ module Spectator::Matchers
# but for verbose output to help the end-user.
abstract def label : String
# Determines whether the matcher is satisfied with the value given to it.
# True is returned if the match was successful, false otherwise.
abstract def match(partial) : MatchData
# Determines whether the matcher is satisfied with the partial given to it.
def match(partial, negated = false) : Expectation
end
end

View file

@ -10,8 +10,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
actual = partial.actual
matched = actual.nil?
MatchData.new(matched, actual, partial.label)

View file

@ -18,8 +18,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial) : MatchData
def match(partial, negated = false)
values = snapshot_values(partial.actual)
MatchData.new(match?(values), values, partial.label, label)
end

View file

@ -10,8 +10,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
actual = partial.actual
matched = match?(actual)
expected_value = @expected

View file

@ -10,8 +10,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end

View file

@ -14,8 +14,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = snapshot_values(partial.actual)
MatchData.new(match?(values), values, partial.label, label)
end

View file

@ -5,8 +5,7 @@ module Spectator::Matchers
# The set's `#size` method is used for this check.
struct SizeMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
actual = partial.actual.size
values = ExpectedActual.new(expected, label, actual, partial.label)
MatchData.new(actual == expected, values)

View file

@ -5,8 +5,7 @@ module Spectator::Matchers
# The set's `#size` method is used for this check.
struct SizeOfMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
actual = partial.actual.size
size = expected.size
values = ExpectedActual.new(size, label, actual, partial.label)

View file

@ -16,8 +16,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
actual = values.actual
if actual.responds_to?(:starts_with?)

View file

@ -27,8 +27,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
actual = partial.actual
MatchData.new(match?(actual), @truthy, actual, partial.label)
end

View file

@ -16,8 +16,7 @@ module Spectator::Matchers
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
actual = partial.actual
MatchData(Expected, typeof(actual)).new(match?(actual), actual, partial.label)
end

View file

@ -5,8 +5,7 @@ module Spectator::Matchers
# has the exact same contents as another, but in any order.
struct UnorderedArrayMatcher(ExpectedType) < ValueMatcher(Enumerable(ExpectedType))
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
def match(partial, negated = false)
expected_elements = expected.to_a
actual = partial.actual.to_a
missing, extra = array_diff(expected, actual)