Skip to content

Commit 689f626

Browse files
katiejotsmeta-codesync[bot]
authored andcommitted
Add enhanced assertNot macro
Summary: Extend the benefits of the WA `assert()` macro to `assertNot()`, overriding the standard OTP definition. Reviewed By: jcpetruzza Differential Revision: D95202529 fbshipit-source-id: 12f253a6afd13d0b199669a42829ec26986f3488
1 parent 21e3960 commit 689f626

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

include/assert.hrl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,87 @@ end).
279279
end).
280280
-endif.
281281

282+
-undef(assertNot).
283+
-ifdef(ELP_ERLANG_SERVICE).
284+
-define(assertNot(BoolExpr), begin
285+
(fun() ->
286+
case (BoolExpr) of
287+
false -> ok;
288+
_ -> erlang:error(assertion_failed)
289+
end
290+
end)()
291+
end).
292+
-else.
293+
-define(assertNot(BoolExpr), begin
294+
((fun() ->
295+
X__F = not is_process_alive(self()),
296+
#{bool_expr := BoolExpr__BoolExpr, meta := Meta__Meta} = wa_assert:'$expand_assert$'((BoolExpr)),
297+
case BoolExpr__BoolExpr of
298+
X__F ->
299+
ok;
300+
X__V ->
301+
R__R =
302+
{assert, [
303+
{module, ?MODULE},
304+
{line, ?LINE},
305+
{expression, (??BoolExpr)},
306+
{expected, false},
307+
case not X__F of
308+
X__V -> {value, true};
309+
_ -> {not_boolean, X__V}
310+
end
311+
]},
312+
case wa_assert:assert_error_info(??BoolExpr, Meta__Meta) of
313+
{ok, EI__EI} ->
314+
erlang:error(R__R, none, [{error_info, EI__EI}]);
315+
{error, no_error_info} ->
316+
erlang:error(R__R)
317+
end
318+
end
319+
end)())
320+
end).
321+
-endif.
322+
-ifdef(ELP_ERLANG_SERVICE).
323+
-define(assertNot(BoolExpr, Comment), begin
324+
(fun() ->
325+
case (BoolExpr) of
326+
false -> ok;
327+
_ -> erlang:error(wa_assert:maybe_format_comment(Comment))
328+
end
329+
end)()
330+
end).
331+
-else.
332+
-define(assertNot(BoolExpr, Comment), begin
333+
((fun() ->
334+
X__F = not is_process_alive(self()),
335+
#{bool_expr := BoolExpr__BoolExpr, meta := Meta__Meta} = wa_assert:'$expand_assert$'((BoolExpr)),
336+
case BoolExpr__BoolExpr of
337+
X__F ->
338+
ok;
339+
X__V ->
340+
R__R =
341+
{assert, [
342+
{module, ?MODULE},
343+
{line, ?LINE},
344+
{comment, wa_assert:maybe_format_comment((Comment))},
345+
{expression, (??BoolExpr)},
346+
{expected, false},
347+
case not X__F of
348+
X__V -> {value, true};
349+
_ -> {not_boolean, X__V}
350+
end
351+
]},
352+
case wa_assert:assert_error_info(??BoolExpr, Meta__Meta) of
353+
{ok, EI__EI} ->
354+
erlang:error(R__R, none, [{error_info, EI__EI}]);
355+
{error, no_error_info} ->
356+
erlang:error(R__R)
357+
end
358+
end
359+
end)())
360+
end).
361+
-endif.
362+
282363
-define(assertEqualSorted(ExpectedList, ActualList),
283364
?assertEqual(lists:sort(ExpectedList), lists:sort(ActualList))
284365
).

test/wa_assert_SUITE.erl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
assert_comparison/1,
3333
assert_map_comprehension/1,
3434
assert_call_with_bindings/1,
35+
assert_not_comparison_error_info/1,
3536
assert_greater_than/1,
3637
assert_less_than/1,
3738
assert_greater_than_or_equal/1,
@@ -76,6 +77,7 @@ all() ->
7677
assert_comparison,
7778
assert_map_comprehension,
7879
assert_call_with_bindings,
80+
assert_not_comparison_error_info,
7981
assert_greater_than,
8082
assert_less_than,
8183
assert_greater_than_or_equal,
@@ -191,6 +193,33 @@ assert_call_with_bindings(_Config) ->
191193
Expected = expected(),
192194
?assertException(error, {assert, _}, ?assert(lists:member(Expected, [actual()]))).
193195

196+
assert_not_comparison_error_info(_Config) ->
197+
X = 10,
198+
Y = 5,
199+
try
200+
?assertNot(X > Y)
201+
catch
202+
error:{assert, Props}:Stacktrace ->
203+
?assertEqual(false, proplists:get_value(expected, Props)),
204+
[{_M, _F, _Args, Info} | _] = Stacktrace,
205+
ErrorInfo = proplists:get_value(error_info, Info),
206+
#{cause := Cause, module := wa_assert, function := format_comparison_error} = ErrorInfo,
207+
#{left := 10, right := 5, operator := '>'} = Cause
208+
end,
209+
Items = [1, 2, 3],
210+
try
211+
?assertNot(length(Items) =:= 3)
212+
catch
213+
error:{assert, Props2}:Stacktrace2 ->
214+
?assertEqual(false, proplists:get_value(expected, Props2)),
215+
Intermediates = get_intermediates(Stacktrace2),
216+
?assert(has_intermediate("length(Items)", Intermediates)),
217+
?assertEqual(3, get_intermediate("length(Items)", Intermediates))
218+
end,
219+
?assertNot(false),
220+
?assertNot(1 > 2),
221+
?assertNot(lists:member(x, [a, b, c])).
222+
194223
%%--------------------------------------------------------------------
195224
%% Comparison Assertion Tests
196225
%%
@@ -419,6 +448,12 @@ format_comment_integration(_Config) ->
419448
?assertNotMatch({ok, _}, {ok, 42}, io_lib:format("unexpected ~p", [ok]))
420449
catch
421450
error:{assertNotMatch, P4} -> ?assertEqual("unexpected ok", proplists:get_value(comment, P4))
451+
end,
452+
%% assertNot with io_lib:format
453+
try
454+
?assertNot(true, io_lib:format("got ~p", [true]))
455+
catch
456+
error:{assert, P5} -> ?assertEqual("got true", proplists:get_value(comment, P5))
422457
end.
423458

424459
%%--------------------------------------------------------------------

0 commit comments

Comments
 (0)