Skip to content

Commit

Permalink
Test the protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
ikitommi committed Jun 9, 2020
1 parent 5f16b75 commit dc5479e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/malli/generator.cljc
Expand Up @@ -10,7 +10,7 @@
(declare generator)

(defprotocol Generator
(-generator [this] "returns generator for schema"))
(-generator [this options] "returns generator for schema"))

(defn- -random [seed] (if seed (random/make-random seed) (random/make-random)))

Expand Down Expand Up @@ -93,7 +93,7 @@

(defn- -create [schema options]
(let [{:gen/keys [gen fmap elements]} (m/properties schema options)
gen (or gen (when-not elements (if (satisfies? Generator schema) (-generator schema) (-schema-generator schema options))))
gen (or gen (when-not elements (if (satisfies? Generator schema) (-generator schema options) (-schema-generator schema options))))
elements (when elements (gen/elements elements))]
(cond
fmap (gen/fmap (m/eval fmap) (or elements gen (gen/return nil)))
Expand Down
19 changes: 15 additions & 4 deletions test/malli/generator_test.cljc
Expand Up @@ -57,12 +57,23 @@

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

(testing "gen/gen"
(dotimes [_ 1000]
(#{1 2} (mg/generate [:and {:gen/gen (gen/elements [1 2])} int?])))
(is (#{1 2} (mg/generate [:and {:gen/gen (gen/elements [1 2])} int?]))))
(dotimes [_ 1000]
(#{"1" "2"} (mg/generate [:and {:gen/gen (gen/elements [1 2]) :gen/fmap str} int?])))))
(is (#{"1" "2"} (mg/generate [:and {:gen/gen (gen/elements [1 2]) :gen/fmap str} int?]))))))

(deftest protocol-test
(let [values #{1 2 3 5 8 13}
schema (reify
m/Schema
(-validator [_] int?)
(-properties [_])
mg/Generator
(-generator [_ _] (gen/elements values)))]
(dotimes [_ 1000]
(is (values (mg/generate schema))))))

0 comments on commit dc5479e

Please sign in to comment.