| 1 |
;;; faith.fnl --- The Fennel Advanced Interactive Test Helper |
| 2 |
|
| 3 |
;; https://git.sr.ht/~technomancy/faith |
| 4 |
|
| 5 |
;; SPDX-License-Identifier: MIT |
| 6 |
;; SPDX-FileCopyrightText: Scott Vokes, Phil Hagelberg, and contributors |
| 7 |
|
| 8 |
;; To use Faith, create a test runner file which calls the `run` function with |
| 9 |
;; a list of module names. The modules should export functions whose |
| 10 |
;; names start with `test-` and which call the assertion functions in the |
| 11 |
;; `faith` module. |
| 12 |
|
| 13 |
;; Copyright © 2009-2013 Scott Vokes and contributors |
| 14 |
;; Copyright © 2023-2024 Phil Hagelberg and contributors |
| 15 |
|
| 16 |
;; Permission is hereby granted, free of charge, to any person obtaining a copy |
| 17 |
;; of this software and associated documentation files (the "Software"), to deal |
| 18 |
;; in the Software without restriction, including without limitation the rights |
| 19 |
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 20 |
;; copies of the Software, and to permit persons to whom the Software is |
| 21 |
;; furnished to do so, subject to the following conditions: |
| 22 |
|
| 23 |
;; The above copyright notice and this permission notice shall be included in |
| 24 |
;; all copies or substantial portions of the Software. |
| 25 |
|
| 26 |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 27 |
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 28 |
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 29 |
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 30 |
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 31 |
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 32 |
;; SOFTWARE. |
| 33 |
|
| 34 |
(local fennel (require :fennel)) |
| 35 |
|
| 36 |
;;; helper functions |
| 37 |
|
| 38 |
(local unpack (or table.unpack _G.unpack)) |
| 39 |
|
| 40 |
(fn now [] |
| 41 |
{:real (or (and (pcall require :socket) |
| 42 |
(package.loaded.socket.gettime)) |
| 43 |
(and (pcall require :posix) |
| 44 |
(package.loaded.posix.gettimeofday) |
| 45 |
(let [t (package.loaded.posix.gettimeofday)] |
| 46 |
(+ t.sec (/ t.usec 1000000)))) |
| 47 |
nil) |
| 48 |
:approx (os.time) |
| 49 |
:cpu (os.clock)}) |
| 50 |
|
| 51 |
(fn fn? [v] (= (type v) :function)) |
| 52 |
|
| 53 |
(fn fail->string [{: where : reason : msg} name] |
| 54 |
(string.format "FAIL: %s:\n%s: %s%s\n" |
| 55 |
name where (or reason "") |
| 56 |
(or (and msg (.. " - " (tostring msg))) ""))) |
| 57 |
|
| 58 |
(fn err->string [{: msg} name] |
| 59 |
(or msg (string.format "ERROR (in %s, couldn't get traceback)" |
| 60 |
(or name "(unknown)")))) |
| 61 |
|
| 62 |
(fn get-where [start] |
| 63 |
(let [traceback (fennel.traceback nil start) |
| 64 |
(_ _ where) (traceback:find "\n\t*([^:]+:[0-9]+):")] |
| 65 |
(or where "?"))) |
| 66 |
|
| 67 |
;;; assertions |
| 68 |
|
| 69 |
;; while I'd prefer to remove all top-level state, this one is difficult |
| 70 |
;; because it has to be set by every assertion, and the assertion functions |
| 71 |
;; themselves do not have access to any stateful arguments given that they |
| 72 |
;; are called directly from user code. |
| 73 |
(var checked 0) |
| 74 |
(var diff-cmd (or (os.getenv "FAITH_DIFF") |
| 75 |
(if (os.getenv "NO_COLOR") |
| 76 |
"diff -u %s %s" |
| 77 |
"diff -u --color=always %s %s"))) |
| 78 |
|
| 79 |
(macro wrap [flag msg ...] |
| 80 |
`(do (set ,(sym :checked) (+ ,(sym :checked) 1)) |
| 81 |
(when (not ,flag) |
| 82 |
(error {:char "F" :type :fail :tostring fail->string |
| 83 |
:reason (string.format ,...) :msg ,msg :where (get-where 4)})))) |
| 84 |
|
| 85 |
(fn pass [] {:char "." :type :pass}) |
| 86 |
|
| 87 |
(fn error-result [msg] {:char "E" :type :err :tostring err->string :msg msg}) |
| 88 |
|
| 89 |
(fn skip [] |
| 90 |
(error {:char :s :type :skip})) |
| 91 |
|
| 92 |
(fn is [got ?msg] |
| 93 |
(wrap got ?msg "Expected truthy value")) |
| 94 |
|
| 95 |
(fn error* [pat f ?msg] |
| 96 |
(case (pcall f) |
| 97 |
(true ?val) (wrap false ?msg "Expected an error, got %s" (fennel.view ?val)) |
| 98 |
(_ err) (let [err-string (if (= (type err) :string) err (fennel.view err))] |
| 99 |
(wrap (err-string:match pat) ?msg |
| 100 |
"Expected error to match pattern %s, was %s" |
| 101 |
pat err-string)))) |
| 102 |
|
| 103 |
(fn extra-fields? [t keys] |
| 104 |
(or (accumulate [extra? false k (pairs t) &until extra?] |
| 105 |
(if (= nil (. keys k)) |
| 106 |
true |
| 107 |
(tset keys k nil))) |
| 108 |
(next keys))) |
| 109 |
|
| 110 |
(fn table= [x y equal?] |
| 111 |
(let [keys {}] |
| 112 |
(and (accumulate [same? true k v (pairs x) &until (not same?)] |
| 113 |
(do (tset keys k true) |
| 114 |
(equal? v (. y k)))) |
| 115 |
(not (extra-fields? y keys))))) |
| 116 |
|
| 117 |
(fn equal? [x y] |
| 118 |
(or (= x y) |
| 119 |
(and (= (type x) :table (type y)) (table= x y equal?)))) |
| 120 |
|
| 121 |
(fn diff-report [expv gotv] |
| 122 |
(let [exp-file (os.tmpname "faithdiff1") |
| 123 |
got-file (os.tmpname "faithdiff2")] |
| 124 |
(with-open [f (io.open exp-file :w)] |
| 125 |
(f:write expv)) |
| 126 |
(with-open [f (io.open got-file :w)] |
| 127 |
(f:write gotv)) |
| 128 |
(let [diff (doto (io.popen (diff-cmd:format exp-file got-file)) |
| 129 |
(: :read) (: :read) (: :read)) ; omit header lines |
| 130 |
out (diff:read :*all)] |
| 131 |
(os.remove exp-file) |
| 132 |
(os.remove got-file) |
| 133 |
(let [(closed _ code) (diff:close)] |
| 134 |
(if (or closed (= 1 code)) |
| 135 |
(.. "\n" out) |
| 136 |
(string.format "Expected:\n%s\nGot:\n%s" expv gotv)))))) |
| 137 |
|
| 138 |
(fn =* [exp got ?msg] |
| 139 |
(let [expv (fennel.view exp) |
| 140 |
gotv (fennel.view got) |
| 141 |
report (if (and (not= expv gotv) (or (expv:find "\n") (gotv:find "\n"))) |
| 142 |
(diff-report expv gotv) |
| 143 |
(string.format "Expected %s, got %s" expv gotv))] |
| 144 |
(wrap (equal? exp got) ?msg report))) |
| 145 |
|
| 146 |
(fn not=* [exp got ?msg] |
| 147 |
(wrap (not (equal? exp got)) ?msg "Expected something other than %s" |
| 148 |
(fennel.view exp))) |
| 149 |
|
| 150 |
(fn <* [...] |
| 151 |
(let [args [...] |
| 152 |
msg (if (= :string (type (. args (length args)))) (table.remove args)) |
| 153 |
correct? (faccumulate [ok? true i 2 (length args) &until (not ok?)] |
| 154 |
(< (. args (- i 1)) (. args i)))] |
| 155 |
(wrap correct? msg |
| 156 |
"Expected arguments in strictly increasing order, got %s" |
| 157 |
(fennel.view args)))) |
| 158 |
|
| 159 |
(fn <=* [...] |
| 160 |
(let [args [...] |
| 161 |
msg (if (= :string (type (. args (length args)))) (table.remove args)) |
| 162 |
correct? (faccumulate [ok? true i 2 (length args) &until (not ok?)] |
| 163 |
(<= (. args (- i 1)) (. args i)))] |
| 164 |
(wrap correct? msg |
| 165 |
"Expected arguments in increasing/equal order, got %s" |
| 166 |
(fennel.view args)))) |
| 167 |
|
| 168 |
(fn almost= [exp got tolerance ?msg] |
| 169 |
(wrap (<= (math.abs (- exp got)) tolerance) ?msg |
| 170 |
"Expected %s +/- %s, got %s" exp tolerance got)) |
| 171 |
|
| 172 |
(fn identical [exp got ?msg] |
| 173 |
(wrap (rawequal exp got) ?msg |
| 174 |
"Expected %s, got %s" (fennel.view exp) (fennel.view got))) |
| 175 |
|
| 176 |
(fn match* [pat s ?msg] |
| 177 |
(wrap (: (tostring s) :match pat) ?msg |
| 178 |
"Expected string to match pattern %s, was\n%s" pat s)) |
| 179 |
|
| 180 |
(fn not-match [pat s ?msg] |
| 181 |
(wrap (or (not= (type s) :string) (not (s:match pat))) ?msg |
| 182 |
"Expected string not to match pattern %s, was\n %s" pat s)) |
| 183 |
|
| 184 |
;;; running |
| 185 |
|
| 186 |
(fn dot [char total-count] |
| 187 |
(io.write char) |
| 188 |
(when (= 0 (math.fmod total-count 76)) |
| 189 |
(io.write "\n")) |
| 190 |
(io.stdout:flush)) |
| 191 |
|
| 192 |
(fn print-totals [report] |
| 193 |
(let [{: started-at : ended-at : results} report |
| 194 |
duration (fn [start end] |
| 195 |
(let [decimal-places 2] |
| 196 |
(: (.. "%." (tonumber decimal-places) "f") |
| 197 |
:format |
| 198 |
(math.max (- end start) |
| 199 |
(^ 10 (- decimal-places)))))) |
| 200 |
counts (accumulate [counts {:pass 0 :fail 0 :err 0 :skip 0} |
| 201 |
_ {:type type*} (ipairs results)] |
| 202 |
(doto counts (tset type* (+ (. counts type*) 1))))] |
| 203 |
(print (: (.. "Testing finished %s with %d assertion(s)\n" |
| 204 |
"%d passed, %d failed, %d error(s), %d skipped\n" |
| 205 |
"%.2f second(s) of CPU time used") |
| 206 |
:format |
| 207 |
(if started-at.real |
| 208 |
(: "in %s second(s)" :format |
| 209 |
(duration started-at.real ended-at.real)) |
| 210 |
(: "in approximately %s second(s)" :format |
| 211 |
(- ended-at.approx started-at.approx))) |
| 212 |
checked |
| 213 |
counts.pass |
| 214 |
counts.fail |
| 215 |
counts.err |
| 216 |
counts.skip |
| 217 |
(duration started-at.cpu ended-at.cpu))))) |
| 218 |
|
| 219 |
(fn begin-module [report tests] |
| 220 |
(print (string.format "\nStarting module %s with %d test(s)" |
| 221 |
report.module-name |
| 222 |
(accumulate [count 0 _ (pairs tests)] (+ count 1))))) |
| 223 |
|
| 224 |
(fn done [report] |
| 225 |
(print "\n") |
| 226 |
(each [_ result (ipairs report.results)] |
| 227 |
(when result.tostring (print (result:tostring result.name)))) |
| 228 |
(print-totals report)) |
| 229 |
|
| 230 |
(local default-hooks {:begin false |
| 231 |
: done |
| 232 |
: begin-module |
| 233 |
:end-module false |
| 234 |
:begin-test false |
| 235 |
:end-test (fn [_name result total-count] |
| 236 |
(dot result.char total-count))}) |
| 237 |
|
| 238 |
(fn test-key? [k] |
| 239 |
(and (= (type k) :string) (k:match :^test.*))) |
| 240 |
|
| 241 |
(local ok-types {:fail true :pass true :skip true}) |
| 242 |
|
| 243 |
(fn err-handler [name] |
| 244 |
(fn [e] |
| 245 |
(if (and (= (type e) :table) (. ok-types e.type)) |
| 246 |
e |
| 247 |
(error-result (-> (string.format "\nERROR: %s:\n%s\n" name e) |
| 248 |
(fennel.traceback 4)))))) |
| 249 |
|
| 250 |
(fn run-test [name ?setup test ?teardown report hooks context] |
| 251 |
(when (fn? hooks.begin-test) (hooks.begin-test name)) |
| 252 |
(let [result (case-try (if ?setup (xpcall ?setup (err-handler name)) true) |
| 253 |
true (xpcall #(test (unpack context)) (err-handler name)) |
| 254 |
true (pass) |
| 255 |
(catch (_ err) err))] |
| 256 |
(when ?teardown (pcall ?teardown (unpack context))) |
| 257 |
(table.insert report.results (doto result (tset :name name))) |
| 258 |
(when (fn? hooks.end-test) |
| 259 |
(hooks.end-test name result (length report.results))))) |
| 260 |
|
| 261 |
(fn run-setup-all [setup-all report module-name] |
| 262 |
(if (fn? setup-all) |
| 263 |
(case [(pcall setup-all)] |
| 264 |
[true & context] context |
| 265 |
[false err] (let [msg (: "ERROR in test module %s setup-all: %s" |
| 266 |
:format module-name err)] |
| 267 |
(table.insert report.results |
| 268 |
(doto (error-result msg) |
| 269 |
(tset :name module-name))) |
| 270 |
(values nil err))) |
| 271 |
[])) |
| 272 |
|
| 273 |
(fn run-module [hooks report module-name test-module] |
| 274 |
(assert (= :table (type test-module)) |
| 275 |
(.. "test module must be table: " module-name)) |
| 276 |
(let [module-report {: module-name :started-at (now) :results []}] |
| 277 |
(case (run-setup-all test-module.setup-all report module-name) |
| 278 |
context (do |
| 279 |
(when hooks.begin-module |
| 280 |
(hooks.begin-module module-report test-module)) |
| 281 |
(each [_ {: name : test} |
| 282 |
(ipairs (doto (icollect [name test (pairs test-module)] |
| 283 |
(if (test-key? name) |
| 284 |
{:line (. (debug.getinfo test :S) |
| 285 |
:linedefined) |
| 286 |
: name : test})) |
| 287 |
(table.sort #(< $1.line $2.line))))] |
| 288 |
(run-test name |
| 289 |
test-module.setup |
| 290 |
test |
| 291 |
test-module.teardown |
| 292 |
module-report |
| 293 |
hooks |
| 294 |
context)) |
| 295 |
(case test-module.teardown-all |
| 296 |
teardown (pcall teardown (unpack context))) |
| 297 |
(when hooks.end-module (hooks.end-module module-report)) |
| 298 |
(icollect [_ value |
| 299 |
(ipairs module-report.results) |
| 300 |
&into report.results] |
| 301 |
value))))) |
| 302 |
|
| 303 |
(fn exit [hooks] |
| 304 |
(if hooks.exit (hooks.exit 1) |
| 305 |
_G.___replLocals___ :failed |
| 306 |
(and os os.exit) (os.exit 1))) |
| 307 |
|
| 308 |
(fn run [module-names ?opts] |
| 309 |
(set (checked diff-cmd) (values 0 (or (and ?opts ?opts.diff-cmd) diff-cmd))) |
| 310 |
(io.stdout:setvbuf :line) |
| 311 |
;; don't count load time against the test runtime |
| 312 |
(each [_ m (ipairs module-names)] (require m)) |
| 313 |
(let [hooks (setmetatable (or (?. ?opts :hooks) {}) {:__index default-hooks}) |
| 314 |
report {:module-name :main :started-at (now) :results []}] |
| 315 |
(when hooks.begin |
| 316 |
(hooks.begin report module-names)) |
| 317 |
(each [_ module-name (ipairs module-names)] |
| 318 |
(case (pcall require module-name) |
| 319 |
(true test-module) (run-module hooks report module-name test-module) |
| 320 |
(false err) (let [error (: "ERROR: Cannot load %q:\n%s" |
| 321 |
:format module-name err)] |
| 322 |
(table.insert report.results |
| 323 |
(doto (error-result error) |
| 324 |
(tset :name module-name)))))) |
| 325 |
(set report.ended-at (now)) |
| 326 |
(when hooks.done (hooks.done report)) |
| 327 |
(when (accumulate [red false |
| 328 |
_ {:type type*} (ipairs report.results) |
| 329 |
&until red] |
| 330 |
(or (= type* :fail) |
| 331 |
(= type* :err))) |
| 332 |
(exit hooks)))) |
| 333 |
|
| 334 |
(when (= ... "--tests") |
| 335 |
(run (doto [...] (table.remove 1))) |
| 336 |
(os.exit 0)) |
| 337 |
|
| 338 |
{: run : skip :version "0.2.0" |
| 339 |
: is :error error* := =* :not= not=* :< <* :<= <=* : almost= |
| 340 |
: identical :match match* : not-match} |