Skip to content

Commit

Permalink
adds #hawk/malli as a reader for =?
Browse files Browse the repository at this point in the history
  • Loading branch information
qnkhuat committed Oct 7, 2023
1 parent f1c1079 commit e428de6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
methodical/methodical {:mvn/version "0.15.1"}
pjstadig/humane-test-output {:mvn/version "0.11.0"}
prismatic/schema {:mvn/version "1.4.1"}
metosin/malli {:mvn/version "0.13.0"}
org.clojure/algo.generic {:mvn/version "0.1.3"}
org.clojure/java.classpath {:mvn/version "1.0.0"}
org.clojure/tools.namespace {:mvn/version "1.3.0"}}
Expand Down
1 change: 1 addition & 0 deletions resources/data_readers.clj
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{hawk/exactly mb.hawk.assert-exprs.approximately-equal/read-exactly
hawk/schema mb.hawk.assert-exprs.approximately-equal/read-schema
hawk/malli mb.hawk.assert-exprs.approximately-equal/read-malli
hawk/approx mb.hawk.assert-exprs.approximately-equal/read-approx}
27 changes: 27 additions & 0 deletions src/mb/hawk/assert_exprs/approximately_equal.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
(:require
[clojure.algo.generic.math-functions :as algo.generic.math]
[clojure.pprint :as pprint]
[malli.core :as m]
[malli.error :as me]
[methodical.core :as methodical]
[schema.core :as s]))

Expand Down Expand Up @@ -155,6 +157,31 @@
[^Schema this actual]
(s/check (.schema this) actual))

(deftype Malli [schema])

(defn read-malli
"Data reader for `#hawk/malli`."
[schema-form]
(->Malli (eval schema-form)))

(defmethod print-dup Malli
[^Malli this ^java.io.Writer writer]
(.write writer (format "#hawk/malli %s" (pr-str (.schema this)))))

(defmethod print-method Malli
[this writer]
((get-method print-dup Malli) this writer))

(defmethod pprint/simple-dispatch Malli
[^Malli this]
(pprint/pprint-logical-block
:prefix "#hawk/malli " :suffix nil
(pprint/write-out (.schema this))))

(methodical/defmethod =?-diff [Malli :default]
[^Malli this actual]
(me/humanize (m/explain (.schema this) actual)))

(deftype Approx [expected epsilon])

(defn read-approx
Expand Down
20 changes: 20 additions & 0 deletions test/mb/hawk/assert_exprs/approximately_equal_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@
(read-string (pr-str (approximately-equal/=?-diff {:a 1, :b #hawk/schema {:c s/Int}}
{:a 1, :b {:c 2.0}})))))))))

(deftest ^:parallel malli-test
(testing "#hawk/malli"
(is (=? #hawk/malli [:map [:a :int]]
{:a 1}))
(testing "Nested inside a collection"
(is (=? {:a 1, :b #hawk/malli [:map-of :keyword :int]}
{:a 1, :b {}}))
(is (=? {:a 1, :b #hawk/malli [:map-of :keyword :int]}
{:a 1, :b {:c 2}}))
(is (=? {:a 1, :b #hawk/malli [:map-of :keyword :int]}
{:a 1, :b {:c 2, :d 3}})))
(testing "failures"
;; serialize these to strings and read them back out because Schema actually returns weird classes like
;; ValidationError or whatever that aren't equal to their printed output
(is (= '{:a ["should be an integer"]}
(read-string (pr-str (approximately-equal/=?-diff #hawk/malli [:map [:a :int]] {:a 1.0})))))
(testing "Inside a collection"
(is (= '{:b {:c ["should be an integer"]}}
(read-string (pr-str (approximately-equal/=?-diff {:a 1, :b #hawk/malli [:map [:c :int]]}
{:a 1, :b {:c 2.0}})))))))))
(deftest ^:parallel approx-test
(testing "#hawk/approx"
(is (=? #hawk/approx [1.5 0.1]
Expand Down

0 comments on commit e428de6

Please sign in to comment.