-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathskeleton-test.ss
88 lines (83 loc) · 2.85 KB
/
skeleton-test.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
(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)))