-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtest-util.ss
163 lines (150 loc) · 5.79 KB
/
test-util.ss
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
(import (except (rnrs) current-output-port))
(define test-fields '(input expected actual))
(define (test-run-solution solution input)
(if (procedure? solution) (apply solution input) solution))
(define (scheme->string o)
(with-output-to-string
(lambda ()
(write o))))
(define (process-condition e)
(if (not (condition? e)) e
`(error
,(if (who-condition? e) (condition-who e)
'unknown)
,(condition-message e)
,@(map scheme->string
(if (not (irritants-condition? e)) '()
(condition-irritants e))))))
(define (test-success description success-predicate
procedure input expected code)
(call/cc
(lambda (k)
(let ([out (open-output-string)])
(dynamic-wind
(lambda () (set! out (open-output-string)))
(lambda ()
(with-exception-handler
(lambda (e)
(k `(fail
(description . ,description)
(code . ,code)
(input . ,input)
(expected . ,expected)
(actual . ,(process-condition e))
(stdout . ,(get-output-string out)))))
(lambda ()
(let ([result (parameterize ([current-output-port out])
(test-run-solution procedure input))])
(unless (success-predicate result expected)
(raise result))
`(pass
(description . ,description)
(code . ,code)
(stdout . ,(get-output-string out)))))))
(lambda () (close-output-port out)))))))
(define (test-error description procedure input code)
(call/cc
(lambda (k)
(let ([out '()])
(dynamic-wind
(lambda () (set! out (open-output-string)))
(lambda ()
(with-exception-handler
(lambda (e)
(k `(pass
(description . ,description)
(code . ,code)
(stdout . ,(get-output-string out)))))
(lambda ()
(let ((result (parameterize ([current-output-port out])
(test-run-solution procedure input))))
`(fail
(description . ,description)
(code . ,code)
(input . ,input)
(expected . error)
(actual . ,result)
(stdout . ,(get-output-string out)))))))
(lambda () (close-output-port out)))))))
(define (run-test test)
(eval (append test `((quote ,test))) (interaction-environment)))
(define (run-test-suite tests . query)
(for-each
(lambda (field)
(unless (and (symbol? field) (memq field test-fields))
(error 'run-test-suite
(format #t "~a not in ~a" field test-fields))))
query)
(let-values ([(passes failures)
(partition
(lambda (result) (eq? 'pass (car result)))
(map run-test tests))])
(cond
[(null? failures) (format #t "~%Well done!~%~%")]
[else
(format
#t
"~%Passed ~a/~a tests.~%~%The following test cases failed:~%~%"
(length passes)
(length tests))
(for-each
(lambda (failure)
(format
#t
"* ~a~%"
(cond
[(assoc 'description (cdr failure)) => cdr]
[else (cdr failure)]))
(for-each
(lambda (field)
(let ([info (assoc field (cdr failure))])
(display " - ")
(write (car info))
(display ": ")
(write (cdr info))
(newline)))
query))
failures)
(error 'test "incorrect solution")])))
(define (run-docker suite)
(write (map run-test suite)))
(define (test suite . query)
(apply run-test-suite suite query))
(define (tests suites . query)
(for-each (lambda (suite) (apply test suite query)) suites))
(define (run-with-cli solution suites)
(let ((args (command-line)))
(cond
;; Normal execution. This is the default behavior used by students
;; running their tests locally.
[(null? (cdr args))
(load solution)
(tests suites 'input 'expected 'actual)]
;; Scheme programs ingesting this output can expect an alist with
;; the keys 'test-lib-version and 'status. No test-lib version
;; means an older version of these test utilities is in use, so there
;; will only be pass/fail lists in the output. When status is 'error,
;; A message is provided for explanation. It is usually a stringified
;; condition. When status is 'completed everything is normal, and the
;; rest of the list comsists of pass/fail lists.
[(string=? (cadr args) "--docker")
(write
`((test-lib-version . 1)
,@(call/cc
(lambda (k)
(with-exception-handler
;; Catch failures while loading/compiling the solution.
(lambda (e)
(k `((status . error)
(message
. ,(string-append
"Failed with value: "
(scheme->string (process-condition e)))))))
(lambda ()
(load solution)
`((status . ok)
,@(fold-left (lambda (results suite)
(append results (map run-test suite)))
'() suites))))))))]
;; You can pass the name of a file to load instead of the "expected" solution filename.
[else (load (cadr args)) (tests suites 'input 'expected 'actual)])))