1
%% This library is free software; you can redistribute it and/or modify
2
%% it under the terms of the GNU Lesser General Public License as
3
%% published by the Free Software Foundation; either version 2 of the
4
%% License, or (at your option) any later version.
6
%% This library is distributed in the hope that it will be useful, but
7
%% WITHOUT ANY WARRANTY; without even the implied warranty of
8
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9
%% Lesser General Public License for more details.
11
%% You should have received a copy of the GNU Lesser General Public
12
%% License along with this library; if not, write to the Free Software
13
%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18
%% Copyright (C) 2004-2006 Micka�l R�mond, Richard Carlsson
20
%% Including this file turns on testing and defines TEST, unless NOTEST
21
%% is defined before the file is included. If both NOTEST and TEST are
22
%% already defined, then TEST takes precedence, and NOTEST will become
25
%% If including this file causes TEST to be defined, then NOASSERT will
26
%% be undefined, even if it was previously defined. If both ASSERT and
27
%% NOASSERT are defined before the file is included, then ASSERT takes
28
%% precedence, and NOASSERT will become undefined regardless of TEST.
30
%% After including this file, EUNIT will be defined if and only if TEST
34
-define(EUNIT_HRL, true).
36
%% allow defining TEST to override NOTEST
41
%% allow NODEBUG to imply NOASSERT, unless overridden below
44
-define(NOASSERT, true).
48
%% note that the main switch used within this file is NOTEST; however,
49
%% both TEST and EUNIT may be used to check whether testing is enabled
51
-undef(NOASSERT). % testing requires that assertions are enabled
62
%% allow ASSERT to override NOASSERT (regardless of TEST/NOTEST)
67
%% Parse transforms for automatic exporting/stripping of test functions.
68
%% (Note that although automatic stripping is convenient, it will make
69
%% the code dependent on this header file and the eunit_striptests
70
%% module for compilation, even when testing is switched off! Using
71
%% -ifdef(EUNIT) around all test code makes the program more portable.)
73
-ifndef(EUNIT_NOAUTO).
75
-compile({parse_transform, eunit_autoexport}).
77
-compile({parse_transform, eunit_striptests}).
81
%% All macros should be available even if testing is turned off, and
82
%% should preferably not require EUnit to be present at runtime.
84
%% We must use fun-call wrappers ((fun () -> ... end)()) to avoid
85
%% exporting local variables, and furthermore we only use variable names
86
%% prefixed with "__", that hopefully will not be bound outside the fun.
88
%% A generic let-macro is particularly useful when writing test cases.
89
%% It is more compact than 'begin X = Y, Z end', and guarantees that
90
%% X gets a new, local binding.
91
%% (Note that lowercase 'let' is a reserved word.)
93
-define(LET(X,Y,Z), ((fun(X)->(Z)end)(Y))).
96
%% It is important that testing code is short and readable.
97
%% An if-then-else macro can make some code much more compact.
98
%% Compare: case f(X) of true->g(X); false->h(X) end
99
%% and: ?IF(f(X), g(Y), h(Z))
101
-define(IF(B,T,F), (case (B) of true->(T); false->(F) end)).
105
%% The plain assert macro should be defined to do nothing if this file
106
%% is included when debugging/testing is turned off.
108
-define(assert(BoolExpr),ok).
111
%% The assert macro is written the way it is so as not to cause warnings
112
%% for clauses that cannot match, even if the expression is a constant.
114
-define(assert(BoolExpr),
118
__V -> .erlang:error({assertion_failed,
121
{expression, (??BoolExpr)},
123
{value, case __V of false -> __V;
124
_ -> {not_a_boolean,__V}
129
-define(assertNot(BoolExpr), ?assert(not (BoolExpr))).
131
-define(_test(Expr), {?LINE, fun () -> (Expr) end}).
133
-define(_assert(BoolExpr), ?_test(?assert(BoolExpr))).
135
-define(_assertNot(BoolExpr), ?_assert(not (BoolExpr))).
137
%% This is mostly a convenience which gives more detailed reports.
138
%% Note: Guard is a guarded pattern, and can not be used for value.
140
-define(assertMatch(Guard,Expr),ok).
142
-define(assertMatch(Guard, Expr),
146
__V -> .erlang:error({assertMatch_failed,
149
{expression, (??Expr)},
150
{expected, (??Guard)},
155
-define(_assertMatch(Guard, Expr), ?_test(?assertMatch(Guard, Expr))).
157
%% This is a convenience macro which gives more detailed reports when
158
%% the expected LHS value is not a pattern, but a computed value
160
-define(assertEqual(Expect,Expr),ok).
162
-define(assertEqual(Expect, Expr),
166
__V -> .erlang:error({assertEqual_failed,
169
{expression, (??Expr)},
175
-define(_assertEqual(Expect, Expr), ?_test(?assertEqual(Expect, Expr))).
177
%% Note: Class and Term are patterns, and can not be used for value.
179
-define(assertException(Class, Term, Expr),ok).
181
-define(assertException(Class, Term, Expr),
184
__V -> .erlang:error({assertException_failed,
187
{expression, (??Expr)},
189
"{ "++(??Class)++" , "++(??Term)
191
{unexpected_success, __V}]})
195
.erlang:error({assertException_failed,
198
{expression, (??Expr)},
200
"{ "++(??Class)++" , "++(??Term)
202
{unexpected_exception,
203
{__C, __T, erlang:get_stacktrace()}}]})
208
-define(assertError(Term, Expr), ?assertException(error, Term, Expr)).
209
-define(assertExit(Term, Expr), ?assertException(exit, Term, Expr)).
210
-define(assertThrow(Term, Expr), ?assertException(throw, Term, Expr)).
212
-define(_assertException(Class, Term, Expr),
213
?_test(?assertException(Class, Term, Expr))).
214
-define(_assertError(Term, Expr), ?_assertException(error, Term, Expr)).
215
-define(_assertExit(Term, Expr), ?_assertException(exit, Term, Expr)).
216
-define(_assertThrow(Term, Expr), ?_assertException(throw, Term, Expr)).
218
%% Macros for running operating system commands. (Note that these
219
%% require EUnit to be present at runtime, or at least eunit_lib.)
221
%% these can be used for simply running commands in a controlled way
222
-define(_cmd_(Cmd), (.eunit_lib:command(Cmd))).
223
-define(cmdStatus(N, Cmd),
226
{(N), __Out} -> __Out;
227
{__N, _} -> .erlang:error({command_failed,
231
{expected_status,(N)},
235
-define(_cmdStatus(N, Cmd), ?_test(?cmdStatus(N, Cmd))).
236
-define(cmd(Cmd), ?cmdStatus(0, Cmd)).
237
-define(_cmd(Cmd), ?_test(?cmd(Cmd))).
239
%% these are only used for testing; they always return 'ok' on success,
240
%% and have no effect if debugging/testing is turned off
242
-define(assertCmdStatus(N, Cmd),ok).
244
-define(assertCmdStatus(N, Cmd),
248
{__N, _} -> .erlang:error({assertCmd_failed,
252
{expected_status,(N)},
257
-define(assertCmd(Cmd), ?assertCmdStatus(0, Cmd)).
260
-define(assertCmdOutput(T, Cmd),ok).
262
-define(assertCmdOutput(T, Cmd),
266
{_, __T} -> .erlang:error({assertCmdOutput_failed,
270
{expected_output,(T)},
276
-define(_assertCmdStatus(N, Cmd), ?_test(?assertCmdStatus(N, Cmd))).
277
-define(_assertCmd(Cmd), ?_test(?assertCmd(Cmd))).
278
-define(_assertCmdOutput(T, Cmd), ?_test(?assertCmdOutput(T, Cmd))).
280
%% Macros to simplify debugging (in particular, they work even when the
281
%% standard output is being redirected by EUnit while running tests)
284
-define(debugMsg(S), ok).
285
-define(debugHere, ok).
286
-define(debugFmt(S, As), ok).
287
-define(debugVal(E), (E)).
288
-define(debugTime(S,E), (E)).
292
io:fwrite(user, <<"~s:~w: ~s\n">>,
296
-define(debugHere, (?debugMsg("<-"))).
297
-define(debugFmt(S, As), (?debugMsg(io_lib:format((S), (As))))).
300
?debugFmt(<<"~s = ~P">>, [(??E), __V, 15]),
303
-define(debugTime(S,E),
305
{__T0, _} = statistics(wall_clock),
307
{__T1, _} = statistics(wall_clock),
308
?debugFmt(<<"~s: ~.3f s">>, [(S), (__T1-__T0)/1000]),