自定义生成器可以在规范生成器上构建(通过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等中记录这一点