当使用正则表达式运算符(如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])
这是不是一个bug,还是有某种预期的行为呢?