This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-01-07
Channels
- # announcements (4)
- # babashka (20)
- # beginners (167)
- # calva (1)
- # cider (18)
- # circleci (10)
- # clara (45)
- # clojure (85)
- # clojure-argentina (1)
- # clojure-europe (3)
- # clojure-finland (3)
- # clojure-greece (2)
- # clojure-italy (9)
- # clojure-nl (30)
- # clojure-spec (32)
- # clojure-survey (39)
- # clojure-uk (72)
- # clojurescript (12)
- # core-async (4)
- # data-science (3)
- # emacs (10)
- # figwheel-main (9)
- # fulcro (44)
- # graalvm (3)
- # jobs (12)
- # jobs-discuss (6)
- # joker (3)
- # juxt (1)
- # leiningen (4)
- # off-topic (23)
- # planck (5)
- # re-frame (4)
- # reitit (2)
- # remote-jobs (1)
- # shadow-cljs (43)
- # spacemacs (8)
- # test-check (19)
- # tools-deps (21)
I need to generate a list of maps that have distinct :number key. For example [{:number 1 ..}, {:number 2 ..}] and I need these numbers to be sequential. I tried (gen/vector-distinct-by :number (gen/hash-map :number gen/pos-int ...)
and it does work but not as sequential numbers. I don't know now if I should try to create a generator to replace gen/pos-int
or the vector-distinct-by
. rsrs
test.check generators are generally for creating random things, not sequential things like that
I was thinking about that when I got into this problem. But I want to model a situation where does not make sense to have this kind of behavior for example, a list of installments for a loan. They are generally numbered here.
there may be some way to do it (gen an int that's the size of the range, gen/fmap that with range, gen/bind that with something that makes coll of maps)
but you're going to be fighting the tide there
gen the collection (w/o numbers) and the minimum number separately; then gen/fmap
them both to set all the numbers
(gen/fmap (fn [{:keys [list-of-maps min-number]}] (map #(assoc %1 :number %2) list-of-maps (iterate inc min-number))) (gen/tuple gen-list-of-maps-without-number gen/large-integer))
@gfredericks thanks for the guidelines... I did some adaptations to work as I intended.
(def gen-list-of-maps-without-number (gen/not-empty (gen/list (gen/hash-map :testing gen/string-alpha-numeric
:values gen/double))))
(gen/fmap (fn [[list-of-maps min-number]]
(map #(assoc %1 :number %2) list-of-maps (iterate inc min-number)))
(gen/not-empty (gen/tuple gen-list-of-maps-without-number (gen/large-integer* {:min 1 :max 72}))))
Otherwise looks good
When I ran a test using defspec
, I tried two kind of conditionals: 1) Only using a (and ..
form and returning true for all branches of the test and 2) using is
from clojure.test
inside the (and.. form. I did this because without the is
I cannot see which branch actually failed, I only get back the map with the current output and the smallest input that provides the failure.
the real problem is that I have 200+ assertions now being reported by clojure.test if I place the is
inside it and I lose the smallest input that provided the failure
I thought about that, in this case here, I am validating the size of the returned list and them some content in the list.
seems like @gfredericks had some ideas about that in this library: https://github.com/gfredericks/test.chuck