Skip to content

Commit 30d7114

Browse files
author
José Valim
committed
Convert doctest Test to a struct
1 parent 1e5633c commit 30d7114

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

lib/ex_unit/lib/ex_unit/doc_test.ex

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ defmodule ExUnit.DocTest do
114114

115115
defexception Error, message: nil
116116

117-
defrecord Test, fun_arity: nil, line: nil, exprs: []
118-
119117
@doc """
120118
This macro is used to generate ExUnit test cases for doctests.
121119
@@ -189,15 +187,15 @@ defmodule ExUnit.DocTest do
189187
{test_name(test, module, n), test_content(test, module, do_import)}
190188
end
191189

192-
defp test_name(Test[fun_arity: nil], m, n) do
190+
defp test_name(%{fun_arity: nil}, m, n) do
193191
"moduledoc at #{inspect m} (#{n})"
194192
end
195193

196-
defp test_name(Test[fun_arity: {f, a}], m, n) do
194+
defp test_name(%{fun_arity: {f, a}}, m, n) do
197195
"doc at #{inspect m}.#{f}/#{a} (#{n})"
198196
end
199197

200-
defp test_content(Test[exprs: exprs, line: line, fun_arity: fun_arity], module, do_import) do
198+
defp test_content(%{exprs: exprs, line: line, fun_arity: fun_arity}, module, do_import) do
201199
file = module.__info__(:compile)[:source] |> String.from_char_data!
202200
location = [line: line, file: Path.relative_to_cwd(file)]
203201
stack = Macro.escape [{module, :__MODULE__, 0, location}]
@@ -357,7 +355,7 @@ defmodule ExUnit.DocTest do
357355

358356
defp extract_from_doc({fa, line, _, _, doc}) do
359357
for test <- extract_tests(line, doc) do
360-
test.fun_arity(fa)
358+
%{test | fun_arity: fa}
361359
end
362360
end
363361

@@ -441,21 +439,21 @@ defmodule ExUnit.DocTest do
441439
end
442440

443441
# End of input and we've still got a test pending.
444-
defp extract_tests([], _, expr_acc, expected_acc, [test=Test[exprs: exprs]|t], _) do
445-
test = test.exprs([{expr_acc, {:test, expected_acc}} | exprs])
442+
defp extract_tests([], _, expr_acc, expected_acc, [test=%{exprs: exprs}|t], _) do
443+
test = %{test | exprs: [{expr_acc, {:test, expected_acc}} | exprs]}
446444
Enum.reverse(reverse_last_test([test|t]))
447445
end
448446

449447
# We've encountered the next test on an adjacent line. Put them into one group.
450-
defp extract_tests([<< "iex>", _ :: binary>>|_] = list, line, expr_acc, expected_acc, [test=Test[exprs: exprs]|t], newtest) when expr_acc != "" and expected_acc != "" do
451-
test = test.exprs([{expr_acc, {:test, expected_acc}} | exprs])
448+
defp extract_tests([<< "iex>", _ :: binary>>|_] = list, line, expr_acc, expected_acc, [test=%{exprs: exprs}|t], newtest) when expr_acc != "" and expected_acc != "" do
449+
test = %{test | exprs: [{expr_acc, {:test, expected_acc}} | exprs]}
452450
extract_tests(list, line, "", "", [test|t], newtest)
453451
end
454452

455453
# Store expr_acc and start a new test case.
456454
defp extract_tests([<< "iex>", string :: binary>>|lines], line, "", expected_acc, acc, true) do
457455
acc = reverse_last_test(acc)
458-
test = Test[line: line]
456+
test = %{line: line, fun_arity: nil, exprs: []}
459457
extract_tests(lines, line, string, expected_acc, [test|acc], false)
460458
end
461459

@@ -490,22 +488,22 @@ defmodule ExUnit.DocTest do
490488
end
491489

492490
# Encountered an empty line, store pending test
493-
defp extract_tests([""|lines], line, expr_acc, expected_acc, [test=Test[exprs: exprs]|t], _) do
494-
test = test.exprs([{expr_acc, {:test, expected_acc}} | exprs])
491+
defp extract_tests([""|lines], line, expr_acc, expected_acc, [test=%{exprs: exprs}|t], _) do
492+
test = %{test | exprs: [{expr_acc, {:test, expected_acc}} | exprs]}
495493
extract_tests(lines, line, "", "", [test|t], true)
496494
end
497495

498496
# Exception test.
499-
defp extract_tests([<< "** (", string :: binary >>|lines], line, expr_acc, "", [test=Test[exprs: exprs]|t], newtest) do
500-
test = test.exprs([{expr_acc, extract_error(string, "")} | exprs])
497+
defp extract_tests([<< "** (", string :: binary >>|lines], line, expr_acc, "", [test=%{exprs: exprs}|t], newtest) do
498+
test = %{test | exprs: [{expr_acc, extract_error(string, "")} | exprs]}
501499
extract_tests(lines, line, "", "", [test|t], newtest)
502500
end
503501

504502
# Finally, parse expected_acc.
505-
defp extract_tests([expected|lines], line, expr_acc, expected_acc, [test=Test[exprs: exprs]|t]=acc, newtest) do
503+
defp extract_tests([expected|lines], line, expr_acc, expected_acc, [test=%{exprs: exprs}|t]=acc, newtest) do
506504
if expected =~ ~r/^#[A-Z][\w\.]*<.*>$/ do
507505
expected = expected_acc <> "\n" <> inspect(expected)
508-
test = test.exprs([{expr_acc, {:inspect, expected}} | exprs])
506+
test = %{test | exprs: [{expr_acc, {:inspect, expected}} | exprs]}
509507
extract_tests(lines, line, "", "", [test|t], newtest)
510508
else
511509
extract_tests(lines, line, expr_acc, expected_acc <> "\n" <> expected, acc, newtest)
@@ -529,8 +527,8 @@ defmodule ExUnit.DocTest do
529527
end
530528

531529
defp reverse_last_test([]), do: []
532-
defp reverse_last_test([test=Test[exprs: exprs] | t]) do
533-
test = test.exprs(Enum.reverse(exprs))
530+
defp reverse_last_test([test=%{exprs: exprs} | t]) do
531+
test = %{test | exprs: Enum.reverse(exprs)}
534532
[test | t]
535533
end
536534
end

lib/ex_unit/lib/ex_unit/server.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ defmodule ExUnit.Server do
44
@timeout 30_000
55
use GenServer.Behaviour
66

7-
defrecord Config, async_cases: [], sync_cases: [], start_load: nil, captured_devices: HashSet.new
7+
defrecord Config, async_cases: HashSet.new, sync_cases: HashSet.new,
8+
start_load: nil, captured_devices: HashSet.new
89

910
def start_link() do
1011
:gen_server.start_link({:local, __MODULE__}, __MODULE__, :ok, [])
@@ -54,7 +55,7 @@ defmodule ExUnit.Server do
5455

5556
{:reply,
5657
{config.async_cases, config.sync_cases, load_us},
57-
config.async_cases([]).sync_cases([]).start_load(nil)}
58+
config.async_cases(HashSet.new).sync_cases(HashSet.new).start_load(nil)}
5859
end
5960

6061
def handle_call({:add_device, device}, _from, config) do
@@ -76,11 +77,11 @@ defmodule ExUnit.Server do
7677
end
7778

7879
def handle_cast({:add_async_case, name}, config) do
79-
{:noreply, config.update_async_cases &[name|&1]}
80+
{:noreply, config.update_async_cases &Set.put(&1, name)}
8081
end
8182

8283
def handle_cast({:add_sync_case, name}, config) do
83-
{:noreply, config.update_sync_cases &[name|&1]}
84+
{:noreply, config.update_sync_cases &Set.put(&1, name)}
8485
end
8586

8687
def handle_cast(request, config) do

0 commit comments

Comments
 (0)