Skip to content

Commit be5e20f

Browse files
author
José Valim
committed
Extract import functionality
1 parent 26599e7 commit be5e20f

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

lib/ex_unit/lib/ex_unit/doc_test.ex

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,11 @@ defmodule ExUnit.DocTest do
101101
* `:only` — generate tests only forfunctions listed
102102
(list of `{function, arity}` tuples)
103103
104-
* `:import` — if false, do not auto-import the current module into the
105-
evaluted example. If true, one can test a function
106-
defined in the module without referring to the module
107-
name. However, this is not feasible when there is a clash
108-
with a number module like Kernel. In these cases, `import`
109-
should be set to `false` and a full `M.f` construct should
110-
be used. False by default.
111-
104+
* `:import` — when true, one can test a function defined in the module
105+
without referring to the module name. However, this is not
106+
feasible when there is a clash with a number module like
107+
Kernel. In these cases, `import` should be set to `false`
108+
and a full `M.f` construct should be used.
112109
113110
## Examples
114111
@@ -163,8 +160,9 @@ defmodule ExUnit.DocTest do
163160
expr_ast = string_to_ast(module, line, file, test.expr)
164161
expected_ast = string_to_ast(module, line, file, expected)
165162

166-
quoted =
167163
quote do
164+
unquote_splicing(test_import(module, do_import))
165+
168166
try do
169167
v = unquote(expected_ast)
170168
case unquote(expr_ast) do
@@ -191,13 +189,6 @@ defmodule ExUnit.DocTest do
191189
unquote(stack)
192190
end
193191
end
194-
if do_import do
195-
quoted = quote do
196-
import unquote(module)
197-
unquote(quoted)
198-
end
199-
end
200-
quoted
201192
end
202193

203194
defp test_content(Test[expected: { :error, exception, message }] = test, module, do_import) do
@@ -208,8 +199,9 @@ defmodule ExUnit.DocTest do
208199

209200
expr_ast = string_to_ast(module, line, file, test.expr)
210201

211-
quoted =
212202
quote do
203+
unquote_splicing(test_import(module, do_import))
204+
213205
try do
214206
v = unquote(expr_ast)
215207
raise ExUnit.ExpectationError,
@@ -241,13 +233,11 @@ defmodule ExUnit.DocTest do
241233
unquote(stack)
242234
end
243235
end
244-
if do_import do
245-
quoted = quote do
246-
import unquote(module)
247-
unquote(quoted)
248-
end
249-
end
250-
quoted
236+
end
237+
238+
defp test_import(_mod, false), do: []
239+
defp test_import(mod, _) do
240+
[quote do: import(unquote(mod))]
251241
end
252242

253243
defp string_to_ast(module, line, file, expr) do

0 commit comments

Comments
 (0)