Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NUnit2022 false-positive on generic Dictionary type #407

Closed
yaakov-h opened this issue Nov 25, 2021 · 3 comments · Fixed by #408
Closed

NUnit2022 false-positive on generic Dictionary type #407

yaakov-h opened this issue Nov 25, 2021 · 3 comments · Fixed by #408
Assignees
Labels

Comments

@yaakov-h
Copy link

image

IDictionary<TKey, TValue> implements ICollection<KeyValuePair<TKey, TValue>> and therefore has a Count property.

The code in the screenshot can be found here:

https://github.com/SteamDatabase/ValveKeyValue/blob/a4cef9a8aacdfb92491314176216bf11a4b4cfc0/ValveKeyValue/ValveKeyValue.Test/Text/StronglyTypedCollectionDeserializationTestCase.cs#L24-L43

@manfred-brands
Copy link
Member

Is it only the analyser?
If you suppress NUnit2022 does the actual NUnit engine run fine?

@yaakov-h
Copy link
Author

Yep, the test passes.

@manfred-brands
Copy link
Member

The issue seems to be with the generic parameter constraint.

This does not raise NUnit2022:

[Test]
public void TestCollectionMethod()
{
    IDictionary<int, int> actual = new Dictionary<int, int>() { [1] = 1, [2] = 2 };

    AssertCollectionCount(actual);
    AssertCollectionCount(actual.Values);
    AssertCollectionCount(actual.Keys);

    static void AssertCollectionCount<T>(ICollection<T> instance)
    {
        Assert.That(instance, Has.Count.EqualTo(2));
    }
}

But this version does:

[Test]
public void TestGenericMethod()
{
    IDictionary<int, int> actual = new Dictionary<int, int>() { [1] = 1, [2] = 2 };

    AssertCollectionOfIntCount(actual.Values);
    AssertCollectionOfIntCount(actual.Keys);

    static void AssertCollectionOfIntCount<T>(T instance)
        where T : ICollection<int>
    {
        Assert.That(instance, Has.Count.EqualTo(2));
    }
}

So it seems we are not checking constraints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants