1

If I try to check a macro spec with clojure.spec.test.alpha, no tests are run, but if I define the same macro as a function with the same spec, a sequence of tests are run against the function. I can always generate parameters to unit test the macro, but is there a way to get that for free with spec? Here is an example:

(ns private.tmp.spec-test
  (:require [clojure.spec.alpha :as spec]
            [clojure.spec.test.alpha :as stest]))


;;; Macro


(defmacro twice' [x]
  `(* 2.0 ~x))

(spec/fdef twice'
           :args (spec/cat :x double?)
           :ret double?
           :fn (fn [{{:keys [x]} :args, x2 :ret}]
                 (or (and
                      (Double/isNaN x)
                      (Double/isNaN x2))
                     (= x2 (+ x x)))))

(println (stest/summarize-results (stest/check `twice')))  ;; {:total 0}


;;; Function


(defn twice [x]
  (* 2.0 x))

(spec/fdef twice
           :args (spec/cat :x double?)
           :ret double?
           :fn (fn [{{:keys [x]} :args, x2 :ret}]
                 (or (and
                      (Double/isNaN x)
                      (Double/isNaN x2))
                     (= x2 (+ x x)))))

(println (stest/summarize-results (stest/check `twice)))  ;; {:total 1, :check-passed 1}
4

1 回答 1

0

我在 Clojure、Google Group 上问过这个问题,一致认为不支持检查宏。首选的测试方法是通过为单元测试生成参数test.check

https://groups.google.com/forum/#!topic/clojure/RxnwKcha0cE

于 2018-01-16T03:34:32.083 回答