|
21 | 21 | assert_test_fails/1, |
22 | 22 | assert_test_passes/1 |
23 | 23 | ]). |
| 24 | +:- multifile prolog:message//1. |
24 | 25 | /** <module> The test API for plunit_assert |
25 | 26 |
|
26 | 27 | A unit testing library for Prolog, providing an expressive xUnit-like API for |
|
382 | 383 | %; Type = unknown |
383 | 384 | ). |
384 | 385 |
|
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]). |
388 | 389 |
|
389 | 390 | call_protected(Cond, Callback) :- |
390 | 391 | setup_call_cleanup( |
|
437 | 438 | % Meta test to check that Goal would trigger a PlUnit test fail |
438 | 439 | % |
439 | 440 | % @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 | + |
440 | 449 | 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 |
442 | 461 | -> feedback('Asserted test failure but test passed: ~q', [Goal]), |
443 | 462 | fail |
444 | 463 | ; true |
445 | 464 | ). |
446 | 465 |
|
| 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 | + |
447 | 477 | %! assert_test_passes(:Goal) is semidet |
448 | 478 | % |
449 | 479 | % Meta test to check that Goal would not trigger a PlUnit test fail |
|
0 commit comments