This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-07-09
Channels
- # announcements (17)
- # babashka (8)
- # beginners (68)
- # calva (28)
- # clj-kondo (36)
- # cljsrn (1)
- # clojure (232)
- # clojure-dev (3)
- # clojure-europe (13)
- # clojure-nl (14)
- # clojure-spec (9)
- # clojure-uk (11)
- # clojuredesign-podcast (3)
- # clojurescript (38)
- # core-async (3)
- # cursive (1)
- # datahike (4)
- # datomic (4)
- # fulcro (56)
- # graphql (1)
- # helix (3)
- # honeysql (5)
- # introduce-yourself (1)
- # kaocha (2)
- # lsp (67)
- # malli (7)
- # meander (2)
- # off-topic (1)
- # pathom (9)
- # re-frame (55)
- # reitit (3)
- # releases (8)
- # remote-jobs (12)
- # shadow-cljs (12)
- # sql (3)
- # tools-deps (55)
- # vim (5)
- # xtdb (3)
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)
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 ?
instead of even?
you can use (every-pred int? even?)
(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?)))))
is this what you mean?
but in general - yes
ahhh, yes indeed that's correct