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/return support in malli.generator #933

Merged
merged 1 commit into from
Aug 16, 2023
Merged
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
8 changes: 7 additions & 1 deletion src/malli/generator.cljc
Original file line number Diff line number Diff line change
@@ -469,6 +469,10 @@
;; Creating a generator by different means, centralized under [[-create]]
;;

(defn- -create-from-return [props]
(when (contains? props :gen/return)
(gen/return (:gen/return props))))

(defn- -create-from-elements [props]
(some-> (:gen/elements props) gen-elements))

@@ -486,7 +490,8 @@
(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-elements props)
(or (-create-from-return props)
(-create-from-elements props)
(-create-from-schema props options)
(-create-from-gen props schema options)
(gen/return nil)))))
@@ -495,6 +500,7 @@
(let [props (merge (m/type-properties schema)
(m/properties schema))]
(or (-create-from-fmap props schema options)
(-create-from-return props)
(-create-from-elements props)
(-create-from-schema props options)
(-create-from-gen props schema options)
17 changes: 15 additions & 2 deletions test/malli/generator_test.cljc
Original file line number Diff line number Diff line change
@@ -123,10 +123,14 @@
(testing "map entries"
(is (= {:korppu "koira"
:piilomaan "pikku aasi"
:muuli "mukkelis"}
:muuli "mukkelis"
:aasi "isaskarin"
:kissa "pikimustan kissan"}
(mg/generate [:map {:gen/fmap '#(assoc % :korppu "koira")}
[:piilomaan {:gen/fmap '(partial str "pikku ")} [:string {:gen/elements ["aasi"]}]]
[:muuli {:gen/elements ["mukkelis"]} [:string {:gen/elements ["???"]}]]]))))
[:muuli {:gen/elements ["mukkelis"]} [:string {:gen/elements ["???"]}]]
[:aasi {:gen/return "isaskarin"} [:string {:gen/return "???"}]]
[:kissa {:gen/fmap '(partial str "pikimustan ")} [:string {:gen/return "kissan"}]]]))))

(testing "ref"
(testing "recursion"
@@ -171,6 +175,10 @@
{:gen/gen (gen/return 42)}
#"abc"]))
"Using :gen/gen")
(is (= 42 (mg/generate [:re
{:gen/return 42}
#"abc"]))
"Using :gen/return")
(is (= 42 (mg/generate [:re
{:gen/fmap (fn [_] 42)
:gen/schema :int}
@@ -211,6 +219,11 @@
(testing "with generator"
(is (re-matches #"kikka_\d+" (mg/generate [:and {:gen/fmap '(partial str "kikka_")} pos-int?])))))

(testing "gen/return"
(is (every? nil? (mg/sample [:and {:gen/return nil} int?] {:size 1000})))
(is (every? #{1} (mg/sample [:and {:gen/return 1} int?] {:size 1000})))
(is (every? #{"1"} (mg/sample [:and {:gen/return 1, :gen/fmap 'str} int?] {:size 1000}))))

(testing "gen/elements"
(is (every? #{1 2} (mg/sample [:and {:gen/elements [1 2]} int?] {:size 1000})))
(is (every? #{"1" "2"} (mg/sample [:and {:gen/elements [1 2], :gen/fmap 'str} int?] {:size 1000}))))