Created
May 4, 2020 11:46
-
-
Save katox/d5817209aec78edb052f851c86e34f99 to your computer and use it in GitHub Desktop.
clojure.test helper function for Malli
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
(def conforming? 'conforming?) | |
(defmethod clojure.test/assert-expr 'conforming? [msg form] | |
;; (is (conforming? schema structure)) | |
;; Asserts that evaluating structure conforms to the given malli schema | |
`(let [schema# ~(nth form 1) | |
structure# ~(nth form 2)] | |
(try (let [result# (m/validate schema# structure#)] | |
(if result# | |
(do-report {:type :pass, :message ~msg, | |
:expected '~(nth form 1), :actual structure#}) | |
(let [explanation# (m/explain schema# structure#) | |
msg# (str "Failed to conform schema:\n" | |
(with-out-str | |
(clojure.pprint/pprint | |
(me/humanize explanation#)))) | |
value# (:value explanation#)] | |
(do-report {:type :fail | |
:message msg#, | |
:expected '~(nth form 1) | |
:actual value#})))) | |
(catch Exception e# | |
(do-report {:type :fail, :message ~msg, | |
:expected '~form, :actual e#}) | |
e#)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment