Fork me on GitHub
#clojure-spec
<
2021-07-09
>
Jim Newton12:07:34

I am not a spec expert, but my application has a spec interface which accepts specs and deals with them. I have some tests that are failing with randomly generated data. I'd like advise about how to prepare my tests.

(s/def ::test-spec-1 (s/* (s/alt :1  (s/cat :3 neg? :4 even?)  
                                 :2  (s/cat :5 odd? :6 pos?))))
I have a spec defined as above, ::test-spec-1, when this is applied to a sequence containing a floating point number, rather than the spec telling me that it fails to match, instead the even? function raises an exception
ERROR in (t-canonicalize-spec-type) (core.clj:1391)
Uncaught exception, not in assertion.
expected: nil
  actual: java.lang.IllegalArgumentException: Argument must be an integer: 1.0
 at clojure.core$even_QMARK_.invokeStatic (core.clj:1391)

Jim Newton12:07:36

do I need to write my own functions int-and-odd? , int-add-pos?, etc? or is there a more idiomatic way of handling this ?

delaguardo12:07:05

instead of even? you can use (every-pred int? even?)

delaguardo12:07:26

same for odd?

4
Jim Newton12:07:47

(s/def ::test-spec-1 (s/* (s/alt :1  (s/cat :3 (every-pred int? neg?) :4 (every-pred int? even?)
                                 :2  (s/cat :5 (every-pred int? odd?) :6 (every-pred int? pos?)))))

Jim Newton12:07:52

is this what you mean?

delaguardo12:07:52

(every-pred int? neg?) this is unnecessary because neg? is expecting number

4
delaguardo12:07:16

but in general - yes

Jim Newton12:07:18

ahhh, yes indeed that's correct