Skip to content

Instantly share code, notes, and snippets.

@dustingetz
Last active June 10, 2020 17:25
Show Gist options
  • Save dustingetz/f0cdcad7e6d804249624240c2a0870ef to your computer and use it in GitHub Desktop.
Save dustingetz/f0cdcad7e6d804249624240c2a0870ef to your computer and use it in GitHub Desktop.
(ns user.test
(:refer-clojure :exclude [test])
(:require
[meander.epsilon :as m]
[taoensso.timbre :refer [error]]))
(def tests-enabled true)
(defmacro test [& body]
(when tests-enabled
(let [parsed (m/rewrite (vec body)
[] []
[!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 (last forms) '=> expected 'passed)
(error 'test (last forms) '=> expected 'failed)))
(catch Throwable e
(error e)))))))
(test
1
1
1
(inc 42) => 43
(inc 3) => 4
(inc 3) => 5
nil => nil
nil => true
)
@dustingetz
Copy link
Author

test (inc 42) => 43 passed
test (inc 3) => 4 passed
error [user.test:20]
  test (inc 3) => 5 failed
test nil => nil passed
error [user.test:20]
  test nil => true failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment