|
| 1 | +defmodule ExUnit.Callbacks do |
| 2 | + @moduledoc %B""" |
| 3 | + This module defines four callbacks: `setup_all`, `teardown_all`, |
| 4 | + `setup` and `teardown`. Those callbacks are defined via macros |
| 5 | + and receives a keyword list of metadata. The callback may |
| 6 | + optionally define extra data which will be available in the test |
| 7 | + cases. |
| 8 | +
|
| 9 | + ## Examples |
| 10 | +
|
| 11 | + defmodule AssertionTest do |
| 12 | + use ExUnit.Case, async: true |
| 13 | +
|
| 14 | + setup do |
| 15 | + IO.puts "This is a setup callback" |
| 16 | +
|
| 17 | + # Return no extra meta data |
| 18 | + [hello: "world"] |
| 19 | + end |
| 20 | +
|
| 21 | + setup context do |
| 22 | + # We can access the test name in the context |
| 23 | + IO.puts "Setting up: #{context[:test]}" |
| 24 | +
|
| 25 | + # The metadata returned by the previous setup as well |
| 26 | + assert context[:hello] == "world" |
| 27 | + end |
| 28 | +
|
| 29 | + test "always pass" do |
| 30 | + assert true |
| 31 | + end |
| 32 | + end |
| 33 | +
|
| 34 | + """ |
| 35 | + |
| 36 | + @doc false |
| 37 | + defmacro __using__(_) do |
| 38 | + quote do |
| 39 | + @exunit_setup [] |
| 40 | + @exunit_teardown [] |
| 41 | + @exunit_setup_all [] |
| 42 | + @exunit_teardown_all [] |
| 43 | + |
| 44 | + @before_compile unquote(__MODULE__) |
| 45 | + @after_compile unquote(__MODULE__) |
| 46 | + import unquote(__MODULE__) |
| 47 | + |
| 48 | + def __exunit__(:setup, context) do |
| 49 | + context = __exunit_setup__(context) |
| 50 | + ExUnit.Callbacks.__setup__(__MODULE__, context) |
| 51 | + end |
| 52 | + |
| 53 | + def __exunit__(:teardown, context) do |
| 54 | + context = __exunit_teardown__(context) |
| 55 | + ExUnit.Callbacks.__teardown__(__MODULE__, context) |
| 56 | + end |
| 57 | + |
| 58 | + def __exunit__(:setup_all, context) do |
| 59 | + context = __exunit_setup_all__(context) |
| 60 | + ExUnit.Callbacks.__setup_all__(__MODULE__, context) |
| 61 | + end |
| 62 | + |
| 63 | + def __exunit__(:teardown_all, context) do |
| 64 | + context = __exunit_teardown_all__(context) |
| 65 | + ExUnit.Callbacks.__teardown_all__(__MODULE__, context) |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + @doc false |
| 71 | + defmacro __before_compile__(env) do |
| 72 | + [ compile_callbacks(env, :exunit_setup), |
| 73 | + compile_callbacks(env, :exunit_teardown), |
| 74 | + compile_callbacks(env, :exunit_setup_all), |
| 75 | + compile_callbacks(env, :exunit_teardown_all) ] |
| 76 | + end |
| 77 | + |
| 78 | + defmacro setup(var // quote(do: _), block) do |
| 79 | + quote do |
| 80 | + name = :"__exunit_setup_#{length(@exunit_setup)}" |
| 81 | + defp name, [unquote(Macro.escape var)], [], unquote(Macro.escape block) |
| 82 | + @exunit_setup [name|@exunit_setup] |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + defmacro teardown(var // quote(do: _), block) do |
| 87 | + quote do |
| 88 | + name = :"__exunit_teardown_#{length(@exunit_teardown)}" |
| 89 | + defp name, [unquote(Macro.escape var)], [], unquote(Macro.escape block) |
| 90 | + @exunit_teardown [name|@exunit_teardown] |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + defmacro setup_all(var // quote(do: _), block) do |
| 95 | + quote do |
| 96 | + name = :"__exunit_setup_all_#{length(@exunit_setup_all)}" |
| 97 | + defp name, [unquote(Macro.escape var)], [], unquote(Macro.escape block) |
| 98 | + @exunit_setup_all [name|@exunit_setup_all] |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + defmacro teardown_all(var // quote(do: _), block) do |
| 103 | + quote do |
| 104 | + name = :"__exunit_teardown_all_#{length(@exunit_teardown_all)}" |
| 105 | + defp name, [unquote(Macro.escape var)], [], unquote(Macro.escape block) |
| 106 | + @exunit_teardown_all [name|@exunit_teardown_all] |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + ## Helpers |
| 111 | + |
| 112 | + defp compile_callbacks(env, kind) do |
| 113 | + callbacks = Module.get_attribute(env.module, kind) |> Enum.reverse |
| 114 | + |
| 115 | + acc = |
| 116 | + Enum.reduce callbacks, quote(do: context), fn(callback, acc) -> |
| 117 | + quote do |
| 118 | + context = unquote(acc) |
| 119 | + Keyword.merge(context, unquote(callback)(context)) |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + quote do |
| 124 | + defp unquote(:"__#{kind}__")(context), do: unquote(acc) |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + ## Deprecated handling |
| 129 | + |
| 130 | + @doc false |
| 131 | + def __after_compile__(env, _binary) do |
| 132 | + test_case = env.module |
| 133 | + |
| 134 | + Enum.each [:setup, :teardown, :setup_all, :teardown_all], fn(kind) -> |
| 135 | + if Enum.any? 0..2, function_exported?(test_case, kind, &1) do |
| 136 | + IO.puts "[Warning] #{kind} callback in #{inspect test_case} is deprecated. Please use the #{kind} macro instead." |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + @doc false |
| 142 | + def __setup__(test_case, context) do |
| 143 | + cond do |
| 144 | + function_exported?(test_case, :setup, 2) -> |
| 145 | + test_case.setup(context, context[:test]) |
| 146 | + function_exported?(test_case, :setup, 1) -> |
| 147 | + test_case.setup(context) |
| 148 | + function_exported?(test_case, :setup, 0) -> |
| 149 | + test_case.setup |
| 150 | + true -> |
| 151 | + context |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + @doc false |
| 156 | + def __teardown__(test_case, context) do |
| 157 | + cond do |
| 158 | + function_exported?(test_case, :teardown, 2) -> |
| 159 | + test_case.teardown(context, context[:test]) |
| 160 | + function_exported?(test_case, :teardown, 1) -> |
| 161 | + test_case.teardown(context) |
| 162 | + function_exported?(test_case, :teardown, 0) -> |
| 163 | + test_case.teardown |
| 164 | + true -> |
| 165 | + context |
| 166 | + end |
| 167 | + end |
| 168 | + |
| 169 | + @doc false |
| 170 | + def __teardown_all__(test_case, context) do |
| 171 | + cond do |
| 172 | + function_exported?(test_case, :teardown_all, 1) -> |
| 173 | + test_case.teardown_all(context) |
| 174 | + function_exported?(test_case, :teardown_all, 0) -> |
| 175 | + test_case.teardown_all |
| 176 | + true -> |
| 177 | + context |
| 178 | + end |
| 179 | + end |
| 180 | + |
| 181 | + @doc false |
| 182 | + def __setup_all__(test_case, context) do |
| 183 | + cond do |
| 184 | + function_exported?(test_case, :setup_all, 0) -> |
| 185 | + test_case.setup_all |
| 186 | + true -> |
| 187 | + context |
| 188 | + end |
| 189 | + end |
| 190 | +end |
0 commit comments