-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtesting.scm
121 lines (103 loc) · 4.02 KB
/
testing.scm
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
;;; ----------------------------------------------------------------------
;;; Copyright 2007-2009 Alexey Radul.
;;; ----------------------------------------------------------------------
;;; This file is part of Test Manager.
;;;
;;; Test Manager is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Test Manager 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 the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Test Manager. If not, see <http://www.gnu.org/licenses/>.
;;; ----------------------------------------------------------------------
;;;; Test Registration
(define (register-test test)
(tg:register-test! *current-test-group* test))
(define *anonymous-test-count* 0)
(define (generate-test-name)
(set! *anonymous-test-count* (+ *anonymous-test-count* 1))
(string->symbol
(string-append "anonymous-test-" (number->string *anonymous-test-count*))))
(define (detect-docstring structure)
(if (string? structure)
structure
#f))
(define-syntax define-test
(syntax-rules ()
((define-test (name formal ...) body-exp1 body-exp2 ...)
(let ((proc (lambda (formal ...) body-exp1 body-exp2 ...)))
(register-test
(make-single-test `name proc (detect-docstring (quote body-exp1))))))
((define-test () body-exp1 body-exp2 ...)
(let ((proc (lambda () body-exp1 body-exp2 ...)))
(register-test
(make-single-test (generate-test-name) proc (detect-docstring (quote body-exp1))))))
((define-test form)
(let ((proc (lambda () form)))
(register-test
(make-single-test (generate-test-name) proc (quote form)))))))
(define-syntax define-each-test
(syntax-rules ()
((define-each-test form ...)
(begin (define-test form) ...))))
(define-syntax define-each-check
(syntax-rules ()
((define-each-check form ...)
(begin (define-test () (check form)) ...))))
(define (clear-registered-tests!)
(tg:clear! (current-test-group)))
;;;; Test Running
;; Poor man's dynamic dispatch by storing the
;; procedures that do the job in a record
(define (run-given-test test-runner test)
((tr:run-one test-runner) (list (st:name test)) test))
(define (run-given-group test-runner group name-stack)
((tr:run-group test-runner) group name-stack))
(define (run-given-test-or-group test-runner test name-stack)
(cond ((test-group? test)
(run-given-group test-runner test name-stack))
((single-test? test)
(run-given-test test-runner test))
(else
(error "Unknown test type" test))))
(define (report-results test-runner)
((tr:report-results test-runner)))
;; Allows access to old test results if needed and keeps failure
;; continuations from getting garbage collected.
(define *last-test-runner* #f)
(define (run-test test-name-stack . opt-test-runner)
(let-optional opt-test-runner ((test-runner (make-standard-test-runner)))
(let loop ((test (current-test-group))
(stack-left test-name-stack)
(stack-traversed '()))
(cond ((null? stack-left)
(run-given-test-or-group test-runner test (reverse stack-traversed)))
((test-group? test)
(tg:in-group-context test
(lambda ()
(tg:in-test-context test
(lambda ()
(loop (tg:get test (car stack-left))
(cdr stack-left)
(cons (car stack-left) stack-traversed)))))))
(else
(error "Name stack did not lead to a valid test" test-name-stack))))
(set! *last-test-runner* test-runner)
(report-results test-runner)))
(define (run-registered-tests . opt-test-runner)
(apply run-test (cons '() opt-test-runner)))
(cond-expand
(guile
'TODO)
(else
(define (run-tests-and-exit)
(let ((v (show-time run-registered-tests)))
(newline)
(flush-output)
(%exit v)))))