jacobsa/oglematchers

Add SameElements matcher

Closed this issue · 7 comments

This could be useful for slices used as sets.

For example I kicked off 5 goroutines and I am waiting on all of them but I'm adding results they produce to the slice as they finish. So I may end up with something like [1, 3, 4, 2, 5] but if I can say something like:

 SameElements(results, []int{1,2,3,4,5}) 

and that returns true, this would be great. The matcher would basically check if the elements on the expected slice would appear only once on the given slice. Just my 2¢.

@mholt, what do you think about this?

@ahmetalpbalkan Realizing that, for large slices, it may not be time-efficient, I like the idea.

Would maps be more desirable for time efficiency? I guess it's more clunky since you have to have a value, which would just be true or something, and not every type can be used as map keys.

So the slice could be [1,1,1,2,2,1] and I may say SameElements(slice, []int{2,2,1,1,1,1}) I am kinda looking for something like that. Probably you wouldn't use large slices in tests? It's just ShouldResemble, that does not care about the order, but the occurrence count of elements.

Ah, I see what you mean now. Do you intend for this to be an assertion available for GoConvey? I think it would be useful, definitely. @jacobsa would know better whether it belongs here or in oglematchers or in GoConvey specifically.

Yes, please consider.

googlemock has UnorderedElementsAre:

https://groups.google.com/forum/#!topic/googlemock/kz4tACpCBME

Is this equivalent to your idea of SameElements?

@jacobsa correct!