当使用常规表达式运算符(如 s/alt
、s/cat
等)来描述序列时,s/with-gen
并未按照我预想的方式运作。它完整地忽略了传递给定义的生成器。例如
(require '[clojure.alpha.spec :as s])
(require '[clojure.alpha.spec.gen :as gen])
(s/def ::some-spec
(s/with-gen string? (constantly (gen/return "olar"))))
(gen/sample (s/gen ::some-spec))
;;=> ("olar" "olar" "olar" "olar" "olar" "olar" "olar" "olar" "olar" "olar")
上述生成器的工作方式正如我预期,现在如果我们尝试为 s/alt 创建一个类似的生成器,我们会得到意外的结果
(s/def ::some-other-spec
(s/with-gen
(s/alt :greetings #{:olar :oizito :oi :hello :hey :yo :hallo}
:farewell #{:bye :bye-bye :hasta-la-vista :tchussy :tchau-kakao})
(constantly (gen/return [:tchau-kakao]))))
(gen/sample (s/gen ::some-other-spec))
;;=> => ([:olar] [:hasta-la-vista] [:hallo] [:hallo] [:tchussy] [:oi] [:bye-bye] [:tchussy] [:tchau-kakao] [:yo])
这是缺陷还是某种预期的行为?