EqualityComparer does not respect value-equality for null
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
NUnit Framework |
Triaged
|
Wishlist
|
Unassigned |
Bug Description
I am using a third-party library that includes Null Objects with value comparisons that return true for null. However, the NUnit equality comparer uses reference equality (via the == operator with System.Object references) to check for null and returns false if only one input is the null pointer. This means that the value-equality comparison for the custom type is never reached.
The following is a quick & dirty implementation of a Null Object with a few tests that demonstrate the problem:
public class MyObject
{
public static MyObject NullObject = new MyObject(-1);
public int Value { get; set; }
public MyObject(int value)
{
this.Value = value;
}
public static bool operator ==(MyObject left, MyObject right)
{
if (ReferenceEqual
return (ReferenceEqual
else if (ReferenceEqual
return false;
return Equals(left, right);
}
public static bool operator !=(MyObject left, MyObject right)
{
return !(left == right);
}
public override bool Equals(object obj)
{
if (ReferenceEqual
return (ReferenceEqual
else if (ReferenceEqual
return false;
MyObject myobj = obj as MyObject;
if (myobj == null)
return false;
return this.Value == myobj.Value;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
[TestFixture]
public class TestNullObject
{
// TEST PASSES
[Test]
public void TestIsTrue()
{
}
// TESTS FAIL
[Test]
public void TestIsNull()
{
}
[Test]
public void TestAreEqual1()
{
}
[Test]
public void TestAreEqual2()
{
}
}
Changed in nunitv2: | |
status: | New → Triaged |
importance: | Undecided → Wishlist |
affects: | nunitv2 → nunit-3.0 |
Looks like a valid scenario in my opinion. It's not straightforward to get right as we're doing all sorts of equality in NUnit but I put together a spike which passes all tests plus the ones Meg posted here except the Is.Null one. I think that one should not be supported as Is.Null should check for reference equality, which you cannot override.
I would like to hear what Charlie thinks too.
lp:~simone.busoli/nunitv2/null-equality