Skip to content

Commit ff73830

Browse files
committed
Return values from running doctests and order tests
1 parent 950bcc0 commit ff73830

File tree

2 files changed

+48
-55
lines changed

2 files changed

+48
-55
lines changed

lib/ex_unit/lib/ex_unit/doc_test.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ defmodule ExUnit.DocTest do
238238

239239
extract(module)
240240
|> filter_by_opts(module, opts)
241-
|> Stream.with_index(1)
242-
|> Enum.map(fn {test, acc} ->
243-
compile_test(test, module, import, acc, file, tags)
241+
|> Enum.sort_by(& &1.line)
242+
|> Enum.with_index(fn test, index ->
243+
compile_test(test, module, import, index + 1, file, tags)
244244
end)
245245
end
246246

@@ -380,7 +380,7 @@ defmodule ExUnit.DocTest do
380380
def __test__(value, expected, doctest, last_expr, expected_expr, stack) do
381381
case value do
382382
^expected ->
383-
:ok
383+
{:ok, value}
384384

385385
_ ->
386386
error = [
@@ -411,7 +411,7 @@ defmodule ExUnit.DocTest do
411411

412412
case result do
413413
:ok ->
414-
:ok
414+
{:ok, value}
415415

416416
{extra, stack} ->
417417
expr = "inspect(#{last_expr}) === #{String.trim(expected_expr)}"

lib/ex_unit/test/ex_unit/doc_test_test.exs

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -635,18 +635,17 @@ defmodule ExUnit.DocTestTest do
635635
"""
636636

637637
assert output =~ """
638-
9) doctest ExUnit.DocTestTest.Invalid.dedented_past_fence/0 (9) (ExUnit.DocTestTest.ActuallyCompiled)
638+
9) doctest ExUnit.DocTestTest.Invalid.b/0 (9) (ExUnit.DocTestTest.ActuallyCompiled)
639639
test/ex_unit/doc_test_test.exs:#{doctest_line}
640-
Doctest did not compile, got: (SyntaxError) test/ex_unit/doc_test_test.exs:#{starting_line + 57}:5: unexpected token: "`" (column 5, code point U+0060)
641-
#{line_placeholder(starting_line + 57)} |
642-
#{starting_line + 57} | ```
643-
#{line_placeholder(starting_line + 57)} | ^
640+
Doctest did not compile, got: (SyntaxError) test/ex_unit/doc_test_test.exs:#{starting_line + 33}:6: syntax error before: '*'
641+
#{line_placeholder(starting_line + 33)} |
642+
#{starting_line + 33} | 1 + * 1
643+
#{line_placeholder(starting_line + 33)} | ^
644644
doctest:
645-
iex> 1 + 2
646-
3
647-
```
645+
iex> 1 + * 1
646+
1
648647
stacktrace:
649-
test/ex_unit/doc_test_test.exs:#{starting_line + 56}: ExUnit.DocTestTest.Invalid (module)
648+
test/ex_unit/doc_test_test.exs:#{starting_line + 33}: ExUnit.DocTestTest.Invalid (module)
650649
"""
651650

652651
assert output =~ """
@@ -680,15 +679,30 @@ defmodule ExUnit.DocTestTest do
680679
"""
681680

682681
assert output =~ """
683-
12) doctest ExUnit.DocTestTest.Invalid.invalid_utf8/0 (12) (ExUnit.DocTestTest.ActuallyCompiled)
682+
12) doctest ExUnit.DocTestTest.Invalid.dedented_past_fence/0 (12) (ExUnit.DocTestTest.ActuallyCompiled)
683+
test/ex_unit/doc_test_test.exs:#{doctest_line}
684+
Doctest did not compile, got: (SyntaxError) test/ex_unit/doc_test_test.exs:#{starting_line + 57}:5: unexpected token: "`" (column 5, code point U+0060)
685+
#{line_placeholder(starting_line + 57)} |
686+
#{starting_line + 57} | ```
687+
#{line_placeholder(starting_line + 57)} | ^
688+
doctest:
689+
iex> 1 + 2
690+
3
691+
```
692+
stacktrace:
693+
test/ex_unit/doc_test_test.exs:#{starting_line + 56}: ExUnit.DocTestTest.Invalid (module)
694+
"""
695+
696+
assert output =~ """
697+
13) doctest ExUnit.DocTestTest.Invalid.invalid_utf8/0 (13) (ExUnit.DocTestTest.ActuallyCompiled)
684698
test/ex_unit/doc_test_test.exs:#{doctest_line}
685699
Doctest did not compile, got: (UnicodeConversionError) invalid encoding starting at <<255, 34, 41>>
686700
stacktrace:
687701
test/ex_unit/doc_test_test.exs:#{starting_line + 63}: ExUnit.DocTestTest.Invalid (module)
688702
"""
689703

690704
assert output =~ """
691-
13) doctest ExUnit.DocTestTest.Invalid.misplaced_opaque_type/0 (13) (ExUnit.DocTestTest.ActuallyCompiled)
705+
14) doctest ExUnit.DocTestTest.Invalid.misplaced_opaque_type/0 (14) (ExUnit.DocTestTest.ActuallyCompiled)
692706
test/ex_unit/doc_test_test.exs:#{doctest_line}
693707
Doctest did not compile, got: (TokenMissingError) test/ex_unit/doc_test_test.exs:#{starting_line + 69}:7: missing terminator: } (for "{" starting at line #{starting_line + 69})
694708
#{line_placeholder(starting_line + 69)} |
@@ -702,28 +716,8 @@ defmodule ExUnit.DocTestTest do
702716
test/ex_unit/doc_test_test.exs:#{starting_line + 69}: ExUnit.DocTestTest.Invalid (module)
703717
"""
704718

705-
assert output =~ "14) doctest ExUnit.DocTestTest.Invalid.raising_inspect/0"
706-
assert output =~ "iex> ExUnit.DocTestTest.Haiku.new(:this, :is, {:not, :a, :haiku})"
707-
708-
assert output =~
709-
"test/ex_unit/doc_test_test.exs:#{starting_line + 82}: ExUnit.DocTestTest.Invalid (module)"
710-
711719
assert output =~ """
712-
15) doctest ExUnit.DocTestTest.Invalid.b/0 (15) (ExUnit.DocTestTest.ActuallyCompiled)
713-
test/ex_unit/doc_test_test.exs:#{doctest_line}
714-
Doctest did not compile, got: (SyntaxError) test/ex_unit/doc_test_test.exs:#{starting_line + 33}:6: syntax error before: '*'
715-
#{line_placeholder(starting_line + 33)} |
716-
#{starting_line + 33} | 1 + * 1
717-
#{line_placeholder(starting_line + 33)} | ^
718-
doctest:
719-
iex> 1 + * 1
720-
1
721-
stacktrace:
722-
test/ex_unit/doc_test_test.exs:#{starting_line + 33}: ExUnit.DocTestTest.Invalid (module)
723-
"""
724-
725-
assert output =~ """
726-
16) doctest ExUnit.DocTestTest.Invalid.t/0 (16) (ExUnit.DocTestTest.ActuallyCompiled)
720+
15) doctest ExUnit.DocTestTest.Invalid.t/0 (15) (ExUnit.DocTestTest.ActuallyCompiled)
727721
test/ex_unit/doc_test_test.exs:#{doctest_line}
728722
Doctest did not compile, got: (SyntaxError) test/ex_unit/doc_test_test.exs:#{starting_line + 75}:6: syntax error before: '*'
729723
#{line_placeholder(starting_line + 75)} |
@@ -735,6 +729,12 @@ defmodule ExUnit.DocTestTest do
735729
stacktrace:
736730
test/ex_unit/doc_test_test.exs:#{starting_line + 75}: ExUnit.DocTestTest.Invalid (module)
737731
"""
732+
733+
assert output =~ "16) doctest ExUnit.DocTestTest.Invalid.raising_inspect/0"
734+
assert output =~ "iex> ExUnit.DocTestTest.Haiku.new(:this, :is, {:not, :a, :haiku})"
735+
736+
assert output =~
737+
"test/ex_unit/doc_test_test.exs:#{starting_line + 82}: ExUnit.DocTestTest.Invalid (module)"
738738
end
739739

740740
test "pattern matching assertions in doctests" do
@@ -900,24 +900,7 @@ defmodule ExUnit.DocTestTest do
900900
end
901901
end
902902

903-
test "fails when testing single function not found" do
904-
message = """
905-
test/ex_unit/doc_test_test\.exs: undefined or private function given to doctest:
906-
907-
ExUnit.DocTestTest.SomewhatGoodModuleWithOnly.three/0
908-
909-
"""
910-
911-
assert_raise ExUnit.DocTest.Error, message, fn ->
912-
defmodule NeverCompiled do
913-
import ExUnit.DocTest
914-
915-
doctest ExUnit.DocTestTest.SomewhatGoodModuleWithOnly, only: [three: 0], import: true
916-
end
917-
end
918-
end
919-
920-
test "fails when testing multiple functions not found" do
903+
test "fails when testing functions are not found" do
921904
message = """
922905
test/ex_unit/doc_test_test\.exs: undefined or private functions given to doctest:
923906
@@ -989,6 +972,16 @@ defmodule ExUnit.DocTestTest do
989972
end
990973
end
991974

975+
test "doctest direct invocation" do
976+
defmodule Direct do
977+
use ExUnit.Case, register: false
978+
doctest ExUnit.DocTestTest.GoodModule, import: true
979+
end
980+
981+
[head | _] = Direct.__ex_unit__().tests
982+
assert apply(Direct, head.name, [%{}]) == {:ok, 2}
983+
end
984+
992985
defp line_placeholder(line_number) do
993986
digits =
994987
line_number

0 commit comments

Comments
 (0)