Open ikitommi opened 3 years ago
relates to #349
We're using this to write clojure.test assertions like (is (valid [:int] "asdf"))
. Would you like it as a PR in malli.test?
(defmethod t/assert-expr 'valid
[msg [_ schema data]]
`(let [is-valid?# (m/validate ~schema ~data)]
(t/do-report {:actual ~data
:expected (-> ~schema
(m/explain ~data)
(me/humanize))
:message ~msg
:type (if is-valid?# :pass :fail)})))
Failures look like:
FAIL in () (util_test.clj:14)
expected: ["should be an integer"]
actual: "asdf"
Metabase's test runner has something similar:
(defmethod t/assert-expr 'valid [msg [_ schema data]] `(let [is-valid?# (m/validate ~schema ~data)] (t/do-report {:actual ~data :expected (-> ~schema (m/explain ~data) (me/humanize)) :message ~msg :type (if is-valid?# :pass :fail)})))
Will this evaluate schema
and data
three times, once for is-valid?#
, once for actual
, and again for expected
?
I am using this version now:
(defmethod test/assert-expr 'validate
[msg [_ schema actual]]
;; Prevent double-evaluation of `schema` and `actual`:
`(let [schema# ~schema
actual# ~actual]
(test/do-report
{:type (if (m/validate schema# actual#) :pass :fail)
:message ~msg
:expected (me/humanize (m/explain schema# actual#))
:actual actual#})))
common helpers for malli assertions for tests. Can be done in user space, but, might be handy as a ns like
malli.test
.From Slack: