Skip to content

Assert.Equal and ILookup pitfall #3028

@vc-74

Description

@vc-74

Not sure if this should be considered a bug but Assert.Equal compares IEnumerable<T> and IReadOnlyDictionary<TKey, TValue> correctly, so one could assume ILookup<TKey, TValue> are covered too.

It is not the case, Assert.Equal sees lookups as collections of IGrouping<TKey, TElement> which implements IEnumerable<TElement> and therefore only compares values regardless of keys.

I don't think the IReadOnlyDictionary<TKey, TValue> case is explicitly covered, but comparing collections of KeyValuePair<TKey, TValue> compares both the key and the value which is not the case with IGrouping<TKey, TElement>.

As an example:

ILookup<bool, int> evens =
    Enumerable.Range(0, 10).ToLookup(
        i => ((i % 2) == 0),
        i => i);

ILookup<bool, int> odds =
    Enumerable.Range(0, 10).ToLookup(
        i => ((i % 2) == 1),
        i => i);

Assert.Equal(evens, odds);

succeeds because both lookups have the same values but the keys associated to these values are different.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions