-
Notifications
You must be signed in to change notification settings - Fork 0
/
faith.fnl
300 lines (248 loc) · 11.1 KB
/
faith.fnl
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
;;; faith.fnl --- The Fennel Advanced Interactive Test Helper
;; https://git.sr.ht/~technomancy/faith
;; SPDX-License-Identifier: MIT
;; SPDX-FileCopyrightText: Scott Vokes, Phil Hagelberg, and contributors
;; To use Faith, create a test runner file which calls the `run` function with
;; a list of module names. The modules should export functions whose
;; names start with `test-` and which call the assertion functions in the
;; `faith` module.
;; Copyright © 2009-2013 Scott Vokes and contributors
;; Copyright © 2023 Phil Hagelberg and contributors
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
(local fennel (require :fennel))
;;; helper functions
(local unpack (or table.unpack _G.unpack))
(fn now []
{:real (or (and (pcall require :socket)
(package.loaded.socket.gettime))
(and (pcall require :posix)
(package.loaded.posix.gettimeofday)
(let [t (package.loaded.posix.gettimeofday)]
(+ t.sec (/ t.usec 1000000))))
nil)
:approx (os.time)
:cpu (os.clock)})
(fn result-table [name]
{:started-at (now) :err [] :fail [] : name :pass [] :skip [] :ran 0 :tests []})
(fn combine-results [to from]
(each [_ s (ipairs [:pass :fail :skip :err])]
(each [name val (pairs (. from s))]
(tset (. to s) name val))))
(fn fn? [v] (= (type v) :function))
(fn count [t] (accumulate [c 0 _ (pairs t)] (+ c 1)))
(fn fail->string [{: where : reason : msg} name]
(string.format "FAIL: %s: %s\n %s%s\n"
where name (or reason "")
(or (and msg (.. " - " (tostring msg))) "")))
(fn err->string [{: msg} name]
(or msg (string.format "ERROR (in %s, couldn't get traceback)"
(or name "(unknown)"))))
(fn get-where [start]
(let [traceback (fennel.traceback nil start)
(_ _ where) (traceback:find "\n *([^:]+:[0-9]+):")]
(or where "?")))
;;; assertions
;; while I'd prefer to remove all top-level state, this one is difficult
;; because it has to be set by every assertion, and the assertion functions
;; themselves do not have access to any stateful arguments given that they
;; are called directly from user code.
(var checked 0)
(macro wrap [flag msg ...]
`(do (set ,(sym :checked) (+ ,(sym :checked) 1))
(when (not ,flag)
(error {:char "F" :type :fail :tostring fail->string
:reason (string.format ,...) :msg ,msg :where (get-where 4)}))))
(fn pass [] {:char "." :type :pass})
(fn error-result [msg] {:char "E" :type :err :tostring err->string :msg msg})
(fn skip []
(error {:char :s :type :skip}))
(fn is [got ?msg]
(wrap got ?msg "Expected truthy value"))
(fn error* [pat f ?msg]
(case (pcall f)
(true ?val) (wrap false ?msg "Expected an error, got %s" (fennel.view ?val))
(_ err) (let [err-string (if (= (type err) :string) err (fennel.view err))]
(wrap (err-string:match pat) ?msg
"Expected error to match pattern %s, was %s"
pat err-string))))
(fn extra-fields? [t keys]
(or (accumulate [extra? false k (pairs t) &until extra?]
(if (= nil (. keys k))
true
(tset keys k nil)))
(next keys)))
(fn table= [x y equal?]
(let [keys {}]
(and (accumulate [same? true k v (pairs x) &until (not same?)]
(do (tset keys k true)
(equal? v (. y k))))
(not (extra-fields? y keys)))))
(fn equal? [x y]
(or (= x y)
(and (= (type x) :table (type y)) (table= x y equal?))))
(fn =* [exp got ?msg]
(wrap (equal? exp got) ?msg "Expected %s, got %s"
(fennel.view exp) (fennel.view got)))
(fn not=* [exp got ?msg]
(wrap (not (equal? exp got)) ?msg "Expected something other than %s"
(fennel.view exp)))
(fn <* [...]
(let [args [...]
msg (if (= :string (type (. args (length args)))) (table.remove args))
correct? (faccumulate [ok? true i 2 (length args) &until (not ok?)]
(< (. args (- i 1)) (. args i)))]
(wrap correct? msg
"Expected arguments in strictly increasing order, got %s"
(fennel.view args))))
(fn <=* [...]
(let [args [...]
msg (if (= :string (type (. args (length args)))) (table.remove args))
correct? (faccumulate [ok? true i 2 (length args) &until (not ok?)]
(<= (. args (- i 1)) (. args i)))]
(wrap correct? msg
"Expected arguments in increasing/equal order, got %s"
(fennel.view args))))
(fn almost= [exp got tolerance ?msg]
(wrap (<= (math.abs (- exp got)) tolerance) ?msg
"Expected %s +/- %s, got %s" exp tolerance got))
(fn identical [exp got ?msg]
(wrap (= exp got) ?msg
"Expected %s, got %s" (fennel.view exp) (fennel.view got)))
(fn match* [pat s ?msg]
(wrap (: (tostring s) :match pat) ?msg
"Expected string to match pattern %s, was\n%s" pat s))
(fn not-match [pat s ?msg]
(wrap (or (not= (type s) :string) (not (s:match pat))) ?msg
"Expected string not to match pattern %s, was\n %s" pat s))
;;; running
(fn dot [c ran]
(io.write c)
(when (= 0 (math.fmod ran 76))
(io.write "\n"))
(io.stdout:flush))
(fn print-totals [{: pass : fail : skip : err : started-at : ended-at}]
(let [duration (fn [start end]
(let [decimal-places 2]
(: (.. "%." (tonumber decimal-places) "f")
:format
(math.max (- end start)
(^ 10 (- decimal-places))))))]
(print (: (.. "Testing finished %s with %d assertion(s)\n"
"%d passed, %d failed, %d error(s), %d skipped\n"
"%.2f second(s) of CPU time used")
:format
(if started-at.real
(: "in %s second(s)" :format
(duration started-at.real ended-at.real))
(: "in approximately %s second(s)" :format
(- ended-at.approx started-at.approx)))
checked
(count pass) (count fail) (count err) (count skip)
(duration started-at.cpu ended-at.cpu)))))
(fn begin-module [s-env tests]
(print (string.format "\nStarting module %s with %d test(s)"
s-env.name (count tests))))
(fn done [results]
(print "\n")
(each [_ ts (ipairs [results.fail results.err results.skip])]
(each [name result (pairs ts)]
(when result.tostring (print (result:tostring name)))))
(print-totals results))
(local default-hooks {:begin false
: done
: begin-module
:end-module false
:begin-test false
:end-test (fn [_name result ran] (dot result.char ran))})
(fn test-key? [k]
(and (= (type k) :string) (k:match :^test.*)))
(local ok-types {:fail true :pass true :skip true})
(fn err-handler [name]
(fn [e]
(if (and (= (type e) :table) (. ok-types e.type))
e
(error-result (-> (string.format "\nERROR: %s:\n%s\n" name e)
(fennel.traceback 4))))))
(fn run-test [name ?setup test ?teardown module-result hooks context]
(when (fn? hooks.begin-test) (hooks.begin-test name))
(let [result (case-try (if ?setup (xpcall ?setup (err-handler name)) true)
true (xpcall #(test (unpack context)) (err-handler name))
true (pass)
(catch (_ err) err))]
(when ?teardown (pcall ?teardown (unpack context)))
(tset module-result result.type name result)
(set module-result.ran (+ module-result.ran 1))
(when (fn? hooks.end-test) (hooks.end-test name result module-result.ran))))
(fn run-setup-all [setup-all results module-name]
(if (fn? setup-all)
(case [(pcall setup-all)]
[true & context] context
[false err] (let [msg (: "ERROR in test module %s setup-all: %s"
:format module-name err)]
(tset results.err module-name (error-result msg))
(values nil err)))
[]))
(fn run-module [hooks results module-name test-module]
(assert (= :table (type test-module)) (.. "test module must be table: "
module-name))
(let [result (result-table module-name)]
(case (run-setup-all test-module.setup-all results module-name)
context (do
(when hooks.begin-module (hooks.begin-module result test-module))
(each [name test (pairs test-module)]
(when (test-key? name)
(table.insert result.tests test)
(run-test name
test-module.setup
test
test-module.teardown
result
hooks
context)))
(case test-module.teardown-all
teardown (pcall teardown (unpack context)))
(when hooks.end-module (hooks.end-module result))
(combine-results results result)))))
(fn exit [hooks]
(if hooks.exit (hooks.exit 1)
_G.___replLocals___ :failed
(and os os.exit) (os.exit 1)))
(fn run [module-names ?hooks]
(set checked 0)
(io.stdout:setvbuf :line)
;; don't count load time against the test runtime
(each [_ m (ipairs module-names)] (require m))
(let [hooks (setmetatable (or ?hooks {}) {:__index default-hooks})
results (result-table :main)]
(when hooks.begin
(hooks.begin results module-names))
(each [_ module-name (ipairs module-names)]
(case (pcall require module-name)
(true test-mod) (run-module hooks results module-name test-mod)
(false err) (tset results.err module-name
(error-result (: "ERROR: Cannot load %q:\n%s"
:format module-name err)))))
(set results.ended-at (now))
(when hooks.done (hooks.done results))
(when (or (next results.err) (next results.fail))
(exit hooks))))
(when (= ... "--tests")
(run (doto [...] (table.remove 1)))
(os.exit 0))
{: run : skip :version "0.1.3-dev-a466fe7"
: is :error error* := =* :not= not=* :< <* :<= <=* : almost=
: identical :match match* : not-match}