StefanStolz / DeepEqualityComparer

Generic IEqualityComparer which performs a deep comparison of object trees
https://www.nuget.org/packages/DeepEqualityComparer
MIT License
0 stars 0 forks source link

Properties in enumerable types are not compared #5

Open WolfgangKluge opened 4 years ago

WolfgangKluge commented 4 years ago

I've types that implement IEnumerable and properties. On these, only the enumerable is examined during the deep equal comparison. Example


public class Foo : IEnumerable {
    private readonly List<string> data = new List<string>();
    public int Bar { get; }

    public Foo(int bar) {
        Bar = bar;
    }

    public void Add(string d) => data.Add(d);

    IEnumerator IEnumerable.GetEnumerator() => data.GetEnumerator();
}

[TestMethod]
public void Test() {
    var comparer = DeepEqualityComparer.CreateConfiguration()
        .SetLoggingTextWriter(Console.Out, false)
        .CreateEqualityComparer()
    ;

    var v1 = new Foo(1) { "x" };
    var v2 = new Foo(1) { "x" };
    var v3 = new Foo(1) { "x", "y" };
    var v4 = new Foo(1) { "y" };
    var v5 = new Foo(2) { "x" };

    Assert.IsTrue(comparer.Equals(v1, v2));
    Assert.IsFalse(comparer.Equals(v1, v3));
    Assert.IsFalse(comparer.Equals(v1, v4));
    Assert.IsFalse(comparer.Equals(v1, v4));
    Assert.IsFalse(comparer.Equals(v1, v5)); // fails
}

I see https://github.com/StefanStolz/DeepEqualityComparer/blob/bd935f0666139000ab10c4e891608742413e51f1/source/DeepEqualityComparer/DeepEqualityComparer/DeepEqualityComparer.cs#L290-L294 where you check if both objects are pure enumerables. This is not the case - so I think the return statement some lines below https://github.com/StefanStolz/DeepEqualityComparer/blob/bd935f0666139000ab10c4e891608742413e51f1/source/DeepEqualityComparer/DeepEqualityComparer/DeepEqualityComparer.cs#L335-L340 is incorrect (though, not tested).

StefanStolz commented 4 years ago

I can confirm this wrong behaviour when comparing types with properties and implementing IEnumerable<>. I'm working on a solution an will release an update in the next days.