简体   繁体   English

如何设置clojureScript项目以使用规范并在运行时测试clojure.core函数?

[英]How do I set up a clojureScript project to use specs and test the clojure.core functions at runtime?

Clojure 1.9 introduced specs . Clojure 1.9引入了规范 Functions in the clojure.core library now have specs. clojure.core库中的函数现在有规格。 How do I set up a clojurescript project to use specs and test the clojure.core functions at runtime? 如何设置clojurescript项目以使用规范并在运行时测试clojure.core函数?

I used the libraries [org.clojure/test.check "0.10.0-alpha2"] and [org.clojure/spec.alpha "0.1.123"] to install specs and the command instrument . 我使用库[org.clojure/test.check "0.10.0-alpha2"][org.clojure/spec.alpha "0.1.123"]来安装specs和命令instrument It worked to detect problems in functions that I wrote specs. 它可以检测我编写规范的函数中的问题。 But it didn't detect problems with clojure.core (for instance, map ). 但它没有检测到clojure.core问题(例如, map )。

Maybe specs do not work with clojurescript yet. 也许specs不适用于clojurescript。

This is because there are no specs for clojure.core functions, only specs for a couple of macros - look here: https://github.com/clojure/core.specs.alpha/blob/master/src/main/clojure/clojure/core/specs/alpha.clj#L53 这是因为没有针对clojure.core函数的规范,只有几个宏的规范 - 请看这里: https//github.com/clojure/core.specs.alpha/blob/master/src/main/clojure/ Clojure的/核心/规格/ alpha.clj#L53

Note, that: 注意:

macros are always checked during macro expansion, you do not need to call instrument for macro specs 宏扩展期间始终检查宏,您无需为宏规范调用仪器

( https://clojure.org/guides/spec#_macros ) https://clojure.org/guides/spec#_macros

You can write your own specs for core functions and register them as shown here: https://clojure.github.io/clojure/branch-master/clojure.spec-api.html#clojure.spec/fdef 您可以为核心函数编写自己的规范并注册它们,如下所示: https//clojure.github.io/clojure/branch-master/clojure.spec-api.html#clojure.spec/fdef

(s/fdef clojure.core/symbol
  :args (s/alt :separate (s/cat :ns string? :n string?)
               :str string?
               :sym symbol?)
  :ret symbol?)

However, be careful with this since you might get into nasty issues if you don't get it right. 但是,要小心这一点,因为如果你没有把它弄好,你可能会遇到令人讨厌的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM