3
votes

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
are you asking to have validation of the structure of the arguments passed to each of (or at least most of) the functions in core.clj at invocation time? (coulden't include all of them because spec uses them, so it would be baseless recursion without some tricks. - Arthur Ulfeldt
Yes, I would like to validate as many functions as possible. - dilvan
I used the libraries [org.clojure/test.check "0.10.0-alpha2"][org.clojure/spec.alpha "0.1.123"] and used instrument. It worked to detect problems in functions that I wrote specs. But it didn't detected problems with clojure.core (for instance, map). Maybe specs do not work with clojurescript yet (I'm using [org.clojure/clojure "1.9.0-beta1"] [org.clojure/clojurescript "1.9.946"])? - dilvan

1 Answers

2
votes

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.