Open flaeppe opened 1 year ago
Why not use collections.Counter
?
Yes, of course. That's not a dirty approach though and then what about the left hand side?
What if it wraps some sort of "EqualCounter"? So it counts items using equality instead of identity. Could that help align with other dirty types?
assert Counter([3, 2, 1, 3, 2, 1]) == {1: 2, 2: 2, 3: 2}
Assuming the left side was something more complicated and you wanted to only do transformations on the right, maybe combined with other dirty operators:
assert [3, 2, 1, 3, 2, 1] == IsListOrTuple(*Counter({1: 2, 2: 2, 3: 2}).elements(), check_order=False)
That way you don't have to "explicitly multiply each element".
But then it's the precondition of having a list/tuple
IsIterable
and IsSequence
sound like sensible things to have.
What if I have something non-hashable on the left hand side that I want to count?
assert [{"a": 1}, {"b": 2}, {"a": 1}, {"c": 3}, {"a": 1}] == IsListOrTuple(???)
If I have my EqualCounter
I could perhaps do
assert [{"a": 1}, {"b": 2}, {"a": 1}, {"c": 3}, {"a": 1}] == EqualCounter((IsDict(a=1), 2), (IsDict(b=2), 1), (IsDict(c=3), 1))
As the "EqualCounter" in this case would compare with each expected item with ==
. Additionally one would probably have to figure out a decent way on how to handle items being equal to multiple other items, I suppose a naive approach is to only count each item once and towards the first encountered equal element.
It might be easiest to add your own custom type?
Yeah, sure, I'll try that out
I wonder if there's a dirty equals way to assert on element count?
I suppose it's possible to use
IsListOrTuple
withcheck_order=False
. But then it's the precondition of having a list/tuple and explicitly multiply each element.e.g.
Another one is
Contains
, but as far as I can see it can't be upper bounded on "how many of something"?Could this be some extension of
Contains
? e.g. under a parametercounts={<elem>: <count>}
.I suppose this might also imply some sort of
partial={True|False}
argument forContains
, for convenience?Additionally,
<element>
ofcounts
in a case like above could probably be useful as dirty equals types? e.g.