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

4

1 回答 1

2

缺少括号?尝试:

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
    ).

请注意,标准运算符定义是:

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

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

Wrt“将栏保持在绿色窗口中”,您可以使用 Logtalk 工具轻松完成此操作lgtunit(您也可以使用它来测试 Prolog 代码)。例如https://logtalk.org/files/blog/2019_11_06/xunit_report.htmlhttps://logtalk.org/2019/11/06/testing-multiple-implementations-of-a-protocol.htmlhttps:// /logtalk.org/2019/12/02/generating-code-coverage-reports.html进行介绍。

于 2020-04-07T11:42:23.803 回答