Created
November 5, 2016 01:37
-
-
Save kennyjwilli/8bf30478b8a2762d2d09baabc17e2f10 to your computer and use it in GitHub Desktop.
clojure.spec.test integration with clojure.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro defspec-test | |
([name sym-or-syms] `(defspec-test ~name ~sym-or-syms nil)) | |
([name sym-or-syms opts] | |
(when t/*load-tests* | |
`(def ~(vary-meta name assoc :test `(fn [] | |
(let [check-results# (clojure.spec.test/check ~sym-or-syms ~opts) | |
checks-passed?# (every? nil? (map :failure check-results#))] | |
(if checks-passed?# | |
(t/do-report {:type :pass | |
:message (str "Generative tests pass for " | |
(str/join ", " (map :sym check-results#)))}) | |
(doseq [failed-check# (filter :failure check-results#) | |
:let [r# (clojure.spec.test/abbrev-result failed-check#) | |
failure# (:failure r#)]] | |
(t/do-report | |
{:type :fail | |
:message (with-out-str (clojure.spec/explain-out failure#)) | |
:expected (->> r# :spec rest (apply hash-map) :ret) | |
:actual (if (instance? Throwable failure#) | |
failure# | |
(:clojure.spec.test/val failure#))}))) | |
checks-passed?#))) | |
(fn [] (t/test-var (var ~name))))))) |
This is fabulous and really should be in a library
Guess what, it's about to be lambdaisland/kaocha#95
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work! Here is a very small change that prints the check result with Expound
For the following test:
The normal output is:
whereas the output with Expound is: