Skip to content

Commit

Permalink
Merge pull request #1120 from frenchy64/generator-fmap-no-gen
Browse files Browse the repository at this point in the history
Require generator for `:gen/fmap`
  • Loading branch information
ikitommi authored Nov 23, 2024
2 parents f8c6a9a + 33915cb commit c715a85
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ We use [Break Versioning][breakver]. The version numbers follow a `<major>.<mino

Malli is in well matured [alpha](README.md#alpha).

## NEXT

* **BREAKING**: `:gen/fmap` property requires its schema to create a generator.
* previous behavior defaulted to a `nil`-returning generator, even if the schema doesn't accept `nil`
* use `:gen/return nil` property to restore this behavior

## 0.16.4 (2024-08-30)

* Distribute `:merge` over `:multi` [#1086](https://github.com/metosin/malli/pull/1086), see [documentation](README.md#distributive-schemas)
Expand Down
25 changes: 11 additions & 14 deletions src/malli/generator.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -552,25 +552,22 @@
(defn- -create-from-schema [props options]
(some-> (:gen/schema props) (generator options)))

(defn- -create-from-fmap [props schema options]
(defn- -create-from-fmap [gen 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-elements props)
(-create-from-schema props options)
(-create-from-gen props schema options)
nil-gen))))
gen)))

(defn- -create [schema options]
(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)
(m/-fail! ::no-generator {:options options
:schema schema}))))
(m/properties schema))
gen (or (-create-from-return props)
(-create-from-elements props)
(-create-from-schema props options)
(-create-from-gen props schema options)
(m/-fail! ::no-generator {:options options
:schema schema}))]
(or (-create-from-fmap gen props schema options)
gen)))

;;
;; public api
Expand Down
16 changes: 13 additions & 3 deletions test/malli/generator_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,14 @@

(testing "generator override"
(testing "without generator"
(let [schema [:fn {:gen/fmap '(fn [_] (rand-int 10))}
(let [schema [:fn {:gen/elements [5]
:gen/fmap '(fn [i] (rand-int i))}
'(fn [x] (<= 0 x 10))]
generator (mg/generator schema)]
(dotimes [_ 100]
(m/validate schema (mg/generate generator)))))
(let [v (mg/generate generator)]
(is (m/validate schema v))
(is (<= 0 v 5))))))
(testing "with generator"
(is (re-matches #"kikka_\d+" (mg/generate [:and {:gen/fmap '(partial str "kikka_")} pos-int?])))))

Expand Down Expand Up @@ -999,11 +1002,18 @@

(defn alphanumeric-char? [c]
{:pre [(char? c)]}
(let [i (int c)]
(let [int (fn [c]
#?(:clj (int c)
:cljs (.charCodeAt c 0)))
i (int c)]
(or (<= (int \a) i (int \z))
(<= (int \A) i (int \Z))
(<= (int \0) i (int \9)))))

(deftest alphanumeric-char?-test
(is (alphanumeric-char? \a))
(is (not (alphanumeric-char? \-))))

(defn alphanumeric-string? [s]
{:pre [(string? s)]}
(every? alphanumeric-char? s))
Expand Down

0 comments on commit c715a85

Please sign in to comment.