Clojure.spec check for fdef

I am trying to write a higher order function in clojure.spec with version 1.9.0-alpha11 and could not get the execution check for the returned function.

Using the example in the Higher Order section of the clojure.spec guide section, I define the adder form as:

 (defn adder [x] #(+ x %)) 

As described in the manual, I create a specification as:

 (s/fdef adder :args (s/cat :x number?) :ret (s/fspec :args (s/cat :y number?) :ret number?) :fn #(= (-> % :args :x) ((:ret %) 0))) 

When I execute it in repl, an exception is thrown instead of the required specification validation error:

 user> (def add2 (adder 2)) #'user/add2 user> (add2 2) 4 user> (add2 "s") ClassCastException java.lang.String cannot be cast to java.lang.Number clojure.lang.Numbers.add (Numbers.java:128) user> 

I tried to include spec with (stest/instrument `adder) . Although this works for functions, it does not work for higher-order functions .

+5
source share
1 answer

Since it is in clojure 1.9-alpha14, higher-order functions cannot be tested this way. Looking at clojure.spec.test/instrument , he searches for functions in the spec registry, which will include only fdef'd functions.

The fspec function in :ret will be used when testing adder through clojure.spec.test/check and along with documenting the function, which is its value.

+3
source

All Articles