From 99716653988df3fd7af5329d30e76d4df91caddb Mon Sep 17 00:00:00 2001 From: radhika Date: Thu, 17 Aug 2023 00:26:10 +1200 Subject: [PATCH] Add :gen/return support in malli.generator --- src/malli/generator.cljc | 8 +++++++- test/malli/generator_test.cljc | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/malli/generator.cljc b/src/malli/generator.cljc index 88ab509e9..59f4a98bd 100644 --- a/src/malli/generator.cljc +++ b/src/malli/generator.cljc @@ -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) diff --git a/test/malli/generator_test.cljc b/test/malli/generator_test.cljc index 20d088621..d64050653 100644 --- a/test/malli/generator_test.cljc +++ b/test/malli/generator_test.cljc @@ -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}))))