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

Add :gen/resolve property #1043

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/malli/generator.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,25 @@
(when-not (:gen/elements props)
(-generator schema options))))

(defn- -create-from-resolve
[props]
(when (contains? props :gen/resolve)
(let [sym (-> (:gen/resolve props))]
#?(:clj (let [v (requiring-resolve sym)]
(if (instance? clojure.lang.IDeref v)
@v
v))
:cljs (when (cljs.core/exists? sym)
(resolve sym))))))

(defn- -create-from-schema [props options]
(some-> (:gen/schema props) (generator options)))

(defn- -create-from-fmap [props schema options]
(when-some [fmap (:gen/fmap props)]
(gen/fmap (m/eval fmap (or options (m/options schema)))
(or (-create-from-return props)
(-create-from-resolve props)
(-create-from-elements props)
(-create-from-schema props options)
(-create-from-gen props schema options)
Expand All @@ -521,6 +533,7 @@
(m/properties schema))]
(or (-create-from-fmap props schema options)
(-create-from-return props)
(-create-from-resolve props)
(-create-from-elements props)
(-create-from-schema props options)
(-create-from-gen props schema options)
Expand Down
8 changes: 7 additions & 1 deletion test/malli/generator_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,13 @@

(testing "gen/gen"
(is (every? #{1 2} (mg/sample [:and {:gen/gen (gen/elements [1 2])} int?] {:size 1000})))
(is (every? #{"1" "2"} (mg/sample [:and {:gen/gen (gen/elements [1 2]) :gen/fmap str} int?] {:size 1000})))))
(is (every? #{"1" "2"} (mg/sample [:and {:gen/gen (gen/elements [1 2]) :gen/fmap str} int?] {:size 1000}))))
(testing "gen/resolve"
(is (every? #{1 2} (mg/sample [:and {:gen/resolve 'malli.generator-test/resolvable-gen} int?] {:size 1000})))
(is (every? #{"1" "2"} (mg/sample [:and {:gen/resolve 'malli.generator-test/resolvable-gen :gen/fmap str} int?] {:size 1000})))))

(def resolvable-gen
(gen/elements [1 2]))

(defn- schema+coll-gen [type children-gen]
(gen/let [children children-gen]
Expand Down
Loading