Skip to content

Commit

Permalink
Add :gen/return support in malli.generator
Browse files Browse the repository at this point in the history
  • Loading branch information
radhikalism committed Aug 16, 2023
1 parent 23f0c02 commit 9971665
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/malli/generator.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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)))))
Expand All @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions test/malli/generator_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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}))))
Expand Down

0 comments on commit 9971665

Please sign in to comment.