Skip to content

Commit 8247382

Browse files
author
José Valim
committed
Merge pull request #1233 from alco/1226-doctest-perf
Optimize doctest generation. Closes #1226
2 parents b19c389 + 9b0d972 commit 8247382

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

lib/ex_unit/lib/ex_unit/doc_test.ex

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -191,52 +191,40 @@ defmodule ExUnit.DocTest do
191191
end
192192
whole_expr = String.strip(whole_expr)
193193

194-
{ exception, message } = case Enum.find(exprs, exc_filter_fn) do
194+
exception = case Enum.find(exprs, exc_filter_fn) do
195195
{ _, {:error, exception, message} } ->
196-
{ exception, message }
196+
inspect(exception) <> "[message: \"#{message}\"]"
197197
nil ->
198-
{ nil, nil }
198+
"nothing"
199199
end
200200

201201
quote do
202202
unquote_splicing(test_import(module, do_import))
203-
unquote(gen_code_for_tests(tests, whole_expr, exception, message, stack))
203+
unquote(gen_code_for_tests(tests, whole_expr, exception, stack))
204204
end
205205
end
206206

207-
defp gen_code_for_tests(tests, whole_expr, exception, message, stack) do
207+
defp gen_code_for_tests(tests, whole_expr, exception, stack) do
208208
quote do
209+
stack = unquote(stack)
209210
try do
210211
# Put all tests into one context
211212
unquote_splicing(tests)
212213
rescue
213214
e in [ExUnit.ExpectationError] ->
214-
raise e, [], unquote(stack)
215-
216-
# If there was no exception among the tests, `exception` here will be
217-
# nil and this clause won't match.
218-
error in [unquote(exception)] ->
219-
unless error.message == unquote(message) do
220-
raise ExUnit.ExpectationError,
221-
[ prelude: "Expected doctest",
222-
description: unquote(whole_expr),
223-
expected: "#{inspect unquote(exception)} with message #{inspect unquote(message)}",
224-
reason: "raise",
225-
actual: inspect(error) ],
226-
unquote(stack)
227-
end
215+
raise e, [], stack
228216

229217
error ->
230218
raise ExUnit.ExpectationError,
231219
[ prelude: "Expected doctest",
232220
description: unquote(whole_expr),
233-
expected: "#{inspect unquote(exception)}",
221+
expected: unquote(exception),
234222
# We're using a combined message here because all expressions
235223
# (those that are expected to raise and those that aren't) are in
236224
# the same try block above.
237225
reason: "complete or raise",
238226
actual: inspect(error) ],
239-
unquote(stack)
227+
stack
240228
end
241229
end
242230
end
@@ -261,18 +249,36 @@ defmodule ExUnit.DocTest do
261249
end
262250
end
263251

264-
defp test_case_content(expr, { :error, exception, _ }, module, line, file, stack) do
252+
defp test_case_content(expr, { :error, exception, message }, module, line, file, stack) do
265253
expr_ast = string_to_ast(module, line, file, expr)
266254

267255
quote do
268-
v = unquote(expr_ast)
269-
raise ExUnit.ExpectationError,
270-
[ prelude: "Expected doctest",
271-
description: unquote(expr),
272-
expected: "#{inspect unquote(exception)}[]",
273-
reason: "raise",
274-
actual: inspect(v) ],
275-
unquote(stack)
256+
stack = unquote(stack)
257+
expr = unquote(expr)
258+
exception = inspect(unquote(exception)) <> "[message: \"#{unquote(message)}\"]"
259+
try do
260+
v = unquote(expr_ast)
261+
raise ExUnit.ExpectationError,
262+
[ prelude: "Expected doctest",
263+
description: expr,
264+
expected: exception,
265+
reason: "raise",
266+
actual: inspect(v) ],
267+
stack
268+
rescue
269+
error in [unquote(exception)] ->
270+
unless error.message == unquote(message) do
271+
raise ExUnit.ExpectationError,
272+
[ prelude: "Expected doctest",
273+
description: expr,
274+
expected: exception,
275+
reason: "raise",
276+
actual: inspect(error) ],
277+
stack
278+
end
279+
280+
other -> raise other
281+
end
276282
end
277283
end
278284

0 commit comments

Comments
 (0)