Skip to content

Instantly share code, notes, and snippets.

@dustingetz

dustingetz/microtest.clj

Last active Jun 10, 2020
Embed
What would you like to do?
(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

This comment has been minimized.

Copy link
Owner Author

@dustingetz dustingetz commented Jun 10, 2020

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
You can’t perform that action at this time.