Skip to content

Commit 80564fc

Browse files
committed
Do not quote doctest excerpt
1 parent 94e0e38 commit 80564fc

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

lib/ex_unit/lib/ex_unit/doc_test.ex

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ defmodule ExUnit.DocTest do
313313
end
314314

315315
tests =
316-
Enum.map(exprs, fn {expr, expected, formatted} ->
317-
test_case_content(expr, expected, location, stack, formatted)
316+
Enum.map(exprs, fn {expr, expected, doctest} ->
317+
test_case_content(expr, expected, location, stack, doctest)
318318
end)
319319

320320
{:__block__, [], test_import(module, do_import) ++ tests}
@@ -327,69 +327,65 @@ defmodule ExUnit.DocTest do
327327
end) > 1
328328
end
329329

330-
defp test_case_content(expr, :test, location, stack, formatted) do
331-
string_to_quoted(location, stack, expr, "\n" <> formatted) |> insert_assertions()
330+
defp test_case_content(expr, :test, location, stack, doctest) do
331+
string_to_quoted(location, stack, expr, doctest) |> insert_assertions()
332332
end
333333

334-
defp test_case_content(expr, {:test, expected}, location, stack, formatted) do
335-
doctest = "\n" <> formatted <> "\n" <> expected
334+
defp test_case_content(expr, {:test, expected}, location, stack, doctest) do
336335
expr_ast = string_to_quoted(location, stack, expr, doctest) |> insert_assertions()
337336
expected_ast = string_to_quoted(location, stack, expected, doctest)
338337
last_expr = Macro.to_string(last_expr(expr_ast))
339338

340339
quote do
341340
value = unquote(expr_ast)
342341
expected = unquote(expected_ast)
343-
formatted = unquote(formatted)
342+
doctest = unquote(doctest)
344343
last_expr = unquote(last_expr)
345344
expected_expr = unquote(expected)
346345
stack = unquote(stack)
347346

348-
ExUnit.DocTest.__test__(value, expected, formatted, last_expr, expected_expr, stack)
347+
ExUnit.DocTest.__test__(value, expected, doctest, last_expr, expected_expr, stack)
349348
end
350349
end
351350

352-
defp test_case_content(expr, {:inspect, expected}, location, stack, formatted) do
353-
doctest = "\n" <> formatted <> "\n" <> expected
351+
defp test_case_content(expr, {:inspect, expected}, location, stack, doctest) do
354352
expr_ast = string_to_quoted(location, stack, expr, doctest) |> insert_assertions()
355-
expected_ast = string_to_quoted(location, stack, expected, doctest)
356353
last_expr = Macro.to_string(last_expr(expr_ast))
357354

358355
quote do
359356
value = unquote(expr_ast)
360-
expected = unquote(expected_ast)
361-
formatted = unquote(formatted)
357+
expected = unquote(expected)
358+
doctest = unquote(doctest)
362359
last_expr = unquote(last_expr)
363-
expected_expr = unquote(expected)
360+
expected_expr = unquote(inspect(expected))
364361
stack = unquote(stack)
365362

366-
ExUnit.DocTest.__inspect__(value, expected, formatted, last_expr, expected_expr, stack)
363+
ExUnit.DocTest.__inspect__(value, expected, doctest, last_expr, expected_expr, stack)
367364
end
368365
end
369366

370-
defp test_case_content(expr, {:error, exception, message}, location, stack, formatted) do
371-
doctest = "\n" <> formatted <> "\n** (#{inspect(exception)}) #{inspect(message)}"
367+
defp test_case_content(expr, {:error, exception, message}, location, stack, doctest) do
372368
expr_ast = string_to_quoted(location, stack, expr, doctest)
373369

374370
quote do
375371
stack = unquote(stack)
376372
message = unquote(message)
377-
formatted = unquote(formatted)
373+
doctest = unquote(doctest)
378374
exception = unquote(exception)
379-
ExUnit.DocTest.__error__(fn -> unquote(expr_ast) end, message, exception, formatted, stack)
375+
ExUnit.DocTest.__error__(fn -> unquote(expr_ast) end, message, exception, doctest, stack)
380376
end
381377
end
382378

383379
@doc false
384-
def __test__(value, expected, formatted, last_expr, expected_expr, stack) do
380+
def __test__(value, expected, doctest, last_expr, expected_expr, stack) do
385381
case value do
386382
^expected ->
387383
:ok
388384

389385
_ ->
390386
error = [
391387
message: "Doctest failed",
392-
doctest: "\n" <> formatted <> "\n" <> expected_expr,
388+
doctest: doctest,
393389
expr: "#{last_expr} === #{String.trim(expected_expr)}",
394390
left: value,
395391
right: expected
@@ -400,7 +396,7 @@ defmodule ExUnit.DocTest do
400396
end
401397

402398
@doc false
403-
def __inspect__(value, expected, formatted, last_expr, expected_expr, parent_stack) do
399+
def __inspect__(value, expected, doctest, last_expr, expected_expr, parent_stack) do
404400
result =
405401
try do
406402
inspect(value, safe: false)
@@ -418,15 +414,14 @@ defmodule ExUnit.DocTest do
418414
:ok
419415

420416
{extra, stack} ->
421-
doctest = "\n" <> formatted <> "\n" <> expected_expr
422417
expr = "inspect(#{last_expr}) === #{String.trim(expected_expr)}"
423418
error = [doctest: doctest, expr: expr] ++ extra
424419
reraise ExUnit.AssertionError, error, stack ++ parent_stack
425420
end
426421
end
427422

428423
@doc false
429-
def __error__(fun, message, exception, formatted, stack) do
424+
def __error__(fun, message, exception, doctest, stack) do
430425
try do
431426
fun.()
432427
rescue
@@ -451,12 +446,10 @@ defmodule ExUnit.DocTest do
451446
end
452447

453448
if failed do
454-
doctest = "\n" <> formatted <> "\n** (#{inspect(exception)}) #{inspect(message)}"
455449
reraise ExUnit.AssertionError, [message: failed, doctest: doctest], stack
456450
end
457451
else
458452
_ ->
459-
doctest = "\n" <> formatted <> "\n** (#{inspect(exception)}) #{inspect(message)}"
460453
failed = "Doctest failed: expected exception #{inspect(exception)} but nothing was raised"
461454
error = [message: failed, doctest: doctest]
462455
reraise ExUnit.AssertionError, error, stack
@@ -862,7 +855,8 @@ defmodule ExUnit.DocTest do
862855
end
863856

864857
defp add_expr(%{exprs: exprs} = test, expr, expected, formatted) do
865-
%{test | exprs: [{expr, tag_expected(expected), formatted} | exprs]}
858+
doctest = "\n" <> formatted <> "\n" <> expected
859+
%{test | exprs: [{expr, tag_expected(expected), doctest} | exprs]}
866860
end
867861

868862
defp tag_expected(string) do
@@ -876,7 +870,7 @@ defmodule ExUnit.DocTest do
876870

877871
_ ->
878872
if inspectable?(string) do
879-
{:inspect, inspect(string)}
873+
{:inspect, string}
880874
else
881875
{:test, string}
882876
end

lib/ex_unit/test/ex_unit/doc_test_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ defmodule ExUnit.DocTestTest do
577577
Doctest failed
578578
doctest:
579579
iex> :oops
580-
"#Inspect<[]>"
580+
#Inspect<[]>
581581
code: inspect(:oops) === "#Inspect<[]>"
582582
left: ":oops"
583583
right: "#Inspect<[]>"
@@ -600,7 +600,7 @@ defmodule ExUnit.DocTestTest do
600600
Doctest failed: expected exception WhatIsThis but got RuntimeError with message "oops"
601601
doctest:
602602
iex> raise "oops"
603-
** (WhatIsThis) "oops"
603+
** (WhatIsThis) oops
604604
stacktrace:
605605
test/ex_unit/doc_test_test.exs:#{starting_line + 18}: ExUnit.DocTestTest.Invalid (module)
606606
"""
@@ -615,7 +615,7 @@ defmodule ExUnit.DocTestTest do
615615
"oops"
616616
doctest:
617617
iex> raise "oops"
618-
** (RuntimeError) "hello"
618+
** (RuntimeError) hello
619619
stacktrace:
620620
test/ex_unit/doc_test_test.exs:#{starting_line + 21}: ExUnit.DocTestTest.Invalid (module)
621621
"""

0 commit comments

Comments
 (0)