Skip to content

Is.EqualTo(...).Using(StructuralComparisons.StructuralEqualityComparer or StructuralComparer) not working #3447

Closed
@marklam

Description

@marklam

System.Collections.Immutable.ImmutableArray implements both reference equality and structural equality via IStructuralEquatable.

I want to be able to check two arrays for structural equality, but only some of the overloads of EqualConstraint.Using work as expected.

The two overloads that take IEqualityComparer or IComparer don't use the correct comparison, whereas the versions that take delegates do.

The attached code (which was built with packages NUnit 3.12.0 and System.Collections.Immutable 1.7.0 fails the last two tests.

using System;
using System.Collections;
using System.Collections.Immutable;
using NUnit.Framework;

namespace NUnitStructural
{
    [TestFixture]
    public class Test
    {
        [Test]
        public void ImmutableArrayNotReferenceEquality()
        {
            var x = ImmutableArray.Create(1, 2, 3);
            var y = ImmutableArray.Create(1, 2, 3);

            Assert.That(x != y);
        }

        [Test]
        public void ImmutableArrayWithStructuralEqualityComparison()
        {
            var x = ImmutableArray.Create(1, 2, 3);
            var y = ImmutableArray.Create(1, 2, 3);

            Comparison<ImmutableArray<int>> comparison = (a, b) => StructuralComparisons.StructuralComparer.Compare(a, b);
            Assert.That(x, Is.EqualTo(y).Using(comparison));
        }

        [Test]
        public void ImmutableArrayWithStructuralEqualityComparerEquals()
        {
            var x = ImmutableArray.Create(1, 2, 3);
            var y = ImmutableArray.Create(1, 2, 3);

            var del = new Func<ImmutableArray<int>, ImmutableArray<int>, bool>((a, b) => StructuralComparisons.StructuralEqualityComparer.Equals(a, b));
            Assert.That(x, Is.EqualTo(y).Using<ImmutableArray<int>>(del));
        }

        [Test]
        public void ImmutableArrayWithStructuralComparerEquals()
        {
            var x = ImmutableArray.Create(1, 2, 3);
            var y = ImmutableArray.Create(1, 2, 3);

            var del = new Func<ImmutableArray<int>, ImmutableArray<int>, bool>((a, b) => StructuralComparisons.StructuralComparer.Compare(a, b) == 0);
            Assert.That(x, Is.EqualTo(y).Using<ImmutableArray<int>>(del));
        }

        [Test]
        public void ImmutableArrayWithStructuralEqualityComparer()
        {
            var x = ImmutableArray.Create(1, 2, 3);
            var y = ImmutableArray.Create(1, 2, 3);

            Assert.That(x, Is.EqualTo(y).Using(StructuralComparisons.StructuralEqualityComparer));
        }

        [Test]
        public void ImmutableArrayWithStructuralComparer()
        {
            var x = ImmutableArray.Create(1, 2, 3);
            var y = ImmutableArray.Create(1, 2, 3);

            Assert.That(x, Is.EqualTo(y).Using(StructuralComparisons.StructuralComparer));
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions