This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-03-27
Channels
- # aws (8)
- # beginners (172)
- # boot-dev (4)
- # cider (16)
- # cljs-dev (123)
- # cljsjs (4)
- # clojure (90)
- # clojure-brasil (3)
- # clojure-dev (7)
- # clojure-dusseldorf (1)
- # clojure-finland (1)
- # clojure-italy (59)
- # clojure-russia (3)
- # clojure-seattle (2)
- # clojure-seattle-old (1)
- # clojure-spec (40)
- # clojure-uk (28)
- # clojurescript (327)
- # clojurewerkz (3)
- # code-reviews (8)
- # cursive (4)
- # datomic (24)
- # editors (1)
- # emacs (19)
- # fulcro (147)
- # funcool (1)
- # graphql (1)
- # hoplon (34)
- # jobs-rus (1)
- # lein-figwheel (5)
- # leiningen (20)
- # luminus (14)
- # midje (1)
- # off-topic (8)
- # onyx (7)
- # parinfer (47)
- # pedestal (1)
- # perun (1)
- # portkey (46)
- # re-frame (25)
- # reagent (9)
- # remote-jobs (4)
- # ring-swagger (5)
- # rum (1)
- # shadow-cljs (113)
- # slack-help (8)
- # spacemacs (7)
- # sql (9)
- # tools-deps (23)
- # uncomplicate (3)
- # unrepl (3)
- # yada (6)
Maybe you should try making ::ratom
with it’s own with-gen so that this with-gen also determines the contents of the ratom.
@hkjels I made a small example in JVM Clojure which should be straightforward to port to cljs:
(s/def ::ratom
(s/with-gen #(instance? clojure.lang.IAtom %)
#(gen/fmap atom (gen/string-alphanumeric))))
(gen/sample (s/gen ::ratom))
;; (#atom["" 0x6ff87e8] #atom["" 0x710f246c] #atom["7" 0x43b11d93] #atom["N" 0x6fd3f101] #atom["k6" 0x15e9ebd4] #atom["xFdxB" 0x1dcd8141] #atom["" 0x56db5aee] #atom["J7" 0x26f0f2cd] #atom["67Pzk94" 0x7c1d0a8c] #atom["e" 0xdab4bc7])
(s/def ::set-ratom
(s/with-gen (s/and #(instance? clojure.lang.IAtom %)
(fn [a]
(set? @a)))
#(gen/fmap atom (gen/fmap set (gen/string-alphanumeric)))))
(s/def ::id string?)
(s/def ::value string?)
(s/def ::label string?)
(s/def ::item (s/keys :req-un [::id ::value] :opt-un [::label]))
(s/def ::items
(s/coll-of ::item :kind? set))
(take 2 (gen/sample (s/gen ::items))) ([{:id "", :value ""} {:id "", :value "", :label ""} {:id "", :value ""} {:id "", :value ""} {:id "", :value ""}] [{:id "", :value "", :label ""} {:id "", :value "6"} {:id "E", :value "2", :label ""} {:id "", :value "", :label "w"}])
Indeed, the generator doesn’t return sets@hkjels This is a workaround. Not pretty, but it works:
(s/def ::items
(s/with-gen
(s/coll-of ::item :kind? set)
#(gen/fmap set (s/gen (s/coll-of ::item)))))
I’m not sure if there’s a better way as I’ve not used this part of spec much yet.I like it how spec lets you specify things gradually or as fine-grained as you want
My google-fu is failing me: I want to write a spec for a function that takes an atom (actually a Reagent atom). Is there a way to do that?
@manutter51 Maybe this helps: https://clojurians.slack.com/archives/C1B1BB2Q3/p1522140603000191
@manutter51 replace the exact type with something from here: https://github.com/reagent-project/reagent/blob/master/src/reagent/ratom.cljs#L121
(s/def :test/ratom? #(instance? reagent.ratom/RAtom %))
(s/valid? :test/ratom? 1) ; false
(s/valid? :test/ratom? (r/atom nil)) ; true
Thanks much, I’ll see if I can get that to work. The tricky part is this all has to work in CLJS, but I want to match both regular atoms and Reagent atoms so I can pass in test dummies
(I mean, testing the interface may be useful too, but the contents, maybe not so much)
yes, you can just as well create ratom dummies, so it’s a good question why you want to do that
The advice I remember is that spec is mostly useful for testing values. Testing references is likely less useful
since testing the reference at any point in time doesn’t say much about what it will become at any point in the future
it might be more useful to spec the functions that mutate that reference, or, say, use spec in a validator
Yeah, that’s a good point, I could just use ratoms everywhere
That’s probably the best solution — tks much