aboutsummaryrefslogtreecommitdiffstats
path: root/tests/impl.scm
blob: 0047e348ae84e413bb7b5b02bf7b5f28dee99043 (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
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
408
409
410
411
412
;;; This is a meta-test suite for Conspire, similar to the meta-test
;;; suite of SRFI-64.
;;; 
;;; This test suite is not as straightforward as a normal test suite,
;;; since it tests the testing library itself. Hopefully, if the tests
;;; pass, then the library works.

(test-group "test-ref, test-contains? and test-ref/default"
  (test-group "default keys"
    (for-each (lambda (key value)
                (let ((name (symbol->string key)))
                  (test-body (string-append "contains " name)
                    (test-contains? key))
                  (test-eq name value (test-ref key))))
              '(skip-test? when-test-skipped before-test! setup-test!
                           cleanup-test! after-test report-test
                           on-exception-in-test
                           skip-group? when-group-skipped before-group!
                           setup-group! cleanup-group! after-group
                           report-group on-exception-in-group)
              (list default-skip-test? default-when-test-skipped
                    default-before-test! default-setup-test! default-cleanup-test!
                    default-after-test default-report-test
                    default-on-exception-in-test
                    default-skip-group? default-when-group-skipped
                    default-before-group! default-setup-group! default-cleanup-group!
                    default-after-group default-report-group
                    default-on-exception-in-group)))
  (let ((pair (cons #f #f)))
    (test-eq "default pair" pair (test-ref/default 'nonexistent-key pair))))

(test-group "test-set and test-set!"
  (let ((pair (cons #f #f)))
    (test-set! 'nonexistent-key pair)
    (test-eq "set unused key" pair (test-ref 'nonexistent-key))
    (parameterize ((test-info (test-info-dict)))
      (test-eq "persistent across parameterization"
               pair
               (test-ref 'nonexistent-key))
      (let ((pair2 (cons #f #f)))
        (test-body "made a new pair"
          (not (eq? pair pair2)))
        (test-set! 'nonexistent-key pair2)
        (test-eq "set key to a new pair"
                 pair2
                 (test-ref 'nonexistent-key))))
    (test-eq "outside of parameterization"
             pair
             (test-ref 'nonexistent-key)))
  (parameterize ((test-info (test-set 'other-key 123)))
    (test-eqv "other key in parameterization"
              123
              (test-ref 'other-key)))
  (test-eqv "does not affect outside"
            456
            (test-ref/default 'other-key 456))
  (test-body "deleted key"
    (test-delete! 'nonexistent-key)
    (not (test-contains? 'nonexistent-keys))))

;;; Now the actual test procedures need to be tested. This is done by
;;; running everything in a dummy test-info that is overridden at each
;;; test site with new procedures.
;;; 
;;; The dummy test info does not escape from exceptions, like the default
;;; `on-exception` handler. Hence exceptions inside of a `call-as-test`
;;; will trip the actual exception handler for the meta test-info.

(define dummy-dict
  (let ((dummy (lambda _ #f)))
    (dict-set! (test-dto)
               default-test-info-dict
               'skip-test? dummy
               'when-test-skipped dummy
               'before-test! dummy
               'setup-test! dummy
               'cleanup-test! dummy
               'after-test values
               'report-test dummy
               'on-exception-in-test dummy
               'skip-group? dummy
               'when-group-skipped dummy
               'before-group! dummy
               'setup-group! dummy
               'cleanup-group! dummy
               'after-group values
               'report-group dummy
               'on-exception-in-group dummy)))

(test-group "call-as-test, dummy dict"
  (test-group "before test"
    (test-body "skip-test? skips tests when returning #f"
      (parameterize ((test-info
                      (dict-set! (test-dto)
                                 dummy-dict
                                 'skip-test?
                                 (lambda (name) #t))))
        (let ((called? #f))
          (call-as-test "name" (lambda () (set! called? #t)))
          (not called?))))
    (test-body "before-test gets the test name"
      (parameterize ((test-info
                      (dict-set! (test-dto)
                                 dummy-dict
                                 'before-test!
                                 (lambda (name)
                                   (unless (equal? name "1234")
                                     (raise "exception"))
                                   #t))))
        (let ((called? #f))
          (call-as-test "1234"
                        (lambda () (set! called? #t)))
          called?))))
  (test-group "after test"
    (let ((called? #f)
          (pair (cons #f #f)))
      (test-body "after test is called with previous test-info"
        (parameterize ((test-info
                        (dict-set! (test-dto) dummy-dict
                                   'after-test
                                   (lambda (previous-dict)
                                     (set! called?
                                           (dict-ref (test-dto)
                                                     previous-dict
                                                     'after-test-test))))))
          (call-as-test #f
                        (lambda ()
                          (test-set! 'after-test-test #t))))
        called?))
    (let* ((pair (cons #f #f)))
      (test-eq "return value of call-as-test is after-test"
               pair
               (parameterize ((test-info
                               (dict-set! (test-dto) dummy-dict
                                          'after-test
                                          (lambda _ pair))))
                 (call-as-test #f (lambda () #f))))))
  (test-body "setup-test runs in dynamic extent"
    (let ((global-name "setup-test-name")
          (called? #f)
          (pair (cons #f #f)))
      (parameterize ((test-info
                      (dict-set! (test-dto)
                                 dummy-dict
                                 'setup-test!
                                 (lambda (name)
                                   (test-set! 'setup-test-test pair)))))
        (call-as-test global-name
                      (lambda ()
                        (set! called? (eq? (test-ref 'setup-test-test) pair)))))
      (and called? (not (test-contains? 'setup-test-test)))))
  (test-group "on-exception"
    (test-body "not called"
      (call/cc
       (lambda (return)
         (define inside-test-info
           (dict-set! (test-dto)
                      dummy-dict
                      'exception-not-called?
                      #t
                      'on-exception-in-test
                      (lambda (exn return)
                        (test-set! 'exception-not-called? #f)
                        (return))
                      'after-test
                      (lambda (previous-dict)
                        (return (dict-ref (test-dto)
                                          previous-dict
                                          'exception-not-called?)))))
         (parameterize ((test-info inside-test-info))
           (call-as-test #f (lambda () #f)))
         #f)))
    (let* ((message "exception message")
           (caught #f))
      (test-body "called"
        (parameterize ((test-info (dict-set! (test-dto) dummy-dict
                                             'on-exception-in-test
                                             (lambda (exn return)
                                               (set! caught exn)
                                               (return)))))
          (call-as-test #f (lambda () (raise message)))
          caught)))))

;;; ;;;;;;;;;;;;;;;;
;;; Testing the default test handler, except for 'report-test.
;;; 
;;; The default report test writes to standard output, and the
;;; return value of report-test is the return value of call-as-test
;;; because report-test is called by after-test.
;;; 
;;; For `silent-dict`, the report procedures return the dto and 
;;; dictionary inside the test/group. This is different from the
;;; dictionary containing the test/group, which is returned using
;;; `(inspect-test-info values)` inside the dynamic extent.

(define silent-dict
  (dict-set! default-test-dto default-test-info-dict
             'before-test! values
             'before-group! values
             'report-test values
             'report-group values))

(test-group "call-as-test, some defaults"
  (test-group "no name"
    (define-values (dict outer-dict)
      (parameterize ((test-info silent-dict))
        (let ((inner-dict (call-as-test #f (lambda () #f))))
          (values inner-dict (test-info-dict)))))
    (test-eqv "name is #f" #f (dict-ref (test-dto) dict 'name))
    (test-eqv "name-stack" '() (dict-ref (test-dto) dict 'name-stack))
    (test-eqv "outer name is #f" #f (dict-ref (test-dto) outer-dict 'name))
    (test-eqv "outer name-stack" '() (dict-ref (test-dto) outer-dict 'name-stack)))
  (test-group "named"
    (define-values (dict outer-dict)
      (parameterize ((test-info silent-dict))
        (let ((inner-dict (call-as-test "asdfasdf" (lambda () #f))))
          (values inner-dict (test-info-dict)))))
    (test-equal "name is #f" "asdfasdf" (dict-ref (test-dto) dict 'name))
    (test-equal "name-stack" '("asdfasdf") (dict-ref (test-dto) dict 'name-stack))
    (test-eqv "outer name is #f" #f (dict-ref (test-dto) outer-dict 'name))
    (test-eqv "outer name-stack" '() (dict-ref (test-dto) outer-dict 'name-stack)))
  (test-group "tests that success is true"
    (define dict
      (parameterize ((test-info silent-dict))
        (call-as-test #f (lambda () (test-set! 'success? #t)))
        (test-info-dict)))
    (test-eqv "passed number" 1 (dict-ref (test-dto) dict 'passed))
    (test-eqv "failed number" 0 (dict-ref (test-dto) dict 'failed))
    (test-eqv "test number" 1 (dict-ref (test-dto) dict 'tests)))
  (test-group "test that success is false"
    (define dict
      (parameterize ((test-info silent-dict))
        (call-as-test #f (lambda () (test-set! 'success? #f)))
        (test-info-dict)))
    (test-eqv "passed number" 0 (dict-ref (test-dto) dict 'passed))
    (test-eqv "failed number" 1 (dict-ref (test-dto) dict 'failed))
    (test-eqv "test number" 1 (dict-ref (test-dto) dict 'tests)))
  (test-group "success not set"
    (define dict
      (parameterize ((test-info silent-dict))
        (call-as-test #f (lambda () #f))
        (test-info-dict)))
    (test-eqv "passed number" 0 (dict-ref (test-dto) dict 'passed))
    (test-eqv "failed number" 1 (dict-ref (test-dto) dict 'failed))
    (test-eqv "test number" 1 (dict-ref (test-dto) dict 'tests)))
  (test-group "catching exceptions"
    (define pair (cons #f #f))
    (define-values (inner-dict outer-dict)
      (parameterize ((test-info silent-dict))
        (let ((inner-dict (call-as-test #f (lambda () (raise pair)))))
          (values inner-dict (test-info-dict)))))
    (test-eqv "exception" pair (dict-ref (test-dto) inner-dict 'exception))
    (test-eqv "passed number" 0 (dict-ref (test-dto) outer-dict 'passed))
    (test-eqv "failed number" 1 (dict-ref (test-dto) outer-dict 'failed))
    (test-eqv "test number" 1 (dict-ref (test-dto) outer-dict 'tests))))

(test-group "call-as-group"
  (test-group "group with no tests"
    (define dict
      (parameterize ((test-info silent-dict))
        (call-as-group #f (lambda () #f))
        (test-info-dict)))
    (test-eqv "passed number" 0 (dict-ref (test-dto) dict 'passed))
    (test-eqv "tests number" 0 (dict-ref (test-dto) dict 'tests))
    (test-eqv "failed number" 0 (dict-ref (test-dto) dict 'failed))
    (test-eqv "skipped number" 0 (dict-ref (test-dto) dict 'skipped)))
  (test-group "group with 1 test"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-group #f (test-body #f #t))
        (test-info-dict)))
    (test-eqv "passed number" 1 (dict-ref (test-dto) dict 'passed))
    (test-eqv "tests number" 1 (dict-ref (test-dto) dict 'tests))
    (test-eqv "failed number" 0 (dict-ref (test-dto) dict 'failed))
    (test-eqv "skipped number" 0 (dict-ref (test-dto) dict 'skipped)))
  (test-group "group with multiple tests"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-group #f
          (call-as-test #f (lambda () (test-set! 'success? #t)))
          (call-as-test #f (lambda () (test-set! 'success? #t)))
          (call-as-test #f (lambda () (test-set! 'success? #f)))
          (test-skip-all
           (call-as-test #f (lambda () (test-set! 'success? #f)))))
        (test-info-dict)))
    (begin
      (test-eqv "passed number" 2 (dict-ref (test-dto) dict 'passed))
      (test-eqv "tests number" 4 (dict-ref (test-dto) dict 'tests))
      (test-eqv "failed number" 1 (dict-ref (test-dto) dict 'failed))
      (test-eqv "skipped number" 1 (dict-ref (test-dto) dict 'skipped))))
  (test-group "nested groups"
    (define inner-dict #f)
    (define dict
      (parameterize ((test-info silent-dict))
        (test-group "gr1"
          (test-body #f #t)
          (test-group "gr2"
            (test-body #f
              (set! inner-dict (test-info-dict))
              #f)))
        (inspect-test-info values)))
    (test-eqv "passed number" 1 (dict-ref (test-dto) dict 'passed))
    (test-eqv "tests number" 2 (dict-ref (test-dto) dict 'tests))
    (test-eqv "failed number" 1 (dict-ref (test-dto) dict 'failed))
    (test-eqv "skipped number" 0 (dict-ref (test-dto) dict 'skipped))
    (test-eqv "inner name" #f (dict-ref (test-dto) inner-dict 'name))
    (test-equal "inner name stack" '("gr2" "gr1")
                (dict-ref (test-dto) inner-dict 'name-stack))))

(test-group "test named application"
  (test-body "true"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-named-application "not" (not not) (arg #f))
        (test-info-dict)))
    (eqv? 1 (dict-ref (test-dto) dict 'passed)))
  (test-body "false"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-named-application "not" (not not) (arg #t))
        (test-info-dict)))
    (eqv? 1 (dict-ref (test-dto) dict 'failed))))

(test-group "test-equal"
  (test-body "true"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-equal "equal" "abc" "abc")
        (test-info-dict)))
    (eqv? 1 (dict-ref (test-dto) dict 'passed)))
  (test-body "false"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-equal "equal" "abc" "def")
        (test-info-dict)))
    (eqv? 1 (dict-ref (test-dto) dict 'failed))))

(test-group "test-eq"
  (test-body "true"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-eq "eq" 'abc 'abc)
        (test-info-dict)))
    (eqv? 1 (dict-ref (test-dto) dict 'passed)))
  (test-body "false"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-eq "eq" 'abc 'def)
        (test-info-dict)))
    (eqv? 1 (dict-ref (test-dto) dict 'failed))))

(test-group "test-eqv"
  (test-body "true"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-eqv "eqv" 100 100)
        (test-info-dict)))
    (eqv? 1 (dict-ref (test-dto) dict 'passed)))
  (test-body "false"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-eqv "eqv" 100 200)
        (test-info-dict)))
    (eqv? 1 (dict-ref (test-dto) dict 'failed))))

(test-group "test-approximate"
  (test-body "true"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-approximate "approx" 1 1.001 0.01)
        (test-info-dict)))
    (eqv? 1 (dict-ref (test-dto) dict 'passed)))
  (test-body "false"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-approximate "approx" 100 1.01 0.01)
        (test-info-dict)))
    (eqv? 1 (dict-ref (test-dto) dict 'failed))))

(test-group "test-error"
  (test-body "thrown exception"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-error #f
          (lambda (ex) (equal? ex "exception"))
          (raise "exception"))
        (test-info-dict)))
    (eqv? 1 (dict-ref (test-dto) dict 'passed)))
  (test-body "no thrown exception"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-error #f
          (lambda (ex) #t)
          #t)
        (test-info-dict)))
    (eqv? 1 (dict-ref (test-dto) dict 'failed)))
  (test-body "incorrectly thrown exception"
    (define dict
      (parameterize ((test-info silent-dict))
        (test-error #f number? (raise "exception"))
        (test-info-dict)))
    (eqv? 1 (dict-ref (test-dto) dict 'failed))))

(test-group "expect-to-fail"
  (expect-to-fail
    (test-body "1 = 2" (eqv? 1 2))
    (cond-expand
      ;; SKINT will actually raise a noncatchable error here!
      ((not skint) (test-body "type error" (car '())))
      (else #f))))

(test-exit)