1%% Licensed under the Apache License, Version 2.0 (the "License"); you may 2%% not use this file except in compliance with the License. You may obtain 3%% a copy of the License at <http://www.apache.org/licenses/LICENSE-2.0> 4%% 5%% Unless required by applicable law or agreed to in writing, software 6%% distributed under the License is distributed on an "AS IS" BASIS, 7%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 8%% See the License for the specific language governing permissions and 9%% limitations under the License. 10%% 11%% Alternatively, you may use this file under the terms of the GNU Lesser 12%% General Public License (the "LGPL") as published by the Free Software 13%% Foundation; either version 2.1, or (at your option) any later version. 14%% If you wish to allow use of your version of this file only under the 15%% terms of the LGPL, you should delete the provisions above and replace 16%% them with the notice and other provisions required by the LGPL; see 17%% <http://www.gnu.org/licenses/>. If you do not delete the provisions 18%% above, a recipient may use your version of this file under the terms of 19%% either the Apache License or the LGPL. 20%% 21%% NOTE: An object file that uses the macros in this header file shall 22%% never be considered a derived work under the the LGPL; these macros 23%% shall be regarded as "small" regardless of the exact line count. 24%% 25%% Copyright (C) 2004-2006 Mickaël Rémond, Richard Carlsson 26 27-ifndef(EUNIT_HRL). 28-define(EUNIT_HRL, true). 29 30%% Including this file turns on testing and defines TEST, unless NOTEST 31%% is defined before the file is included. If both NOTEST and TEST are 32%% already defined, then TEST takes precedence, and NOTEST will become 33%% undefined. 34%% 35%% If NODEBUG is defined before this file is included, the debug macros 36%% are disabled, unless DEBUG is also defined, in which case NODEBUG 37%% will become undefined. NODEBUG also implies NOASSERT, unless testing 38%% is enabled. 39%% 40%% Defining NOASSERT disables asserts. NODEBUG implies NOASSERT unless 41%% testing is enabled. If including this file causes TEST to be defined, 42%% then NOASSERT will be undefined, even if it was previously defined and 43%% even if NODEBUG is defined. If both ASSERT and NOASSERT are defined 44%% before the file is included, then ASSERT takes precedence, and NOASSERT 45%% will become undefined regardless of TEST. 46%% 47%% After including this file, EUNIT will be defined if and only if TEST 48%% is defined. 49 50%% allow defining TEST to override NOTEST 51-ifdef(TEST). 52-undef(NOTEST). 53-endif. 54 55%% allow defining DEBUG to override NODEBUG 56-ifdef(DEBUG). 57-undef(NODEBUG). 58-endif. 59 60%% note that the main switch used within this file is NOTEST; however, 61%% both TEST and EUNIT may be used to check whether testing is enabled 62-ifndef(NOTEST). 63-ifndef(ASSERT). 64-define(ASSERT, true). % testing requires that assertions are enabled 65-endif. 66-ifndef(TEST). 67-define(TEST, true). 68-endif. 69-ifndef(EUNIT). 70-define(EUNIT, true). 71-endif. 72-else. 73-undef(EUNIT). 74-endif. 75 76%% include the assert macros; ASSERT overrides NOASSERT if defined 77-include_lib("stdlib/include/assert.hrl"). 78 79%% Parse transforms for automatic exporting/stripping of test functions. 80%% (Note that although automatic stripping is convenient, it will make 81%% the code dependent on this header file and the eunit_striptests 82%% module for compilation, even when testing is switched off! Using 83%% -ifdef(EUNIT) around all test code makes the program more portable.) 84 85-ifndef(EUNIT_NOAUTO). 86-ifndef(NOTEST). 87-compile({parse_transform, eunit_autoexport}). 88-else. 89-compile({parse_transform, eunit_striptests}). 90-endif. 91-endif. 92 93%% All macros should be available even if testing is turned off, and 94%% should preferably not require EUnit to be present at runtime. 95%% 96%% We must use fun-call wrappers ((fun () -> ... end)()) to avoid 97%% exporting local variables, and furthermore we only use variable names 98%% prefixed with "__", that hopefully will not be bound outside the fun. 99 100%% A generic let-macro is particularly useful when writing test cases. 101%% It is more compact than 'begin X = Y, Z end', and guarantees that 102%% X gets a new, local binding. 103%% (Note that lowercase 'let' is a reserved word.) 104-ifndef(LET). 105-define(LET(X,Y,Z), begin ((fun(X)->(Z)end)(Y)) end). 106-endif. 107 108%% It is important that testing code is short and readable. 109%% An if-then-else macro can make some code much more compact. 110%% Compare: case f(X) of true->g(X); false->h(X) end 111%% and: ?IF(f(X), g(Y), h(Z)) 112-ifndef(IF). 113-define(IF(B,T,F), begin (case (B) of true->(T); false->(F) end) end). 114-endif. 115 116%% This macro yields 'true' if the value of E matches the guarded 117%% pattern G, otherwise 'false'. 118-ifndef(MATCHES). 119-define(MATCHES(G,E), begin (case (E) of G -> true; _ -> false end) end). 120-endif. 121 122%% This macro can be used at any time to check whether or not the code 123%% is currently running directly under eunit. Note that it does not work 124%% in secondary processes if they have been assigned a new group leader. 125-ifndef(UNDER_EUNIT). 126-define(UNDER_EUNIT, 127 (?MATCHES({current_function,{eunit_proc,_,_}}, 128 erlang:process_info(erlang:group_leader(), 129 current_function)))). 130-endif. 131 132%% General test macros 133 134-define(_test(Expr), {?LINE, fun () -> (Expr) end}). 135-define(_assert(BoolExpr), ?_test(?assert(BoolExpr))). 136-define(_assertNot(BoolExpr), ?_assert(not (BoolExpr))). 137-define(_assertMatch(Guard, Expr), ?_test(?assertMatch(Guard, Expr))). 138-define(_assertNotMatch(Guard, Expr), ?_test(?assertNotMatch(Guard, Expr))). 139-define(_assertEqual(Expect, Expr), ?_test(?assertEqual(Expect, Expr))). 140-define(_assertNotEqual(Unexpected, Expr), 141 ?_test(?assertNotEqual(Unexpected, Expr))). 142-define(_assertException(Class, Term, Expr), 143 ?_test(?assertException(Class, Term, Expr))). 144-define(_assertError(Term, Expr), ?_assertException(error, Term, Expr)). 145-define(_assertExit(Term, Expr), ?_assertException(exit, Term, Expr)). 146-define(_assertThrow(Term, Expr), ?_assertException(throw, Term, Expr)). 147-define(_assertNotException(Class, Term, Expr), 148 ?_test(?assertNotException(Class, Term, Expr))). 149 150%% Macros for retrieving the output of a test case 151 152-ifndef(capturedOutput). 153-define(capturedOutput, 154 case ?UNDER_EUNIT of 155 true -> eunit_proc:get_output(); 156 false -> "" 157 end). 158-endif. 159 160%% Macros for running operating system commands. (Note that these 161%% require EUnit to be present at runtime, or at least eunit_lib.) 162 163%% these can be used for simply running commands in a controlled way 164-define(_cmd_(Cmd), (eunit_lib:command(Cmd))). 165-define(cmdStatus(N, Cmd), 166 begin 167 ((fun () -> 168 case ?_cmd_(Cmd) of 169 {(N), __Out} -> __Out; 170 {__N, _} -> erlang:error({command_failed, 171 [{module, ?MODULE}, 172 {line, ?LINE}, 173 {command, (Cmd)}, 174 {expected_status,(N)}, 175 {status,__N}]}) 176 end 177 end)()) 178 end). 179-define(_cmdStatus(N, Cmd), ?_test(?cmdStatus(N, Cmd))). 180-define(cmd(Cmd), ?cmdStatus(0, Cmd)). 181-define(_cmd(Cmd), ?_test(?cmd(Cmd))). 182 183%% these are only used for testing; they always return 'ok' on success, 184%% and have no effect if debugging/testing is turned off 185-ifdef(NOASSERT). 186-define(assertCmdStatus(N, Cmd), ok). 187-else. 188-define(assertCmdStatus(N, Cmd), 189 begin 190 ((fun () -> 191 case ?_cmd_(Cmd) of 192 {(N), _} -> ok; 193 {__N, _} -> erlang:error({assertCmd_failed, 194 [{module, ?MODULE}, 195 {line, ?LINE}, 196 {command, (Cmd)}, 197 {expected_status,(N)}, 198 {status,__N}]}) 199 end 200 end)()) 201 end). 202-endif. 203-define(assertCmd(Cmd), ?assertCmdStatus(0, Cmd)). 204 205-ifdef(NOASSERT). 206-define(assertCmdOutput(T, Cmd), ok). 207-else. 208-define(assertCmdOutput(T, Cmd), 209 begin 210 ((fun () -> 211 case ?_cmd_(Cmd) of 212 {_, (T)} -> ok; 213 {_, __T} -> erlang:error({assertCmdOutput_failed, 214 [{module, ?MODULE}, 215 {line, ?LINE}, 216 {command,(Cmd)}, 217 {expected_output,(T)}, 218 {output,__T}]}) 219 end 220 end)()) 221 end). 222-endif. 223 224-define(_assertCmdStatus(N, Cmd), ?_test(?assertCmdStatus(N, Cmd))). 225-define(_assertCmd(Cmd), ?_test(?assertCmd(Cmd))). 226-define(_assertCmdOutput(T, Cmd), ?_test(?assertCmdOutput(T, Cmd))). 227 228%% Macros to simplify debugging (in particular, they work even when the 229%% standard output is being redirected by EUnit while running tests) 230 231-ifdef(NODEBUG). 232-define(debugMsg(S), ok). 233-define(debugHere, ok). 234-define(debugFmt(S, As), ok). 235-define(debugVal(E), (E)). 236-define(debugValAll(E), (E)). 237-define(debugTime(S, E), (E)). 238-else. 239-define(debugMsg(S), 240 begin 241 io:fwrite(user, <<"~ts:~w:~w: ~ts\n">>, 242 [?FILE, ?LINE, self(), S]), 243 ok 244 end). 245-define(debugHere, (?debugMsg("<-"))). 246-define(debugFmt(S, As), (?debugMsg(io_lib:format((S), (As))))). 247-define(debugVal(E, D), 248 begin 249 ((fun (__V) -> 250 ?debugFmt(<<"~ts = ~tP">>, 251 [(??E), __V, D]), 252 __V 253 end)(E)) 254 end). 255-ifndef(EUNIT_DEBUG_VAL_DEPTH). 256-define(EUNIT_DEBUG_VAL_DEPTH, 15). 257-endif. 258-define(debugVal(E), ?debugVal(E, ?EUNIT_DEBUG_VAL_DEPTH)). 259-define(debugTime(S, E), 260 begin 261 ((fun () -> 262 {__T0, _} = statistics(wall_clock), 263 __V = (E), 264 {__T1, _} = statistics(wall_clock), 265 ?debugFmt(<<"~ts: ~.3f s">>, [(S), (__T1-__T0)/1000]), 266 __V 267 end)()) 268 end). 269-endif. 270 271-endif. % EUNIT_HRL 272