Verified Commit 708fd692 authored by Mike Miller's avatar Mike Miller
Browse files

Add actual label to match data description

parent babc7ebc
......@@ -26,7 +26,7 @@ module Spectator::Matchers
match_data = matcher.match(element)
break match_data unless match_data.matched?
end
found || SuccessfulMatchData.new(description)
found || SuccessfulMatchData.new(match_data_description(actual))
end
# Negated matching for this matcher is not supported.
......
......@@ -32,10 +32,10 @@ module Spectator::Matchers
if missing.empty? && extra.empty?
# Contents are identical.
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
# Content differs.
FailedMatchData.new(description, "#{actual.label} does not contain exactly #{expected.label}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} does not contain exactly #{expected.label}",
expected: expected_elements.inspect,
actual: actual_elements.inspect,
missing: missing.empty? ? "None" : missing.inspect,
......@@ -56,13 +56,13 @@ module Spectator::Matchers
if missing.empty? && extra.empty?
# Contents are identical.
FailedMatchData.new(description, "#{actual.label} contains exactly #{expected.label}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} contains exactly #{expected.label}",
expected: "Not #{expected_elements.inspect}",
actual: actual_elements.inspect
)
else
# Content differs.
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
end
end
......
......@@ -28,9 +28,9 @@ module Spectator::Matchers
def match(actual : Expression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value)
if match?(snapshot)
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
FailedMatchData.new(description, "#{actual.label} does not have attributes #{expected.label}", values(snapshot).to_a)
FailedMatchData.new(match_data_description(actual), "#{actual.label} does not have attributes #{expected.label}", values(snapshot).to_a)
end
end
......@@ -39,9 +39,9 @@ module Spectator::Matchers
def negated_match(actual : Expression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value)
if match?(snapshot)
FailedMatchData.new(description, "#{actual.label} has attributes #{expected.label}", negated_values(snapshot).to_a)
FailedMatchData.new(match_data_description(actual), "#{actual.label} has attributes #{expected.label}", negated_values(snapshot).to_a)
else
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
end
end
......
......@@ -30,21 +30,21 @@ module Spectator::Matchers
before, after = change(actual)
if expected_before == before
if before == after
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} did not change #{expression.label}",
before: before.inspect,
after: after.inspect
)
elsif expected_after == after
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label} to #{expected_after.inspect}",
FailedMatchData.new(match_data_description(actual), "#{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(description, "#{expression.label} was not initially #{expected_before.inspect}",
FailedMatchData.new(match_data_description(actual), "#{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(description, "#{actual.label} changed #{expression.label} from #{expected_before.inspect} to #{expected_after.inspect}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} changed #{expression.label} from #{expected_before.inspect} to #{expected_after.inspect}",
before: before.inspect,
after: after.inspect
)
else
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
end
else
FailedMatchData.new(description, "#{expression.label} was not initially #{expected_before.inspect}",
FailedMatchData.new(match_data_description(actual), "#{expression.label} was not initially #{expected_before.inspect}",
expected: expected_before.inspect,
actual: before.inspect,
)
......
......@@ -27,18 +27,18 @@ module Spectator::Matchers
def match(actual : Expression(T)) : MatchData forall T
before, after = change(actual)
if expected != before
FailedMatchData.new(description, "#{expression.label} was not initially #{expected}",
FailedMatchData.new(match_data_description(actual), "#{expression.label} was not initially #{expected}",
expected: expected.inspect,
actual: before.inspect,
)
elsif before == after
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label} from #{expected}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} did not change #{expression.label} from #{expected}",
before: before.inspect,
after: after.inspect,
expected: "Not #{expected.inspect}"
)
else
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
end
end
......@@ -47,14 +47,14 @@ module Spectator::Matchers
def negated_match(actual : Expression(T)) : MatchData forall T
before, after = change(actual)
if expected != before
FailedMatchData.new(description, "#{expression.label} was not initially #{expected}",
FailedMatchData.new(match_data_description(actual), "#{expression.label} was not initially #{expected}",
expected: expected.inspect,
actual: before.inspect
)
elsif before == after
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
FailedMatchData.new(description, "#{actual.label} changed #{expression.label} from #{expected}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} changed #{expression.label} from #{expected}",
before: before.inspect,
after: after.inspect,
expected: expected.inspect
......
......@@ -25,12 +25,12 @@ module Spectator::Matchers
def match(actual : Expression(T)) : MatchData forall T
before, after = change(actual)
if before == after
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} did not change #{expression.label}",
before: before.inspect,
after: after.inspect
)
else
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
end
end
......@@ -39,9 +39,9 @@ module Spectator::Matchers
def negated_match(actual : Expression(T)) : MatchData forall T
before, after = change(actual)
if before == after
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
FailedMatchData.new(description, "#{actual.label} changed #{expression.label}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} changed #{expression.label}",
before: before.inspect,
after: after.inspect
)
......
......@@ -25,14 +25,14 @@ module Spectator::Matchers
def match(actual : Expression(T)) : MatchData forall T
before, after = change(actual)
if before == after
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} did not change #{expression.label}",
before: before.inspect,
after: after.inspect
)
elsif @evaluator.call(before, after)
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label} #{@relativity}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} did not change #{expression.label} #{@relativity}",
before: before.inspect,
after: after.inspect
)
......
......@@ -27,15 +27,15 @@ module Spectator::Matchers
def match(actual : Expression(T)) : MatchData forall T
before, after = change(actual)
if before == after
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} did not change #{expression.label}",
before: before.inspect,
after: after.inspect,
expected: expected.inspect
)
elsif expected == after
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label} to #{expected}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} did not change #{expression.label} to #{expected}",
before: before.inspect,
after: after.inspect,
expected: expected.inspect
......
......@@ -29,10 +29,10 @@ module Spectator::Matchers
if missing.empty?
# Contents are present.
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
# Content is missing.
FailedMatchData.new(description, "#{actual.label} does not contain #{expected.label}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} does not contain #{expected.label}",
expected: expected.value.inspect,
actual: actual_value.inspect,
missing: missing.inspect,
......@@ -52,13 +52,13 @@ module Spectator::Matchers
if satisfied
# Contents are present.
FailedMatchData.new(description, "#{actual.label} contains #{expected.label}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} contains #{expected.label}",
expected: "Not #{expected.value.inspect}",
actual: actual_value.inspect
)
else
# Content is missing.
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
end
end
......
......@@ -46,9 +46,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(description)
SuccessfulMatchData.new(match_data_description(actual_label))
else
FailedMatchData.new(description, "#{actual_label} does not end with #{expected.label} (using #ends_with?)",
FailedMatchData.new(match_data_description(actual_label), "#{actual_label} does not end with #{expected.label} (using #ends_with?)",
expected: expected.value.inspect,
actual: actual_value.inspect
)
......@@ -62,9 +62,9 @@ module Spectator::Matchers
last = list.last
if expected.value === last
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual_label))
else
FailedMatchData.new(description, "#{actual_label} does not end with #{expected.label} (using expected === last)",
FailedMatchData.new(match_data_description(actual_label), "#{actual_label} does not end with #{expected.label} (using expected === last)",
expected: expected.value.inspect,
actual: last.inspect,
list: list.inspect
......@@ -76,12 +76,12 @@ module Spectator::Matchers
# This method expects (and uses) the `#ends_with?` method on the value.
private def negated_match_ends_with(actual_value, actual_label)
if actual_value.ends_with?(expected.value)
FailedMatchData.new(description, "#{actual_label} ends with #{expected.label} (using #ends_with?)",
FailedMatchData.new(match_data_description(actual_label), "#{actual_label} ends with #{expected.label} (using #ends_with?)",
expected: "Not #{expected.value.inspect}",
actual: actual_value.inspect
)
else
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual_label))
end
end
......@@ -92,13 +92,13 @@ module Spectator::Matchers
last = list.last
if expected.value === last
FailedMatchData.new(description, "#{actual_label} ends with #{expected.label} (using expected === last)",
FailedMatchData.new(match_data_description(actual_label), "#{actual_label} ends with #{expected.label} (using expected === last)",
expected: "Not #{expected.value.inspect}",
actual: last.inspect,
list: list.inspect
)
else
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual_label))
end
end
end
......
......@@ -33,16 +33,16 @@ module Spectator::Matchers
def match(actual : Expression(T)) : MatchData forall T
exception = capture_exception { actual.value }
if exception.nil?
FailedMatchData.new(description, "#{actual.label} did not raise", expected: ExceptionType.inspect)
FailedMatchData.new(match_data_description(actual), "#{actual.label} did not raise", expected: ExceptionType.inspect)
else
if exception.is_a?(ExceptionType)
if (value = expected.value).nil?
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
if value === exception.message
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
FailedMatchData.new(description, "#{actual.label} raised #{exception.class}, but the message is not #{expected.label}",
FailedMatchData.new(match_data_description(actual), "#{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(description, "#{actual.label} did not raise #{ExceptionType}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} did not raise #{ExceptionType}",
expected: ExceptionType.inspect,
actual: exception.class.inspect
)
......@@ -64,28 +64,28 @@ module Spectator::Matchers
def negated_match(actual : Expression(T)) : MatchData forall T
exception = capture_exception { actual.value }
if exception.nil?
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
if exception.is_a?(ExceptionType)
if (value = expected.value).nil?
FailedMatchData.new(description, "#{actual.label} raised #{exception.class}",
FailedMatchData.new(match_data_description(actual), "#{actual.label} raised #{exception.class}",
expected: "Not #{ExceptionType}",
actual: exception.class.inspect
)
else
if value === exception.message
FailedMatchData.new(description, "#{actual.label} raised #{exception.class} with message matching #{expected.label}",
FailedMatchData.new(match_data_description(actual), "#{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(description)
SuccessfulMatchData.new(match_data_description(actual))
end
end
else
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
end
end
end
......
......@@ -39,10 +39,10 @@ module Spectator::Matchers
if missing.empty?
# Contents are present.
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual_label))
else
# Content is missing.
FailedMatchData.new(description, "#{actual_label} does not have #{expected.label}",
FailedMatchData.new(match_data_description(actual_label), "#{actual_label} does not have #{expected.label}",
expected: expected.value.inspect,
actual: actual_value.inspect,
missing: missing.inspect,
......@@ -58,9 +58,9 @@ module Spectator::Matchers
end
if missing.empty?
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual_label))
else
FailedMatchData.new(description, "#{actual_label} does not have #{expected.label}",
FailedMatchData.new(match_data_description(actual_label), "#{actual_label} does not have #{expected.label}",
expected: expected.value.inspect,
actual: actual_value.inspect,
missing: missing.inspect,
......@@ -89,13 +89,13 @@ module Spectator::Matchers
if satisfied
# Contents are present.
FailedMatchData.new(description, "#{actual_label} has #{expected.label}",
FailedMatchData.new(match_data_description(actual_label), "#{actual_label} has #{expected.label}",
expected: "Not #{expected.value.inspect}",
actual: actual_value.inspect
)
else
# Content is missing.
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual_label))
end
end
......@@ -107,9 +107,9 @@ module Spectator::Matchers
end
if satisfied
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual_label))
else
FailedMatchData.new(description, "#{actual_label} does not have #{expected.label}",
FailedMatchData.new(match_data_description(actual_label), "#{actual_label} does not have #{expected.label}",
expected: expected.value.inspect,
actual: actual_value.inspect,
missing: missing.inspect,
......
......@@ -21,5 +21,17 @@ module Spectator::Matchers
# Performs the test against the expression (value or block), but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa.
abstract def negated_match(actual : Expression(T)) : MatchData forall T
private def match_data_description(actual : Expression(T)) : String forall T
match_data_description(actual.label)
end
private def match_data_description(actual_label : String | Symbol) : String
"#{actual_label} #{description}"
end
private def match_data_description(actual_label : Nil) : String
description
end
end
end
......@@ -24,9 +24,9 @@ module Spectator::Matchers
def match(actual : Expression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value)
if match?(snapshot)
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
FailedMatchData.new(description, "#{actual.label} is not #{expected.label}", values(snapshot).to_a)
FailedMatchData.new(match_data_description(actual), "#{actual.label} is not #{expected.label}", values(snapshot).to_a)
end
end
......@@ -35,9 +35,9 @@ module Spectator::Matchers
def negated_match(actual : Expression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value)
if match?(snapshot)
FailedMatchData.new(description, "#{actual.label} is #{expected.label}", values(snapshot).to_a)
FailedMatchData.new(match_data_description(actual), "#{actual.label} is #{expected.label}", values(snapshot).to_a)
else
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
end
end
......
......@@ -17,9 +17,9 @@ module Spectator::Matchers
def match(actual : Expression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value)
if snapshot.values.all?
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
FailedMatchData.new(description, "#{actual.label} does not respond to #{label}", values(snapshot).to_a)
FailedMatchData.new(match_data_description(actual), "#{actual.label} does not respond to #{label}", values(snapshot).to_a)
end
end
......@@ -29,9 +29,9 @@ module Spectator::Matchers
snapshot = snapshot_values(actual.value)
# Intentionally check truthiness of each value.
if snapshot.values.any? # ameba:disable Performance/AnyInsteadOfEmpty
FailedMatchData.new(description, "#{actual.label} responds to #{label}", values(snapshot).to_a)
FailedMatchData.new(match_data_description(actual), "#{actual.label} responds to #{label}", values(snapshot).to_a)
else
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
end
end
......
......@@ -25,9 +25,9 @@ module Spectator::Matchers
# Additionally, `#failure_message` and `#values` are called for a failed match.
def match(actual : Expression(T)) : MatchData forall T
if match?(actual)
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
FailedMatchData.new(description, failure_message(actual), values(actual).to_a)
FailedMatchData.new(match_data_description(actual), failure_message(actual), values(actual).to_a)
end
end
......@@ -41,9 +41,9 @@ module Spectator::Matchers
def negated_match(actual : Expression(T)) : MatchData forall T
# TODO: Invert description.
if does_not_match?(actual)
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
FailedMatchData.new(description, failure_message_when_negated(actual), negated_values(actual).to_a)
FailedMatchData.new(match_data_description(actual), failure_message_when_negated(actual), negated_values(actual).to_a)
end
end
......
......@@ -45,9 +45,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(description)
SuccessfulMatchData.new(match_data_description(actual_label))
else
FailedMatchData.new(description, "#{actual_label} does not start with #{expected.label} (using #starts_with?)",
FailedMatchData.new(match_data_description(actual_label), "#{actual_label} does not start with #{expected.label} (using #starts_with?)",
expected: expected.value.inspect,
actual: actual_value.inspect
)
......@@ -61,9 +61,9 @@ module Spectator::Matchers
first = list.first
if expected.value === first
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual_label))
else
FailedMatchData.new(description, "#{actual_label} does not start with #{expected.label} (using expected === first)",
FailedMatchData.new(match_data_description(actual_label), "#{actual_label} does not start with #{expected.label} (using expected === first)",
expected: expected.value.inspect,
actual: first.inspect,
list: list.inspect
......@@ -75,12 +75,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(description, "#{actual_label} starts with #{expected.label} (using #starts_with?)",
FailedMatchData.new(match_data_description(actual_label), "#{actual_label} starts with #{expected.label} (using #starts_with?)",
expected: "Not #{expected.value.inspect}",
actual: actual_value.inspect
)
else
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual_label))
end
end
......@@ -91,13 +91,13 @@ module Spectator::Matchers
first = list.first
if expected.value === first
FailedMatchData.new(description, "#{actual_label} starts with #{expected.label} (using expected === first)",
FailedMatchData.new(match_data_description(actual_label), "#{actual_label} starts with #{expected.label} (using expected === first)",
expected: "Not #{expected.value.inspect}",
actual: first.inspect,
list: list.inspect
)
else
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual_label))
end
end
end
......
</
......@@ -28,9 +28,9 @@ module Spectator::Matchers
missing, extra = array_diff(expected_elements, actual_elements)
if missing.empty? && extra.empty?
SuccessfulMatchData.new(description)
SuccessfulMatchData.new(match_data_description(actual))
else
FailedMatchData.new(description, "#{actual.label} does not contain #{expected.label} (unordered)",