Ветка импликации не видит переменных - PullRequest
1 голос
/ 07 апреля 2020

Вот небольшой тест:

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

Когда я загружаю его в SWI-Prolog:

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

Ветвь -> не имеет доступа к (локальной ) переменные Eos и Gos? Но почему?

У него есть доступ к Value, потому что тот появляется в голове, я полагаю.

И действительно:

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

Это на самом деле не видно почему возникает такое ограничение, ожидайте, что, возможно, это часть, которая не была реализована в компиляторе?

(Кстати, нам нужен один из тех, которые "держат полосу в зеленом windows" для модульного тестирования из Мир Java, вот так: (украден у здесь )

junit test bar

1 Ответ

2 голосов
/ 07 апреля 2020

Отсутствует скобка? Попробуйте:

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.

, если "держать полосу в зеленом windows", вы можете легко сделать это с помощью Logtalk lgtunit инструмент (который вы также можете использовать для тестирования кода Пролог). Например, https://logtalk.org/files/blog/2019_11_06/xunit_report.html См. https://logtalk.org/2019/11/06/testing-multiple-implementations-of-a-protocol.html и https://logtalk.org/2019/12/02/generating-code-coverage-reports.html для ознакомления.

...