Skip to content

Equality checking of IsEquivalentTo is inconsistent between objects and collections #3454

@davidmehren

Description

@davidmehren

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions