nunit/nunit.analyzers

NUnit2010 - Is.EqualTo does not consider Enums

matode opened this issue · 2 comments

If there is a SomeEnum defined with e.g. 2 entries "Undefined" and "SomeOtherValue" and there is a test with an Assert like

var someValue = TestEnum.SomeOtherValue; //for simplicity. The value is obtained from an API call in testee.
Assert.That((someValue&SomeEnum.Undefined)==0);

The CodeFix for NUnit2010 changes the assert to

Assert.That(someValue&SomeEnum.Undefined, Is.EqualTo(0));

This test fails with the error, that the value was "Undefined" and not 0.

Either the Is.EqualTo implementation has to be changed in NUnit framework or the CodeFix should replace the "0" with the corresponding element in the enum.

I don't think the Analyzer should replace 0 with Undefined. It was not in the original code.

Note that the Analyzer raises NUnit2021 over the changed code:

Assert.That((someValue & SomeEnum.Undefined), Is.EqualTo(0));

image

This hints that the developer might consider changing it code for clarity.

Although the underlying value of Undefined is 0 should it really compare as equal?

What if I had 2 enums. Should we really accept these as Equal?

[Test]
public void EnumTest()
{
    Assert.That(OneEnum.Defined, Is.EqualTo(AnotherEnum.Defined));
}

private enum OneEnum
{
    Undefined = 0,
    Defined = 1,
};
private enum AnotherEnum
{
    Undefined = 0,
    Defined = 1,
};

Maybe enum compared to int should be allowed, but enum A vs enum B should not.

If we accept the first, then both NUnit and Nunit Analyzer (rule 2021) must be updated to account for that.

Sorry, I do not see NUnit2021 finding for it on my system. This might be because of my setup.

But if it is so, then it is obvious for the developer, that this check cannot work.
You can close this.