Skip to content

Commit 875b4c7

Browse files
committed
Improve doctest exception tracking and reporting
1 parent bef41d7 commit 875b4c7

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

lib/ex_unit/lib/ex_unit/doc_test.ex

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,41 @@ defmodule ExUnit.DocTest do
213213

214214
quoted =
215215
quote do
216-
assert_raise unquote(exception), unquote(message), fn ->
217-
unquote(expr_ast)
216+
try do
217+
v = unquote(expr_ast)
218+
location = [line: unquote(line), file: Path.relative_to(unquote(file), System.cwd!)]
219+
stack = [{ unquote(module), :__MODULE__, 0, location }]
220+
raise ExUnit.ExpectationError,
221+
[ prelude: "Expected doctest",
222+
expected: unquote(test.expr),
223+
actual: "by raising #{inspect unquote(exception)} exception",
224+
reason: "terminate",
225+
instead: v ],
226+
stack
227+
rescue
228+
e in [ExUnit.ExpectationError] -> raise(e)
229+
error in [unquote(exception)] ->
230+
unless error.message == unquote(message) do
231+
location = [line: unquote(line), file: Path.relative_to(unquote(file), System.cwd!)]
232+
stack = [{ unquote(module), :__MODULE__, 0, location }]
233+
raise ExUnit.ExpectationError,
234+
[ prelude: "Expected doctest",
235+
expected: unquote(test.expr),
236+
actual: "by raising #{inspect unquote(exception)} exception with a #{inspect unquote(message)} message",
237+
reason: "terminate",
238+
instead: error ],
239+
stack
240+
end
241+
error ->
242+
location = [line: unquote(line), file: Path.relative_to(unquote(file), System.cwd!)]
243+
stack = [{ unquote(module), :__MODULE__, 0, location }]
244+
raise ExUnit.ExpectationError,
245+
[ prelude: "Expected doctest",
246+
expected: unquote(test.expr),
247+
actual: "by raising #{inspect unquote(exception)}",
248+
reason: "terminate",
249+
instead: error ],
250+
stack
218251
end
219252
end
220253
if do_import do

0 commit comments

Comments
 (0)