From 3011d16bfcff0dbdd369d4701751ab106dde100f Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Sat, 8 Jun 2013 13:17:11 +0200 Subject: Wrap eunit macros into begin ... end blocks This makes typos such as missing commas between two assertions (e.g. ?assert(true) ?assert(true)) syntax errors instead of silently compiling and failing with a badfun error at runtime. This won't break any existing code as parenthesed expressions and blocks have the same precedence and none of these macros can be used as patterns. --- lib/eunit/include/eunit.hrl | 64 +++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 20 deletions(-) (limited to 'lib/eunit') diff --git a/lib/eunit/include/eunit.hrl b/lib/eunit/include/eunit.hrl index 8ebdb6ba16..84242a09aa 100644 --- a/lib/eunit/include/eunit.hrl +++ b/lib/eunit/include/eunit.hrl @@ -102,7 +102,7 @@ %% X gets a new, local binding. %% (Note that lowercase 'let' is a reserved word.) -ifndef(LET). --define(LET(X,Y,Z), ((fun(X)->(Z)end)(Y))). +-define(LET(X,Y,Z), begin ((fun(X)->(Z)end)(Y)) end). -endif. %% It is important that testing code is short and readable. @@ -110,13 +110,13 @@ %% Compare: case f(X) of true->g(X); false->h(X) end %% and: ?IF(f(X), g(Y), h(Z)) -ifndef(IF). --define(IF(B,T,F), (case (B) of true->(T); false->(F) end)). +-define(IF(B,T,F), begin (case (B) of true->(T); false->(F) end) end). -endif. %% This macro yields 'true' if the value of E matches the guarded %% pattern G, otherwise 'false'. -ifndef(MATCHES). --define(MATCHES(G,E), (case (E) of G -> true; _ -> false end)). +-define(MATCHES(G,E), begin (case (E) of G -> true; _ -> false end) end). -endif. %% This macro can be used at any time to check whether or not the code @@ -140,6 +140,7 @@ %% for clauses that cannot match, even if the expression is a constant. -undef(assert). -define(assert(BoolExpr), + begin ((fun () -> case (BoolExpr) of true -> ok; @@ -152,7 +153,8 @@ _ -> {not_a_boolean,__V} end}]}) end - end)())). + end)()) + end). -endif. -define(assertNot(BoolExpr), ?assert(not (BoolExpr))). @@ -168,6 +170,7 @@ -define(assertMatch(Guard, Expr), ok). -else. -define(assertMatch(Guard, Expr), + begin ((fun () -> case (Expr) of Guard -> ok; @@ -178,7 +181,8 @@ {pattern, (??Guard)}, {value, __V}]}) end - end)())). + end)()) + end). -endif. -define(_assertMatch(Guard, Expr), ?_test(?assertMatch(Guard, Expr))). @@ -187,6 +191,7 @@ -define(assertNotMatch(Guard, Expr), ok). -else. -define(assertNotMatch(Guard, Expr), + begin ((fun () -> __V = (Expr), case __V of @@ -198,7 +203,8 @@ {value, __V}]}); _ -> ok end - end)())). + end)()) + end). -endif. -define(_assertNotMatch(Guard, Expr), ?_test(?assertNotMatch(Guard, Expr))). @@ -208,6 +214,7 @@ -define(assertEqual(Expect, Expr), ok). -else. -define(assertEqual(Expect, Expr), + begin ((fun (__X) -> case (Expr) of __X -> ok; @@ -218,7 +225,8 @@ {expected, __X}, {value, __V}]}) end - end)(Expect))). + end)(Expect)) + end). -endif. -define(_assertEqual(Expect, Expr), ?_test(?assertEqual(Expect, Expr))). @@ -227,6 +235,7 @@ -define(assertNotEqual(Unexpected, Expr), ok). -else. -define(assertNotEqual(Unexpected, Expr), + begin ((fun (__X) -> case (Expr) of __X -> erlang:error({assertNotEqual_failed, @@ -236,7 +245,8 @@ {value, __X}]}); _ -> ok end - end)(Unexpected))). + end)(Unexpected)) + end). -endif. -define(_assertNotEqual(Unexpected, Expr), ?_test(?assertNotEqual(Unexpected, Expr))). @@ -247,6 +257,7 @@ -define(assertException(Class, Term, Expr), ok). -else. -define(assertException(Class, Term, Expr), + begin ((fun () -> try (Expr) of __V -> erlang:error({assertException_failed, @@ -271,7 +282,8 @@ {__C, __T, erlang:get_stacktrace()}}]}) end - end)())). + end)()) + end). -endif. -define(assertError(Term, Expr), ?assertException(error, Term, Expr)). @@ -291,6 +303,7 @@ -define(assertNotException(Class, Term, Expr), ok). -else. -define(assertNotException(Class, Term, Expr), + begin ((fun () -> try (Expr) of _ -> ok @@ -316,7 +329,8 @@ _ -> ok end end - end)())). + end)()) + end). -endif. -define(_assertNotException(Class, Term, Expr), ?_test(?assertNotException(Class, Term, Expr))). @@ -327,6 +341,7 @@ %% these can be used for simply running commands in a controlled way -define(_cmd_(Cmd), (eunit_lib:command(Cmd))). -define(cmdStatus(N, Cmd), + begin ((fun () -> case ?_cmd_(Cmd) of {(N), __Out} -> __Out; @@ -337,7 +352,8 @@ {expected_status,(N)}, {status,__N}]}) end - end)())). + end)()) + end). -define(_cmdStatus(N, Cmd), ?_test(?cmdStatus(N, Cmd))). -define(cmd(Cmd), ?cmdStatus(0, Cmd)). -define(_cmd(Cmd), ?_test(?cmd(Cmd))). @@ -348,6 +364,7 @@ -define(assertCmdStatus(N, Cmd), ok). -else. -define(assertCmdStatus(N, Cmd), + begin ((fun () -> case ?_cmd_(Cmd) of {(N), _} -> ok; @@ -358,7 +375,8 @@ {expected_status,(N)}, {status,__N}]}) end - end)())). + end)()) + end). -endif. -define(assertCmd(Cmd), ?assertCmdStatus(0, Cmd)). @@ -366,6 +384,7 @@ -define(assertCmdOutput(T, Cmd), ok). -else. -define(assertCmdOutput(T, Cmd), + begin ((fun () -> case ?_cmd_(Cmd) of {_, (T)} -> ok; @@ -376,7 +395,8 @@ {expected_output,(T)}, {output,__T}]}) end - end)())). + end)()) + end). -endif. -define(_assertCmdStatus(N, Cmd), ?_test(?assertCmdStatus(N, Cmd))). @@ -394,26 +414,30 @@ -define(debugTime(S, E), (E)). -else. -define(debugMsg(S), - (begin - io:fwrite(user, <<"~s:~w:~w: ~s\n">>, - [?FILE, ?LINE, self(), S]), - ok - end)). + begin + io:fwrite(user, <<"~s:~w:~w: ~s\n">>, + [?FILE, ?LINE, self(), S]), + ok + end). -define(debugHere, (?debugMsg("<-"))). -define(debugFmt(S, As), (?debugMsg(io_lib:format((S), (As))))). -define(debugVal(E), + begin ((fun (__V) -> ?debugFmt(<<"~s = ~P">>, [(??E), __V, 15]), __V - end)(E))). + end)(E)) + end). -define(debugTime(S, E), + begin ((fun () -> {__T0, _} = statistics(wall_clock), __V = (E), {__T1, _} = statistics(wall_clock), ?debugFmt(<<"~s: ~.3f s">>, [(S), (__T1-__T0)/1000]), __V - end)())). + end)()) + end). -endif. -- cgit v1.2.3