Fork me on GitHub
#test-check
<
2020-01-07
>
bartuka12:01:45

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

bartuka15:01:18

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

gfredericks13:01:50

gen the collection (w/o numbers) and the minimum number separately; then gen/fmap them both to set all the numbers

gfredericks14:01:06

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

bartuka16:01:04

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

gfredericks16:01:27

@iagwanderson the not-empty around tuple can be removed

👍 4
gfredericks16:01:11

Otherwise looks good

bartuka23:01:39

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.

bartuka23:01:47

I would like to know the branch that failed

bartuka23:01:49

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

hiredman23:01:08

make each branch a separate defspec

bartuka23:01:17

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

bartuka23:01:38

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

hiredman23:01:57

they are distinct properties however

bartuka23:01:05

seems like @gfredericks had some ideas about that in this library: https://github.com/gfredericks/test.chuck