Skip to content

Commit c715a85

Browse files
authored
Merge pull request #1120 from frenchy64/generator-fmap-no-gen
Require generator for `:gen/fmap`
2 parents f8c6a9a + 33915cb commit c715a85

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ We use [Break Versioning][breakver]. The version numbers follow a `<major>.<mino
1414

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

17+
## NEXT
18+
19+
* **BREAKING**: `:gen/fmap` property requires its schema to create a generator.
20+
* previous behavior defaulted to a `nil`-returning generator, even if the schema doesn't accept `nil`
21+
* use `:gen/return nil` property to restore this behavior
22+
1723
## 0.16.4 (2024-08-30)
1824

1925
* Distribute `:merge` over `:multi` [#1086](https://github.com/metosin/malli/pull/1086), see [documentation](README.md#distributive-schemas)

src/malli/generator.cljc

+11-14
Original file line numberDiff line numberDiff line change
@@ -552,25 +552,22 @@
552552
(defn- -create-from-schema [props options]
553553
(some-> (:gen/schema props) (generator options)))
554554

555-
(defn- -create-from-fmap [props schema options]
555+
(defn- -create-from-fmap [gen props schema options]
556556
(when-some [fmap (:gen/fmap props)]
557557
(gen/fmap (m/eval fmap (or options (m/options schema)))
558-
(or (-create-from-return props)
559-
(-create-from-elements props)
560-
(-create-from-schema props options)
561-
(-create-from-gen props schema options)
562-
nil-gen))))
558+
gen)))
563559

564560
(defn- -create [schema options]
565561
(let [props (-merge (m/type-properties schema)
566-
(m/properties schema))]
567-
(or (-create-from-fmap props schema options)
568-
(-create-from-return props)
569-
(-create-from-elements props)
570-
(-create-from-schema props options)
571-
(-create-from-gen props schema options)
572-
(m/-fail! ::no-generator {:options options
573-
:schema schema}))))
562+
(m/properties schema))
563+
gen (or (-create-from-return props)
564+
(-create-from-elements props)
565+
(-create-from-schema props options)
566+
(-create-from-gen props schema options)
567+
(m/-fail! ::no-generator {:options options
568+
:schema schema}))]
569+
(or (-create-from-fmap gen props schema options)
570+
gen)))
574571

575572
;;
576573
;; public api

test/malli/generator_test.cljc

+13-3
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,14 @@
241241

242242
(testing "generator override"
243243
(testing "without generator"
244-
(let [schema [:fn {:gen/fmap '(fn [_] (rand-int 10))}
244+
(let [schema [:fn {:gen/elements [5]
245+
:gen/fmap '(fn [i] (rand-int i))}
245246
'(fn [x] (<= 0 x 10))]
246247
generator (mg/generator schema)]
247248
(dotimes [_ 100]
248-
(m/validate schema (mg/generate generator)))))
249+
(let [v (mg/generate generator)]
250+
(is (m/validate schema v))
251+
(is (<= 0 v 5))))))
249252
(testing "with generator"
250253
(is (re-matches #"kikka_\d+" (mg/generate [:and {:gen/fmap '(partial str "kikka_")} pos-int?])))))
251254

@@ -999,11 +1002,18 @@
9991002

10001003
(defn alphanumeric-char? [c]
10011004
{:pre [(char? c)]}
1002-
(let [i (int c)]
1005+
(let [int (fn [c]
1006+
#?(:clj (int c)
1007+
:cljs (.charCodeAt c 0)))
1008+
i (int c)]
10031009
(or (<= (int \a) i (int \z))
10041010
(<= (int \A) i (int \Z))
10051011
(<= (int \0) i (int \9)))))
10061012

1013+
(deftest alphanumeric-char?-test
1014+
(is (alphanumeric-char? \a))
1015+
(is (not (alphanumeric-char? \-))))
1016+
10071017
(defn alphanumeric-string? [s]
10081018
{:pre [(string? s)]}
10091019
(every? alphanumeric-char? s))

0 commit comments

Comments
 (0)