From fca90f9f9a6b50ee5324e16cde3885216f990b16 Mon Sep 17 00:00:00 2001 From: Adam Helinski Date: Tue, 27 Apr 2021 15:10:01 +0200 Subject: [PATCH 1/2] Fix #427 Comparing `-coll-gen` and `-coll-gen-distict` in the `malli.generator` namespace, it seems there was no reason for the latter to handle recursive generation any different. Both implementations now create a generator based on the same initial conditions. --- src/malli/generator.cljc | 5 ++--- test/malli/generator_test.cljc | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/malli/generator.cljc b/src/malli/generator.cljc index 0fe257600..19cf43717 100644 --- a/src/malli/generator.cljc +++ b/src/malli/generator.cljc @@ -50,7 +50,7 @@ (let [{:keys [min max]} (-min-max schema options) [continue options] (-recur schema options) child (-> schema m/children first) - gen (if continue (generator child options))] + gen (when continue (generator child options))] (gen/fmap f (cond (not gen) (gen/vector gen/any 0 0) (and min (= min max)) (gen/vector gen min) @@ -63,8 +63,7 @@ (let [{:keys [min max]} (-min-max schema options) [continue options] (-recur schema options) child (-> schema m/children first) - gen (if-not (and (= :ref (m/type child)) continue (<= (or min 0) 0)) - (generator child options))] + gen (when continue (generator child options))] (gen/fmap f (if gen (gen/vector-distinct gen {:min-elements min, :max-elements max, :max-tries 100}) (gen/vector gen/any 0 0))))) diff --git a/test/malli/generator_test.cljc b/test/malli/generator_test.cljc index 2db9e0fe1..2f7ce33ce 100644 --- a/test/malli/generator_test.cljc +++ b/test/malli/generator_test.cljc @@ -267,3 +267,10 @@ ::E] valid? (m/validator schema)] (is (every? valid? (mg/sample schema {:size 10000}))))) + +(deftest recursive-distinct-col-test + (is (not (every? empty? + (mg/sample [:set + {:registry {::foo :int}} + [:ref ::foo]] + {:size 1000}))))) From 82d6fb6b27ccc4cf9387b5854fb4214ac77a9990 Mon Sep 17 00:00:00 2001 From: Adam Helinski Date: Tue, 27 Apr 2021 15:13:31 +0200 Subject: [PATCH 2/2] Fix typos Attention, this is technically a breaking change since `malli.generator/-coll-distict-gen` has been renamed. --- src/malli/generator.cljc | 4 ++-- src/malli/util.cljc | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/malli/generator.cljc b/src/malli/generator.cljc index 19cf43717..b8b0a74ed 100644 --- a/src/malli/generator.cljc +++ b/src/malli/generator.cljc @@ -59,7 +59,7 @@ max (gen/vector gen 0 max) :else (gen/vector gen))))) -(defn- -coll-distict-gen [schema f options] +(defn- -coll-distinct-gen [schema f options] (let [{:keys [min max]} (-min-max schema options) [continue options] (-recur schema options) child (-> schema m/children first) @@ -199,7 +199,7 @@ (defmethod -schema-generator :multi [schema options] (-multi-gen schema options)) (defmethod -schema-generator :vector [schema options] (-coll-gen schema identity options)) (defmethod -schema-generator :sequential [schema options] (-coll-gen schema identity options)) -(defmethod -schema-generator :set [schema options] (-coll-distict-gen schema set options)) +(defmethod -schema-generator :set [schema options] (-coll-distinct-gen schema set options)) (defmethod -schema-generator :enum [schema options] (gen/elements (m/children schema options))) (defmethod -schema-generator :maybe [schema options] diff --git a/src/malli/util.cljc b/src/malli/util.cljc index 255b8c999..7c08adde2 100644 --- a/src/malli/util.cljc +++ b/src/malli/util.cljc @@ -168,7 +168,7 @@ @state))) (defn distinct-by - "Returns a sequence of distict (f x) values)" + "Returns a sequence of distinct (f x) values)" [f coll] (let [seen (atom #{})] (filter (fn [x] (let [v (f x)] (if-not (@seen v) (swap! seen conj v)))) coll)))