Skip to content

Commit

Permalink
Merge pull request #429 from helins/gen_set_recur
Browse files Browse the repository at this point in the history
Fix #427
  • Loading branch information
ikitommi authored Apr 27, 2021
2 parents 821c0ca + 82d6fb6 commit abf3931
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/malli/generator.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -59,12 +59,11 @@
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)
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)))))
Expand Down Expand Up @@ -200,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]
Expand Down
2 changes: 1 addition & 1 deletion src/malli/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
7 changes: 7 additions & 0 deletions test/malli/generator_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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})))))

0 comments on commit abf3931

Please sign in to comment.