ObjectsComparer
ObjectsComparer copied to clipboard
Tuples with complex types compared using DefaultValue comparer
This basically checks for object equality rather than comparing properties of underlying objects. IsComparable extension method should take this into account and return false.
Can you please provide an example?
using System;
using NUnit.Framework;
using ObjectsComparer.Tests.TestClasses;
namespace ObjectsComparer.Tests
{
[TestFixture]
public class TupleTest
{
[Test]
public void TupleEquality()
{
var a1 = new A { IntProperty = 10, DateTimeProperty = new DateTime(2017, 1, 1), Property3 = 5 };
var a2 = new A { IntProperty = 10, DateTimeProperty = new DateTime(2017, 1, 1), Property3 = 5 };
var b1 = new B { Property1 = "string" };
var b2 = new B { Property1 = "string" };
var comparer = new Comparer<(A, B)>();
var isEqual = comparer.Compare((a1, b1), (a2, b2));
Assert.IsTrue(isEqual);
}
}
}
I am on .net6 by the way, thanks for looking. I dealt with it in my code by looping through elements of tuple like:
if (actualFromJson is ITuple actualTuple)
{
//to deal with complex tuples
var tupleDifferences = new List<List<Difference>>();
var expectedTuple = expected as ITuple;
for (var i = 0; i < actualTuple.Length; i++)
{
var comp = new Comparer();
var isOk = comp.Compare(expectedTuple[i].GetType(), expectedTuple[i], actualTuple[i], out var diffs);
if (!isOk) tupleDifferences.Add(diffs.ToList());
}
if (tupleDifferences.Any())
{
var diffs = tupleDifferences.SelectMany(x => x).ToList();
Assert.IsTrue(false, string.Join(Environment.NewLine, diffs.Select(x => x.ToString()).ToArray()));
}
return;
}