Unit Tests in SWI-Prolog: Visibility of User Predicates from within a Module

别等时光非礼了梦想. 提交于 2019-12-24 10:48:41

问题


I want to write unit tests in SWI-Prolog (version 7.6.4) in order to streamline and automate testing, which is currently done only in a manual, ad-hoc fashion.

The files to be tested contain complex algorithms that make use of predicates from modules, which in turn operate on user-defined predicates (that serve as input data or problem instance). As a minimal example, consider the following:

File 'graph.pl' (input data and algorithm):

:- use_module(path).

edge(a,b).
edge(b,c).
edge(c,d).

reachable(X,Y) :-
    path(X,Y), !.
reachable(X,Y) :-
    path(Y,X), !.

File 'path.pl' (the module):

:- module(path, [path/2]).

path(X,X).
path(X,Y) :-
    user:edge(X,Z),
    path(Z,Y).

Queries run as expected:

?- [graph].
true.

?- reachable(a,a).
true.

?- reachable(a,d).
true.

?- reachable(d,a).
true.

Let us include these queries into a test file 'graph.plt':

:- begin_tests(graph).
:- include(graph).

test(1) :-
    reachable(a,a).
test(2) :-
    reachable(a,d).
test(3) :-
    reachable(d,a).

:- end_tests(graph).

When I then run the tests, I get:

?- ['graph.plt'].
true.

?- run_tests.
% PL-Unit: graph .
ERROR: /home/jens/temp/graph.plt:6:
    test 2: received error: path:path/2: Undefined procedure: edge/2
ERROR: /home/jens/temp/graph.plt:8:
    test 3: received error: path:path/2: Undefined procedure: edge/2
done
% 2 tests failed
% 1 tests passed
false.

That is to say, when called from within the test suite, the module is no longer able to 'see' the predicate 'edge' under the 'user:' namespace. Is this a bug, or am I missing something?


回答1:


I found the answer myself. It turned out that nothing was wrong here, but this problem was just another case of RTFM. From the PlUnit documentation:

3 Using separate test files

Test-units can be embedded in normal Prolog source-files. Alternatively, tests for a source-file can be placed in another file alongside the file to be tested. Test files use the extension .plt. The predicate load_test_files/1 can load all files that are related to source-files loaded into the current project.

So if using separate .plt files for testing, you are supposed to load the original source file first, then call load_test_files/1 (possibly with make or make(all) as options), and then run_tests:

?- [graph].
true.

?- load_test_files([]).
true.

?- run_tests.
% PL-Unit: graph ... done
% All 3 tests passed
true.


来源:https://stackoverflow.com/questions/57263196/unit-tests-in-swi-prolog-visibility-of-user-predicates-from-within-a-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!