assert.hrl(3)
π assert.hrl β the assert macros, bestie edition π
okay so this is the page for assert.hrl(3) from the Erlang Module Definition corner of the manual, and honestly? it's a bop. it's an include file that hands you a whole wardrobe of macros for dropping assertions into your code. let's get into it π«Ά
β¨ NAME β¨
assert.hrl β Assert macros. that's it, that's the name, short and iconic π
πΈ DESCRIPTION πΈ
The include file assert.hrl provides macros for inserting assertions in your program code. yes bestie, actual assertions, inside your code, no cap.
Include the following directive in the module from which the function is called:
-include_lib("stdlib/include/assert.hrl").
When an assertion succeeds, the assert macro yields the atom ok. slay. when an assertion fails, an exception of type error is generated β like, full drama, exception energy π.
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. lowkey useful for debugging β¨
heads up though: all entries in the Info list are optional β do not rely programmatically on any of them being present. fr fr, none of them are guaranteed. treat them like a surprise gift, not a contract π
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"). yes you can leave little love notes for future-you, obsessed π
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. zero. none. free real estate π
For example, using erlc to compile your modules, the following disables all assertions:
erlc -DNOASSERT=true *.erl
(The value of NOASSERT does not matter, only the fact that it is defined.) so true, false, banana, doesn't matter bestie, the vibe is "defined or not" π
A few other macros also have effect on the enabling or disabling of assertions:
- If
NODEBUGis defined, it impliesNOASSERT(unlessDEBUGis also defined, which overridesNODEBUG). - If
ASSERTis defined, it overridesNOASSERT, 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. pick your fighter, both are valid π
β¨ Macros β¨
assert(BoolExpr)/assert(BoolExpr, Comment)β tests thatBoolExprcompletes normally returningtrue.assertNot(BoolExpr)/assertNot(BoolExpr, Comment)β tests thatBoolExprcompletes normally returningfalse.assertMatch(GuardedPattern, Expr)/assertMatch(GuardedPattern, Expr, Comment)β tests thatExprcompletes normally yielding a value that matchesGuardedPattern, for example:
?assertMatch({bork, _}, f())
Notice that a guard when ... can be included:
?assertMatch({bork, X} when X > 0, f())
assertNotMatch(GuardedPattern, Expr)/assertNotMatch(GuardedPattern, Expr, Comment)β tests thatExprcompletes normally yielding a value that does not matchGuardedPattern.
As in assertMatch, GuardedPattern can have a when part.
assertEqual(ExpectedValue, Expr)/assertEqual(ExpectedValue, Expr, Comment)β tests thatExprcompletes normally yielding a value that is exactly equal toExpectedValue.assertNotEqual(ExpectedValue, Expr)/assertNotEqual(ExpectedValue, Expr, Comment)β tests thatExprcompletes normally yielding a value that is not exactly equal toExpectedValue.assertException(Class, Term, Expr)/assertException(Class, Term, Expr, Comment)β tests thatExprcompletes abnormally with an exception of typeClassand with the associatedTerm. The assertion fails ifExprraises 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 thatExprdoes not evaluate abnormally with an exception of typeClassand with the associatedTerm. The assertion succeeds ifExprraises 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 toassertException(error, Term, Expr).assertExit(Term, Expr)/assertExit(Term, Expr, Comment)β equivalent toassertException(exit, Term, Expr).assertThrow(Term, Expr)/assertThrow(Term, Expr, Comment)β equivalent toassertException(throw, Term, Expr).
π¦ See Also π¦
compile, erlc(3)
and that's the whole page bestie!! πΈπ thank you for reading, you're doing amazing sweetie. this one was brought to you by Ericsson AB, riding with stdlib 8.0.4, and the man page is literally called assert.hrl(3). go assert something (or don't, -DNOASSERT=true is right there and honestly valid) β¨π
ππ«Ά
π¦ where this page comes from
- ubuntu stonking: erlang-doc 1:29.0.6+dfsg-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms