Fork me on GitHub

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

Alex Miller (Clojure team)13:01:59

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.

Alex Miller (Clojure team)13:01:16

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)

Alex Miller (Clojure team)13:01:28

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}))))


@iagwanderson the not-empty around tuple can be removed

👍 4

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.


I would like to know the branch that failed


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


make each branch a separate defspec


I thought about that, in this case here, I am validating the size of the returned list and them some content in the list.


it seems a lot of duplication of code to test only the count for example


they are distinct properties however


seems like @gfredericks had some ideas about that in this library: