自定义生成器可能基于规范生成器构建(通过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等中记录此内容。