1

Here is a little test:

:- begin_tests(tuturuu).

test(foo) :- Expected=[a,b],Got=[a,b,c],verdict(foo,Expected,Got).

% helper: transform lists A,B into ordered sets As,Bs

setify(A,B,As,Bs) :-      % A,B -> As,Bs
   list_to_ord_set(A,As), % A->As
   list_to_ord_set(B,Bs). % B->Bs

% did the test pass? set-compare Expected and Got

verdict(Value,Expected,Got) :-   % V,E,G -> b?
   setify(Expected,Got,Eos,Gos),  
   format("For ~w, expected ~w and got ~w\n",[Value,Eos,Gos]),
   ord_seteq(Eos,Gos)
   -> true ; (format("For ~w, expected ~w but got ~w\n",[Value,Eos,Gos]),fail).

:- end_tests(tuturuu).


rt :- run_tests(tuturuu).

When I load this into SWI-Prolog:

?- [foo].
Warning: /home/somone/foo.pl:13:
Warning:    Singleton variable in branch: Eos
Warning:    Singleton variable in branch: Gos
true.

The -> branch doesn't have access to the (local) variables Eos and Gos? But why?

It has access to Value because that one appears in the head I suppose.

And indeed:

?- rt.
% PL-Unit: tuturuu For foo, expected [a,b] and got [a,b,c]
For foo, expected _29026 but got _29032                       <--- YUP NO ACCESS
ERROR: /home/someone/foo.pl:3:
        test foo: failed

 done
% 1 test failed
% 0 tests passed
false.

It don't really see why this restriction occurs, expect that maybe it's a part that hasn't been implemented in the compiler?

(Btw, we need one of those "keep the bar in the green windows" for unit testing from the Java World, like this: (stolen from here)

junit test bar

David Tonhofer
  • 14,559
  • 5
  • 55
  • 51
  • Without running code. `"For ~w, expected ~w and got ~w\n"` should be `'For ~w, expected ~w and got ~w\n'`. Change `"`, to `'`. – Guy Coder Apr 07 '20 at 11:41
  • @GuyCoder Why the atom though? – David Tonhofer Apr 07 '20 at 11:59
  • Why the atom though? I can't remember when but for something I did before it worked better with atoms. The string works but sometimes you just do something because it causes you less pain. i.e. With your question and as Paulo notes, I always use `( ) ` with `-> ;`, even if they are not needed. In other programming languages when creating the conditions for `if` statements I always add all of the `( )` whether they are needed or not because I switch between languages often and it just saves me headaches. – Guy Coder Apr 07 '20 at 12:35

1 Answers1

2

Missing parenthesis? Try:

verdict(Value,Expected,Got) :-   % V,E,G -> b?
    setify(Expected,Got,Eos,Gos),  
    (   ord_seteq(Eos,Gos)
    ->  true
    ;   format("For ~w, expected ~w but got ~w\n",[Value,Eos,Gos]),
        fail
    ).

Note that the standard operator definitions are:

?- current_op(Priority, Type, ',').
Priority = 1000,
Type = xfy.

?- current_op(Priority, Type, '->').
Priority = 1050,
Type = xfy.

W.r.t. "keep the bar in the green windows", you can do it easily with Logtalk lgtunit tool (which you can also use to test Prolog code). E.g. https://logtalk.org/files/blog/2019_11_06/xunit_report.html See https://logtalk.org/2019/11/06/testing-multiple-implementations-of-a-protocol.html and https://logtalk.org/2019/12/02/generating-code-coverage-reports.html for an introduction.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Paulo Moura
  • 18,373
  • 3
  • 23
  • 33