Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

=? supports #hawk/malli #10

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 18 additions & 0 deletions docs/approximately-equal.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ expected: {:a 1, :b #hawk/schema {(pred keyword?) (pred integer?)}}
+ nil
```

### `#hawk/malli`

`#hawk/malli` compares things to a `malli` schema:

```clj
(is (=? {:a 1, :b #hawk/malli [:map-of :keyword :int]}
{:a 1, :b {:c 2}}))
=> ok

(is (=? {:a 1, :b #hawk/malli [:map-of :keyword :int]}
{:a 1, :b {:c 2.0}}))
=>
expected: {:a 1, :b #hawk/malli [:map-of :keyword :int]}
actual: {:a 1, :b {:c 2.0}}
diff: - {:b {:c ["should be an integer"]}}

```

### `#hawk/approx`

`#hawk/approx` compares whether two numbers are approximately equal:
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
18 changes: 18 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,24 @@
(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"
(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