【问题标题】:How to check Clojure, macro specs?如何检查 Clojure,宏规范?
【发布时间】:2018-01-11 20:05:52
【问题描述】:

如果我尝试使用 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 macros clojure.spec generative-testing


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      相关资源
      最近更新 更多