ObjectsComparer icon indicating copy to clipboard operation
ObjectsComparer copied to clipboard

Tuples with complex types compared using DefaultValue comparer

Open zakkra opened this issue 2 years ago • 3 comments

This basically checks for object equality rather than comparing properties of underlying objects. IsComparable extension method should take this into account and return false.

zakkra avatar Jun 30 '22 15:06 zakkra

Can you please provide an example?

ValeraT1982 avatar Jun 30 '22 23:06 ValeraT1982

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);
        }
    }
}

zakkra avatar Jul 01 '22 10:07 zakkra

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;
            }

zakkra avatar Jul 01 '22 10:07 zakkra