假设我有一个hello_name.pl:
greeting (Name): -
write ('hello'),
write (Name),
writeln ('!').
而且我想把我的东西放在里面
catch_output (greeting ('Moncho'), ConsoleOutput),
assertion ('hello Moncho!' =:= ConsoleOutput).
如果使用swi-prolog
注: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).