-
Notifications
You must be signed in to change notification settings - Fork 4
/
hytest.hy
347 lines (309 loc) · 11.2 KB
/
hytest.hy
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
(import [hy.importer [import-file-to-module]]
[collections [OrderedDict]]
[os [walk getcwd path]]
traceback
warnings
sys)
(if-python2
(try
(import [cStringIO [StringIO]])
(except []
(import [StringIO [StringIO]])))
(import [io [StringIO]]))
(def __version__ "0.2")
(try
(import colorama)
(except [])
(else (colorama.init)))
(try
(import [hy.models [HyExpression HyInteger HyString HySymbol]])
(except []
(import [hy.models.expression [HyExpression]
hy.models.integer [HyInteger]
hy.models.string [HyString]
hy.models.symbol [HySymbol]])))
(defclass SkipException [Exception] [])
(defclass FailException [Exception] [])
(defmacro skip-test [reason]
`(raise (.SkipException (__import__ "hytest") ~reason)))
(defmacro skip-test-if [cond reason]
`(if ~cond (raise (.SkipException (__import__ "hytest") ~reason))))
(defmacro skip-test-unless [cond reason]
`(if-not ~cond (raise (.SkipException (__import__ "hytest") ~reason))))
(defmacro fail-test [&optional [msg ""]]
`(raise (.FailException (__import__ "hytest") ~msg)))
(defn fm [s &rest args]
`(% ~(HyString s) (cut (, ~@args) 0 ~(HyInteger (s.count "%")))))
(defmacro --hytest-fm [s &rest args]
`(% ~s (, ~@args)))
(defn tst [exp msg]
`(if-not ~exp (fail-test ~msg)))
(defn cmp-base [op lhs rhs fms]
(setv ln (gensym) rn (gensym) astr (gensym))
`(do
(setv ~ln ~lhs ~rn ~rhs ~astr ~(if-python2 `basestring `(, str bytes)))
~(tst `(~op ~ln ~rn)
`(+
~(fm fms `(repr ~ln) `(repr ~rn))
(if (and (isinstance ~ln ~astr) (isinstance ~rn ~astr)
(in "\n" ~ln) (in "\n" ~rn))
(+ "\n" (.join "\n"
(.ndiff (__import__ "difflib")
(.splitlines ~ln)
(.splitlines ~rn))))
"")))
(del ~ln ~rn ~astr)))
(defn test-eq [lhs rhs] (cmp-base `= lhs rhs "%s != %s"))
(defn test-ne [lhs rhs] (cmp-base `!= lhs rhs "%s == %s"))
(defn test-lt [lhs rhs] (cmp-base `< lhs rhs "%s >= %s"))
(defn test-gt [lhs rhs] (cmp-base `> lhs rhs "%s <= %s"))
(defn test-lte [lhs rhs] (cmp-base `<= lhs rhs "%s > %s"))
(defn test-gte [lhs rhs] (cmp-base `>= lhs rhs "%s < %s"))
(defn test-is [lhs rhs] (cmp-base `is lhs rhs "%s is not %s"))
(defn test-is-not [lhs rhs] (cmp-base `is-not lhs rhs "%s is %s"))
(defn test-is-nil [x] (test-is x `None))
(defn test-is-not-nil [x] (test-is-not x `None))
(defn almost-base [op] `(fn [lhs rhs] (~op (round (- lhs rhs) 7) 0)))
(defn test-almost-eq [lhs rhs] (cmp-base (almost-base `=) lhs rhs
"%s is not almost equal to %s"))
(defn test-almost-ne [lhs rhs] (cmp-base (almost-base `!=) lhs rhs
"%s is almost equal to %s"))
(defn test-regex [s r] (cmp-base `(fn [s r] (.search (__import__ "re") r s)) s r
"%s does not match regex %s"))
(defn test-not-regex [s r] (cmp-base `(fn [s r]
(not (.search (__import__ "re") r s))) s r
"%s matches regex %s"))
(defn items-base [op] `(fn [lhs rhs] (~op (sorted lhs) (sorted rhs))))
(defn test-items-eq [lhs rhs] (cmp-base (items-base `=) lhs rhs
"items in %s are not equal to items in %s"))
(defn test-items-ne [lhs rhs] (cmp-base (items-base `!=) lhs rhs
"items in %s are equal to items in %s"))
(defn test-true [x] (cmp-base `(fn [x _] x) x `None "%s is not true"))
(defn test-not [x] (cmp-base `(fn [x _] (not x)) x `None "%s is not true"))
(defn test-in [x sq] (cmp-base `in x sq "item %s not in sequence %s"))
(defn test-not-in [x sq] (cmp-base `not-in x sq "item %s is in sequence %s"))
(def raise-var (gensym))
(defn test-raises-any [&rest body]
`(try
~@body
(except [])
(else (fail-test "code did not raise exception"))))
(defn test-raises [exceptions &rest body]
(setv strexc (HyString (.join ", " (map str exceptions))))
`(try
~@body
(except [[~@exceptions]])
(else (fail-test (+ "code did not raise one of: " ~strexc)))))
(defn test-raises-msg [m &rest body]
`(try
~@body
(except [~raise-var Exception]
(if-not (.search (__import__ "re") ~m (str ~raise-var))
(fail-test
~(fm "exception message '%s' did not match %s" `(str ~raise-var) m))))
(else (fail-test "code did not raise exception"))))
(defn test-not-raises-any [&rest body]
`(try
~@body
(except [~raise-var Exception]
(fail-test (+ "code raised exception " (repr ~raise-var))))))
(defn test-not-raises [exceptions &rest body]
`(try
~@body
(except [~raise-var [~@exceptions]]
(fail-test (+ "code raised exception " (repr ~raise-var))))))
(defn test-not-raises-msg [m &rest body]
`(try
~@body
(except [~raise-var Exception]
(if (.search (__import__ "re") ~m (str ~raise-var))
(fail-test ~(fm "raised exception message '%s' matched %s"
`(str ~raise-var) m))))))
(def opmap {"=" test-eq
"!=" test-ne
"<" test-lt
">" test-gt
"<=" test-lte
">=" test-gte
"eq" test-eq
"ne" test-ne
"lt" test-lt
"gt" test-gt
"lte" test-lte
"gte" test-gte
"is" test-is
"is-not" test-is-not
"is-nil" test-is-nil
"is-not-nil" test-is-not-nil
"is-none" test-is-nil
"~" test-almost-eq
"!~" test-almost-ne
"aeq" test-almost-eq
"ane" test-almost-ne
"=~" test-regex
"!=~" test-not-regex
"re" test-regex
"not-re" test-not-regex
"=:" test-items-eq
"!=:" test-items-ne
"ieq" test-items-eq
"ine" test-items-ne
"True" test-true
"False" test-not
"not" test-not
"in" test-in
"not-in" test-not-in
"raises" test-raises-any
"raises-exc" test-raises
"raises-msg" test-raises-msg
"not-raises" test-not-raises-any
"not-raises-exc" test-not-raises
"not-raises-msg" test-not-raises-msg})
(defmacro test [name &rest args]
(setv sname (.replace (str name) "_" "-"))
(if-not (in sname opmap)
(macro-error name (% "unknown comparator: %s" sname)))
(apply (get opmap sname) args))
(def mods [])
(def tests (OrderedDict))
(defn add-test [func file]
(if-not (in file tests)
(assoc tests file (.OrderedDict (__import__ "collections"))))
(setv mytests (get tests file))
(if (in func.__name__ mytests)
(warnings.warn (+ "duplicate test: " file ":" func.__name__)))
(assoc mytests func.__name__ func))
(defn get-setup-and-teardown [body]
(setv body (list body))
(defmacro get-f2 [x] `(if (and body (get body 0)) (get (get body 0) 0) None))
(if (= (get-f2 body) (HySymbol "test_setup"))
(setv setup (cut (.pop body 0) 1))
(setv setup (HyExpression [])))
(if (= (get-f2 body) (HySymbol "test_teardown"))
(setv teardown (cut (.pop body 0) 1))
(setv teardown (HyExpression [])))
(, (tuple body) setup teardown))
(defmacro test-set [name &rest body]
(setv [body setup teardown] (get-setup-and-teardown body))
`(do
(defn ~name [] ~@setup (try (do ~@body) (finally ~@teardown)))
(.add-test (__import__ "hytest") ~name __file__)))
(defmacro test-set-fails [name &rest body]
(setv [body setup teardown] (get-setup-and-teardown body))
(setv skipexc (gensym))
`(do
(defn ~name []
~@setup
(setv ~skipexc (getattr (__import__ "hytest") "SkipException"))
(try
(do ~@body)
(except [~skipexc] (raise))
(except [])
(finally ~@teardown)))
(.add-test (__import__ "hytest") ~name __file__)))
(defn main [this &rest args]
(if (or (in "-h" args) (in "--help" args))
(sys.exit (% "usage: %s [--version] <tests-to-run>" this)))
(when (in "--version" args)
(print (+ "hytest v" __version__))
(-> 0 int sys.exit))
(setv wanted-names args)
(defn starstr [s]
(setv stars (* "*" (len s)))
(print stars)
(print s)
(print stars))
(load-tests)
(setv wanted (OrderedDict))
(when wanted-names
(for [n wanted-names]
(if-not (in ":" n)
(sys.exit (% "test name %s must have module specifier" n))
(setv [mod test] (n.split ":")))
(if-not (in mod tests)
(sys.exit (+ "unknown module: " mod)))
(if-not (in test (get tests mod))
(sys.exit (+ "unknown test: " n)))
(if-not (in mod wanted)
(assoc wanted mod (OrderedDict)))
(assoc (get wanted mod) test (get (get tests mod) test))))
(unless wanted
(setv wanted tests))
(setv run 0)
(setv skipped [])
(setv traces [])
(setv outputs (, [] [] []))
(setv stdout sys.stdout)
(setv stderr sys.stderr)
(for [[mod mtests] (wanted.items)]
(sys.stdout.write (% "\033[34m%s\033[0m " mod))
(for [[name tst] (mtests.items)]
(setv fullname (--hytest-fm "%s:%s" mod name))
(setv out (StringIO))
(setv err (StringIO))
(setv sys.stdout out)
(setv sys.stderr err)
(try
(try
(tst)
(except [] (raise))
(finally
(setv sys.stdout stdout)
(setv sys.stderr stderr)))
(except [e SkipException]
(skipped.append (, fullname (str e)))
(.append (get outputs 1) (, fullname (out.getvalue) (err.getvalue)))
(sys.stdout.write "\033[35mS\033[0m"))
(except [e Exception]
(sys.stdout.write "\033[31mF\033[0m")
(traces.append (, fullname (traceback.format-exc)))
(.append (get outputs 0) (, fullname (out.getvalue) (err.getvalue))))
(else
(sys.stdout.write "\033[32m.\033[0m")
(.append (get outputs 2) (, fullname (out.getvalue) (err.getvalue))))
(finally
(+= run 1))))
(print))
(defn print-bufs [n]
(setv st (get outputs n))
(if-not st
(raise (ValueError "")))
(setv (, tst out err) (get st 0))
(when out
(starstr (% "CAPTURED STDOUT: %s: " tst))
(print out))
(when err
(starstr (% "CAPTURED STDERR: %s: " tst))
(print err))
(st.pop 0))
(while True
(try
(print-bufs 2)
(except [ValueError] (break))))
(for [[tst trace] traces]
(print_bufs 0)
(print "\033[31m")
(starstr (% "ERROR: %s:" tst))
(print trace)
(print "\033[0m"))
(for [[tst reason] skipped]
(print_bufs 1)
(print "\033[35m")
(starstr (--hytest-fm "SKIPPED %s: %s" tst reason))
(print "\033[0m"))
(print (% "\033[34mTests run: %d" run))
(print (% "\033[32mTests succeeded: %d" (- run (len traces) (len skipped))))
(print (% "\033[31mTests failed: %d\033[0m" (len traces)))
(print (% "\033[35mTests skipped: %d\033[0m" (len skipped)))
(-> traces bool int))
(defn find-tests [dir]
(setv test-paths [])
(for [[root dirs files] (walk dir)]
(for [f files]
(if (and (f.startswith "test") (= (get (path.splitext f) 1) ".hy"))
(test-paths.append (path.join root f)))))
test-paths)
(defn load-tests []
(for [p (find-tests (getcwd))]
(mods.append
(import-file-to-module (get (path.splitext (path.basename p)) 0) p))))