在使用clojure.spec exercise-fn工作时遇到一些麻烦

时间:2017-11-01 19:38:06

标签: clojure clojure.spec

在尝试使用spec库时,我在尝试使用exercise-fn时遇到错误。我已将此缩减为the main guide page处的已发布示例,但没有任何更改。

相关代码:

(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”]

任何人都有任何想法,为什么这打破了?感谢。

1 个答案:

答案 0 :(得分:3)

您需要反引用函数名称,它将添加名称空间前缀:

(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

相关问题