Skip to content

Commit f1721c8

Browse files
committed
Reduce amount of injected code in case
1 parent 57abca4 commit f1721c8

File tree

1 file changed

+44
-32
lines changed

1 file changed

+44
-32
lines changed

lib/ex_unit/lib/ex_unit/case.ex

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -259,48 +259,60 @@ defmodule ExUnit.Case do
259259
end
260260

261261
quote do
262-
unless Module.has_attribute?(__MODULE__, :ex_unit_tests) do
263-
tag_check =
264-
[:moduletag, :describetag, :tag]
265-
|> Enum.any?(&Module.has_attribute?(__MODULE__, &1))
266-
267-
if tag_check do
268-
raise "you must set @tag, @describetag, and @moduletag after the call to \"use ExUnit.Case\""
269-
end
270-
271-
attributes = [
272-
:ex_unit_tests,
273-
:tag,
274-
:describetag,
275-
:moduletag,
276-
:ex_unit_registered_test_attributes,
277-
:ex_unit_registered_describe_attributes,
278-
:ex_unit_registered_module_attributes,
279-
:ex_unit_used_describes
280-
]
281-
282-
Enum.each(attributes, &Module.register_attribute(__MODULE__, &1, accumulate: true))
283-
284-
@before_compile ExUnit.Case
285-
@after_compile ExUnit.Case
286-
@ex_unit_async false
287-
@ex_unit_describe nil
262+
unless ExUnit.Case.__register__(__MODULE__, unquote(opts)) do
288263
use ExUnit.Callbacks
289264
end
290265

291-
async = unquote(opts)[:async]
292-
293-
if is_boolean(async) do
294-
@ex_unit_async async
295-
end
296-
297266
import ExUnit.Callbacks
298267
import ExUnit.Assertions
299268
import ExUnit.Case, only: [describe: 2, test: 1, test: 2, test: 3]
300269
import ExUnit.DocTest
301270
end
302271
end
303272

273+
@doc false
274+
def __register__(module, opts) do
275+
registered? = Module.has_attribute?(module, :ex_unit_tests)
276+
277+
unless registered? do
278+
tag_check = Enum.any?([:moduletag, :describetag, :tag], &Module.has_attribute?(module, &1))
279+
280+
if tag_check do
281+
raise "you must set @tag, @describetag, and @moduletag after the call to \"use ExUnit.Case\""
282+
end
283+
284+
attributes = [
285+
:ex_unit_tests,
286+
:tag,
287+
:describetag,
288+
:moduletag,
289+
:ex_unit_registered_test_attributes,
290+
:ex_unit_registered_describe_attributes,
291+
:ex_unit_registered_module_attributes,
292+
:ex_unit_used_describes
293+
]
294+
295+
Enum.each(attributes, &Module.register_attribute(module, &1, accumulate: true))
296+
297+
attributes = [
298+
before_compile: ExUnit.Case,
299+
after_compile: ExUnit.Case,
300+
ex_unit_async: false,
301+
ex_unit_describe: nil
302+
]
303+
304+
Enum.each(attributes, fn {k, v} -> Module.put_attribute(module, k, v) end)
305+
end
306+
307+
async? = opts[:async]
308+
309+
if is_boolean(async?) do
310+
Module.put_attribute(module, :ex_unit_async, async?)
311+
end
312+
313+
registered?
314+
end
315+
304316
@doc """
305317
Defines a test with `message`.
306318

0 commit comments

Comments
 (0)