-
Notifications
You must be signed in to change notification settings - Fork 0
/
minitest.zp
171 lines (153 loc) · 5.68 KB
/
minitest.zp
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
164
165
166
167
168
169
170
171
(module "minitest"
(export
(list :assert-equal assert-equal)
(list :assert-not-equal assert-not-equal)
(list :assert-true assert-true)
(list :assert-false assert-false)
(list :assert-truthy assert-truthy)
(list :assert-falsy assert-falsy)
(list :assert-nil assert-nil)
(list :assert-not-nil assert-not-nil)
(list :all-passed all-passed)
(list :exit-first-fail exit-first-fail)
(list :colorize colorize)
(list :verbose verbose)
(list :results results))
(pass-count 0)
(fail-count 0)
(_exit #f)
(_colorize #f)
(_verbose #f)
(_c (lambda (x) (if _colorize (color x))))
(exit-first-fail (lambda (x)
"sets whether the tests should terminate after the first failing test.
(defaults: false)
params:
- x: the truth value
complexity: O(1)
returns: <par>x</par>"
(set! _exit x)))
(colorize (lambda (x)
"sets whether the output should be colorized.
(defaults: false)
params:
- x: the truth value
complexity: O(1)
returns: <par>x</par>"
(set! _colorize x)))
(verbose (lambda (x)
"sets whether the output should be verbose.
(defaults: false)
params:
- x: the truth value
complexity: O(1)
returns: <par>x</par>"
(set! _verbose x)))
(unit-test-handler (lambda (expected actual descr what . eq)
(let ((test (get-from eq 0 (lambda (x y) (eqv? x y)))))
(if (not (test expected actual))
(begin (if _verbose
(begin (_c :red) (map display (reverse (list "Test \"" descr "\" failed\n\t"))) (_c :reset)
(map display (reverse (list "Expected " what ": \"" expected "\", actual value: \"" actual "\"\n"))))
(begin (_c :red) (display "F") (_c :reset)))
(if _exit (begin (_c :red) (write "Exiting on first fail") (_c :reset) (exit 1)))
(set! fail-count (+ fail-count 1)))
(begin (if _verbose
(begin (_c :green) (write (++ "Test \"" descr "\" passed")) (_c :reset))
(begin (_c :green) (display "P") (_c :reset)))
(set! pass-count (+ pass-count 1)))))))
(assert-equal (lambda (x y . descr)
"assert that <par>x</par> and <par>y</par> are equal.
params:
- x: the expected value
- y: the actual value
- descr: optional argument that describes the test
complexity: O(1)
returns: the number of passed tests"
(let ((descr (get-from descr 0 "assert-equal")))
(unit-test-handler x y descr "value"))))
(assert-not-equal (lambda (x y . descr)
"assert that <par>x</par> and <par>y</par> are not equal.
params:
- x: the expected value
- y: the actual value
- descr: optional argument that describes the test
complexity: O(1)
returns: the number of passed tests"
(let ((descr (get-from descr 0 "assert-not-equal")))
(unit-test-handler x y descr "not" (lambda (x y) (not (eqv? x y)))))))
(assert-true (lambda (x . descr)
"assert that <par>x</par> is true.
params:
- x: the given value
- descr: optional argument that describes the test
complexity: O(1)
returns: the number of passed tests"
(let ((descr (get-from descr 0 "assert-true")))
(assert-equal #t x descr))))
(assert-false (lambda (x . descr)
"assert that <par>x</par> is false.
params:
- x: the given value
- descr: optional argument that describes the test
complexity: O(1)
returns: the number of passed tests"
(let ((descr (get-from descr 0 "assert-false")))
(assert-equal #f x descr))))
(assert-truthy (lambda (x . descr)
"assert that <par>x</par> is truthy.
params:
- x: the given value
- descr: optional argument that describes the test
complexity: O(1)
returns: the number of passed tests"
(let ((descr (get-from descr 0 "assert-truthy")))
(unit-test-handler x (truthy? x) descr "to be truthy" (lambda (x y) (truthy? x))))))
(assert-falsy (lambda (x . descr)
"assert that <par>x</par> is falsy.
params:
- x: the given value
- descr: optional argument that describes the test
complexity: O(1)
returns: the number of passed tests"
(let ((descr (get-from descr 0 "assert-falsy")))
(unit-test-handler x (falsy? x) descr "to be falsy" (lambda (x y) (falsy? x))))))
(assert-nil (lambda (x . descr)
"assert that <par>x</par> is nil.
params:
- x: the given value
- descr: optional argument that described the test
complexity: O(1)
returns: the number of passed tests"
(let ((descr (get-from descr 0 "assert-nil")))
(assert-equal (nil) x descr))))
(assert-not-nil (lambda (x . descr)
"assert that <par>x</par> is not nil.
params:
- x: the given value
- descr: optional argument that described the test
complexity: O(1)
returns: the number of passed tests"
(let ((descr (get-from descr 0 "assert-not-nil")))
(assert-not-equal (nil) x descr))))
(results (lambda ()
"prints the test results.
complexity: O(1)
returns: the number of failed tests"
(begin
(write "Results: ")
(if _verbose (begin
(_c :green)
(if (> pass-count 0) (display (++ "\t|" (string:replicate pass-count "="))))
(if (eq? fail-count 0) (write "|"))
(_c :red)
(if (eq? pass-count 0) (display "\t|"))
(if (> fail-count 0) (write (++ (string:replicate fail-count "=") "|")))))
(_c :green) (map display (reverse (list "\tPassed: " pass-count)))
(_c :red) (map display (reverse (list " Failed: " fail-count "\n"))) (_c :reset)
fail-count)))
(all-passed (lambda ()
"returns whether the tests all passed.
complexity: O(1)
returns: a boolean"
(= fail-count 0))))