Skip to content

Commit 71831d6

Browse files
committed
Minor doctest source code improvement
1 parent 13c334f commit 71831d6

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

lib/ex_unit/lib/ex_unit/doc_test.ex

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,11 @@ defmodule ExUnit.DocTest do
160160
:"test doc at #{inspect m}.#{f}/#{a} (#{n})"
161161
end
162162

163-
defp test_content(Test[expected: { :ok, expected }] = test, module, do_import) do
163+
defp test_content(Test[expected: { :test, expected }] = test, module, do_import) do
164164
line = test.line
165165
file = module.__info__(:compile)[:source]
166+
location = [line: line, file: Path.relative_to(file, System.cwd!)]
167+
stack = Macro.escape [{ module, :__MODULE__, 0, location }]
166168

167169
expr_ast = string_to_ast(line, file, test.expr)
168170
expected_ast = string_to_ast(line, file, expected)
@@ -174,31 +176,25 @@ defmodule ExUnit.DocTest do
174176
case unquote(expr_ast) do
175177
^v -> :ok
176178
instead ->
177-
location = [line: unquote(line), file: Path.relative_to(unquote(file), System.cwd!)]
178-
stack = [{ unquote(module), :__MODULE__, 0, location }]
179179
raise ExUnit.ExpectationError,
180180
[ prelude: "Expected doctest",
181181
expected: unquote(test.expr),
182182
actual: inspect(v),
183183
reason: "evaluate to",
184184
instead: instead ],
185-
stack
185+
unquote(stack)
186186
end
187187
rescue
188188
e in [ExUnit.ExpectationError] ->
189-
location = [line: unquote(line), file: Path.relative_to(unquote(file), System.cwd!)]
190-
stack = [{ unquote(module), :__MODULE__, 0, location }]
191-
raise e, [], stack
189+
raise e, [], unquote(stack)
192190
e ->
193-
location = [line: unquote(line), file: Path.relative_to(unquote(file), System.cwd!)]
194-
stack = [{ unquote(module), :__MODULE__, 0, location }]
195191
raise ExUnit.ExpectationError,
196192
[ prelude: "Expected doctest",
197193
expected: unquote(test.expr),
198194
actual: "without an exception",
199195
reason: "complete",
200196
instead: e ],
201-
stack
197+
unquote(stack)
202198
end
203199
end
204200
if do_import do
@@ -213,46 +209,42 @@ defmodule ExUnit.DocTest do
213209
defp test_content(Test[expected: { :error, exception, message }] = test, module, do_import) do
214210
line = test.line
215211
file = module.__info__(:compile)[:source]
212+
location = [line: line, file: Path.relative_to(file, System.cwd!)]
213+
stack = Macro.escape [{ module, :__MODULE__, 0, location }]
216214

217215
expr_ast = string_to_ast(line, file, test.expr)
218216

219217
quoted =
220218
quote do
221219
try do
222220
v = unquote(expr_ast)
223-
location = [line: unquote(line), file: Path.relative_to(unquote(file), System.cwd!)]
224-
stack = [{ unquote(module), :__MODULE__, 0, location }]
225221
raise ExUnit.ExpectationError,
226222
[ prelude: "Expected doctest",
227223
expected: unquote(test.expr),
228224
actual: "by raising #{inspect unquote(exception)} exception",
229225
reason: "terminate",
230226
instead: v ],
231-
stack
227+
unquote(stack)
232228
rescue
233229
e in [ExUnit.ExpectationError] -> raise(e)
234230
error in [unquote(exception)] ->
235231
unless error.message == unquote(message) do
236-
location = [line: unquote(line), file: Path.relative_to(unquote(file), System.cwd!)]
237-
stack = [{ unquote(module), :__MODULE__, 0, location }]
238232
raise ExUnit.ExpectationError,
239233
[ prelude: "Expected doctest",
240234
expected: unquote(test.expr),
241235
actual: "by raising #{inspect unquote(exception)} exception with a #{inspect unquote(message)} message",
242236
reason: "terminate",
243237
instead: error ],
244-
stack
238+
unquote(stack)
245239
end
246240
error ->
247-
location = [line: unquote(line), file: Path.relative_to(unquote(file), System.cwd!)]
248-
stack = [{ unquote(module), :__MODULE__, 0, location }]
249241
raise ExUnit.ExpectationError,
250242
[ prelude: "Expected doctest",
251243
expected: unquote(test.expr),
252244
actual: "by raising #{inspect unquote(exception)}",
253245
reason: "terminate",
254246
instead: error ],
255-
stack
247+
unquote(stack)
256248
end
257249
end
258250
if do_import do
@@ -306,12 +298,12 @@ defmodule ExUnit.DocTest do
306298
defp extract_tests([], _line, "", "", acc), do: Enum.reverse(acc)
307299

308300
defp extract_tests([], line, expr_acc, expected_acc, acc) do
309-
test = Test[expr: expr_acc, line: line, expected: { :ok, expected_acc }]
301+
test = Test[expr: expr_acc, line: line, expected: { :test, expected_acc }]
310302
Enum.reverse([test|acc])
311303
end
312304

313305
defp extract_tests([<< "iex>", _ :: binary>>|_] = list, line, expr_acc, expected_acc, acc) when expr_acc != "" and expected_acc != "" do
314-
test = Test[expr: expr_acc, line: line, expected: { :ok, expected_acc }]
306+
test = Test[expr: expr_acc, line: line, expected: { :test, expected_acc }]
315307
extract_tests(list, line, "", "", [test|acc])
316308
end
317309

@@ -340,7 +332,7 @@ defmodule ExUnit.DocTest do
340332
end
341333

342334
defp extract_tests([""|lines], line, expr_acc, expected_acc, acc) do
343-
test = Test[expr: expr_acc, line: line, expected: { :ok, expected_acc }]
335+
test = Test[expr: expr_acc, line: line, expected: { :test, expected_acc }]
344336
extract_tests(lines, line, "", "", [test|acc])
345337
end
346338

0 commit comments

Comments
 (0)