Skip to content

Commit fa920eb

Browse files
author
José Valim
committed
Rename bundle/2 to describe/2
1 parent 953aac7 commit fa920eb

File tree

7 files changed

+168
-139
lines changed

7 files changed

+168
-139
lines changed

CHANGELOG.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ plug
136136
Mix also includes `mix escript.install` and `mix escript.uninstall` tasks for managing escripts. The tasks was designed in a way to mimic the existing `mix archive` functionality except that:
137137

138138
* Archives must be used sparingly because every new archive installed affects Mix performance, as every new archive is loaded when Mix boots. Escripts solve this by being managed apart from your Elixir/Mix installed
139-
* Archives depends on the current Elixir version. Therefore, updating your Elixir version may break an archive. Fortunately, escripts bundle Elixir inside themselves, and therefore do not depend on your Elixir system version
139+
* Archives depends on the current Elixir version. Therefore, updating your Elixir version may break an archive. Fortunately, escripts include Elixir inside themselves, and therefore do not depend on your Elixir system version
140140

141141
Escripts will be installed at `~/.mix/escripts` which must be added to your [`PATH` environment variable](https://en.wikipedia.org/wiki/PATH_(variable)).
142142

@@ -214,15 +214,15 @@ At the end of the run, ExUnit will also report it as a property, including both
214214
1 property, 10 tests, 0 failures
215215
```
216216

217-
### Named setups and bundles
217+
### Named setups and describes
218218

219-
Finally, ExUnit v1.3 includes the ability to organize tests together in bundles:
219+
Finally, ExUnit v1.3 includes the ability to organize tests together in describe blocks:
220220

221221
```elixir
222222
defmodule StringTest do
223223
use ExUnit.Case, async: true
224224

225-
bundle "String.capitalize/2" do
225+
describe "String.capitalize/2" do
226226
test "uppercases the first grapheme" do
227227
assert "T" <> _ = String.capitalize("test")
228228
end
@@ -234,25 +234,25 @@ defmodule StringTest do
234234
end
235235
```
236236

237-
Every test inside a bundle will be tagged with the bundle name. This allows developers to run tests that belong to particular bundles, be them in the same file or across many files:
237+
Every test inside a describe block will be tagged with the describe block name. This allows developers to run tests that belong to particular blocks, be them in the same file or across many files:
238238

239239
```
240-
$ mix test --only bundle:"String.capitalize/2"
240+
$ mix test --only describe:"String.capitalize/2"
241241
```
242242

243-
Note bundles cannot be nested. Instead of relying on hierarchy for composition, we want developers to build on top of named setups. For example:
243+
Note describe blocks cannot be nested. Instead of relying on hierarchy for composition, we want developers to build on top of named setups. For example:
244244

245245
```elixir
246246
defmodule UserManagementTest do
247247
use ExUnit.Case, async: true
248248

249-
bundle "when user is logged in and is an admin" do
249+
describe "when user is logged in and is an admin" do
250250
setup [:log_user_in, :set_type_to_admin]
251251

252252
test ...
253253
end
254254

255-
bundle "when user is logged in and is a manager" do
255+
describe "when user is logged in and is a manager" do
256256
setup [:log_user_in, :set_type_to_manager]
257257

258258
test ...
@@ -264,7 +264,7 @@ defmodule UserManagementTest do
264264
end
265265
```
266266

267-
By forbidding hierarchies in favor of named setups, it is straight-forward for the developer to glance at each bundle and know exactly the setup steps involved.
267+
By forbidding hierarchies in favor of named setups, it is straight-forward for the developer to glance at each describe block and know exactly the setup steps involved.
268268

269269
## v1.3.0-dev
270270

@@ -320,7 +320,7 @@ By forbidding hierarchies in favor of named setups, it is straight-forward for t
320320
* [ExUnit] Raise a straight-forward error message in case a duplicate test name is defined
321321
* [ExUnit] Bump the default number of max cases to double of schedulers to support both IO and CPU bound tests
322322
* [ExUnit] Support for named setups in `setup` and `setup_all`
323-
* [ExUnit] Support for bundling tests together with `bundle/2`
323+
* [ExUnit] Support for bundling tests together with `describe/2`
324324

325325
#### IEx
326326

lib/elixir/src/elixir_utils.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ convert_to_boolean(Line, Expr, Bool, S) when is_integer(Line) ->
230230
_ -> do_convert_to_boolean(Line, Expr, Bool, S)
231231
end.
232232

233-
%% Notice we use a temporary var and bundle nil
233+
%% Notice we use a temporary var and include nil
234234
%% and false checks in the same clause since
235235
%% it makes dialyzer happy.
236236
do_convert_to_boolean(Line, Expr, Bool, S) ->

lib/ex_unit/lib/ex_unit/callbacks.ex

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ defmodule ExUnit.Callbacks do
9595
@doc false
9696
defmacro __using__(_) do
9797
quote do
98-
@ex_unit_bundle nil
98+
@ex_unit_describe nil
9999
@ex_unit_setup []
100100
@ex_unit_setup_all []
101101

@@ -123,7 +123,7 @@ defmodule ExUnit.Callbacks do
123123
do_setup(quote(do: _), block)
124124
else
125125
quote do
126-
@ex_unit_setup ExUnit.Callbacks.__callback__(unquote(block), @ex_unit_bundle) ++
126+
@ex_unit_setup ExUnit.Callbacks.__callback__(unquote(block), @ex_unit_describe) ++
127127
@ex_unit_setup
128128
end
129129
end
@@ -147,7 +147,7 @@ defmodule ExUnit.Callbacks do
147147
quote bind_quoted: [var: escape(var), block: escape(block)] do
148148
name = :"__ex_unit_setup_#{length(@ex_unit_setup)}"
149149
defp unquote(name)(unquote(var)), unquote(block)
150-
@ex_unit_setup [{name, @ex_unit_bundle} | @ex_unit_setup]
150+
@ex_unit_setup [{name, @ex_unit_describe} | @ex_unit_setup]
151151
end
152152
end
153153

@@ -164,7 +164,8 @@ defmodule ExUnit.Callbacks do
164164
do_setup_all(quote(do: _), block)
165165
else
166166
quote do
167-
@ex_unit_bundle && raise "cannot invoke setup_all/1 inside bundle"
167+
@ex_unit_describe && raise "cannot invoke setup_all/1 inside describe as setup_all/1 " <>
168+
"always applies to all tests in a module"
168169
@ex_unit_setup_all ExUnit.Callbacks.__callback__(unquote(block), nil) ++
169170
@ex_unit_setup_all
170171
end
@@ -187,7 +188,7 @@ defmodule ExUnit.Callbacks do
187188

188189
defp do_setup_all(var, block) do
189190
quote bind_quoted: [var: escape(var), block: escape(block)] do
190-
@ex_unit_bundle && raise "cannot invoke setup_all/2 inside bundle"
191+
@ex_unit_describe && raise "cannot invoke setup_all/2 inside describe"
191192
name = :"__ex_unit_setup_all_#{length(@ex_unit_setup_all)}"
192193
defp unquote(name)(unquote(var)), unquote(block)
193194
@ex_unit_setup_all [{name, nil} | @ex_unit_setup_all]
@@ -216,17 +217,17 @@ defmodule ExUnit.Callbacks do
216217

217218
## Helpers
218219

219-
@reserved [:case, :file, :line, :test, :async, :registered, :bundle]
220+
@reserved [:case, :file, :line, :test, :async, :registered, :describe]
220221

221222
@doc false
222-
def __callback__(callback, bundle) do
223+
def __callback__(callback, describe) do
223224
for k <- List.wrap(callback) do
224225
if not is_atom(k) do
225226
raise ArgumentError, "setup/setup_all expect a callback name as an atom or " <>
226227
"a list of callback names, got: #{inspect k}"
227228
end
228229

229-
{k, bundle}
230+
{k, describe}
230231
end |> Enum.reverse()
231232
end
232233

@@ -287,17 +288,17 @@ defmodule ExUnit.Callbacks do
287288
[] ->
288289
quote do: context
289290
[h | t] ->
290-
Enum.reduce t, compile_merge(h), fn callback_bundle, acc ->
291+
Enum.reduce t, compile_merge(h), fn callback_describe, acc ->
291292
quote do
292293
context = unquote(acc)
293-
unquote(compile_merge(callback_bundle))
294+
unquote(compile_merge(callback_describe))
294295
end
295296
end
296297
end
297298

298299
quote do
299300
def __ex_unit__(unquote(kind), context) do
300-
bundle = Map.get(context, :bundle, nil)
301+
describe = Map.get(context, :describe, nil)
301302
unquote(acc)
302303
end
303304
end
@@ -309,9 +310,9 @@ defmodule ExUnit.Callbacks do
309310
end
310311
end
311312

312-
defp compile_merge({callback, bundle}) do
313+
defp compile_merge({callback, describe}) do
313314
quote do
314-
if unquote(bundle) == bundle do
315+
if unquote(describe) == describe do
315316
unquote(compile_merge({callback, nil}))
316317
else
317318
context

lib/ex_unit/lib/ex_unit/case.ex

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ defmodule ExUnit.Case do
125125
* `:async` - if the test case is in async mode
126126
* `:type` - the type of the test (`:test`, `:property`, etc)
127127
* `:registered` - used for `ExUnit.Case.register_attribute/3` values
128-
* `:bundle` - the bundle the test belongs to
128+
* `:describe` - the describe block the test belongs to
129129
130130
The following tags customize how tests behaves:
131131
@@ -190,7 +190,7 @@ defmodule ExUnit.Case do
190190
config :logger, backends: []
191191
"""
192192

193-
@reserved [:case, :file, :line, :test, :async, :registered, :bundle, :type]
193+
@reserved [:case, :file, :line, :test, :async, :registered, :describe, :type]
194194

195195
@doc false
196196
defmacro __using__(opts) do
@@ -203,19 +203,19 @@ defmodule ExUnit.Case do
203203
async = !!unquote(opts)[:async]
204204

205205
unless Module.get_attribute(__MODULE__, :ex_unit_tests) do
206-
Enum.each [:ex_unit_tests, :tag, :bundletag, :moduletag, :ex_unit_registered],
206+
Enum.each [:ex_unit_tests, :tag, :describetag, :moduletag, :ex_unit_registered],
207207
&Module.register_attribute(__MODULE__, &1, accumulate: true)
208208

209209
@before_compile ExUnit.Case
210210
@after_compile ExUnit.Case
211211
@ex_unit_async async
212-
@ex_unit_bundle nil
212+
@ex_unit_describe nil
213213
use ExUnit.Callbacks
214214
end
215215

216216
import ExUnit.Callbacks
217217
import ExUnit.Assertions
218-
import ExUnit.Case, only: [bundle: 2, test: 1, test: 2, test: 3]
218+
import ExUnit.Case, only: [describe: 2, test: 1, test: 2, test: 3]
219219
import ExUnit.DocTest
220220
end
221221
end
@@ -283,20 +283,20 @@ defmodule ExUnit.Case do
283283
end
284284

285285
@doc """
286-
Bundles tests together.
286+
Describes tests together.
287287
288-
Every bundle receives a name which is used as prefix for upcoming tests.
289-
Inside a bundle, `ExUnit.Callbacks.setup/1` may be invoked and it will
290-
define a setup callback to run only for the current bundle. The bundle
291-
name is also added as a tag, allowing developers to run tests for
292-
specific bundles.
288+
Every describe block receives a name which is used as prefix for
289+
upcoming tests. Inside a block, `ExUnit.Callbacks.setup/1` may be
290+
invoked and it will define a setup callback to run only for the
291+
current block. The describe name is also added as a tag, allowing
292+
developers to run tests for specific blocks.
293293
294294
## Examples
295295
296296
defmodule StringTest do
297297
use ExUnit.Case, async: true
298298
299-
bundle "String.capitalize/1" do
299+
describe "String.capitalize/1" do
300300
test "first grapheme is in uppercase" do
301301
assert String.capitalize("hello") == "Hello"
302302
end
@@ -307,28 +307,56 @@ defmodule ExUnit.Case do
307307
end
308308
end
309309
310-
When using Mix, you can run all tests in a bundle as:
310+
When using Mix, you can run all tests in a describe block as:
311311
312-
mix test --only bundle:"String.capitalize/1"
312+
mix test --only describe:"String.capitalize/1"
313313
314+
Note describe blocks cannot be nested. Instead of relying on hierarchy
315+
for composition, developers should build on top of named setups. For
316+
example:
317+
318+
defmodule UserManagementTest do
319+
use ExUnit.Case, async: true
320+
321+
describe "when user is logged in and is an admin" do
322+
setup [:log_user_in, :set_type_to_admin]
323+
324+
test ...
325+
end
326+
327+
describe "when user is logged in and is a manager" do
328+
setup [:log_user_in, :set_type_to_manager]
329+
330+
test ...
331+
end
332+
333+
defp log_user_in(context) do
334+
# ...
335+
end
336+
end
337+
338+
By forbidding hierarchies in favor of named setups, it is straight-forward
339+
for the developer to glance at each describe block and know exactly the
340+
setup steps involved.
314341
"""
315-
defmacro bundle(message, do: block) do
342+
defmacro describe(message, do: block) do
316343
quote do
317-
if @ex_unit_bundle do
318-
raise "cannot call bundle/2 inside another bundle"
344+
if @ex_unit_describe do
345+
raise "cannot call describe/2 inside another describe. See the documentation " <>
346+
"for describe/2 on named setups and how to handle hierarchies"
319347
end
320348

321-
@ex_unit_bundle (case unquote(message) do
349+
@ex_unit_describe (case unquote(message) do
322350
msg when is_binary(msg) -> msg
323-
msg -> raise ArgumentError, "bundle name must be a string, got: #{inspect msg}"
351+
msg -> raise ArgumentError, "describe name must be a string, got: #{inspect msg}"
324352
end)
325-
Module.delete_attribute(__ENV__.module, :bundletag)
353+
Module.delete_attribute(__ENV__.module, :describetag)
326354

327355
try do
328356
unquote(block)
329357
after
330-
@ex_unit_bundle nil
331-
Module.delete_attribute(__ENV__.module, :bundletag)
358+
@ex_unit_describe nil
359+
Module.delete_attribute(__ENV__.module, :describetag)
332360
end
333361
end
334362
end
@@ -377,9 +405,9 @@ defmodule ExUnit.Case do
377405
tag = Module.get_attribute(mod, :tag)
378406
async = Module.get_attribute(mod, :ex_unit_async)
379407

380-
{name, bundle, bundletag} =
381-
if bundle = Module.get_attribute(mod, :ex_unit_bundle) do
382-
{:"#{type} #{bundle} #{name}", bundle, Module.get_attribute(mod, :bundletag)}
408+
{name, describe, describetag} =
409+
if describe = Module.get_attribute(mod, :ex_unit_describe) do
410+
{:"#{type} #{describe} #{name}", describe, Module.get_attribute(mod, :describetag)}
383411
else
384412
{:"#{type} #{name}", nil, []}
385413
end
@@ -389,11 +417,11 @@ defmodule ExUnit.Case do
389417
end
390418

391419
tags =
392-
(tags ++ tag ++ bundletag ++ moduletag)
420+
(tags ++ tag ++ describetag ++ moduletag)
393421
|> normalize_tags
394422
|> validate_tags
395423
|> Map.merge(%{line: line, file: file, registered: registered,
396-
async: async, bundle: bundle, type: type})
424+
async: async, describe: describe, type: type})
397425

398426
test = %ExUnit.Test{name: name, case: mod, tags: tags}
399427
Module.put_attribute(mod, :ex_unit_tests, test)

0 commit comments

Comments
 (0)