模块内用户谓词的可见性

模块内用户谓词的可见性

本文介绍了SWI-Prolog 中的单元测试:模块内用户谓词的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写单元SWI-Prolog(7.6.4 版)中的测试,以简化和自动化测试,目前仅以手动、临时方式完成.

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:

文件graph.pl"(输入数据和算法):

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

文件path.pl"(模块):

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

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

查询按预期运行:

?- [graph].
true.

?- reachable(a,a).
true.

?- reachable(a,d).
true.

?- reachable(d,a).
true.

让我们将这些查询包含到测试文件graph.plt"中:

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.

也就是说,当从测试套件中调用时,模块不再能够看到"user:"命名空间下的谓词edge".这是一个错误,还是我遗漏了什么?

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?

推荐答案

我自己找到了答案.结果发现这里没有什么问题,但这个问题只是RTFM的另一个案例.来自 PlUnit 文档:

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 使用单独的测试文件

测试单元可以嵌入到普通的 Prolog 源文件中.或者,可以将源文件的测试放在另一个文件中与要测试的文件一起.测试文件使用扩展名 .plt.谓词 load_test_files/1 可以加载所有与加载到当前项目中的源文件.

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.

因此,如果使用单独的 .plt 文件进行测试,您应该先加载原始源文件,然后调用 load_test_files/1(可能使用 makemake(all) 作为 options),然后 run_tests:

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.

这篇关于SWI-Prolog 中的单元测试:模块内用户谓词的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 00:02