【问题标题】:How can I catch the output to the console in Prolog to verify it?如何在 Prolog 中将输出捕获到控制台以进行验证?
【发布时间】:2020-01-18 15:47:44
【问题描述】:

假设我有一个 hello_name.pl:

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

我想在我的 plunit 中放入类似的东西

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

【问题讨论】:

  • 作为附录,不要使用write(它的意思是这样的序列化术语),使用format。我准备了一些关于“在 Prolog 中打印”in this gist(正在进行中)的概述。
  • @DavidTonhofer 谢谢先生,我在Codewars kata I've created添加了您的链接
  • 感兴趣的:Help with set_prolog_IO/3

标签: unit-testing console prolog output plunit


【解决方案1】:

如果您使用的是

见: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).

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 2011-10-31
    相关资源
    最近更新 更多