Skip to content

Instantly share code, notes, and snippets.

@dustingetz
Created September 8, 2020 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustingetz/0d28424d7208932c3fa33edd2df68f7d to your computer and use it in GitHub Desktop.
Save dustingetz/0d28424d7208932c3fa33edd2df68f7d to your computer and use it in GitHub Desktop.
(ns user.dustin.minitest2
(:refer-clojure :exclude [test])
(:require
[meander.epsilon :as m]
[taoensso.timbre :refer [error]]))
(declare tests test!)
(def ^:dynamic *tests* (atom {}))
(defmacro tests [& body]
`(do
(swap! *tests* assoc (ns-name *ns*) '~body)
; if repl mode, just run them
(test!))) ; danger
(defn test!
([] (test! (ns-name *ns*)))
([ns]
(let [parsed (m/rewrite (vec (get @*tests* ns))
[] []
[!xs ... '=> ?v & ?more]
[[[!xs ...] ?v] & (m/cata ?more)])]
(doseq [[forms expected] parsed]
(try
(doseq [form forms]
(let [v (eval form)]
(set! *3 *2) (set! *2 *1) (set! *1 v)))
(let [e (eval expected)]
(if (= *1 e)
(println 'test 'passed (last forms) '=> expected)
(error 'test 'failed (last forms) '=> expected e)))
(catch Exception e
; continue
(error e)))))))
(tests
1
1
1
(inc 42) => 43
(inc 3) => 4
(inc 3) => 5
nil => nil
nil => true
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment