aboutsummaryrefslogtreecommitdiffstats
path: root/mcgoron.srfi.64.scm
blob: c0d1e1d64f53608591465bb906802738d8a1b611 (plain) (blame)
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
#| Copyright 2024 Peter McGoron
 |
 | Licensed under the Apache License, Version 2.0 (the "License");
 |
 | you may not use this file except in compliance with the License.
 | You may obtain a copy of the License at
 |
 |     http://www.apache.org/licenses/LICENSE-2.0
 |
 | Unless required by applicable law or agreed to in writing, software
 | distributed under the License is distributed on an "AS IS" BASIS,
 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | See the License for the specific language governing permissions and
 | limitations under the License.
 |#

(define *global-make-displayable*
  ;; The list of default container objects that are traversed using
  ;; MAKE-DISPLAYABLE.
  (list
   (cons pair?
         (lambda (runner value)
           (cons (make-displayable runner (car value))
                 (make-displayable runner (cdr value)))))
   (cons vector?
         (lambda (runner value)
           (vector-map (lambda (value)
                         (make-displayable runner value))
                       value)))
   (cons eof-object?
         (lambda (_)
           `(an-eof-object)))))

(cond-expand
 (chicken
  (set! *global-make-displayable*
        ;; Handle chicken condition objects.
        (cons
         (cons condition?
               (lambda (runner value)
                 (make-displayable runner (condition->list value))))
         *global-make-displayable*)))
 (else))

(define set-displayable!
  (case-lambda
    ((predicate? transformer) (set-displayable! (test-runner-current)
                                                predicate?
                                                transformer))
    ((runner predicate? transformer)
     (let ((aux (test-runner-aux-value runner)))
       (%set-displayables! aux
                           (cons (cons predicate? transformer)
                                 (get-displayables aux)))))))

(define (make-displayable runner value)
  ;; Uses the DISPLAYABLES in RUNNER to transform VALUE into something
  ;; that is easier to print.
  (let loop ((lst (get-displayables (test-runner-aux-value runner))))
    (if (null? lst)
        value
        (let* ((pair (car lst))
               (predicate? (car pair))
               (transformer (cdr pair)))
          (if (predicate? value)
              (transformer runner value)
              (loop (cdr lst)))))))

(define-record-type <group-information>
  ;; Counts the number of tests passed or failed in a group (Because the
  ;; reference implementation does not do this).
  (group-information tests-passed-in-group
                     tests-failed-in-group
                     tests-skipped-in-group)
  group-information?
  (tests-passed-in-group get-tests-passed-in-group
                         set-tests-passed-in-group!)
  (tests-failed-in-group get-tests-failed-in-group
                         set-tests-failed-in-group!)
  (tests-skipped-in-group get-tests-skipped-in-group
                          set-tests-skipped-in-group!))

(define-record-type <aux>
  ;; Auxillary data stored in the test runner.
  ;; 
  ;; DISPLAYABLES is the list of display transformers. It is carried
  ;; through all tests.
  ;; 
  ;; GROUP-INFORMATION-QUEUE is a list consisting of all information about
  ;; tests passed and failed in a group. When an element is popped the
  ;; stats are added to the next-in-line.
  ;; 
  ;; DYNAMIC-PROPERTIES is a property alist that can be modified by the
  ;; testing program (test-result-set! only works in implementation or
  ;; in runner code). They are reset at the end of each test.
  ;; 
  ;; VERBOSITY is a list.
  ;; 
  ;; If VERBOSITY contains 'FAILS, then only failures are displayed.
  ;; Otherwise all results are displayed.
  ;; 
  ;; If VERBOSITY contains 'GROUP-STACK, then the entire chain of groups
  ;; is displayed.
  (aux group-information-queue
       displayables
       dynamic-properties
       verbosity)
  aux?
  (group-information-queue get-group-queue set-group-queue!)
  (displayables get-displayables %set-displayables!)
  (dynamic-properties get-dynamic-properties set-dynamic-properties!)
  (verbosity %get-verbosity %set-verbosity!))

(define (add-to-tests-in-group! op aux num)
  (define (getter op)
    (case op
      ((skipped) get-tests-skipped-in-group)
      ((passed) get-tests-passed-in-group)
      ((failed) get-tests-failed-in-group)
      (else (error "invalid getter" op))))
  (define (setter op)
    (case op
      ((skipped) set-tests-skipped-in-group!)
      ((passed) set-tests-passed-in-group!)
      ((failed) set-tests-failed-in-group!)
      (else (error "invalid setter" op))))
  (let ((queue (get-group-queue aux)))
    (when (not (null? queue))
      (let ((group (car queue))
            (set (setter op))
            (get (getter op)))
        (set group (+ num (get group)))))))

(define dynamic-property-set!
  (let ((dynamic-property-set!
         (lambda (runner property value)
           (let* ((aux (test-runner-aux-value runner))
                  (old (get-dynamic-properties aux)))
             (set-dynamic-properties! aux
                                      (cons (cons property value)
                                            old))))))
    (case-lambda
      ((property value) (dynamic-property-set! (test-runner-current)
                                               property
                                               value))
      ((runner property value) (dynamic-property-set! runner
                                                      property
                                                      value)))))

(define (get-verbosity runner)
  (%get-verbosity (test-runner-aux-value runner)))

(define (verbosity-has runner value)
  (memq 'fails (get-verbosity runner)))

(define set-verbosity!
  (case-lambda
    ((value) (set-verbosity! (test-runner-current) value))
    ((runner value)
     (%set-verbosity! (test-runner-aux-value runner) value))))

(define-syntax test-call
  (syntax-rules ()
    ((test-call name (%procedure %args ...))
     (test-assert
      name
      (begin
        (dynamic-property-set! 'form (quote (list %procedure %args ...)))
        (let ((procedure %procedure)
              (args (list %args ...)))
          (dynamic-property-set! 'procedure procedure)
          (dynamic-property-set! 'args args)
          (apply procedure args)))))))

;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Procedures for the test runner
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (wnl runner form)
  ;; Expand all forms with MAKE-DISPLAYABLE and pretty print FORM.
  (let ((form (make-displayable runner form)))
    (cond-expand
      (chicken (pretty-print form))
      (else (begin
              (write form)
              (newline))))))

(define (report-this-test? runner)
  (if (test-passed? runner)
      (not (verbosity-has runner 'fails))
      #t))

(define (maybe-write-stack runner)
  ;; This function returns a list of list because it is spliced into
  ;; the output.
  (if (verbosity-has runner 'group-stack)
      `((stack ,@(test-runner-group-stack runner)))
      '()))

(define (on-test-end runner)
  (let ((kind (test-result-kind runner))
        (aux (test-runner-aux-value runner)))
    (cond
      ((eq? kind 'skip)
       (add-to-tests-in-group! 'skipped aux 1))
      ((test-passed? runner)
       (add-to-tests-in-group! 'passed aux 1))
      (else
       (add-to-tests-in-group! 'failed aux 1)))
    (when (report-this-test? runner)
      (wnl runner
           `(test ,(test-runner-test-name runner)
                  ,@(maybe-write-stack runner)
                  (result ,kind)
                  (properties ,@(get-dynamic-properties aux)
                              ,@(test-result-alist runner)))))
    (set-dynamic-properties! aux '())))

(define (on-group-begin runner suite-name count)
  (let ((aux (test-runner-aux-value runner)))
    (set-group-queue! aux
                      (cons (group-information 0 0 0)
                            (get-group-queue aux))))
  (wnl runner `(start ,suite-name)))

(define (on-group-end runner)
  (let* ((aux (test-runner-aux-value runner))
         (current-group (car (get-group-queue aux)))
         (passed (get-tests-passed-in-group current-group))
         (failed (get-tests-failed-in-group current-group))
         (skipped (get-tests-skipped-in-group current-group)))
    (wnl runner
         `(end (passed ,passed)
               (failed ,failed)
               (skipped ,skipped)
               (tests ,(+ passed failed skipped))))
    (let ((next-group-queue (cdr (get-group-queue aux))))
      (set-group-queue! aux next-group-queue)
      (when (not (null? next-group-queue))
        (let ((next-group (car next-group-queue)))
          (add-to-tests-in-group! 'passed aux passed)
          (add-to-tests-in-group! 'skipped aux skipped)
          (add-to-tests-in-group! 'failed aux failed))))))

(define (on-bad-count runner actual expected)
  (wnl runner
       `(warning expected ,expected
                 got ,actual)))

(define (on-bad-end-name runner begin-name end-name)
  (error "mismatch in test names" begin-name end-name
         (test-runner-group-stack runner)
         runner))

(define (on-final runner)
  (wnl runner
       `(summary
         (pass (expected ,(test-runner-pass-count runner))
               (not-expected ,(test-runner-xpass-count runner)))
         (fail (not-expected ,(test-runner-fail-count runner))
               (expected ,(test-runner-xfail-count runner)))
         (skipped ,(test-runner-skip-count runner)))))

(define (factory)
  (let ((runner (test-runner-null)))
    (test-runner-aux-value! runner
                            (aux '() *global-make-displayable* '()
                                 '()))
    (test-runner-on-test-end! runner on-test-end)
    (test-runner-on-group-begin! runner on-group-begin)
    (test-runner-on-group-end! runner on-group-end)
    (test-runner-on-bad-end-name! runner on-bad-end-name)
    (test-runner-on-bad-count! runner on-bad-end-name)
    (test-runner-on-final! runner on-final)
    runner))