Skip to content

Instantly share code, notes, and snippets.

@mwmitchell
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwmitchell/8890906 to your computer and use it in GitHub Desktop.
Save mwmitchell/8890906 to your computer and use it in GitHub Desktop.
(ns truth.core)
(defn get-data []
{:data {:name {:first "matt"}}})
(defmacro truth [lbl val tests redefs]
(loop [t tests]
(if-not (seq t)
true
`(with-redefs [~@(mapcat identity redefs)]
(if-not (-> ~val ~(first t))
(format "[%s] failed on: (-> %s %s)" ~lbl '~val '~(first t))
(truth ~lbl ~val ~(rest t) ~redefs))))))
;; Run through each test form, one at a time -
;; applying the `val` to the form as (-> `val` `test-form`).
;; If one fails, throw an exception and include
;; the form in the error message
(truth "get-data looks good" (get-data)
[map?
(-> :data :name :first (string?))]
{map? (fn [_] true)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment