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.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With