1

在尝试使用规范库时,我在尝试使用 exercise-fn 时遇到错误。我已将其简化为主要指南页面上发布的示例,没有任何变化。

相关代码:

(ns spec1
  (:require [clojure.spec.alpha :as s]))

;;this and the fdef are literal copies from the example page
(defn adder [x] #(+ x %))

(s/fdef adder
  :args (s/cat :x number?)
  :ret (s/fspec :args (s/cat :y number?)
                :ret number?)
  :fn #(= (-> % :args :x) ((:ret %) 0)))

现在,输入以下内容

(s/exercise-fn adder)

给出错误:

Exception No :args spec found, can't generate  clojure.spec.alpha/exercise-fn (alpha.clj:1833)

使用的依赖项/版本,[org.clojure/clojure "1.9.0-beta3"] [org.clojure/tools.logging "0.4.0"] [org.clojure/test.check "0.9.0"]

任何人都知道为什么这会破坏?谢谢。

4

1 回答 1

5

您需要反引号函数名称,这将添加命名空间前缀:

(s/exercise-fn `adder)

例如,在我的测试代码中:

(s/fdef ranged-rand
  :args (s/and
          (s/cat :start int? :end int?)
          #(< (:start %) (:end %) 1e9)) ; need add 1e9 limit to avoid integer overflow
  :ret int?
  :fn (s/and #(>= (:ret %) (-> % :args :start))
             #(< (:ret %) (-> % :args :end))))

(dotest
  (when true
    (stest/instrument `ranged-rand)
    (is (thrown? Exception (ranged-rand 8 5))))
  (spyx (s/exercise-fn `ranged-rand)))

结果是:

(s/exercise-fn (quote tst.tupelo.x.spec/ranged-rand)) 
  => ([(-2 0) -1] [(-4 1) -1] [(-2 0) -2] [(-1 0) -1] [(-14 6) -4] 
      [(-36 51) 45] [(-28 -3) -7] [(0 28) 27] [(-228 -53) -130] [(-2 0) -1])

请注意,使用了命名空间限定的函数名tst.tupelo.x.spec/ranged-rand

于 2017-11-01T19:42:26.113 回答