nendo 0.3.3 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,165 @@
1
+ ;;-*- mode: nendo; syntax: scheme -*-;;
2
+ ;;;
3
+ ;;; nendo.test - test framework
4
+ ;;;
5
+ ;;; Copyright (c) 2000-2009 Shiro Kawai <shiro@acm.org>
6
+ ;;;
7
+ ;;; Redistribution and use in source and binary forms, with or without
8
+ ;;; modification, are permitted provided that the following conditions
9
+ ;;; are met:
10
+ ;;;
11
+ ;;; 1. Redistributions of source code must retain the above copyright
12
+ ;;; notice, this list of conditions and the following disclaimer.
13
+ ;;;
14
+ ;;; 2. Redistributions in binary form must reproduce the above copyright
15
+ ;;; notice, this list of conditions and the following disclaimer in the
16
+ ;;; documentation and/or other materials provided with the distribution.
17
+ ;;;
18
+ ;;; 3. Neither the name of the authors nor the names of its contributors
19
+ ;;; may be used to endorse or promote products derived from this
20
+ ;;; software without specific prior written permission.
21
+ ;;;
22
+ ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
+ ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
+ ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25
+ ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26
+ ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27
+ ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28
+ ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29
+ ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30
+ ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31
+ ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32
+ ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
+ ;;;
34
+ ;;; $Id: test.scm,v 1.29 2008-05-10 13:35:56 shirok Exp $
35
+ ;;
36
+ ;; ported by Kiyoka Nishiyama for Nendo.
37
+ ;;
38
+ ;; List of discrepancies
39
+ (define *test-record-file* "test.record")
40
+ (define *test-output-file* STDOUT)
41
+ (define *discrepancy-list* '())
42
+
43
+ (define (test-record-file file) (set! *test-record-file* file)) ;;public API
44
+ (define (test-output-file file) (set! *test-output-file* file)) ;;putlic API
45
+
46
+ (define *test-counts* (vector 0 0 0 0))
47
+ (define (test-count++)
48
+ (vector-set! *test-counts* 0 (+ (vector-ref *test-counts* 0) 1)))
49
+ (define (test-pass++)
50
+ (vector-set! *test-counts* 1 (+ (vector-ref *test-counts* 1) 1)))
51
+ (define (test-fail++)
52
+ (vector-set! *test-counts* 2 (+ (vector-ref *test-counts* 2) 1)))
53
+ (define (test-abort++)
54
+ (vector-set! *test-counts* 3 (+ (vector-ref *test-counts* 3) 1)))
55
+
56
+ (define (format-summary)
57
+ (sprintf "Total: %5d tests, %5d passed, %5d failed, %5d aborted.\n"
58
+ (vector-ref *test-counts* 0)
59
+ (vector-ref *test-counts* 1)
60
+ (vector-ref *test-counts* 2)
61
+ (vector-ref *test-counts* 3)))
62
+
63
+ (define (read-summary)
64
+ (when (and (string? *test-record-file*)
65
+ (File.exist? *test-record-file*))
66
+ (with-open *test-record-file*
67
+ (lambda (f)
68
+ (let ((m (rxmatch #/Total:\s+(\d+)\s+tests,\s+(\d+)\s+passed,\s+(\d+)\s+failed,\s+(\d+)\s+aborted/ (f.readline.chomp))))
69
+ (when m
70
+ (for-each (lambda (i)
71
+ (vector-set! *test-counts* i
72
+ (to-i (rxmatch-substring m (+ i 1)))))
73
+ '(0 1 2 3)))))))
74
+ ;; We write out aborted+1, in case if the test process fails before test-end
75
+ ;; For normal case, it will be overwritten by test-end.
76
+ (let ((orig-abort (vector-ref *test-counts* 3)))
77
+ (vector-set! *test-counts* 3 (+ orig-abort 1))
78
+ (write-summary)
79
+ (vector-set! *test-counts* 3 orig-abort)))
80
+
81
+ (define (write-summary)
82
+ (when (string? *test-record-file*)
83
+ (with-open *test-record-file*
84
+ (lambda (f)
85
+ (f.printf "%s" (format-summary)))
86
+ "w")))
87
+
88
+ (define (test msg expect thunk . compare)
89
+ (let ((cmp (get-optional compare equal?))
90
+ (f *test-output-file*))
91
+ (f.printf "test %s, expects %s ==> " msg (write-to-string expect))
92
+ (f.flush)
93
+ (test-count++)
94
+ (let ((r (thunk)))
95
+ (cond ((cmp expect r)
96
+ (f.printf "ok\n")
97
+ (test-pass++))
98
+ (else
99
+ (f.printf "ERROR: GOT %s\n" (write-to-string r))
100
+ (set! *discrepancy-list*
101
+ (cons (list msg expect r) *discrepancy-list*))
102
+ (test-fail++)))
103
+ (f.flush)
104
+ #t)))
105
+
106
+ ;; A convenient macro version
107
+ (define test*
108
+ (macro (msg expect form . compare)
109
+ `(test ,msg ,expect (lambda () ,form) ,@compare)))
110
+
111
+
112
+ ;; Logging and bookkeeping -----------------------------------------
113
+ (define (make-padding-string num char)
114
+ (string-join
115
+ (map
116
+ (lambda (x) char)
117
+ (range num))))
118
+
119
+ (define (test-section msg)
120
+ (let ((f *test-output-file*)
121
+ (msglen (string-length msg)))
122
+ (f.printf "<%s>%s\n" msg (make-padding-string (max 5 (- 77 msglen)) "-"))
123
+ msg))
124
+
125
+ (define (test-start msg)
126
+ (let* ((s (sprintf "Testing %s ... " msg))
127
+ (pad (make-padding-string (max 3 (- 65 (string-length s))) " "))
128
+ (f *test-output-file*))
129
+ (f.printf "%s%s" s pad)
130
+ (f.flush)
131
+ (read-summary)
132
+ (f.printf "\n")
133
+
134
+ (set! *discrepancy-list* '())
135
+ (let ((msglen (string-length msg)))
136
+ (f.printf "Testing %s %s\n" msg (make-padding-string (max 5 (- 70 msglen)) "-"))
137
+ (f.flush))
138
+ msg))
139
+
140
+
141
+ (define (test-end)
142
+ (let1 f *test-output-file*
143
+ (if (null? *discrepancy-list*)
144
+ (f.printf "passed.\n")
145
+ (begin
146
+ (f.printf "failed.\ndiscrepancies found. Errors are:\n")
147
+ (for-each (lambda (r)
148
+ (f.printf "test %s: expects %s => got %s\n"
149
+ (first r)
150
+ (write-to-string (second r))
151
+ (write-to-string (third r))))
152
+ (reverse *discrepancy-list*))))
153
+
154
+ (when *test-record-file*
155
+ (write-summary))
156
+
157
+ ;; Returns the number of failed tests.
158
+ (length *discrepancy-list*)))
159
+
160
+
161
+ (define (test-module module-name)
162
+ ;; nothing to do
163
+ module-name)
164
+
165
+