Store matcher description in MatchData

This commit is contained in:
Michael Miller 2020-01-02 19:16:33 -07:00
parent 00fe913d77
commit d64ecc4192
17 changed files with 86 additions and 78 deletions

View file

@ -26,7 +26,7 @@ module Spectator::Matchers
match_data = matcher.match(element)
break match_data unless match_data.matched?
end
found || SuccessfulMatchData.new
found || SuccessfulMatchData.new(description)
end
# Negated matching for this matcher is not supported.

View file

@ -31,7 +31,7 @@ module Spectator::Matchers
when Int # Content differs.
failed_content_mismatch(expected_elements, actual_elements, index, actual.label)
when true # Contents are identical.
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else # Size differs.
failed_size_mismatch(expected_elements, actual_elements, actual.label)
end
@ -45,11 +45,11 @@ module Spectator::Matchers
case compare_arrays(expected_elements, actual_elements)
when Int # Contents differ.
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
when true # Contents are identical.
failed_content_identical(expected_elements, actual_elements, actual.label)
else # Size differs.
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
end
end
@ -85,7 +85,7 @@ module Spectator::Matchers
# Produces match data for a failure when the array sizes differ.
private def failed_size_mismatch(expected_elements, actual_elements, actual_label)
FailedMatchData.new("#{actual_label} does not contain exactly #{expected.label} (size mismatch)",
FailedMatchData.new(description, "#{actual_label} does not contain exactly #{expected.label} (size mismatch)",
expected: expected_elements.inspect,
actual: actual_elements.inspect,
"expected size": expected_elements.size.to_s,
@ -95,7 +95,7 @@ module Spectator::Matchers
# Produces match data for a failure when the array content is mismatched.
private def failed_content_mismatch(expected_elements, actual_elements, index, actual_label)
FailedMatchData.new("#{actual_label} does not contain exactly #{expected.label} (element mismatch)",
FailedMatchData.new(description, "#{actual_label} does not contain exactly #{expected.label} (element mismatch)",
expected: expected_elements[index].inspect,
actual: actual_elements[index].inspect,
index: index.to_s
@ -104,7 +104,7 @@ module Spectator::Matchers
# Produces match data for a failure when the arrays are identical, but they shouldn't be (negation).
private def failed_content_identical(expected_elements, actual_elements, actual_label)
FailedMatchData.new("#{actual_label} contains exactly #{expected.label}",
FailedMatchData.new(description, "#{actual_label} contains exactly #{expected.label}",
expected: "Not #{expected_elements.inspect}",
actual: actual_elements.inspect
)

View file

@ -28,9 +28,9 @@ module Spectator::Matchers
def match(actual : TestExpression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value)
if match?(snapshot)
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new("#{actual.label} does not have attributes #{expected.label}", **values(snapshot))
FailedMatchData.new(description, "#{actual.label} does not have attributes #{expected.label}", **values(snapshot))
end
end
@ -39,9 +39,9 @@ module Spectator::Matchers
def negated_match(actual : TestExpression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value)
if match?(snapshot)
FailedMatchData.new("#{actual.label} has attributes #{expected.label}", **negated_values(snapshot))
FailedMatchData.new(description, "#{actual.label} has attributes #{expected.label}", **negated_values(snapshot))
else
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
end
end

View file

@ -30,21 +30,21 @@ module Spectator::Matchers
before, after = change(actual)
if expected_before == before
if before == after
FailedMatchData.new("#{actual.label} did not change #{expression.label}",
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}",
before: before.inspect,
after: after.inspect
)
elsif expected_after == after
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new("#{actual.label} did not change #{expression.label} to #{expected_after.inspect}",
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label} to #{expected_after.inspect}",
before: before.inspect,
after: after.inspect,
expected: expected_after.inspect
)
end
else
FailedMatchData.new("#{expression.label} was not initially #{expected_before.inspect}",
FailedMatchData.new(description, "#{expression.label} was not initially #{expected_before.inspect}",
expected: expected_before.inspect,
actual: before.inspect,
)
@ -57,15 +57,15 @@ module Spectator::Matchers
before, after = change(actual)
if expected_before == before
if expected_after == after
FailedMatchData.new("#{actual.label} changed #{expression.label} from #{expected_before.inspect} to #{expected_after.inspect}",
FailedMatchData.new(description, "#{actual.label} changed #{expression.label} from #{expected_before.inspect} to #{expected_after.inspect}",
before: before.inspect,
after: after.inspect
)
else
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
end
else
FailedMatchData.new("#{expression.label} was not initially #{expected_before.inspect}",
FailedMatchData.new(description, "#{expression.label} was not initially #{expected_before.inspect}",
expected: expected_before.inspect,
actual: before.inspect,
)

View file

@ -27,18 +27,18 @@ module Spectator::Matchers
def match(actual : TestExpression(T)) : MatchData forall T
before, after = change(actual)
if expected != before
FailedMatchData.new("#{expression.label} was not initially #{expected}",
FailedMatchData.new(description, "#{expression.label} was not initially #{expected}",
expected: expected.inspect,
actual: before.inspect,
)
elsif before == after
FailedMatchData.new("#{actual.label} did not change #{expression.label} from #{expected}",
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label} from #{expected}",
before: before.inspect,
after: after.inspect,
expected: "Not #{expected.inspect}"
)
else
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
end
end
@ -47,14 +47,14 @@ module Spectator::Matchers
def negated_match(actual : TestExpression(T)) : MatchData forall T
before, after = change(actual)
if expected != before
FailedMatchData.new("#{expression.label} was not initially #{expected}",
FailedMatchData.new(description, "#{expression.label} was not initially #{expected}",
expected: expected.inspect,
actual: before.inspect
)
elsif before == after
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new("#{actual.label} changed #{expression.label} from #{expected}",
FailedMatchData.new(description, "#{actual.label} changed #{expression.label} from #{expected}",
before: before.inspect,
after: after.inspect,
expected: expected.inspect

View file

@ -25,12 +25,12 @@ module Spectator::Matchers
def match(actual : TestExpression(T)) : MatchData forall T
before, after = change(actual)
if before == after
FailedMatchData.new("#{actual.label} did not change #{expression.label}",
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}",
before: before.inspect,
after: after.inspect
)
else
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
end
end
@ -39,9 +39,9 @@ module Spectator::Matchers
def negated_match(actual : TestExpression(T)) : MatchData forall T
before, after = change(actual)
if before == after
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new("#{actual.label} changed #{expression.label}",
FailedMatchData.new(description, "#{actual.label} changed #{expression.label}",
before: before.inspect,
after: after.inspect
)

View file

@ -25,14 +25,14 @@ module Spectator::Matchers
def match(actual : TestExpression(T)) : MatchData forall T
before, after = change(actual)
if before == after
FailedMatchData.new("#{actual.label} did not change #{expression.label}",
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}",
before: before.inspect,
after: after.inspect
)
elsif @evaluator.call(before, after)
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new("#{actual.label} did not change #{expression.label} #{@relativity}",
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label} #{@relativity}",
before: before.inspect,
after: after.inspect
)

View file

@ -27,15 +27,15 @@ module Spectator::Matchers
def match(actual : TestExpression(T)) : MatchData forall T
before, after = change(actual)
if before == after
FailedMatchData.new("#{actual.label} did not change #{expression.label}",
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}",
before: before.inspect,
after: after.inspect,
expected: expected.inspect
)
elsif expected == after
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new("#{actual.label} did not change #{expression.label} to #{expected}",
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label} to #{expected}",
before: before.inspect,
after: after.inspect,
expected: expected.inspect

View file

@ -44,9 +44,9 @@ module Spectator::Matchers
# This method expects (and uses) the `#ends_with?` method on the value.
private def match_ends_with(actual_value, actual_label)
if actual_value.ends_with?(expected.value)
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new("#{actual_label} does not end with #{expected.label} (using #ends_with?)",
FailedMatchData.new(description, "#{actual_label} does not end with #{expected.label} (using #ends_with?)",
expected: expected.value.inspect,
actual: actual_value.inspect
)
@ -60,9 +60,9 @@ module Spectator::Matchers
last = list.last
if expected.value === last
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new("#{actual_label} does not end with #{expected.label} (using expected === last)",
FailedMatchData.new(description, "#{actual_label} does not end with #{expected.label} (using expected === last)",
expected: expected.value.inspect,
actual: last.inspect,
list: list.inspect
@ -74,12 +74,12 @@ module Spectator::Matchers
# This method expects (and uses) the `#ends_with?` method on the value.
private def negated_match_ends_with(actual)
if actual.value.ends_with?(expected.value)
FailedMatchData.new("#{actual.label} ends with #{expected.label} (using #ends_with?)",
FailedMatchData.new(description, "#{actual.label} ends with #{expected.label} (using #ends_with?)",
expected: expected.value.inspect,
actual: actual.value.inspect
)
else
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
end
end
@ -90,13 +90,13 @@ module Spectator::Matchers
last = list.last
if expected.value === last
FailedMatchData.new("#{actual.label} ends with #{expected.label} (using expected === last)",
FailedMatchData.new(description, "#{actual.label} ends with #{expected.label} (using expected === last)",
expected: expected.value.inspect,
actual: last.inspect,
list: list.inspect
)
else
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
end
end
end

View file

@ -33,16 +33,16 @@ module Spectator::Matchers
def match(actual : TestExpression(T)) : MatchData forall T
exception = capture_exception { actual.value }
if exception.nil?
FailedMatchData.new("#{actual.label} did not raise", expected: ExceptionType.inspect)
FailedMatchData.new(description, "#{actual.label} did not raise", expected: ExceptionType.inspect)
else
if exception.is_a?(ExceptionType)
if (value = expected.value).nil?
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
if value === exception.message
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new("#{actual.label} raised #{exception.class}, but the message is not #{expected.label}",
FailedMatchData.new(description, "#{actual.label} raised #{exception.class}, but the message is not #{expected.label}",
"expected type": ExceptionType.inspect,
"actual type": exception.class.inspect,
"expected message": value.inspect,
@ -51,7 +51,7 @@ module Spectator::Matchers
end
end
else
FailedMatchData.new("#{actual.label} did not raise #{ExceptionType}",
FailedMatchData.new(description, "#{actual.label} did not raise #{ExceptionType}",
expected: ExceptionType.inspect,
actual: exception.class.inspect
)
@ -64,28 +64,28 @@ module Spectator::Matchers
def negated_match(actual : TestExpression(T)) : MatchData forall T
exception = capture_exception { actual.value }
if exception.nil?
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
if exception.is_a?(ExceptionType)
if (value = expected.value).nil?
FailedMatchData.new("#{actual.label} raised #{exception.class}",
FailedMatchData.new(description, "#{actual.label} raised #{exception.class}",
expected: "Not #{ExceptionType}",
actual: exception.class.inspect
)
else
if value === exception.message
FailedMatchData.new("#{actual.label} raised #{exception.class} with message matching #{expected.label}",
FailedMatchData.new(description, "#{actual.label} raised #{exception.class} with message matching #{expected.label}",
"expected type": ExceptionType.inspect,
"actual type": exception.class.inspect,
"expected message": value.inspect,
"actual message": exception.message.to_s
)
else
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
end
end
else
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
end
end
end

View file

@ -15,11 +15,13 @@ module Spectator::Matchers
getter values : Array(Tuple(Symbol, String))
# Creates the match data.
def initialize(@failure_message, @values)
def initialize(description, @failure_message, @values)
super(description)
end
# Creates the match data.
def initialize(@failure_message, **values)
def initialize(description, @failure_message, **values)
super(description)
@values = values.to_a
end
end

View file

@ -3,5 +3,10 @@ module Spectator::Matchers
abstract struct MatchData
# Indicates whether the match as successful or not.
abstract def matched? : Bool
getter description : String
def initialize(@description : String)
end
end
end

View file

@ -24,9 +24,9 @@ module Spectator::Matchers
def match(actual : TestExpression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value)
if match?(snapshot)
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new("#{actual.label} is not #{expected.label}", **values(snapshot))
FailedMatchData.new(description, "#{actual.label} is not #{expected.label}", **values(snapshot))
end
end
@ -35,9 +35,9 @@ module Spectator::Matchers
def negated_match(actual : TestExpression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value)
if match?(snapshot)
FailedMatchData.new("#{actual.label} is #{expected.label}", **values(snapshot))
FailedMatchData.new(description, "#{actual.label} is #{expected.label}", **values(snapshot))
else
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
end
end

View file

@ -17,9 +17,9 @@ module Spectator::Matchers
def match(actual : TestExpression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value)
if match?(snapshot)
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new("#{actual.label} does not respond to #{label}", **values(snapshot))
FailedMatchData.new(description, "#{actual.label} does not respond to #{label}", **values(snapshot))
end
end
@ -28,9 +28,9 @@ module Spectator::Matchers
def negated_match(actual : TestExpression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value)
if match?(snapshot)
FailedMatchData.new("#{actual.label} responds to #{label}", **values(snapshot))
FailedMatchData.new(description, "#{actual.label} responds to #{label}", **values(snapshot))
else
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
end
end

View file

@ -25,9 +25,9 @@ module Spectator::Matchers
# Additionally, `#failure_message` and `#values` are called for a failed match.
def match(actual : TestExpression(T)) : MatchData forall T
if match?(actual)
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new(failure_message(actual), values(actual).to_a)
FailedMatchData.new(description, failure_message(actual), values(actual).to_a)
end
end
@ -39,10 +39,11 @@ module Spectator::Matchers
# Otherwise, a `FailedMatchData` instance is returned.
# Additionally, `#failure_message_when_negated` and `#negated_values` are called for a failed match.
def negated_match(actual : TestExpression(T)) : MatchData forall T
# TODO: Invert description.
if does_not_match?(actual)
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new(failure_message_when_negated(actual), negated_values(actual).to_a)
FailedMatchData.new(description, failure_message_when_negated(actual), negated_values(actual).to_a)
end
end

View file

@ -43,9 +43,9 @@ module Spectator::Matchers
# This method expects (and uses) the `#starts_with?` method on the value.
private def match_starts_with(actual_value, actual_label)
if actual_value.starts_with?(expected.value)
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new("#{actual_label} does not start with #{expected.label} (using #starts_with?)",
FailedMatchData.new(description, "#{actual_label} does not start with #{expected.label} (using #starts_with?)",
expected: expected.value.inspect,
actual: actual_value.inspect
)
@ -59,9 +59,9 @@ module Spectator::Matchers
first = list.first
if expected.value === first
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new("#{actual_label} does not start with #{expected.label} (using expected === first)",
FailedMatchData.new(description, "#{actual_label} does not start with #{expected.label} (using expected === first)",
expected: expected.value.inspect,
actual: first.inspect,
list: list.inspect
@ -73,12 +73,12 @@ module Spectator::Matchers
# This method expects (and uses) the `#starts_with?` method on the value.
private def negated_match_starts_with(actual_value, actual_label)
if actual_value.starts_with?(expected.value)
FailedMatchData.new("#{actual_label} starts with #{expected.label} (using #starts_with?)",
FailedMatchData.new(description, "#{actual_label} starts with #{expected.label} (using #starts_with?)",
expected: expected.value.inspect,
actual: actual_value.inspect
)
else
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
end
end
@ -89,13 +89,13 @@ module Spectator::Matchers
first = list.first
if expected.value === first
FailedMatchData.new("#{actual_label} starts with #{expected.label} (using expected === first)",
FailedMatchData.new(description, "#{actual_label} starts with #{expected.label} (using expected === first)",
expected: expected.value.inspect,
actual: first.inspect,
list: list.inspect
)
else
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
end
end
end

View file

@ -25,9 +25,9 @@ module Spectator::Matchers
missing, extra = array_diff(expected_elements, actual_elements)
if missing.empty? && extra.empty?
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
else
FailedMatchData.new("#{actual_label} does not contain #{expected.label} (unordered)",
FailedMatchData.new(description, "#{actual_label} does not contain #{expected.label} (unordered)",
expected: expected_elements.inspect,
actual: actual_elements.inspect,
missing: missing.inspect,
@ -44,12 +44,12 @@ module Spectator::Matchers
missing, extra = array_diff(expected_elements, actual_elements)
if missing.empty? && extra.empty?
FailedMatchData.new("#{actual_label} contains #{expected.label} (unordered)",
FailedMatchData.new(description, "#{actual_label} contains #{expected.label} (unordered)",
expected: "Not #{expected_elements.inspect}",
actual: actual_elements.inspect,
)
else
SuccessfulMatchData.new
SuccessfulMatchData.new(description)
end
end