@@ -101,14 +101,11 @@ defmodule ExUnit.DocTest do
101
101
* `:only` — generate tests only forfunctions listed
102
102
(list of `{function, arity}` tuples)
103
103
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.
112
109
113
110
## Examples
114
111
@@ -163,8 +160,9 @@ defmodule ExUnit.DocTest do
163
160
expr_ast = string_to_ast ( module , line , file , test . expr )
164
161
expected_ast = string_to_ast ( module , line , file , expected )
165
162
166
- quoted =
167
163
quote do
164
+ unquote_splicing ( test_import ( module , do_import ) )
165
+
168
166
try do
169
167
v = unquote ( expected_ast )
170
168
case unquote ( expr_ast ) do
@@ -191,13 +189,6 @@ defmodule ExUnit.DocTest do
191
189
unquote ( stack )
192
190
end
193
191
end
194
- if do_import do
195
- quoted = quote do
196
- import unquote ( module )
197
- unquote ( quoted )
198
- end
199
- end
200
- quoted
201
192
end
202
193
203
194
defp test_content ( Test [ expected : { :error , exception , message } ] = test , module , do_import ) do
@@ -208,8 +199,9 @@ defmodule ExUnit.DocTest do
208
199
209
200
expr_ast = string_to_ast ( module , line , file , test . expr )
210
201
211
- quoted =
212
202
quote do
203
+ unquote_splicing ( test_import ( module , do_import ) )
204
+
213
205
try do
214
206
v = unquote ( expr_ast )
215
207
raise ExUnit.ExpectationError ,
@@ -241,13 +233,11 @@ defmodule ExUnit.DocTest do
241
233
unquote ( stack )
242
234
end
243
235
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 ) ) ]
251
241
end
252
242
253
243
defp string_to_ast ( module , line , file , expr ) do
0 commit comments