From e428de699c6269f20b309408fa6cb0e5ba69eabc Mon Sep 17 00:00:00 2001 From: Ngoc Khuat Date: Sat, 7 Oct 2023 15:05:32 +0700 Subject: [PATCH] =?UTF-8?q?adds=20#hawk/malli=20as=20a=20reader=20for=20?= =?UTF-8?q?=3D=3F?= --- deps.edn | 1 + resources/data_readers.clj | 1 + .../hawk/assert_exprs/approximately_equal.clj | 27 +++++++++++++++++++ .../assert_exprs/approximately_equal_test.clj | 20 ++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/deps.edn b/deps.edn index e2657e5..202ef07 100644 --- a/deps.edn +++ b/deps.edn @@ -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"}} diff --git a/resources/data_readers.clj b/resources/data_readers.clj index 6439327..1f759a8 100644 --- a/resources/data_readers.clj +++ b/resources/data_readers.clj @@ -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} diff --git a/src/mb/hawk/assert_exprs/approximately_equal.clj b/src/mb/hawk/assert_exprs/approximately_equal.clj index b80b015..5aa23f6 100644 --- a/src/mb/hawk/assert_exprs/approximately_equal.clj +++ b/src/mb/hawk/assert_exprs/approximately_equal.clj @@ -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])) @@ -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 diff --git a/test/mb/hawk/assert_exprs/approximately_equal_test.clj b/test/mb/hawk/assert_exprs/approximately_equal_test.clj index 76decba..cb95b8e 100644 --- a/test/mb/hawk/assert_exprs/approximately_equal_test.clj +++ b/test/mb/hawk/assert_exprs/approximately_equal_test.clj @@ -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]