Improve doctest failure messages for exception assertions (#4889)

This commit is contained in:
Aleksei Magusev
2016-06-26 12:03:59 +02:00
committed by GitHub
parent 7a2e04b800
commit e1674518fb
4 changed files with 39 additions and 23 deletions
+9 -7
View File
@@ -476,17 +476,19 @@ defmodule ExUnit.Assertions do
def assert_raise(exception, message, function) when is_function(function) do
error = assert_raise(exception, function)
is_match = cond do
match? = cond do
is_binary(message) -> Exception.message(error) == message
Regex.regex?(message) -> Exception.message(error) =~ message
end
msg = "Wrong message for #{inspect exception}\n" <>
"Expected:\n" <>
" #{inspect message}\n" <>
"Got:\n" <>
" #{inspect Exception.message(error)}"
assert is_match, message: msg
message =
"Wrong message for #{inspect exception}\n" <>
"expected:\n" <>
" #{inspect message}\n" <>
"actual:\n" <>
" #{inspect Exception.message(error)}"
assert match?, message: message
error
end
+20 -10
View File
@@ -330,25 +330,35 @@ defmodule ExUnit.DocTest do
quote do
stack = unquote(stack)
expr = unquote(String.trim(expr))
spec = inspect(unquote(exception)) <> " with message " <> inspect(unquote(message))
expr = unquote(String.trim(expr))
try do
unquote(expr_ast)
rescue
error ->
unless error.__struct__ == unquote(exception) and
Exception.message(error) == unquote(message) do
got = inspect(error.__struct__) <> " with message " <> inspect(Exception.message(error))
reraise ExUnit.AssertionError,
[message: "Doctest failed: expected exception #{spec} but got #{got}",
expr: expr],
stack
actual_exception = error.__struct__
actual_message = Exception.message(error)
message =
cond do
actual_exception != unquote(exception) ->
"Doctest failed: expected exception #{inspect(unquote(exception))} but got #{inspect(actual_exception)} with message #{inspect(actual_message)}"
actual_message != unquote(message) ->
"Doctest failed: wrong message for #{inspect(actual_exception)}\n" <>
"expected:\n" <>
" #{inspect(unquote(message))}\n" <>
"actual:\n" <>
" #{inspect(actual_message)}"
true ->
nil
end
if message do
reraise ExUnit.AssertionError, [message: message, expr: expr], stack
end
else
_ ->
reraise ExUnit.AssertionError,
[message: "Doctest failed: expected exception #{spec} but nothing was raised",
[message: "Doctest failed: expected exception #{inspect(unquote(exception))} but nothing was raised",
expr: expr],
stack
end
+4 -4
View File
@@ -453,9 +453,9 @@ defmodule ExUnit.AssertionsTest do
rescue
error in [ExUnit.AssertionError] ->
"Wrong message for RuntimeError" <>
"\nExpected:" <>
"\nexpected:" <>
"\n \"foo\"" <>
"\nGot:" <>
"\nactual:" <>
"\n \"bar\"" = error.message
end
@@ -466,9 +466,9 @@ defmodule ExUnit.AssertionsTest do
rescue
error in [ExUnit.AssertionError] ->
"Wrong message for RuntimeError" <>
"\nExpected:" <>
"\nexpected:" <>
"\n ~r/ba[zk]/" <>
"\nGot:" <>
"\nactual:" <>
"\n \"bar\"" = error.message
end
+6 -2
View File
@@ -298,7 +298,7 @@ defmodule ExUnit.DocTestTest do
assert output =~ """
5) test moduledoc at ExUnit.DocTestTest.Invalid (5) (ExUnit.DocTestTest.ActuallyCompiled)
test/ex_unit/doc_test_test.exs:248
Doctest failed: expected exception WhatIsThis with message "oops" but got RuntimeError with message "oops"
Doctest failed: expected exception WhatIsThis but got RuntimeError with message "oops"
code: raise "oops"
stacktrace:
test/ex_unit/doc_test_test.exs:139: ExUnit.DocTestTest.Invalid (module)
@@ -307,7 +307,11 @@ defmodule ExUnit.DocTestTest do
assert output =~ """
6) test moduledoc at ExUnit.DocTestTest.Invalid (6) (ExUnit.DocTestTest.ActuallyCompiled)
test/ex_unit/doc_test_test.exs:248
Doctest failed: expected exception RuntimeError with message "hello" but got RuntimeError with message "oops"
Doctest failed: wrong message for RuntimeError
expected:
"hello"
actual:
"oops"
code: raise "oops"
stacktrace:
test/ex_unit/doc_test_test.exs:142: ExUnit.DocTestTest.Invalid (module)