Skip to content

Commit 20ebb45

Browse files
glazzarijosevalim
authored andcommitted
Format ExUnit.Callbacks (#6886)
1 parent f989d65 commit 20ebb45

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

lib/ex_unit/lib/ex_unit/callbacks.ex

+39-29
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ defmodule ExUnit.Callbacks do
121121

122122
@doc false
123123
defmacro __before_compile__(env) do
124-
[compile_callbacks(env, :setup),
125-
compile_callbacks(env, :setup_all)]
124+
[compile_callbacks(env, :setup), compile_callbacks(env, :setup_all)]
126125
end
127126

128127
@doc """
@@ -139,7 +138,7 @@ defmodule ExUnit.Callbacks do
139138
else
140139
quote do
141140
@ex_unit_setup ExUnit.Callbacks.__callback__(unquote(block), @ex_unit_describe) ++
142-
@ex_unit_setup
141+
@ex_unit_setup
143142
end
144143
end
145144
end
@@ -179,10 +178,12 @@ defmodule ExUnit.Callbacks do
179178
do_setup_all(quote(do: _), block)
180179
else
181180
quote do
182-
@ex_unit_describe && raise "cannot invoke setup_all/1 inside describe as setup_all/1 " <>
183-
"always applies to all tests in a module"
181+
@ex_unit_describe &&
182+
raise "cannot invoke setup_all/1 inside describe as setup_all/1 " <>
183+
"always applies to all tests in a module"
184+
184185
@ex_unit_setup_all ExUnit.Callbacks.__callback__(unquote(block), nil) ++
185-
@ex_unit_setup_all
186+
@ex_unit_setup_all
186187
end
187188
end
188189
end
@@ -225,7 +226,9 @@ defmodule ExUnit.Callbacks do
225226
@spec on_exit(term, (() -> term)) :: :ok | no_return
226227
def on_exit(name_or_ref \\ make_ref(), callback) when is_function(callback, 0) do
227228
case ExUnit.OnExitHandler.add(self(), name_or_ref, callback) do
228-
:ok -> :ok
229+
:ok ->
230+
:ok
231+
229232
:error ->
230233
raise ArgumentError, "on_exit/2 callback can only be invoked from the test process"
231234
end
@@ -266,17 +269,19 @@ defmodule ExUnit.Callbacks do
266269
This function returns `{:ok, pid}` in case of success, otherwise it
267270
returns `{:error, reason}`.
268271
"""
269-
@spec start_supervised(Supervisor.child_spec | module | {module, term}, keyword) ::
270-
Supervisor.on_start_child
272+
@spec start_supervised(Supervisor.child_spec() | module | {module, term}, keyword) ::
273+
Supervisor.on_start_child()
271274
def start_supervised(child_spec_or_module, opts \\ []) do
272275
sup =
273276
case ExUnit.OnExitHandler.get_supervisor(self()) do
274277
{:ok, nil} ->
275278
{:ok, sup} = Supervisor.start_link([], @supervisor_opts)
276279
ExUnit.OnExitHandler.put_supervisor(self(), sup)
277280
sup
281+
278282
{:ok, sup} ->
279283
sup
284+
280285
:error ->
281286
raise ArgumentError, "start_supervised/2 can only be invoked from the test process"
282287
end
@@ -288,25 +293,24 @@ defmodule ExUnit.Callbacks do
288293
Same as `start_supervised/2` but returns the PID on success and raises if
289294
not started properly.
290295
"""
291-
@spec start_supervised!(Supervisor.child_spec | module | {module, term}, keyword) :: pid
296+
@spec start_supervised!(Supervisor.child_spec() | module | {module, term}, keyword) :: pid
292297
def start_supervised!(child_spec_or_module, opts \\ []) do
293298
case start_supervised(child_spec_or_module, opts) do
294299
{:ok, pid} ->
295300
pid
301+
296302
{:ok, pid, _info} ->
297303
pid
304+
298305
{:error, reason} ->
299-
raise "failed to start child with the spec #{inspect child_spec_or_module}.\n" <>
300-
"Reason: #{start_supervised_error(reason)}"
306+
raise "failed to start child with the spec #{inspect(child_spec_or_module)}.\n" <>
307+
"Reason: #{start_supervised_error(reason)}"
301308
end
302309
end
303310

304-
defp start_supervised_error({{:EXIT, reason}, _info}),
305-
do: Exception.format_exit(reason)
306-
defp start_supervised_error({reason, _info}),
307-
do: Exception.format_exit(reason)
308-
defp start_supervised_error(reason),
309-
do: Exception.format_exit(reason)
311+
defp start_supervised_error({{:EXIT, reason}, _info}), do: Exception.format_exit(reason)
312+
defp start_supervised_error({reason, _info}), do: Exception.format_exit(reason)
313+
defp start_supervised_error(reason), do: Exception.format_exit(reason)
310314

311315
@doc """
312316
Stops a child process started via `start_supervised/2`.
@@ -325,10 +329,12 @@ defmodule ExUnit.Callbacks do
325329
case ExUnit.OnExitHandler.get_supervisor(self()) do
326330
{:ok, nil} ->
327331
{:error, :not_found}
332+
328333
{:ok, sup} ->
329334
with :ok <- Supervisor.terminate_child(sup, id),
330335
:ok <- Supervisor.delete_child(sup, id),
331336
do: :ok
337+
332338
:error ->
333339
raise ArgumentError, "stop_supervised/1 can only be invoked from the test process"
334340
end
@@ -342,12 +348,14 @@ defmodule ExUnit.Callbacks do
342348
def __callback__(callback, describe) do
343349
for k <- List.wrap(callback) do
344350
if not is_atom(k) do
345-
raise ArgumentError, "setup/setup_all expect a callback name as an atom or " <>
346-
"a list of callback names, got: #{inspect k}"
351+
raise ArgumentError,
352+
"setup/setup_all expect a callback name as an atom or " <>
353+
"a list of callback names, got: #{inspect(k)}"
347354
end
348355

349356
{k, describe}
350-
end |> Enum.reverse()
357+
end
358+
|> Enum.reverse()
351359
end
352360

353361
@doc false
@@ -383,39 +391,41 @@ defmodule ExUnit.Callbacks do
383391
Map.merge(context, data, fn
384392
k, v1, v2 when k in @reserved ->
385393
if v1 == v2, do: v1, else: raise_merge_reserved!(mod, k, v2)
394+
386395
_, _, v ->
387396
v
388397
end)
389398
end
390399

391400
defp raise_merge_failed!(mod, return_value) do
392-
raise "expected ExUnit callback in #{inspect mod} to return :ok | keyword | map, " <>
393-
"got #{inspect return_value} instead"
401+
raise "expected ExUnit callback in #{inspect(mod)} to return :ok | keyword | map, " <>
402+
"got #{inspect(return_value)} instead"
394403
end
395404

396405
defp raise_merge_reserved!(mod, key, value) do
397-
raise "ExUnit callback in #{inspect mod} is trying to set " <>
398-
"reserved field #{inspect key} to #{inspect value}"
406+
raise "ExUnit callback in #{inspect(mod)} is trying to set " <>
407+
"reserved field #{inspect(key)} to #{inspect(value)}"
399408
end
400409

401410
defp escape(contents) do
402411
Macro.escape(contents, unquote: true)
403412
end
404413

405414
defp compile_callbacks(env, kind) do
406-
callbacks = Module.get_attribute(env.module, :"ex_unit_#{kind}") |> Enum.reverse
415+
callbacks = Module.get_attribute(env.module, :"ex_unit_#{kind}") |> Enum.reverse()
407416

408417
acc =
409418
case callbacks do
410419
[] ->
411-
quote do: context
420+
quote(do: context)
421+
412422
[h | t] ->
413-
Enum.reduce t, compile_merge(h), fn callback_describe, acc ->
423+
Enum.reduce(t, compile_merge(h), fn callback_describe, acc ->
414424
quote do
415425
context = unquote(acc)
416426
unquote(compile_merge(callback_describe))
417427
end
418-
end
428+
end)
419429
end
420430

421431
quote do

0 commit comments

Comments
 (0)