-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.ss
132 lines (105 loc) · 4.01 KB
/
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
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
#lang planet masm/sines
(define (execute-test-pred-with equality-pred thunk e)
(with-exception-handler
(lambda (ex)
(display ex) (newline)
#f)
(lambda () (equality-pred (thunk) e))))
(define (check-expect-helper-aux equality-pred test-thunk test-form expected)
(with-exception-handler
(lambda (ex)
(failure "~S thrown an exception ~S; expected ~S" test-form ex expected))
(lambda ()
(let ([test-result (test-thunk)])
(if (equality-pred test-result expected)
(success test-form expected)
(failure "~S evaluated to ~S; expected ~S" test-form test-result expected))))))
(define-syntax-rule (check-expect-helper equality-pred test expected)
(check-expect-helper-aux equality-pred (lambda () test) 'test expected))
(define (check-expect-one-of-helper-aux equality-pred test-thunk test-form . expected-list)
(with-exception-handler
(lambda (ex)
(failure "~S thrown an exception ~S; expected one of ~S" test-form ex expected-list))
(lambda ()
(let ([test-result (test-thunk)])
(cond [(find (lambda (expected)
(equality-pred test-result expected))
expected-list)
=> (lambda (expected) (success test-form expected))]
[else (failure "~S evaluated to ~S; expected one of ~S" test-form test-result expected-list)])))))
(define-syntax-rule (check-expect-one-of-helper equality-pred test expected more-expected ...)
(check-expect-one-of-helper-aux equality-pred (lambda () test) 'test expected more-expected ...))
(define-syntax (check-expect stx)
(syntax-case stx ()
[(_ test expected)
#'(check-expect-helper equal? test expected)]))
(define-syntax (check-expect-one-of stx)
(syntax-case stx ()
[(_ test expected more-expected ...)
#'(check-expect-one-of-helper equal? test expected more-expected ...)]))
(define-syntax (check-expect-eq stx)
(syntax-case stx ()
[(_ test expected)
#'(check-expect-helper eq? test expected)]))
(define-syntax (check-expect-eq-one-of stx)
(syntax-case stx ()
[(_ test expected more-expected ...)
#'(check-expect-one-of-helper eq? test expected more-expected ...)]))
(define-syntax (check-expect-eqv stx)
(syntax-case stx ()
[(_ test expected)
#'(check-expect-helper eqv? test expected)]))
(define-syntax (check-expect-eqv-one-of stx)
(syntax-case stx ()
[(_ test expected more-expected ...)
#'(check-expect-one-of-helper eqv? test expected more-expected ...)]))
(define-syntax (check-quoted-expect stx)
(syntax-case stx ()
[(_ test expected)
#'(check-expect test 'expected)]))
(define-syntax (check-quoted-expect-one-of stx)
(syntax-case stx ()
[(_ test expected more-expected ...)
#'(check-expect-one-of test 'expected 'more-expected ...)]))
(define error-count 0)
(define failure-list '())
(define success-list '())
(define (error-x name message . args)
(display name) (display ": ") (display message) (display " - ") (display args) (newline))
(define (failure message . args)
(apply error-x 'check-expect message args)
(set! error-count (add1 error-count))
(set! failure-list (cons (cons message args)
failure-list)))
(define (success test expected)
(set! success-list (cons (list '->/v test expected) success-list))
;; (display test)
;; (newline)
)
(define (report-test-summary)
(newline)
(display (format "\n ~A tests ran\n ~A tests passed\n ~A tests failed\n"
(+ (length success-list) (length failure-list))
(length success-list)
(length failure-list)))
(newline))
(provide
check-expect
check-quoted-expect
check-expect-one-of
check-quoted-expect-one-of
report-test-summary)
;;;
(define-syntax-rule (check-true a)
(check-expect a #t))
(define-syntax-rule (check-false a)
(check-expect a #f))
(define-syntax-rule (check-equal? a b)
(check-expect a b))
(define-syntax-rule (check-eqv? a b)
(check-expect-eqv a b))
(provide
check-true
check-false
check-equal?
check-eqv?)