This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-07-08
Channels
- # aleph (10)
- # announcements (4)
- # beginners (25)
- # calva (4)
- # cider (56)
- # clj-kondo (10)
- # cljdoc (1)
- # cljs-dev (58)
- # clojure (42)
- # clojure-austin (5)
- # clojure-dev (90)
- # clojure-europe (2)
- # clojure-greece (2)
- # clojure-italy (3)
- # clojure-nl (63)
- # clojure-quebec (1)
- # clojure-seattle (2)
- # clojure-spain (4)
- # clojure-spec (19)
- # clojure-uk (25)
- # clojuredesign-podcast (5)
- # clojurescript (24)
- # cursive (15)
- # datomic (1)
- # events (1)
- # figwheel-main (10)
- # fulcro (62)
- # graphql (7)
- # keechma (11)
- # nrepl (1)
- # off-topic (12)
- # re-frame (8)
- # reitit (2)
- # remote-jobs (3)
- # shadow-cljs (127)
- # specter (1)
- # sql (43)
- # testing (2)
- # tools-deps (30)
- # xtdb (1)
Hi, I’d like to write a spec for an entry in a map that must always be a specific string, and also can be generated. So I have {:my-key "my constant string"}
(as part of a bigger structure). A spec like this works:
(s/def ::my-key (partial = "my constant string"))
Now I try to generate data inside my test, and this fails. I’m trying to use s/with-gen
, but I think I’m missing something.
(s/def ::my-key
(s/with-gen (partial = "complexity")
(constantly "complexity")))
yields:
Assert failed: Second arg to such-that must be a generator
(generator? gen)
How should I approach this?@UAEH11THP I tried that too, also gives an error. I got it to work though after some trial and error:
(s/def ::my-key
(s/with-gen (partial = "complexity")
#(gen/return "complexity")))
@U1UQEM078 Thanks! Why is that preferred?
yeah, it is idiomatic to spec constants with a single element set. however, if that constant is either false
or nil
– use false?
and nil?
, because
(s/valid? #{false} false)
=> false
(s/valid? #{nil} nil)
=> false
@U051HUZLD I’m probably misunderstanding:
(s/valid? #{false} false)
=> false
(s/valid? #{false?} false)
=> false
(s/valid? #{nil} nil)
=> false
(s/valid? #{nil?} nil)
=> false
that's an edge case to look out for in general when using sets as predicate functions, which is really all that spec is doing here