4
votes

I'm trying to write output from make_tests to a file, but nothing I've tried seems to insert the correct information in the output file.

I've looked at SWI documentaion at http://www.complang.tuwien.ac.at/SWI-Prolog/Manual/IO.html and have tried a whole bunch of those predicates but none have worked.

Here's what I'm trying to do:

:- use_module(library(test_wizard)).

init_test_file(FILE) :-
   set_prolog_flag(log_query_file, FILE).

gen_test_in(FILE) :-
   make_tests(lists, FILE, current_output).

So running this (in console) for example:

init_test_file('mytest.pro').
member(a, [a,b]).
gen_test_in('mytest.pro').

I get the following output (in console):

true.

6 ?- gen_test_in('r.pro').
:- begin_tests(lists).

test(member, [nondet]) :-
        member(a, [a, b]).

:- end_tests(lists).
true.

How do I get this same output (starting at begin_tests and ending at end_tests in a text file?

I tried doing things like modifying gen_test_in to:

gen_test_in(FILE) :-
   open(FILE, write, Out),
   make_tests(lists, FILE, Out),
   close(Out).

But I just get an empty text file.

1
The goal Close(Out) in the body of gen_test_in/1 can't possibly be right, or can it?repeat
@repeat It took me another 1/2 minute staring at this to notice the capital letter there.user1812457
Sorry didn't intend for it to be capital. I've edited it.Ash

1 Answers

7
votes

You are messing up the arguments here. The library you are using, library(test_wizard), is meant for generating tests from queries. In the predicate you are using, make_tests/3, you have the following three arguments:

  1. The module
  2. The file from which the queries are read
  3. The stream to which the generated tests are written

In the examples you use, you either set your output stream to be current_output (so standard output if you are on the top level), or, in the last example you give, you are opening the file you want to read for writing, and then pass the file and its handle to make_tests/3. Instead, if you have a file called queries.pl:

$ cat queries.pl
member(a, [a,b]).
member(X, [a,b]).

Then:

$ swipl
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.16-36-g42820df)
Copyright (c) 1990-2015 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- use_module(library(test_wizard)).
true.

?- setup_call_cleanup(open('lists.plt', write, Out),
                      make_tests(lists, 'queries.pl', Out),
                      close(Out)).
Out = <stream>(0xe59ef0).

?- halt.
$ cat lists.plt 
:- begin_tests(lists).

test(member, [nondet]) :-
    member(a, [a, b]).
test(member, [all(A==[a, b])]) :-
    member(A, [a, b]).

:- end_tests(lists).