自定义生成器可能会在规范生成器上构建(通过 fmap/bind)。顶层生成器重写将不会在自定义生成器内部起作用
(require '[clojure.spec :as s])
(require '[clojure.test.check.generators :as gen])
;; 一个包含单个整数值的映射
(s/def ::val integer?)
(s/def ::body (s/keys :req [::val]))
;; 此规范与 'body' 的字符串化版本匹配。
;; (read-string 仅用于演示目的)
(s/def ::stringy-body
(s/with-gen
(s/and string? #(s/valid? ::body (read-string %)))
#(gen/fmap pr-str (s/gen ::body))))
(s/valid? ::stringy-body "{:user/val 37}") ;; => true
;; 如预期生成各种字符串化映射
(take 3 (gen/sample (s/gen ::stringy-body)))
;; => ("#:user{:val -1}" "#:user{:val 0}" "#:user{:val -1}")
;; *** 但重写没有被传递 ***
(take 3 (gen/sample (s/gen ::stringy-body {::val #(s/gen #{42})})))
;; ("#:user{:val -1}" "#:user{:val 0}" "#:user{:val 0}")
考虑在 s/gen、s/with-gen 等中记录这一点。