*描述*
如果您对函数进行了instrument,您可能会遇到以下类似的spec错误
(defn f [x] (inc x))
(s/fdef f
:args (s/cat :x (s/and integer? even?))
:ret (s/and integer? odd?))
(t/instrument)
(f 3)
;; ExceptionInfo Call to #'user/f did not conform to spec
;; In: [0] val: 3 fails at: [:args :x] predicate: even?
;; :clojure.spec.alpha/spec #object[clojure.spec.alpha$regex_spec_impl$reify__1200
0x19b3f9a "clojure.spec.alpha$regex_spec_impl$reify__1200@19b3f9a"]
;; :clojure.spec.alpha/value (3)
;; :clojure.spec.alpha/args (3)
;; :clojure.spec.alpha/failure :instrument
;; :clojure.spec.test.alpha/caller {:file "form-init3240393046310519022.clj", :lin
e 1, :var-scope user/eval1413}
;; clojure.core/ex-info (core.clj:4725)
(ex-data *e)
;; {:clojure.spec.alpha/problems
;; [{:path [:args :x],
;; :pred clojure.core/even?,
;; :val 3,
;; :via [],
;; :in [0]}],
;; :clojure.spec.alpha/spec #object[clojure.spec.alpha$regex_spec_impl$reify__1200 0x19b3f9a "clojure.spec.alpha$regex_spec_impl$reify__1200@19b3f9a"],
;; :clojure.spec.alpha/value (3),
;; :clojure.spec.alpha/args (3),
;; :clojure.spec.alpha/failure :instrument,
;; :clojure.spec.test.alpha/caller {:file "form-init3240393046310519022.clj", :line 1, :var-scope user/eval1413}}
正如您所看到的,
- explain-data中有一个正则表达式(即f的参数spec),呈现为{{::s/spec}}
- 每个问题在其{{:path}}中包含{{:args}}
这些事实可能会让spec错误报告程序感到困惑,因为f的参数spec({{(s/and integer? even?)}})没有与{{:args}}对应的子spec(我认为{{:path}}应该只包含表示从spec中选择哪个子spec的线索的键)。
*可能的解决方案*
为了解决这种令人困惑的情况并提高instrument检查的explain-data一致性,我认为有两种选择如下
- *解决方案1.* 从{{:path}}中移除{{:args}}
- *解决方案2.* 修改instrument检查的explain-data,使其具有fspec(而不是其{{:args}})作为{{::s/spec}}
我个人更喜欢*解决方案2.* 因为在explain-data中添加fspec可以为\*explain-out\*实现者提供更丰富的错误信息。
对于{{macroexpand-check}}也是如此。