AutoFixture - Likeness. Comparison of complex objects
Asked Answered
B

1

6

I am trying to compare 2 complex objects with AutoFixture's OfLikeness but unfortunately without success. While comparing nested objects( also with OfLikeness) works as expected, comparing a master object results with error saying that child objects don't match. I assume that the problem is that Likeness apply semantic comparrer only to a master object and nested objects are compared using default Equal implementation which checks for reference match(May be my assumption is wrong?).

This could clarify what I am trying to achieve:

public class ComplexMasterObject{
    public ChildFirst child1 {get;set;}
    public ChildSecond child2 {get;set;}
    public string SimpleProp {get;set;}
}

public class ChildFirst {
    public string SomeStringProp1 {get;set;}
    public int  SomeIntProp1 {get;set;}
}

public class ChildSecond {
    public string SomeStringProp1 {get;set;}
    public int  SomeIntProp1 {get;set;}
}

Test:

public void TestLikeness_Success()
{
     var expected = new ComplexMasterObject {
         Child1 = new ChildFirst {
             SomeStringProp1 = "ChildFirst SomeStringProp1",
             SomeIntProp1 = 1
         },
         Child2 = new ChildSecond {
             SomeStringProp1 = "ChildSecond SomeStringProp1",
             SomeIntProp1 = 2
         },
         SimpleProp = "ComplexMasterObject SimpleProp"
     };

     var input = new ComplexMasterObject {
         Child1 = new ChildFirst {
             SomeStringProp1 = "ChildFirst SomeStringProp1",
             SomeIntProp1 = 1
         },
         Child2 = new ChildSecond {
             SomeStringProp1 = "ChildSecond SomeStringProp1",
             SomeIntProp1 = 2
         },
         SimpleProp = "ComplexMasterObject SimpleProp"
     };

     var child1Likeness = expected.Child1.AsSource().OfLikeness<ChildFirst>();
     var child2Likeness = expected.Child2.AsSource().OfLikeness<ChildSecond>();

     var masterLikeness = expected.AsSource().OfLikeness<ComplexMasterObject>();
     child1Likeness.ShouldEqual(input.Child1); //Passes
     child2Likeness.ShouldEqual(input.Child2); //Passes

     masterLikeness.ShouldEqual(input); 
     // The provided value `ComplexMasterObject` did not match the expected value `ComplexMasterObject`. The following members did not match:
  - Child1.
  - Child2.
}

When I serialize expected and input objects to JSON and compare the results, they are identical.

I tried to use Resemblance, But it didn't work either:

var proxy = expected.AsSource().OfLikeness<ComplexMasterObject>().CreateProxy();
proxy.Equals(input); // returns false

How can perform a semantic comparison on complex object including nested complex properties?

Thank you in advance.

Berna answered 5/11, 2015 at 10:26 Comment(1)
This is a known issue: github.com/AutoFixture/AutoFixture/issues/48 Likeness uses the default Equals implementation for all matched members.Jessy
T
4

Complex structural object-equality in C# (with classes) is most of the times cumbersome, because classes use reference equality by default.

It is possible though to perform complex structural object-equality, using the SemanticComparison package that comes with AutoFixture (but gets distributed as a separate NuGet Package).

Example: SemanticComparer<T>

[Fact]
public void TestComplexClassEquality()
{
    // Arrange
    var value = new ComplexMasterObject
    {
        Child1 = new ChildFirst
        {
            SomeStringProp1 = "1",
            SomeIntProp1    =  2
        },
        Child2 = new ChildSecond
        {
            SomeStringProp1 = "3",
            SomeIntProp1    =  4
        },
        SimpleProp          = "5"
    };

    var other = new ComplexMasterObject
    {
        Child1 = new ChildFirst
        {
            SomeStringProp1 = "1",
            SomeIntProp1    =  2
        },
        Child2 = new ChildSecond
        {
            SomeStringProp1 = "3",
            SomeIntProp1    =  4
        },
        SimpleProp          = "5"
    };

    var sut =
        new SemanticComparer<ComplexMasterObject>(
            new MemberComparer(
                new AnyObjectComparer()),
            new MemberComparer(
                new ChildFirstComparer()),
            new MemberComparer(
                new ChildSecondComparer()));

    // Act
    var actual = sut.Equals(value, other);

    // Assert
    Assert.True(actual);
}

Example: Likeness<T> and its Resemblance proxy

[Fact]
public void TestComplexClassEqualityResemblance()
{
    // Arrange
    var value = new ComplexMasterObject
    {
        Child1 = new ChildFirst
        {
            SomeStringProp1 = "1",
            SomeIntProp1    =  2
        },
        Child2 = new ChildSecond
        {
            SomeStringProp1 = "3",
            SomeIntProp1    =  4
        },
        SimpleProp          = "5"
    };

    var other = new ComplexMasterObject
    {
        Child1 = new ChildFirst
        {
            SomeStringProp1 = "1",
            SomeIntProp1    =  2
        },
        Child2 = new ChildSecond
        {
            SomeStringProp1 = "3",
            SomeIntProp1    =  4
        },
        SimpleProp          = "5"
    };

    var likeness =
        new Likeness<ComplexMasterObject>(
            value,
            new SemanticComparer<ComplexMasterObject>(
                new MemberComparer(
                    new AnyObjectComparer()),
                new MemberComparer(
                    new ChildFirstComparer()),
                new MemberComparer(
                    new ChildSecondComparer())));
    var sut = likeness.ToResemblance();

    // Act
    var actual = sut.Equals(other);

    // Assert
    Assert.True(actual);
}

Object definitions

public sealed class AnyObjectComparer : IEqualityComparer
{
    public new bool Equals(object x, object y)
    {
        return object.Equals(x, y);
    }

    public int GetHashCode(object obj)
    {
        return obj.GetHashCode();
    }
}

public sealed class ChildFirstComparer : IEqualityComparer
{
    public new bool Equals(object x, object y)
    {
        var value = x as ChildFirst;
        var other = y as ChildFirst;

        if (value == null || other == null)
            return false;

        return value.SomeIntProp1    == other.SomeIntProp1
            && value.SomeStringProp1 == other.SomeStringProp1;
    }

    public int GetHashCode(object obj)
    {
        return obj.GetHashCode();
    }
}

public sealed class ChildSecondComparer : IEqualityComparer
{
    public new bool Equals(object x, object y)
    {
        var value = x as ChildSecond;
        var other = y as ChildSecond;

        if (value == null || other == null)
            return false;

        return value.SomeIntProp1    == other.SomeIntProp1
            && value.SomeStringProp1 == other.SomeStringProp1;
    }

    public int GetHashCode(object obj)
    {
        return obj.GetHashCode();
    }
}
Taryn answered 5/11, 2015 at 13:0 Comment(2)
I see 2 major problems with this solution: (1) I'll have to implement IEqualityComparer for every type that I have (and there are a lot of them). (2) It is not very flexible because if I add properties to one of the types I will have to remember to update the IEqualityComparer. I think I would rather go with serializing those objects to JSON string and comparing them. Thank you anywayBerna
Agreed. Given the current constraints there's no better way though (as far as I know, in C#).Taryn

© 2022 - 2024 — McMap. All rights reserved.