Skip to content

Commit

Permalink
Improve ExUnit Callbacks docs
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed May 29, 2024
1 parent 7cf57ec commit 1b7a734
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
21 changes: 12 additions & 9 deletions lib/ex_unit/lib/ex_unit/callbacks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ defmodule ExUnit.Callbacks do
Defines ExUnit callbacks.
This module defines the `setup/1`, `setup/2`, `setup_all/1`, and
`setup_all/2` callbacks, as well as the `on_exit/2`, `start_supervised/2`,
`stop_supervised/1` and `start_link_supervised!/2` functions.
`setup_all/2` macros, as well as process lifecycle and management
functions, such as `on_exit/2`, `start_supervised/2`, `stop_supervised/1`
and `start_link_supervised!/2`.
The setup callbacks may be used to define [test fixtures](https://en.wikipedia.org/wiki/Test_fixture#Software)
and run any initialization code which help bring the system into a known
Expand Down Expand Up @@ -56,13 +57,15 @@ defmodule ExUnit.Callbacks do
## Context
If `setup_all` or `setup` return a keyword list, a map, or a tuple in the shape
of `{:ok, keyword() | map()}`, the keyword list or map will be merged into the
current context and will be available in all subsequent `setup_all`,
`setup`, and the `test` itself.
`setup_all` or `setup` may return one of:
Returning `:ok` leaves the context unchanged (in `setup` and `setup_all`
callbacks).
* the atom `:ok`
* a keyword list or map
* a tuple in the shape of `{:ok, keyword() | map()}`
If a keyword list or map is returned, it be merged into the current context
and will be available in all subsequent `setup_all`, `setup`, and the `test`
itself.
Returning anything else from `setup_all` will force all tests to fail,
while a bad response from `setup` causes the current test to fail.
Expand Down Expand Up @@ -728,7 +731,7 @@ defmodule ExUnit.Callbacks do

defp raise_merge_failed!(mod, kind, return_value) do
raise "expected ExUnit #{kind} callback in #{inspect(mod)} to " <>
"return :ok | keyword | map, got #{inspect(return_value)} instead"
"return the atom :ok, a keyword, or a map, got #{inspect(return_value)} instead"
end

defp raise_merge_reserved!(mod, kind, key, value) do
Expand Down
4 changes: 2 additions & 2 deletions lib/ex_unit/test/ex_unit/callbacks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ defmodule ExUnit.CallbacksTest do

assert output =~
"** (RuntimeError) expected ExUnit setup callback in " <>
"ExUnit.CallbacksTest.SetupErrorTest to return :ok | keyword | map, " <>
"ExUnit.CallbacksTest.SetupErrorTest to return the atom :ok, a keyword, or a map, " <>
"got {:ok, \"foo\"} instead"

# Make sure that at least the right file where the setup/setup_all call is defined is included
Expand All @@ -414,7 +414,7 @@ defmodule ExUnit.CallbacksTest do

assert output =~
"** (RuntimeError) expected ExUnit setup_all callback in " <>
"ExUnit.CallbacksTest.SetupAllErrorTest to return :ok | keyword | map, " <>
"ExUnit.CallbacksTest.SetupAllErrorTest to return the atom :ok, a keyword, or a map, " <>
"got {:ok, \"foo\"} instead"

# Make sure that at least the right file where the setup/setup_all call is defined is included
Expand Down

0 comments on commit 1b7a734

Please sign in to comment.