Skip to content

Commit

Permalink
Merge pull request #1141 from frenchy64/issue-1121-seqable-nil-gen
Browse files Browse the repository at this point in the history
Close #1121: don't generate nil if :seqable is non-empty
  • Loading branch information
ikitommi authored Dec 8, 2024
2 parents 7f5e26a + bae2e70 commit 13984e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/malli/generator.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,13 @@
(gen/one-of gs)))

(defn- -seqable-gen [schema options]
(let [el (-> schema m/children first)]
(let [{:keys [min]} (-min-max schema options)
el (-> schema m/children first)]
(gen-one-of
(-> [nil-gen]
(-> []
(cond->
(or (nil? min) (zero? min))
(conj nil-gen))
(into (map #(-coll-gen schema % options))
[identity vec eduction #(into-array #?(:clj Object) %)])
(conj (-coll-distinct-gen schema set options))
Expand Down
6 changes: 6 additions & 0 deletions test/malli/generator_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1137,3 +1137,9 @@
[{} :map]]]
(is (every? #{{:type nil} {:type {}}} (mg/sample schema)))
(is (every? (m/validator schema) (mg/sample schema))))))

(deftest seqable-generates-non-empty-with-positive-min-test
(is (seq (mg/generate [:seqable {:min 4 :max 4} :int] {:seed 0})))
(doseq [_ (range 100)
v (mg/sample [:seqable {:min 1} :any])]
(is (seq v))))

0 comments on commit 13984e4

Please sign in to comment.