如何检查 Clojure,宏规范?
How to check Clojure, macro specs?
如果我尝试使用 clojure.spec.test.alpha 检查宏规范,则没有测试 运行,但如果我将相同的宏定义为具有相同规范的函数,则一系列测试是 运行 反对功能。我总是可以生成参数来对宏进行单元测试,但是有没有办法通过规范免费获得它?这是一个例子:
(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}
我在 Clojure Google 小组上问过这个问题,一致认为不支持检查宏。首选的测试方法是通过 test.check
.
为单元测试生成参数
如果我尝试使用 clojure.spec.test.alpha 检查宏规范,则没有测试 运行,但如果我将相同的宏定义为具有相同规范的函数,则一系列测试是 运行 反对功能。我总是可以生成参数来对宏进行单元测试,但是有没有办法通过规范免费获得它?这是一个例子:
(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}
我在 Clojure Google 小组上问过这个问题,一致认为不支持检查宏。首选的测试方法是通过 test.check
.