From d199f9f2081c92e40c092a71ee53258a31522639 Mon Sep 17 00:00:00 2001 From: Greg Finzer Date: Sat, 28 Jan 2023 11:04:29 -0500 Subject: [PATCH] Use Enum Comparison Version 4.79 --- .../CompareDictionaryTests.cs | 2 + .../CompareIListTests.cs | 2 + .../Compare-NET-Objects.csproj | 10 +++-- Compare-NET-Objects/RootComparer.cs | 39 +++++++++++-------- Compare-NET-Objects/RootComparerFactory.cs | 2 +- 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/Compare-NET-Objects-Tests/CompareDictionaryTests.cs b/Compare-NET-Objects-Tests/CompareDictionaryTests.cs index fabbb14..888fa7f 100644 --- a/Compare-NET-Objects-Tests/CompareDictionaryTests.cs +++ b/Compare-NET-Objects-Tests/CompareDictionaryTests.cs @@ -51,6 +51,7 @@ public void TestDictionary() Dictionary dict2 = Common.CloneWithSerialization(dict1); + _compare.Config.DateTimeKindToUseWhenUnspecified = DateTimeKind.Local; ComparisonResult result = _compare.Compare(dict1, dict2); if (!result.AreEqual) @@ -75,6 +76,7 @@ public void TestDictionaryNegative() dict2["1002"].DateCreated = DateTime.Now.AddDays(1); + _compare.Config.DateTimeKindToUseWhenUnspecified = DateTimeKind.Local; var result = _compare.Compare(dict1, dict2); Assert.IsFalse(result.AreEqual); diff --git a/Compare-NET-Objects-Tests/CompareIListTests.cs b/Compare-NET-Objects-Tests/CompareIListTests.cs index 84a0b67..8eaaf6d 100644 --- a/Compare-NET-Objects-Tests/CompareIListTests.cs +++ b/Compare-NET-Objects-Tests/CompareIListTests.cs @@ -162,6 +162,7 @@ public void ArrayTest() array2[0] = Common.CloneWithSerialization(p1); array2[1] = Common.CloneWithSerialization(p2); + _compare.Config.DateTimeKindToUseWhenUnspecified = DateTimeKind.Local; ComparisonResult result = _compare.Compare(array1, array2); if (!result.AreEqual) @@ -190,6 +191,7 @@ public void ArrayTestNegative() array2[1] = Common.CloneWithSerialization(p2); array2[1].Name = "Bob"; + _compare.Config.DateTimeKindToUseWhenUnspecified = DateTimeKind.Local; Assert.IsFalse(_compare.Compare(array1, array2).AreEqual); } diff --git a/Compare-NET-Objects/Compare-NET-Objects.csproj b/Compare-NET-Objects/Compare-NET-Objects.csproj index 711bd72..798e6d3 100644 --- a/Compare-NET-Objects/Compare-NET-Objects.csproj +++ b/Compare-NET-Objects/Compare-NET-Objects.csproj @@ -20,18 +20,20 @@ compare comparison equality equal deep objects difference compareobjects deepequal deepequals What you have been waiting for. Perform a deep compare of any two .NET objects using reflection. Shows the differences between the two objects. gfinzer - 4.78.0 - 4.78.0.0 + 4.79.0 + 4.79.0.0 en-US Kellerman Software - Bump Newtonsoft.Json from 12.0.1 to 13.0.1 + ShowCollectionKeyName Config option by gordon-matt https://github.com/GregFinzer/Compare-Net-Objects/pull/275 +CompareBackingFields config option by Mch-Kuzyk https://github.com/GregFinzer/Compare-Net-Objects/pull/279 Copyright © 2023 - 4.78.0.0 + 4.79.0.0 License.txt NuGetIcon.png + Compare .NET Objects diff --git a/Compare-NET-Objects/RootComparer.cs b/Compare-NET-Objects/RootComparer.cs index 051b815..2038e3b 100644 --- a/Compare-NET-Objects/RootComparer.cs +++ b/Compare-NET-Objects/RootComparer.cs @@ -50,22 +50,7 @@ public bool Compare(CompareParms parms) } else { - BaseTypeComparer typeComparer = TypeComparers.FirstOrDefault(o => o.IsTypeMatch(t1, t2)); - - if (typeComparer != null) - { - if (parms.Config.IgnoreObjectTypes || !TypesDifferent(parms, t1, t2)) - { - typeComparer.CompareType(parms); - } - } - else - { - if (EitherObjectIsNull(parms)) return false; - - if (!parms.Config.IgnoreObjectTypes && t1 != null) - throw new NotSupportedException("Cannot compare object of type " + t1.Name); - } + if (!PerformBaseTypeComparison(parms, t1, t2)) return false; } } @@ -80,6 +65,28 @@ public bool Compare(CompareParms parms) return parms.Result.AreEqual; } + private bool PerformBaseTypeComparison(CompareParms parms, Type t1, Type t2) + { + BaseTypeComparer typeComparer = TypeComparers.FirstOrDefault(o => o.IsTypeMatch(t1, t2)); + + if (typeComparer != null) + { + if (parms.Config.IgnoreObjectTypes || !TypesDifferent(parms, t1, t2)) + { + typeComparer.CompareType(parms); + } + } + else + { + if (EitherObjectIsNull(parms)) return false; + + if (!parms.Config.IgnoreObjectTypes && t1 != null) + throw new NotSupportedException("Cannot compare object of type " + t1.Name); + } + + return true; + } + private bool TypesDifferent(CompareParms parms, Type t1, Type t2) { //Objects must be the same type and not be null diff --git a/Compare-NET-Objects/RootComparerFactory.cs b/Compare-NET-Objects/RootComparerFactory.cs index c366c77..5336902 100644 --- a/Compare-NET-Objects/RootComparerFactory.cs +++ b/Compare-NET-Objects/RootComparerFactory.cs @@ -69,7 +69,7 @@ private static RootComparer BuildRootComparer() _rootComparer.TypeComparers.Add(new TimespanComparer(_rootComparer)); _rootComparer.TypeComparers.Add(new StructComparer(_rootComparer)); _rootComparer.TypeComparers.Add(new ImmutableArrayComparer(_rootComparer)); - + _rootComparer.TypeComparers.Add(new EnumComparer(_rootComparer)); return _rootComparer; } #endregion