Fix most problems (hopefully) with ExceptionMatcher

This commit is contained in:
Michael Miller 2019-08-09 14:18:59 -06:00
parent 2251168631
commit c47b47ade9

View file

@ -25,50 +25,61 @@ module Spectator::Matchers
def match(actual) def match(actual)
exception = capture_exception { actual.value } exception = capture_exception { actual.value }
case exception if exception.nil?
when Nil FailedMatchData.new("#{actual.label} did not raise", expected: ExceptionType.inspect)
FailedMatchData.new("#{actual.label} did not raise", expected: ExpectedType.inspect) else
when ExceptionType if exception.is_a?(ExceptionType)
if expected.value.nil? || expected.value === exception.message if (value = expected.value).nil?
SuccessfulMatchData.new SuccessfulMatchData.new
else else
FailedMatchData.new("#{actual.label} raised #{ExpectedType}, but the message is not #{expected.label}", if value === exception.message
SuccessfulMatchData.new
else
FailedMatchData.new("#{actual.label} raised #{exception.class}, but the message is not #{expected.label}",
"expected type": ExceptionType.inspect, "expected type": ExceptionType.inspect,
"actual type": exception.class.inspect, "actual type": exception.class.inspect,
"expected message": expected.value.inspect, "expected message": value.inspect,
"actual message": exception.message "actual message": exception.message.to_s
) )
end end
end
else else
FailedMatchData.new("#{actual.label} did not raise #{ExpectedType}", FailedMatchData.new("#{actual.label} did not raise #{ExceptionType}",
expected: ExpectedType.inspect, expected: ExceptionType.inspect,
actual: exception.class.inspect actual: exception.class.inspect
) )
end end
end end
end
def negated_match(actual) def negated_match(actual)
exception = capture_exception { actual.value } exception = capture_exception { actual.value }
case exception if exception.nil?
when Nil
SuccessfulMatchData.new SuccessfulMatchData.new
when ExceptionType else
if expected.value.nil? if exception.is_a?(ExceptionType)
FailedMatchData.new("#{actual.label} raised #{ExpectedType}") if (value = expected.value).nil?
elsif expected.value === exception.message FailedMatchData.new("#{actual.label} raised #{exception.class}",
FailedMatchData.new("#{actual.label} raised #{ExpectedType} with message matching #{expected.label}", expected: "Not #{ExceptionType}",
actual: exception.class.inspect
)
else
if value === exception.message
FailedMatchData.new("#{actual.label} raised #{exception.class} with message matching #{expected.label}",
"expected type": ExceptionType.inspect, "expected type": ExceptionType.inspect,
"actual type": exception.class.inspect, "actual type": exception.class.inspect,
"expected message": expected.value.inspect, "expected message": value.inspect,
"actual message": exception.message "actual message": exception.message.to_s
) )
else else
SuccessfulMatchData.new SuccessfulMatchData.new
end end
end
else else
SuccessfulMatchData.new SuccessfulMatchData.new
end end
end end
end
# Runs a block of code and returns the exception it threw. # Runs a block of code and returns the exception it threw.
# If no exception was thrown, *nil* is returned. # If no exception was thrown, *nil* is returned.