Skip to content

Commit 97904a3

Browse files
committed
Suppress output from underlying assert_* during deliberate fail
1 parent ce7675c commit 97904a3

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

plunit_assert.pl

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
assert_test_fails/1,
2222
assert_test_passes/1
2323
]).
24+
:- multifile prolog:message//1.
2425
/** <module> The test API for plunit_assert
2526
2627
A unit testing library for Prolog, providing an expressive xUnit-like API for
@@ -382,9 +383,9 @@
382383
%; Type = unknown
383384
).
384385

385-
feedback(Format, Args) :-
386-
format(atom(Atom), Format, Args),
387-
format(user_error, '[plunit_assert] ~s', [Atom]).
386+
% feedback(Format, Args) :-
387+
% format(atom(Atom), Format, Args),
388+
% format(user_error, '[plunit_assert] ~s', [Atom]).
388389

389390
call_protected(Cond, Callback) :-
390391
setup_call_cleanup(
@@ -437,13 +438,42 @@
437438
% Meta test to check that Goal would trigger a PlUnit test fail
438439
%
439440
% @arg Goal The goal to be queried in the form of a plunit_assert predicate
441+
% assert_test_fails(Goal) :-
442+
% ( Goal
443+
% -> feedback('Asserted test failure but test passed: ~q', [Goal]),
444+
% fail
445+
% ; true
446+
% ).
447+
448+
440449
assert_test_fails(Goal) :-
441-
( Goal
450+
% Phase 1: silence all messages from the Goal
451+
setup_call_cleanup(
452+
asserta(
453+
(user:message_hook(plunit_assert(_), _, _) :- !, true),
454+
Ref
455+
),
456+
(Goal -> Success = true ; Success = false),
457+
erase(Ref)
458+
),
459+
% Phase 2: report if the Goal unexpectedly succeeded
460+
( Success == true
442461
-> feedback('Asserted test failure but test passed: ~q', [Goal]),
443462
fail
444463
; true
445464
).
446465

466+
467+
468+
469+
prolog:message(plunit_assert(Msg)) -->
470+
[ '[plunit_assert] ~w'-[Msg] ].
471+
472+
feedback(Format, Args) :-
473+
format(atom(Msg), Format, Args),
474+
print_message(error, plunit_assert(Msg)).
475+
476+
447477
%! assert_test_passes(:Goal) is semidet
448478
%
449479
% Meta test to check that Goal would not trigger a PlUnit test fail

0 commit comments

Comments
 (0)