This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-07-01
Channels
- # beginners (134)
- # boot (4)
- # cider (11)
- # cljs-dev (3)
- # cljsrn (10)
- # clojure (85)
- # clojure-dev (10)
- # clojure-spec (17)
- # clojure-uk (14)
- # clojurescript (19)
- # copenhagen-clojurians (1)
- # data-science (15)
- # datascript (3)
- # datomic (3)
- # fulcro (1)
- # graphql (3)
- # heroku (1)
- # hoplon (1)
- # leiningen (2)
- # nrepl (11)
- # om-next (1)
- # onyx (35)
- # reitit (3)
- # shadow-cljs (43)
- # spacemacs (2)
- # specter (1)
- # test-check (10)
- # tools-deps (1)
- # vim (1)
Does anyone know of an efficient way to generate a multi-spec value with a particular tag? (ie. without doing a such-that and hoping to get lucky)
@codonnell what do you mean by a multi-spec value? have an example?
If i run a stest/check
and hit an error, how do i inspect the value that was generated? my return value is a function. I fspec
ced it, and one of the inputs to that function, generated during stest/check
is causing the spec to fail
(s/def ::type #{::a ::b})
(s/def ::a-value pos-int?)
(s/def ::b-value keyword?)
(defmulti mymap-type ::type)
(defmethod mymap-type ::a [_]
(s/keys :req [::type ::a-value]))
(defmethod mymap-type ::b [_]
(s/keys :req [::type ::b-value]))
(s/def ::mymap (s/multi-spec mymap-type ::type))
;; How to generate a mymap value with tag ::a here without using gen/such-that
something like this?
(s/conform ::mymap {::type ::a
::a-value 1})
(s/conform ::mymap {::type ::b
::b-value :foo})
(map (partial s/explain-data ::mymap)
(gen/sample (tgen/let [type_ (gen/elements [::a ::b])
a-value tgen/pos-int
b-value (gen/keyword)]
(conj {::type type_}
(if (= type_ ::a)
{::a-value (inc a-value)}
{::b-value b-value})))))
tgen
is clojure.test.check.generators
[clojure.spec.gen.alpha :as gen]
pos-int
generates 0
sometimes, hence the inc
this talk was super helpful for me https://www.youtube.com/watch?v=F4VZPxLZUdA
s-pos-int
doesn't generate zeros, fyi
I'd prefer not to build up the value manually like that, since the actual spec I'm working with is much more complex than the toy example I posted above.
I'll definitely check out that talk, thanks.
@codonnell (gen/sample (s/gen ::mymap {::type #(gen/return ::a)}))
It would have been, but there is a bug with providing a generator for the dispatch key - See my bug report and patch https://dev.clojure.org/jira/browse/CLJ-2311
thanks @andreas862, that is exactly what I was looking for. Hopefully your patch is accepted!