2024 Clojure状态调查中分享您的想法!

欢迎!有关如何使用本网站的更多信息,请参阅关于页面。

+1
规范
重新标记

当使用常规表达式运算符(如 s/alts/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])

这是缺陷还是某种预期的行为?

1 答案

0

这似乎是缺陷,我在https://clojure.atlassian.net/browse/CLJ-2723上记录了

...