@@ -107,6 +107,15 @@ defmodule ExUnit.DocTest do
107
107
* `:only` — generate tests only forfunctions listed
108
108
(list of `{function, arity}` tuples)
109
109
110
+ * `:import` — if false, do not auto-import the current module into the
111
+ evaluted example. If true, one can test a function
112
+ defined in the module without referring to the module
113
+ name. However, this is not feasible when there is a clash
114
+ with a number module like Kernel. In these cases, `import`
115
+ should be set to `false` and a full `M.f` construct should
116
+ be used. False by default.
117
+
118
+
110
119
## Examples
111
120
112
121
doctest MyModule, except: [trick_fun: 1]
@@ -125,21 +134,22 @@ defmodule ExUnit.DocTest do
125
134
def __doctests__ ( module , opts ) do
126
135
only = opts [ :only ] || [ ]
127
136
except = opts [ :except ] || [ ]
137
+ do_import = Keyword . get ( opts , :import , false )
128
138
129
139
tests = Enum . filter ( extract ( module ) , fn ( test ) ->
130
140
fa = test . fun_arity
131
141
Enum . all? ( except , & 1 != fa ) and Enum . all? ( only , & 1 == fa )
132
142
end )
133
143
134
144
Enum . map_reduce ( tests , 1 , fn ( test , acc ) ->
135
- { compile_test ( test , module , acc ) , acc + 1 }
145
+ { compile_test ( test , module , do_import , acc ) , acc + 1 }
136
146
end ) |> elem ( 0 )
137
147
end
138
148
139
149
## Compilation of extracted tests
140
150
141
- defp compile_test ( test , module , n ) do
142
- { test_name ( test , module , n ) , test_content ( test , module ) }
151
+ defp compile_test ( test , module , do_import , n ) do
152
+ { test_name ( test , module , n ) , test_content ( test , module , do_import ) }
143
153
end
144
154
145
155
defp test_name ( Test [ fun_arity : nil ] , m , n ) do
@@ -150,16 +160,16 @@ defmodule ExUnit.DocTest do
150
160
:"test doc at #{ inspect m } .#{ f } /#{ a } (#{ n } )"
151
161
end
152
162
153
- defp test_content ( Test [ expected : { :ok , expected } ] = test , module ) do
163
+ defp test_content ( Test [ expected : { :ok , expected } ] = test , module , do_import ) do
154
164
line = test . line
155
165
file = module . __info__ ( :compile ) [ :source ]
156
166
157
167
expr_ast = string_to_ast ( line , file , test . expr )
158
168
expected_ast = string_to_ast ( line , file , expected )
159
169
170
+ quoted =
160
171
quote do
161
172
try do
162
- import unquote ( module )
163
173
v = unquote ( expected_ast )
164
174
case unquote ( expr_ast ) do
165
175
^ v -> :ok
@@ -186,21 +196,34 @@ defmodule ExUnit.DocTest do
186
196
stack
187
197
end
188
198
end
199
+ if do_import do
200
+ quoted = quote do
201
+ import unquote ( module )
202
+ unquote ( quoted )
203
+ end
204
+ end
205
+ quoted
189
206
end
190
207
191
- defp test_content ( Test [ expected : { :error , exception , message } ] = test , module ) do
208
+ defp test_content ( Test [ expected : { :error , exception , message } ] = test , module , do_import ) do
192
209
line = test . line
193
210
file = module . __info__ ( :compile ) [ :source ]
194
211
195
212
expr_ast = string_to_ast ( line , file , test . expr )
196
213
214
+ quoted =
197
215
quote do
198
- import unquote ( module )
199
-
200
216
assert_raise unquote ( exception ) , unquote ( message ) , fn ->
201
217
unquote ( expr_ast )
202
218
end
203
219
end
220
+ if do_import do
221
+ quoted = quote do
222
+ import unquote ( module )
223
+ unquote ( quoted )
224
+ end
225
+ end
226
+ quoted
204
227
end
205
228
206
229
defp string_to_ast ( line , file , expr ) do
0 commit comments