Skip to content

Commit 1e489c8

Browse files
authored
Merge pull request #1143 from frenchy64/float-generator
align :float and :double generators
2 parents f664244 + addf951 commit 1e489c8

File tree

3 files changed

+32
-57
lines changed

3 files changed

+32
-57
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Malli is in well matured [alpha](README.md#alpha).
2222
* use `:gen/return nil` property to restore this behavior
2323
* Support decoding map keys into keywords for `[:map` schemas in `json-transformer` [#1135](https://github.com/metosin/malli/issues/1135)
2424
* FIX: `malli.registry/{mode,type}` not respected in Babashka [#1124](https://github.com/metosin/malli/issues/1124)
25+
* FIX: `:float` accepts doubles but never generates them [#1132](https://github.com/metosin/malli/issues/1132)
2526
* FIX: `:float` missing humanizer [#1122](https://github.com/metosin/malli/issues/1122)
2627
* Updated dependencies:
2728

src/malli/generator.cljc

+10-18
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@
8787
(let [{:gen/keys [infinite? NaN?]} (m/properties schema)]
8888
{:infinite? infinite? :NaN? NaN?}))
8989

90-
(defn- -double-gen [schema options] (gen-double (into (inf-nan schema options) (-min-max schema options))))
91-
9290
(defn- gen-fmap [f gen] (or (-unreachable gen) (gen/fmap f gen)))
9391
(defn- gen-fcat [gen] (gen-fmap #(apply concat %) gen))
9492
(defn- gen-tuple [gens] (or (some -unreachable gens) (apply gen/tuple gens)))
@@ -363,6 +361,14 @@
363361
(gen/return (first es))
364362
(gen/elements es)))
365363

364+
(defn- double-gen [schema options]
365+
(gen/double* (merge (let [props (m/properties schema options)]
366+
{:infinite? (get props :gen/infinite? false)
367+
:NaN? (get props :gen/NaN? false)})
368+
(-> (-min-max schema options)
369+
(update :min #(some-> % double))
370+
(update :max #(some-> % double))))))
371+
366372
(defmulti -schema-generator (fn [schema options] (m/type schema options)) :default ::default)
367373

368374
(defmethod -schema-generator ::default [schema options] (ga/gen-for-pred (m/validator schema options)))
@@ -397,22 +403,8 @@
397403
(defmethod -schema-generator :nil [_ _] nil-gen)
398404
(defmethod -schema-generator :string [schema options] (-string-gen schema options))
399405
(defmethod -schema-generator :int [schema options] (gen/large-integer* (-min-max schema options)))
400-
(defmethod -schema-generator :double [schema options] (-double-gen schema options))
401-
(defmethod -schema-generator :float [schema options]
402-
(let [max-float #?(:clj Float/MAX_VALUE :cljs (.-MAX_VALUE js/Number))
403-
min-float (- max-float)
404-
props (m/properties schema options)
405-
min-max-props (-min-max schema options)
406-
infinite? #?(:clj false :cljs (get props :gen/infinite? false))]
407-
(->> (merge {:infinite? infinite?
408-
:NaN? (get props :gen/NaN? false)}
409-
(-> min-max-props
410-
(update :min #(or (some-> % float)
411-
#?(:clj min-float :cljs nil)))
412-
(update :max #(or (some-> % float)
413-
#?(:clj max-float :cljs nil)))))
414-
(gen/double*)
415-
(gen/fmap float))))
406+
(defmethod -schema-generator :double [schema options] (double-gen schema options))
407+
(defmethod -schema-generator :float [schema options] (double-gen schema options))
416408
(defmethod -schema-generator :boolean [_ _] gen/boolean)
417409
(defmethod -schema-generator :keyword [_ _] gen/keyword)
418410
(defmethod -schema-generator :symbol [_ _] gen/symbol)

test/malli/generator_test.cljc

+21-39
Original file line numberDiff line numberDiff line change
@@ -51,45 +51,27 @@
5151
:qualified-symbol]]
5252
(is (every? (m/validator schema) (mg/sample schema {:size 1000})))))
5353

54-
(testing "double properties"
55-
(let [infinity? #(or (= % ##Inf)
56-
(= % ##-Inf))
57-
NaN? (fn [x]
58-
(#?(:clj Double/isNaN
59-
:cljs js/isNaN)
60-
x))
61-
special? #(or (NaN? %)
62-
(infinity? %))
63-
test-presence (fn [f options]
64-
(some f (mg/sample [:double options]
65-
{:size 1000})))]
66-
(is (test-presence infinity? {:gen/infinite? true}))
67-
(is (test-presence NaN? {:gen/NaN? true}))
68-
(is (test-presence special? {:gen/infinite? true
69-
:gen/NaN? true}))
70-
(is (not (test-presence special? nil)))))
71-
72-
(testing "float properties"
73-
(let [infinity? #(or (= % ##Inf)
74-
(= % ##-Inf))
75-
NaN? (fn [x]
76-
(#?(:clj Float/isNaN
77-
:cljs js/isNaN)
78-
x))
79-
is-float? (fn [n]
80-
#?(:clj (instance? Float n)
81-
:cljs (float? n)))
82-
special? #(or (NaN? %)
83-
(infinity? %))
84-
test-presence (fn [f options]
85-
(some f (mg/sample [:float options]
86-
{:size 1000})))]
87-
(is (test-presence #?(:clj (comp not infinity?) :cljs infinity?) {:gen/infinite? true}))
88-
(is (test-presence is-float? {}))
89-
(is (test-presence NaN? {:gen/NaN? true}))
90-
(is (test-presence special? {:gen/infinite? true
91-
:gen/NaN? true}))
92-
(is (not (test-presence special? nil)))))
54+
(doseq [s [:double :float]]
55+
(testing (str s " properties")
56+
(let [infinity? #(or (= % ##Inf)
57+
(= % ##-Inf))
58+
NaN? (fn [x]
59+
(#?(:clj Double/isNaN
60+
:cljs js/isNaN)
61+
x))
62+
special? #(or (NaN? %)
63+
(infinity? %))
64+
valid? (m/validator s)
65+
test-presence (fn [f options]
66+
(let [vs (mg/sample [s options]
67+
{:size 1000})]
68+
(and (every? valid? vs)
69+
(some f vs))))]
70+
(is (test-presence infinity? {:gen/infinite? true}))
71+
(is (test-presence NaN? {:gen/NaN? true}))
72+
(is (test-presence special? {:gen/infinite? true
73+
:gen/NaN? true}))
74+
(is (not (test-presence special? nil))))))
9375

9476
(testing "qualified-keyword properties"
9577
(testing "no namespace => random"

0 commit comments

Comments
 (0)