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

こ雲淡風輕ζ 提交于 2019-12-10 10:22:11

问题


Clojure 1.9 introduced specs. Functions in the clojure.core library now have specs. How do I set up a clojurescript project to use specs and test the clojure.core functions at runtime?

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. It worked to detect problems in functions that I wrote specs. But it didn't detect problems with clojure.core (for instance, map).

Maybe specs do not work with clojurescript yet.


回答1:


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

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)

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

(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.



来源:https://stackoverflow.com/questions/46565622/how-do-i-set-up-a-clojurescript-project-to-use-specs-and-test-the-clojure-core-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!