自定义生成器可以构建(通过fmap/bind)在spec生成器上。最高层的生成器重写不会在自定义生成器内部生效
(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等中记录这一点