Skip to content

Instantly share code, notes, and snippets.

@katox
Created May 4, 2020 11:46
Show Gist options
  • Save katox/d5817209aec78edb052f851c86e34f99 to your computer and use it in GitHub Desktop.
Save katox/d5817209aec78edb052f851c86e34f99 to your computer and use it in GitHub Desktop.
clojure.test helper function for Malli
(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