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,48 +25,59 @@ 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
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,
"actual type": exception.class.inspect,
"expected message": value.inspect,
"actual message": exception.message.to_s
)
end
end
else else
FailedMatchData.new("#{actual.label} raised #{ExpectedType}, but the message is not #{expected.label}", FailedMatchData.new("#{actual.label} did not raise #{ExceptionType}",
"expected type": ExceptionType.inspect, expected: ExceptionType.inspect,
"actual type": exception.class.inspect, actual: exception.class.inspect
"expected message": expected.value.inspect,
"actual message": exception.message
) )
end end
else
FailedMatchData.new("#{actual.label} did not raise #{ExpectedType}",
expected: ExpectedType.inspect,
actual: exception.class.inspect
)
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}",
"expected type": ExceptionType.inspect, actual: exception.class.inspect
"actual type": exception.class.inspect, )
"expected message": expected.value.inspect, else
"actual message": exception.message if value === exception.message
) FailedMatchData.new("#{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
end
end
else else
SuccessfulMatchData.new SuccessfulMatchData.new
end end
else
SuccessfulMatchData.new
end end
end end