File: assert_hrl.md

package info (click to toggle)
erlang 1%3A27.3.4.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 225,000 kB
  • sloc: erlang: 1,658,966; ansic: 405,769; cpp: 177,850; xml: 82,435; makefile: 15,031; sh: 14,401; lisp: 9,812; java: 8,603; asm: 6,541; perl: 5,836; python: 5,484; sed: 72
file content (156 lines) | stat: -rw-r--r-- 5,378 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!--
%CopyrightBegin%

Copyright Ericsson AB 2023-2024. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

%CopyrightEnd%
-->
# assert.hrl

Assert macros.

## Description

The include file `assert.hrl` provides macros for inserting assertions in your
program code.

Include the following directive in the module from which the function is called:

```erlang
-include_lib("stdlib/include/assert.hrl").
```

When an assertion succeeds, the assert macro yields the atom `ok`. When an
assertion fails, an exception of type `error` is generated. The associated error
term has the form `{Macro, Info}`. `Macro` is the macro name, for example,
`assertEqual`. `Info` is a list of tagged values, such as
`[{module, M}, {line, L}, ...]`, which gives more information about the location
and cause of the exception. All entries in the `Info` list are optional; do not
rely programmatically on any of them being present.

Each assert macro has a corresponding version with an extra argument, for adding
comments to assertions. These can for example be printed as part of error
reports, to clarify the meaning of the check that failed. For example,
`?assertEqual(0, fib(0), "Fibonacci is defined for zero")`. The comment text can
be any character data (string, UTF8-binary, or deep list of such data), and will
be included in the error term as `{comment, Text}`.

If the macro `NOASSERT` is defined when `assert.hrl` is read by the compiler,
the macros are defined as equivalent to the atom `ok`. The test will not be
performed and there is no cost at runtime.

For example, using `erlc` to compile your modules, the following disables all
assertions:

```text
erlc -DNOASSERT=true *.erl
```

(The value of `NOASSERT` does not matter, only the fact that it is defined.)

A few other macros also have effect on the enabling or disabling of assertions:

- If `NODEBUG` is defined, it implies `NOASSERT` (unless `DEBUG` is also
  defined, which overrides `NODEBUG`).
- If `ASSERT` is defined, it overrides `NOASSERT`, that is, the assertions
  remain enabled.

If you prefer, you can thus use only `DEBUG`/`NODEBUG` as the main flags to
control the behavior of the assertions (which is useful if you have other
compiler conditionals or debugging macros controlled by those flags), or you can
use `ASSERT`/`NOASSERT` to control only the assert macros.

## Macros

- **`assert(BoolExpr)`**

- **`assert(BoolExpr, Comment)`** - Tests that `BoolExpr` completes normally
  returning `true`.

- **`assertNot(BoolExpr)`**

- **`assertNot(BoolExpr, Comment)`** - Tests that `BoolExpr` completes normally
  returning `false`.

- **`assertMatch(GuardedPattern, Expr)`**

- **`assertMatch(GuardedPattern, Expr, Comment)`** - Tests that `Expr` completes
  normally yielding a value that matches `GuardedPattern`, for example:

  ```text
  ?assertMatch({bork, _}, f())
  ```

  Notice that a guard `when ...` can be included:

  ```erlang
  ?assertMatch({bork, X} when X > 0, f())
  ```

- **`assertNotMatch(GuardedPattern, Expr)`**

- **`assertNotMatch(GuardedPattern, Expr, Comment)`** - Tests that `Expr`
  completes normally yielding a value that does not match `GuardedPattern`.

  As in `assertMatch`, `GuardedPattern` can have a `when` part.

- **`assertEqual(ExpectedValue, Expr)`**

- **`assertEqual(ExpectedValue, Expr, Comment)`** - Tests that `Expr` completes
  normally yielding a value that is exactly equal to `ExpectedValue`.

- **`assertNotEqual(ExpectedValue, Expr)`**

- **`assertNotEqual(ExpectedValue, Expr, Comment)`** - Tests that `Expr`
  completes normally yielding a value that is not exactly equal to
  `ExpectedValue`.

- **`assertException(Class, Term, Expr)`**

- **`assertException(Class, Term, Expr, Comment)`** - Tests that `Expr`
  completes abnormally with an exception of type `Class` and with the associated
  `Term`. The assertion fails if `Expr` raises a different exception or if it
  completes normally returning any value.

  Notice that both `Class` and `Term` can be guarded patterns, as in
  `assertMatch`.

- **`assertNotException(Class, Term, Expr)`**

- **`assertNotException(Class, Term, Expr, Comment)`** - Tests that `Expr` does
  not evaluate abnormally with an exception of type `Class` and with the
  associated `Term`. The assertion succeeds if `Expr` raises a different
  exception or if it completes normally returning any value.

  As in `assertException`, both `Class` and `Term` can be guarded patterns.

- **`assertError(Term, Expr)`**

- **`assertError(Term, Expr, Comment)`** - Equivalent to
  `assertException(error, Term, Expr)`

- **`assertExit(Term, Expr)`**

- **`assertExit(Term, Expr, Comment)`** - Equivalent to
  `assertException(exit, Term, Expr)`

- **`assertThrow(Term, Expr)`**

- **`assertThrow(Term, Expr, Comment)`** - Equivalent to
  `assertException(throw, Term, Expr)`

## See Also

`m:compile`, [`erlc(3)`](`e:erts:erlc_cmd.md`)