我如何在Prolog中捕获输出到控制台以进行验证?

问题描述 投票:1回答:1

假设我有一个hello_name.pl:

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

而且我想把我的东西放在里面

catch_output (greeting ('Moncho'), ConsoleOutput),
  assertion ('hello Moncho!' =:= ConsoleOutput).
unit-testing console prolog output plunit
1个回答
3
投票

如果使用

参见: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).
© www.soinside.com 2019 - 2024. All rights reserved.