ValeraT1982 / ObjectsComparer

C# Framework provides mechanism to compare complex objects, allows to override comparison rules for specific properties and types.
MIT License
352 stars 86 forks source link

Tuples with complex types compared using DefaultValue comparer #46

Closed zakkra closed 1 year ago

zakkra commented 2 years ago

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

ValeraT1982 commented 2 years ago

Can you please provide an example?

zakkra commented 2 years ago
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 commented 2 years ago

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