Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoFixture - Likeness. Comparison of complex objects

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.

like image 576
Alex Art. Avatar asked Nov 05 '15 10:11

Alex Art.


1 Answers

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();
    }
}
like image 52
Nikos Baxevanis Avatar answered Oct 06 '22 01:10

Nikos Baxevanis