forked from exercism/scheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.scm
144 lines (133 loc) · 4.8 KB
/
test.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
(import (except (rnrs) current-output-port))
(define test-fields '(input output))
(define (test-run-solution solution input)
(if (procedure? solution) (apply solution input) solution))
(define (test-success description success-predicate
procedure input output)
(call/cc
(lambda (k)
(let ([out (open-output-string)])
(with-exception-handler
(lambda (e)
(let ([result `(fail
(description . ,description)
(input . ,input)
(output . ,output)
(stdout . ,(get-output-string out)))])
(close-output-port out)
(k result)))
(lambda ()
(let ([result (parameterize ([current-output-port out])
(test-run-solution procedure input))])
(unless (success-predicate result output)
(error 'exercism-test
"test fails"
description
input
result
output)))
(let ([result `(pass
(description . ,description)
(stdout . ,(get-output-string out)))])
(close-output-port out)
result)))))))
(define (test-error description procedure input)
(call/cc
(lambda (k)
(let ([out (open-output-string)])
(with-exception-handler
(lambda (e)
(let ([result `(pass
(description . ,description)
(stdout . ,(get-output-string out)))])
(close-output-port out)
(k result)))
(lambda ()
(parameterize ([current-output-port out])
(test-run-solution procedure input))
(let ([result `(fail
(description . ,description)
(input . ,input)
(output . error)
(stdout . ,(get-output-string out)))])
(close-output-port out)
result)))))))
(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 (lambda (test) (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 test-cases)
(write (map (lambda (test) (test)) test-cases)))
(define pascals-triangle)
(define test-cases
(list
(lambda ()
(test-success "zero rows" equal? pascals-triangle '(0) '()))
(lambda ()
(test-success "single row" equal? pascals-triangle '(1)
'((1))))
(lambda ()
(test-success "two rows" equal? pascals-triangle '(2)
'((1) (1 1))))
(lambda ()
(test-success "three rows" equal? pascals-triangle '(3)
'((1) (1 1) (1 2 1))))
(lambda ()
(test-success "four rows" equal? pascals-triangle '(4)
'((1) (1 1) (1 2 1) (1 3 3 1))))
(lambda ()
(test-success "five rows" equal? pascals-triangle '(5)
'((1) (1 1) (1 2 1) (1 3 3 1) (1 4 6 4 1))))
(lambda ()
(test-success "six rows" equal? pascals-triangle '(6)
'((1) (1 1) (1 2 1) (1 3 3 1) (1 4 6 4 1) (1 5 10 10 5 1))))
(lambda ()
(test-success "ten rows" equal? pascals-triangle '(10)
'((1) (1 1) (1 2 1) (1 3 3 1) (1 4 6 4 1) (1 5 10 10 5 1)
(1 6 15 20 15 6 1) (1 7 21 35 35 21 7 1)
(1 8 28 56 70 56 28 8 1) (1 9 36 84 126 126 84 36 9 1))))))
(define (test . query)
(apply run-test-suite test-cases query))
(let ([args (command-line)])
(cond
[(null? (cdr args))
(load "pascals-triangle.scm")
(test 'input 'output)]
[(string=? (cadr args) "--docker")
(load "pascals-triangle.scm")
(run-docker test-cases)]
[else (load (cadr args)) (test 'input 'output)]))