2018-03-26 01:25:33 +00:00
|
|
|
# Helper code for running tests
|
|
|
|
|
|
|
|
(var num-tests-passed 0)
|
|
|
|
(var num-tests-run 0)
|
2023-06-01 22:04:07 +00:00
|
|
|
(var suite-name 0)
|
2019-12-15 02:39:14 +00:00
|
|
|
(var start-time 0)
|
2024-08-30 02:03:25 +00:00
|
|
|
(var skip-count 0)
|
|
|
|
(var skip-n 0)
|
2018-03-26 01:25:33 +00:00
|
|
|
|
2021-11-06 16:01:21 +00:00
|
|
|
(def is-verbose (os/getenv "VERBOSE"))
|
|
|
|
|
2024-08-17 14:57:56 +00:00
|
|
|
(defn- assert-no-tail
|
2020-01-16 04:38:06 +00:00
|
|
|
"Override's the default assert with some nice error handling."
|
2020-04-03 20:23:29 +00:00
|
|
|
[x &opt e]
|
2019-12-15 02:39:14 +00:00
|
|
|
(++ num-tests-run)
|
2024-08-30 02:03:25 +00:00
|
|
|
(when (pos? skip-n)
|
|
|
|
(-- skip-n)
|
|
|
|
(++ skip-count)
|
|
|
|
(break x))
|
|
|
|
(default e "assert error")
|
2019-12-15 02:39:14 +00:00
|
|
|
(when x (++ num-tests-passed))
|
2020-09-27 17:18:12 +00:00
|
|
|
(def str (string e))
|
2024-08-17 14:57:56 +00:00
|
|
|
(def stack (debug/stack (fiber/current)))
|
|
|
|
(def frame (last stack))
|
2023-06-03 16:59:19 +00:00
|
|
|
(def line-info (string/format "%s:%d"
|
|
|
|
(frame :source) (frame :source-line)))
|
2019-12-15 02:39:14 +00:00
|
|
|
(if x
|
2023-06-03 16:59:19 +00:00
|
|
|
(when is-verbose (eprintf "\e[32m✔\e[0m %s: %s: %v" line-info (describe e) x))
|
2024-08-17 14:57:56 +00:00
|
|
|
(do
|
|
|
|
(eprintf "\e[31m✘\e[0m %s: %s: %v" line-info (describe e) x) (eflush)))
|
2019-12-15 02:39:14 +00:00
|
|
|
x)
|
2018-03-26 01:25:33 +00:00
|
|
|
|
2024-08-30 02:03:25 +00:00
|
|
|
(defn skip-asserts
|
|
|
|
"Skip some asserts"
|
|
|
|
[n]
|
|
|
|
(+= skip-n n)
|
|
|
|
nil)
|
|
|
|
|
2024-08-17 14:57:56 +00:00
|
|
|
(defmacro assert
|
|
|
|
[x &opt e]
|
|
|
|
(def xx (gensym))
|
2024-08-30 02:03:25 +00:00
|
|
|
(default e ~',x)
|
2024-08-17 14:57:56 +00:00
|
|
|
~(do
|
|
|
|
(def ,xx ,x)
|
|
|
|
(,assert-no-tail ,xx ,e)
|
|
|
|
,xx))
|
|
|
|
|
2019-01-08 17:21:11 +00:00
|
|
|
(defmacro assert-error
|
|
|
|
[msg & forms]
|
|
|
|
(def errsym (keyword (gensym)))
|
|
|
|
~(assert (= ,errsym (try (do ,;forms) ([_] ,errsym))) ,msg))
|
|
|
|
|
2023-05-06 17:08:07 +00:00
|
|
|
(defn check-compile-error
|
|
|
|
[form]
|
|
|
|
(def result (compile form))
|
|
|
|
(assert (table? result) (string/format "expected compilation error for %j, but compiled without error" form)))
|
|
|
|
|
2019-02-22 00:19:47 +00:00
|
|
|
(defmacro assert-no-error
|
|
|
|
[msg & forms]
|
2023-10-08 22:34:50 +00:00
|
|
|
(def e (gensym))
|
|
|
|
(def f (gensym))
|
|
|
|
(if is-verbose
|
|
|
|
~(try (do ,;forms (,assert true ,msg)) ([,e ,f] (,assert false ,msg) (,debug/stacktrace ,f ,e "\e[31m✘\e[0m ")))
|
|
|
|
~(try (do ,;forms (,assert true ,msg)) ([_] (,assert false ,msg)))))
|
2019-02-22 00:19:47 +00:00
|
|
|
|
2023-06-01 22:04:07 +00:00
|
|
|
(defn start-suite [&opt x]
|
|
|
|
(default x (dyn :current-file))
|
|
|
|
(set suite-name
|
|
|
|
(cond
|
|
|
|
(number? x) (string x)
|
|
|
|
(string x)))
|
2019-12-15 02:39:14 +00:00
|
|
|
(set start-time (os/clock))
|
2023-06-01 22:04:07 +00:00
|
|
|
(eprint "Starting suite " suite-name "..."))
|
2018-03-26 01:25:33 +00:00
|
|
|
|
|
|
|
(defn end-suite []
|
2019-12-15 02:39:14 +00:00
|
|
|
(def delta (- (os/clock) start-time))
|
2023-06-01 22:04:07 +00:00
|
|
|
(eprinf "Finished suite %s in %.3f seconds - " suite-name delta)
|
2024-08-30 02:03:25 +00:00
|
|
|
(eprint num-tests-passed " of " num-tests-run " tests passed (" skip-count " skipped).")
|
|
|
|
(if (not= (+ skip-count num-tests-passed) num-tests-run) (os/exit 1)))
|
2024-08-18 16:45:21 +00:00
|
|
|
|
|
|
|
(defn rmrf
|
|
|
|
"rm -rf in janet"
|
|
|
|
[x]
|
|
|
|
(case (os/lstat x :mode)
|
|
|
|
nil nil
|
|
|
|
:directory (do
|
|
|
|
(each y (os/dir x)
|
|
|
|
(rmrf (string x "/" y)))
|
|
|
|
(os/rmdir x))
|
|
|
|
(os/rm x))
|
|
|
|
nil)
|
|
|
|
|
|
|
|
(defn randdir
|
|
|
|
"Get a random directory name"
|
|
|
|
[]
|
|
|
|
(string "tmp_dir_" (slice (string (math/random) ".tmp") 2)))
|