-
Notifications
You must be signed in to change notification settings - Fork 0
/
testeez.lisp
407 lines (370 loc) · 12 KB
/
testeez.lisp
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
;;; @Package Testeez
;;; @Subtitle Lightweight Unit Test Mechanism for R5RS Scheme
;;; @HomePage http://www.neilvandyke.org/testeez/
;;; @Author Neil Van Dyke
;;; @Version 0.5
;;; @Date 2009-05-28
;;; @PLaneT neil/testeez:1:3
;; $Id: testeez.ss,v 1.76 2009/05/29 11:42:28 neilpair Exp $
;;; @legal
;;; Copyright @copyright{} 2005--2009 Neil Van Dyke. This program is Free
;;; Software; you can redistribute it and/or modify it under the terms of the
;;; GNU Lesser General Public License as published by the Free Software
;;; Foundation; either version 3 of the License (LGPL 3), or (at your option)
;;; any later version. This program is distributed in the hope that it will be
;;; useful, but without any warranty; without even the implied warranty of
;;; merchantability or fitness for a particular purpose. See
;;; @indicateurl{http://www.gnu.org/licenses/} for details. For other licenses
;;; and consulting, please contact the author.
;;; @end legal
;;; @section Introduction
;;;
;;; Testeez is a simple lightweight unit test mechanism for R5RS Scheme. It
;;; was written to support regression test suites embedded within the source
;;; code files of the author's portable Scheme libraries.
;;;
;;; @subsection Example
;;;
;;; A series of Testeez tests is listed within the @code{testeez} syntax. For
;;; example:
;;;
;;; @lisp
;;; (testeez
;;; "Foo Station"
;;;
;;; (test/equal "Put two and two together" (+ 2 2) 4)
;;;
;;; (test-define "Bar function" bar (lambda (x) (+ x 42)))
;;;
;;; (test/equal "Bar scene" (bar 69) 0)
;;;
;;; (test/eqv "Full circle" (* (bar -21) 2) 42)
;;;
;;; (test/eqv "Multiple"
;;; (values (+ 2 2) (string #\h #\i) (char-upcase #\p))
;;; (values 4 "hi" #\P)))
;;; @end lisp
;;;
;;; When evaluated, output like the following (which looks prettier fontified
;;; in Emacs's @code{*scheme*} buffer) is printed:
;;;
;;; @smallexample
;;; ;;; BEGIN "Foo Station" TESTS
;;;
;;; ;; 1. Put two and two together
;;; (+ 2 2)
;;; ;; ==> 4
;;; ;; Passed.
;;;
;;; ;; DEFINE: Bar function
;;; (define bar (lambda (x) (+ x 42)))
;;;
;;; ;; 2. Bar scene
;;; (bar 69)
;;; ;; ==> 111
;;; ;; FAILED! Expected:
;;; ;; 0
;;;
;;; ;; 3. Full circle
;;; (* (bar -21) 2)
;;; ;; ==> 42
;;; ;; Passed.
;;;
;;; ;; 4. Multiple
;;; (values (+ 2 2) (string #\h #\i) (char-upcase #\p))
;;; ;; ==> 4
;;; ;; "hi"
;;; ;; #\P
;;; ;; Passed.
;;;
;;; ;;; END "Foo Station" TESTS: FAILED
;;; ;;; (Total: 4 Passed: 3 Failed: 1)
;;; @end smallexample
;;;
;;; @subsection Shorthand
;;;
;;; The @code{testeez} syntax also supports shorthand or abbreviated forms, for
;;; quick interactive use, such as in an editor buffer while rapid-prototyping
;;; a function, and in a REPL while debugging. For an example of shorthand,
;;; the Scheme expression:
;;;
;;; @lisp
;;; (testeez ((+ 1 2) 3) ((* 6 7) 42))
;;; @end lisp
;;;
;;; @noindent
;;; is equivalent to:
;;;
;;; @lisp
;;; (testeez ""
;;; (test/equal "" (+ 1 2) 3)
;;; (test/equal "" (* 6 7) 42))
;;; @end lisp
;;;
;;; Future versions of Testeez will add additional features, such as custom
;;; predicates and handling of errors.
;;;
;;; @subsection Embedding
;;;
;;; By following a simple convention, Testeez tests can be embedded in a Scheme
;;; source file with the code that is tested, while permitting the tests to be
;;; disabled and the dependency on Testeez removed for production code. For
;;; example, to use Testeez in a ``Foo'' library, one can first add a syntax
;;; wrapper around @code{testeez} like so:
;;;
;;; @example
;;; (define-syntax %foo:testeez
;;; (syntax-rules ()
;;; ((_ X ...)
;;; ;; Note: Comment-out exactly one of the following two lines.
;;; ;; (error "Tests disabled.")
;;; (testeez X ...)
;;; )))
;;; @end example
;;;
;;; Then, this wrapper @code{%foo:testeez} can be used in a procedure that
;;; executes the test suite of the ``Foo'' library:
;;;
;;; @lisp
;;; (define (%foo:test)
;;; (%foo:testeez
;;; "Foo Station"
;;; ....))
;;; @end lisp
;;; @section Interface
;;; The interface consists of the @code{testeez} syntax.
(cl:defpackage :%testeez
(:use :schemish)
(:export :test/equiv :test/eq :test/equal :test/eqv :test-define
:test-eval :testeez))
(cl:in-package :%testeez)
(cl:eval-when (:compile-toplevel :load-toplevel :execute)
(schemish-read-macros:enable-scheme-read-syntax))
(cl:eval-when (:compile-toplevel :load-toplevel :execute)
(define (make-data title) (vector title 0 0 0))
(define (data-title o) (vector-ref o 0))
(define (data-total o) (vector-ref o 1))
(define (data-passed o) (vector-ref o 2))
(define (data-failed o) (vector-ref o 3))
(define (set-data-title! o x) (vector-set! o 0 x))
(define (set-data-total! o x) (vector-set! o 1 x))
(define (set-data-passed! o x) (vector-set! o 2 x))
(define (set-data-failed! o x) (vector-set! o 3 x))
(define (print-values-list first-prefix next-prefix val-list)
(display first-prefix)
(if (null? val-list)
(newline)
(let loop ((val-list val-list))
(write (car val-list))
(newline)
(or (null? (cdr val-list))
(begin (display next-prefix)
(loop (cdr val-list)))))))
(define (print-result result-list)
(print-values-list ";; ==> "
";; "
result-list))
(define (start-test data desc expr-quoted)
(set-data-total! data (+ 1 (data-total data)))
(newline)
(display ";; ")
(display (data-total data))
(display ". ")
(display desc)
(newline)
(write expr-quoted)
(newline))
(define (finish-test data pred pred-rest result-list expected-list)
(define (failed)
(set-data-failed! data
(+ 1 (data-failed data)))
(display ";; FAILED! Expected:")
(newline)
(print-values-list ";; "
";; "
expected-list))
(print-result result-list)
(let loop ((pred pred)
(pred-rest pred-rest)
(result-list result-list)
(expected-list expected-list))
(if (null? result-list)
(if (null? expected-list)
(begin (set-data-passed!
data
(+ 1 (data-passed data)))
(display ";; Passed.")
(newline))
(failed))
(if (null? expected-list)
(failed)
(if (cl:funcall pred (car result-list) (car expected-list))
(if (null? pred-rest)
(loop pred
pred-rest
(cdr result-list)
(cdr expected-list))
(loop (car pred-rest)
(cdr pred-rest)
(cdr result-list)
(cdr expected-list)))
(failed))))))
(define (start-eval desc expr-quoted)
(newline)
(display ";; EVAL: ")
(display desc)
(newline)
(write expr-quoted)
(newline))
(define (start-define desc expr-quoted)
(newline)
(display ";; DEFINE: ")
(display desc)
(newline)
(write expr-quoted)
(newline))
(define (start-tests title)
(newline)
(display ";;; BEGIN")
(and title
(begin (display " ")
(write title)))
(display " TESTS")
(newline)
(make-data title))
(define (finish-tests data)
(let ((total (data-total data))
(passed (data-passed data))
(failed (data-failed data)))
;; TODO: Check that total = passed + failed
(newline)
(display ";;; END")
(let ((title (data-title data)))
(and title
(begin (display " ")
(write title))))
(display " TESTS: ")
(display (cond ((zero? failed) "PASSED")
;; ((zero? passed) "ALL FAILED")
(else "FAILED")))
(newline)
(display ";;; (Total: ")
(display total)
(display " Passed: ")
(display passed)
(display " Failed: ")
(display failed)
(display ")")
(newline)))
;;; @defsyntax testeez [ title ] form ...
;;;
;;; The @code{testeez} syntax contains a short string @var{title} and one or
;;; more @var{forms}, of the following syntaxes, which are evaluated in order.
;;;
;;; @table @code
;;;
;;; @item (test/equal @var{desc} @var{expr} @var{expected})
;;; Execute a test case. @var{desc} is a short title or description of the
;;; test case, @var{expr} is a Scheme expression, and @var{expected} is an
;;; expression for the expected value (or multiple values). The test case
;;; passes iff each value of @var{expr} is @code{equal?} to the corresponding
;;; value of @var{expected}.
;;;
;;; @item (test/eq @var{desc} @var{expr} @var{expected})
;;; Like @code{test/equal}, except the equivalence predicate is @code{eq?}
;;; rather than @code{equal?}.
;;;
;;; @item (test/eqv @var{desc} @var{expr} @var{expected})
;;; Like @code{test/equal}, except the equivalence predicate is @code{eqv?}
;;; rather than @code{equal?}.
;;;
;;; @item (test-define @var{desc} @var{name} @var{val})
;;; Bind a variable. @var{desc} is a short description string, @var{name} is
;;; the identifier, and @var{val} is the value expression. The binding is
;;; visible to the remainder of the enclosing @code{testeez} syntax.
;;;
;;; @item (test-eval @var{desc} @var{expr})
;;; Evaluate an expression.
;;;
;;; @item (@var{expr} @var{expected})
;;; Shorthand for @code{(test/equal "" @var{expr} @var{expected})}. This
;;; shorthand is intended for interactive and rapid-prototyping use, not for
;;; released code.
;;;
;;; @end table
;; TODO: Lose the "begin"s.
;; TODO: Expose the custom equivalence predicates, once we're sure we like
;; the syntax. Should add generic predicates first.
(cl:defmacro test/equiv (data-var desc expr expected (pred0 &rest rest-preds) &body body)
(alexandria:with-gensyms (result-list expected-list)
`(begin
(start-test ,data-var ,desc ',expr)
(let ((,result-list (call-with-values (λ () ,expr) #'list))
(,expected-list (call-with-values (λ () ,expected) #'list)))
(finish-test ,data-var (cl:function ,pred0)
,(cons 'list (map (lambda (pred)
(cons 'cl:function pred))
rest-preds))
,result-list
,expected-list))
(body ,data-var ,@body))))
(cl:defmacro test/eq (desc expr expected data-var &body body)
`(test/equiv ,data-var ,desc ,expr ,expected (eq?) ,@body))
(cl:defmacro test/equal (desc expr expected data-var &body body)
`(test/equiv ,data-var ,desc ,expr ,expected (equal?) ,@body))
(cl:defmacro test/eqv (desc expr expected data-var &body body)
`(test/equiv ,data-var ,desc ,expr ,expected (equal?) ,@body))
(cl:defmacro test-define (desc name val data-var &body body)
`(begin (start-define ,desc
(list 'define
',name
',val))
(let ((,name ,val))
(body ,data-var ,@body))))
(cl:defmacro test-eval (desc expr data-var &body body)
(alexandria:with-gensyms (result)
`(begin (start-eval ,desc ',expr)
(let ((,result (call-with-values (lambda () ,expr) #'list)))
(print-result ,result))
(body ,data-var ,@body))))
(cl:defmacro body (data-var &rest forms)
(cond ((or (null? forms)
(equal? forms '(())))
'(values))
(else
(let ((head (car forms))
(tail (cdr forms)))
(if (= (length head) 2)
`(body ,data-var (test/equal "" ,@head) ,@tail)
`(begin
,(append head (cons data-var tail))))))))
(cl:defmacro testeez (title &body body)
(if (list? title)
`(testeez #f ,title ,@body)
(alexandria:with-gensyms (data)
`(let ((,data (start-tests ,title)))
(body ,data ,@body)
(finish-tests ,data))))))
;;; @unnumberedsec History
;;; @table @asis
;;; @item Version 0.5 --- 2016-05-14 ---
;;; Ported to Common Lisp.
;;;
;;; @item Version 0.5 --- 2009-05-28 --- PLaneT @code{(1 3)}
;;; Added support for void return values.
;;;
;;; @item Version 0.4 --- 2009-03-02 --- PLaneT @code{(1 2)}
;;; License is now LGPL 3. Minor changes for PLT 4. Changed to new Scheme
;;; administration system. There is now a @code{main.ss}.
;;;
;;; @item Version 0.3 --- 2005-05-30 --- PLaneT @code{(1 1)}
;;; Shorthand syntax added. Minor formatting change to test log output.
;;;
;;; @item Version 0.2 --- 2005-03-07 --- PLaneT @code{(1 0)}
;;; Multiple values are now supported. @code{test/eq} and @code{test/eqv}
;;; have been added. Minor formatting changes to test log output.
;;;
;;; @item Version 0.1 --- 2005-01-02
;;; Initial release.
;;;
;;; @end table