commit ee4a8d28b6326259662f2afc0413d3e423474538
parent 8ed87c7d5359cb027b95da163616e1de27265325
Author: Daniel Barlow <dan@telent.net>
Date: Tue, 10 Sep 2024 22:41:28 +0100
`describe` macro supports tests inline in code
convert existing asserts to use it
Diffstat:
6 files changed, 55 insertions(+), 12 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,5 +1,8 @@
doc/index.html: doc/index.md
pandoc -t html -f gfm < $< > $@
+check:
+ sh test/run.sh
+
watch:
find . -type f | entr -c -d sh test/run.sh
diff --git a/dunlin.fnl b/dunlin.fnl
@@ -34,3 +34,5 @@
(.. socketdir "/dunlin.sock")))
(Gtk.main)
+
+;; NO-TESTING
diff --git a/keymap.fnl b/keymap.fnl
@@ -1,7 +1,7 @@
(local { : Gdk } (require :lgi))
(local { : view } (require :fennel))
(local lume (require :lume))
-(import-macros {: trace} :macros)
+(import-macros {: trace : describe : expect : expect=} :macros)
(local modifier-keyvals
;; we need to detect and discard modifier-only key events when
@@ -73,11 +73,13 @@
(table.insert chars (Gdk.keyval_name keyval))
(table.concat chars "-")))
-(let [v (index->string "103:0")] (assert (= v "g") v))
-(let [v (index->string "65:0")] (assert (= v "A") v))
-(let [v (index->string "120:4")] (assert (= v "C-x") v))
-(let [v (index->string "100:8")] (assert (= v "M-d") v))
-(let [v (index->string "100:12")] (assert (= v "C-M-d") v))
+(describe
+ index->string
+ (expect= (index->string "103:0") "g")
+ (expect= (index->string "65:0") "A")
+ (expect= (index->string "120:4") "C-x")
+ (expect= (index->string "100:8") "M-d")
+ (expect= (index->string "100:12") "C-M-d"))
(fn command? [tbl]
;; a keymap entry has a string as key, a command
@@ -101,10 +103,12 @@
[k1] (. tbl k1)
x tbl)))
-(let [v (ref {:a 1} [:a])] (assert (= v 1) v))
-(let [v (ref {:a {:c 7}} [:a :c])] (assert (= v 7) v))
-(let [v (ref {:a {:c 7}} [:a ])] (assert (match v {:c 7} true) (view v)))
-(let [v (ref {:a {:c 7}} [:z :d])] (assert (not v) v))
+(describe
+ ref
+ (let [v (ref {:a 1} [:a])] (expect= v 1))
+ (let [v (ref {:a {:c 7}} [:a :c])] (expect= v 7))
+ (let [v (ref {:a {:c 7}} [:a ])] (expect= v {:c 7}))
+ (let [v (ref {:a {:c 7}} [:z :d])] (expect (not v))))
(fn recogniser [source-keymap]
diff --git a/macros.fnl b/macros.fnl
@@ -1,7 +1,27 @@
-
(fn trace [x]
`(do
(print :trace ,(view x) (view ,x))
,x))
-{ : trace }
+(fn expect [assertion]
+ (let [msg (.. "expectation failed: " (view assertion))]
+ `(when (not ,assertion)
+ (assert false ,msg))))
+
+(fn expect= [actual expected]
+ `(let [view# (. (require :fennel) :view)
+ ve# (view# ,expected)
+ va# (view# ,actual)]
+ (when (not (= ve# va#))
+ (assert false
+ (.. "\nexpected " ve# "\ngot " va#)
+ ))))
+
+(fn describe [thing & body]
+ (when _G.RUNNING_TESTS
+ `(do (print "# " ,(view thing))
+ ,(unpack body))))
+
+{ : trace : expect : expect= : describe }
+
+;; NO-TESTING
diff --git a/test/run.sh b/test/run.sh
@@ -2,3 +2,8 @@
for i in test/*.fnl; do
fennel --correlate $i && echo $i OK
done
+for i in *.fnl; do
+ grep -q NO-TESTING "$i" && continue
+ [ "$i" = "macros.fnl" ] && continue
+ fennel test/test-helper.fnl $i && echo $i OK
+done
diff --git a/test/test-helper.fnl b/test/test-helper.fnl
@@ -0,0 +1,9 @@
+(local fennel (require :fennel))
+(local specials (require :fennel.specials))
+
+(local compiler-env
+ (doto (. (specials.make-compiler-env) :_G)
+ (tset "RUNNING_TESTS" true)))
+
+(each [_ f (ipairs arg)]
+ (fennel.dofile f { :correlate true :compilerEnv compiler-env }))