1

Suppose I have a hello_name.pl:

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

And I want to put in my plunit something like

catch_output (greeting ('Moncho'), ConsoleOutput),
  assertion ('hello Moncho!' =:= ConsoleOutput).
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Ruslan López
  • 4,433
  • 2
  • 26
  • 37
  • Just as an addendum, do not use `write` (it is meant so serialize-out terms), use `format`. I have prepared a little overview on "printing in Prolog" [in this gist](https://gist.github.com/dtonhofer/20bd01f68a924912771d8405fca66a09) (Work in progress). – David Tonhofer Jan 19 '20 at 20:59
  • @DavidTonhofer thank you sir, I added your link in the [Codewars kata I've created](https://www.codewars.com/kata/57036f007fd72e3b77000023) – Ruslan López Jan 21 '20 at 01:52
  • Of interest: [Help with set_prolog_IO/3](https://swi-prolog.discourse.group/t/help-with-set-prolog-io-3/1798) – Guy Coder Jan 29 '20 at 10:25

1 Answers1

3

If you are using

See: with_output_to/2

Note: with_output_to/2 is implemented using C in SWI-Prolog so is not portable as Prolog code.

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

With corrections to your code and using 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).
Ruslan López
  • 4,433
  • 2
  • 26
  • 37
Guy Coder
  • 24,501
  • 8
  • 71
  • 136