-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_wizard.pl
250 lines (202 loc) · 7.8 KB
/
test_wizard.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2006-2012, University of Amsterdam
VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(test_wizard,
[ make_tests/3, % +Module, +File, +Out
make_test/3 % +Callable, -Module, -Test
]).
:- autoload(library(apply),[maplist/2]).
:- autoload(library(listing),[portray_clause/2]).
:- autoload(library(lists),[member/2]).
:- autoload(library(readutil),[read_file_to_terms/3]).
:- autoload(library(time),[call_with_time_limit/2]).
/** <module> Test Generation Wizard
Tasks
* Accumulate user queries
* Suggest tests from user queries
*/
setting(max_time(5)).
/*******************************
* UNIT GENERATION *
*******************************/
%! make_tests(+Module, +File, +Out) is det.
%
% Create tests from queries stored in File and write the tests for
% Module to the stream Out.
make_tests(Module, File, Out) :-
read_file_to_terms(File, Queries, []),
findall(Test, ( member(Q, Queries),
make_test(Q, Module, Test)), Tests),
( Tests == []
-> true
; format(Out, ':- begin_tests(~q).~n~n', [Module]),
maplist(portray_clause(Out), Tests),
format(Out, '~n:- end_tests(~q).~n', [Module])
).
/*******************************
* TEST GENERATION *
*******************************/
%! make_test(+Query:callable, -Module, -Test:term) is det.
%
% Generate a test from a query. Test is returned as a clause of
% test/1 or test/2 to be inserted between begin_tests and
% end_tests.
make_test(Query0, Module, (test(Name, Options) :- Query)) :-
find_test_module(Query0, Module, Query),
pred_name(Query, Name),
setting(max_time(Max)),
test_result(Module:Query, Max, Options).
%! find_test_module(+QuerySpec, ?Module, -Query).
%
% Find module to test from a query. Note that it is very common
% for toplevel usage to rely on SWI-Prolog's DWIM.
%
% @tbd What if multiple modules match? We can select the
% local one or ask the user.
find_test_module(Var, _, _) :-
var(Var), !, fail.
find_test_module(M:Query, M0, Query) :-
!,
M0 = M.
find_test_module(Query, M, Query) :-
current_predicate(_, M:Query),
\+ predicate_property(M:Query, imported_from(_M2)).
%! pred_name(+Callable, -Name) is det.
%
% Suggest a name for the test. In the plunit framework the name
% needs not be unique, so we simply take the predicate name.
pred_name(Callable, Name) :-
strip_module(Callable, _, Term),
functor(Term, Name, _Arity).
%! test_result(+Callable, +Maxtime, -Result) is det.
%
% Try running goal and get meaningful results. Results are:
%
% * true(Templ == Var)
% * fail
% * all(Templ == Bindings)
% * throws(Error)
% * timeout
test_result(Callable, Maxtime, Result) :-
term_variables(Callable, Vars),
make_template(Vars, Templ),
catch(call_with_time_limit(Maxtime,
findall(Templ-Det,
call_test(Callable, Det),
Bindings)),
E, true),
( var(E)
-> success(Bindings, Templ, Result)
; error(E, Result)
).
%! success(+Bindings, +Templ, -Result) is det.
%
% Create test-results from non-error cases.
success([], _, [fail]) :- !.
success([[]-true], _, []) :- !.
success([S1-true], Templ, [ true(Templ == S1) ]) :- !.
success([[]-false], _, [ nondet ]) :- !.
success([S1-false], Templ, [ true(Templ == S1), nondet ]) :- !.
success(ListDet, Templ, [all(Templ == List)]) :-
strip_det(ListDet, List).
strip_det([], []).
strip_det([H-_|T0], [H|T]) :-
strip_det(T0, T).
%! error(+ErrorTerm, -Result)
error(Error0, [throws(Error)]) :-
generalise_error(Error0, Error).
generalise_error(error(Formal, _), error(Formal, _)) :- !.
generalise_error(Term, Term).
%! make_template(+Vars, -Template) is det.
%
% Make a nice looking template
make_template([], []) :- !.
make_template([One], One) :- !.
make_template([One, Two], One-Two) :- !.
make_template(List, Vars) :-
Vars =.. [v|List].
%! call_test(:Goal, -Det) is nondet.
%
% True if Goal succeeded. Det is unified to =true= if Goal left
% no choicepoints and =false= otherwise.
call_test(Goal, Det) :-
Goal,
deterministic(Det).
/*******************************
* COLLECT *
*******************************/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Collect toplevel queries if the Prolog flag log_query_file points to the
name of a writeable file. The file is opened in append-mode for
exclusive write to allow for concurrent operation from multiple Prolog
systems using the same logfile.
The file is written in UTF-8 encoding and using ignore_ops(true) to
ensure it can be read.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
:- multifile
user:message_hook/3.
user:message_hook(toplevel_goal(Goal0, Bindings), _Level, _Lines) :-
open_query_log(Out),
bind_vars(Bindings),
clean_goal(Goal0, Goal),
call_cleanup(format(Out, '~W.~n', [Goal, [ numbervars(true),
quoted(true),
ignore_ops(true)
]]), close(Out)),
fail.
clean_goal(Var, _) :-
var(Var), !, fail.
clean_goal(user:Goal, Goal) :- !.
clean_goal(Goal, Goal).
bind_vars([]).
bind_vars([Name=Var|T]) :-
Var = '$VAR'(Name),
bind_vars(T).
open_query_log(Out) :-
current_prolog_flag(log_query_file, File),
exists_file(File),
!,
open(File, append, Out,
[ encoding(utf8),
lock(write)
]).
open_query_log(Out) :-
current_prolog_flag(log_query_file, File),
access_file(File, write),
!,
open(File, write, Out,
[ encoding(utf8),
lock(write),
bom(true)
]),
format(Out,
'/* SWI-Prolog query log. This file contains all syntactically\n \c
correct queries issued in this directory. It is used by the\n \c
test wizard to generate unit tests.\n\c
*/~n~n', []).