Skip to content

Commit b6d4a68

Browse files
committed
Evaluate arithmetic in assert_not_equals/2. Fixes #21
1 parent cafe809 commit b6d4a68

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

plunit_assert.pl

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,17 @@
7676
% @arg B The second of the terms to be compared
7777
% @see assert_equals/2
7878
assert_not_equals(A, B) :-
79-
call_protected(A \= B, fail_assert_not_equals(A, B)).
79+
call_protected(\+ equal_after_eval(A, B),
80+
fail_assert_not_equals(A, B)).
81+
82+
% this differs somewhat from assert_equals/2 at the moment, but this general
83+
% approach may be more suitable once we start comparing compound terms etc
84+
equal_after_eval(A, B) :-
85+
catch(ValA is A, _, fail),
86+
catch(ValB is B, _, fail),
87+
ValA =:= ValB, !.
88+
equal_after_eval(A, B) :-
89+
A == B.
8090

8191
fail_assert_not_equals(A, B) :-
8292
feedback('Asserted ~q and ~q are not equal, but they are', [A, B]).
@@ -278,7 +288,8 @@
278288
assert_output(Goal, Vars, Expected) :-
279289
call(Goal),
280290
find_values(Vars, Actual),
281-
call_protected(Actual == Expected, fail_assert_output(Expected, Actual)).
291+
call_protected(Actual == Expected, fail_assert_output(Expected, Actual)),
292+
!.
282293

283294
find_values([], []).
284295
find_values([V|Vs], [Val|Vals]) :-
@@ -313,8 +324,7 @@
313324

314325
feedback(Format, Args) :-
315326
format(atom(Atom), Format, Args),
316-
format(user_error, '[plunit_assert] ~s', [Atom]),
317-
nl(user_error).
327+
format(user_error, '[plunit_assert] ~s', [Atom]).
318328

319329
call_protected(Cond, Callback) :-
320330
setup_call_cleanup(
@@ -342,7 +352,7 @@
342352
% @arg Goal The goal to be queried in the form of a plunit_assert predicate
343353
assert_test_fails(Goal) :-
344354
( Goal
345-
-> format('[assert_test_fails] Expected failure but succeeded: ~q~n', [Goal]),
355+
-> feedback('Asserted test failure but test passed: ~q', [Goal]),
346356
fail
347357
; true
348358
).

tests/test_pa.pl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
!.
1919

2020
test(pa_equals) :-
21-
assert_equals(3, 3),
22-
assert_equals(dog, dog),
21+
assert_test_passes(assert_equals(3, 3)),
22+
assert_test_passes(assert_equals(dog, dog)),
23+
assert_test_passes(assert_equals(14, 7*2)),
2324
assert_test_fails(assert_equals(7, 6)),
2425
assert_test_fails(assert_equals(dog, cat)),
2526
!.
@@ -30,7 +31,7 @@
3031
test(pa_not_equals) :- assert_test_passes(assert_not_equals(dog, cat)).
3132
test(pa_not_equals) :- assert_test_fails(assert_not_equals(7, 7)).
3233
test(pa_not_equals) :- assert_test_fails(assert_not_equals(dog, dog)).
33-
% TODO See #21 test(pa_not_equals) :- assert_test_fails(assert_not_equals(9, 6+3)).
34+
test(pa_not_equals_bug_21) :- assert_test_fails(assert_not_equals(9, 6+3)).
3435

3536
test(pa_identity) :-
3637
assert_is(foo(cat, 3), foo(cat, 3)),

0 commit comments

Comments
 (0)