Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delayed gen #409

Merged
merged 3 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/malli/generator.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@
(string-from-regex (re-pattern (str/replace (str re) #"^\^?(.*?)(\$?)$" "$1"))))
(miu/-fail! :test-chuck-not-available))))

(defn -ref-gen [schema options]
(let [gen* (delay (generator (m/deref-all schema) options))]
(gen/->Generator (fn [rnd size] ((:gen @gen*) rnd size)))))

(defn -=>-gen [schema options]
(let [{:keys [min max input output] :or {max miu/+max-size+}} (m/-function-info schema)
validate-input (m/validator input)
Expand Down Expand Up @@ -219,7 +223,7 @@

(defmethod -schema-generator :=> [schema options] (-=>-gen schema options))
(defmethod -schema-generator :function [schema options] (-function-gen schema options))
(defmethod -schema-generator :ref [schema options] (generator (m/deref schema) options))
(defmethod -schema-generator :ref [schema options] (-ref-gen schema options))
(defmethod -schema-generator :schema [schema options] (generator (m/deref schema) options))
(defmethod -schema-generator ::m/schema [schema options] (generator (m/deref schema) options))

Expand Down
20 changes: 20 additions & 0 deletions test/malli/generator_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,23 @@
[:vector {:gen/min 2, :gen/max 2} [:ref ::A]]]}}
::A] {:size 1, :seed 1})]
(is (-> sample flatten count (> 1)))))

(deftest slow-recursive-test
(let [schema [:schema {:registry {::A [:tuple [:= :A]]
::B [:tuple [:= :B]]
::C [:tuple [:= :C]]
::D [:tuple [:= :D]]
::E [:tuple [:= :E] [:ref ::item]]
::F [:tuple [:= :F] [:ref ::item]]
::G [:tuple [:= :G] [:ref ::item]]
::item [:multi {:dispatch first}
[:A ::A]
[:B ::B]
[:C ::C]
[:D ::D]
[:E ::E]
[:F ::F]
[:G ::G]]}}
::E]
valid? (m/validator schema)]
(is (every? valid? (mg/sample schema {:size 10000})))))