如何在 Prolog 中将输出捕获到控制台以验证它?

How can I catch the output to the console in Prolog to verify it?

假设我有一个 hello_name.pl:

greeting (Name): -
   write ('hello'),
   write (Name),
   writeln ('!').

我想在我的 plunit 中加入类似

的内容
catch_output (greeting ('Moncho'), ConsoleOutput),
  assertion ('hello Moncho!' =:= ConsoleOutput).

如果您正在使用

参见:with_output_to/2

注意:with_output_to/2 在 SWI-Prolog 中是 implemented using C,因此不能作为 Prolog 代码移植。

?- with_output_to(string(Output),(write('hello'),write('Rusian'),write('!'))), 
   assertion( Output == "helloRusian!").

更正您的代码并使用 SWI-Prolog unit tests

greeting(Name) :-
   write('hello'),
   write(Name),
   writeln('!').

:- begin_tests(your_tests).

test(001, Output == 'helloMoncho!\n') :-
    with_output_to(atom(Output), greeting('Moncho')).

:- end_tests(your_tests).