Skip to content

Instantly share code, notes, and snippets.

@minimal
Created April 12, 2017 14:51
Show Gist options
  • Save minimal/2379a257773dc8a86a76290f0f833ad9 to your computer and use it in GitHub Desktop.
Save minimal/2379a257773dc8a86a76290f0f833ad9 to your computer and use it in GitHub Desktop.
(s/fdef defspec-test
:args (s/cat :name simple-symbol?
:sym-or-syms any?
:rest (s/? (s/cat
:num-tests (s/or :int (s/nilable int?) :form list?)
:last (s/? (s/cat :opts (s/nilable map?)))))))
(defmacro defspec-test
([name sym-or-syms] `(defspec-test ~name ~sym-or-syms nil nil))
([name sym-or-syms num-tests] `(defspec-test ~name ~sym-or-syms ~num-tests nil))
([name sym-or-syms num-tests opts]
(when t/*load-tests*
(let [opts' (if num-tests
(merge (or opts {}) {:clojure.spec.test.check/opts {:num-tests num-tests}})
opts)]
`(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 (if (instance? Throwable failure#)
(do (clojure.repl/pst) (str failure#))
(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))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment