-
-
Notifications
You must be signed in to change notification settings - Fork 96
Description
Considering code like this
public class Tests
{
public class Message
{
public string Content { get; set; }
}
[Test]
public async Task Equality()
{
var a = new Message { Content = "Hello" };
var b = new Message { Content = "Hello" };
var c = new Message { Content = "Bye" };
// Same instance
await Assert.That(a).IsEqualTo(a);
await Assert.That(a).IsNotEqualTo(b);
// structural equivalence
await Assert.That(a).IsEquivalentTo(b);
await Assert.That(a).IsNotEquivalentTo(c);
var listA = new List<Message> { a, a };
var listB = new List<Message> { b, b };
await Assert.That(listA).IsEquivalentTo(listB);
}
}I would have expected the last assertion to pass, but it does not.
TUnit evidently uses two kinds of equality for its implementations of IsEquivalentTo:
For objects, IsEquivalentTo compares structurally (in contrast to IsEqualTo, which uses strict equivalence), while for collections the objects in the collection are compared strictly, like IsEqualTo.
I think this is inconsistent and confusing. In my opinion, IsEquivalentTo should either use structural equality also for collections or IsEquivalentTo for objects should be renamed to something like IsStructurallyEqual to make the difference clear.
I could not find documentation if the current behavior is intentional or if it is currently possible to compare objects in collections structurally.