Skip to content

Commit

Permalink
Merge pull request #296 from GregFinzer/feature/newTypeBug
Browse files Browse the repository at this point in the history
Feature/new type bug
  • Loading branch information
GregFinzer authored Aug 29, 2023
2 parents bea7bd1 + 5314c92 commit 110466f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 36 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,10 @@ documentation/script/
documentation/stylesheets/

# Kellerman Software .Net Automated Builds Specific Directories
Assemblies/
Assemblies/
/Compare-NET-Objects/documentation/designtime
/Compare-NET-Objects/documentation/flash
/Compare-NET-Objects/documentation/images
/Compare-NET-Objects/documentation/script
/Compare-NET-Objects/documentation/scripts
/Compare-NET-Objects/documentation/stylesheets
12 changes: 12 additions & 0 deletions Compare-NET-Objects-Tests/BugTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public void Cleanup()

#region Tests

[Test]
public void OverrideNewTypeShouldBeEqual()
{
OverrideNewType a = new OverrideNewType() { MyProperty1 = "1" };
OverrideNewType b = new OverrideNewType() { MyProperty1 = "1" };

CompareLogic compareLogic = new CompareLogic();
ComparisonResult result = compareLogic.Compare(a, b);
Console.WriteLine(result.DifferencesString);
Assert.IsTrue(result.AreEqual);
}

[Test]
public void OverrideEqualsShouldNotCauseStackOverflow()
{
Expand Down
7 changes: 7 additions & 0 deletions Compare-NET-Objects-Tests/TestClasses/BaseNewType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace KellermanSoftware.CompareNetObjectsTests.TestClasses
{
public class BaseNewType
{
public int MyProperty1 { get; set; }
}
}
30 changes: 6 additions & 24 deletions Compare-NET-Objects-Tests/TestClasses/ClassThatOverridesEquals.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using System.Collections.Generic;
using KellermanSoftware.CompareNetObjects;
using KellermanSoftware.CompareNetObjects;

namespace KellermanSoftware.CompareNetObjectsTests.TestClasses
{
public class ClassThatOverridesEquals
{
public ClassThatOverridesEquals()
{
Name = string.Empty;
}

public int Id { get; set; }
public string Name { get; set; }

Expand All @@ -29,24 +23,12 @@ public override int GetHashCode()
private bool Compare(object object1, object object2)
{
CompareLogic compareLogic = new CompareLogic();
compareLogic.Config = new ComparisonConfig();
compareLogic.Config.AutoClearCache = true;
compareLogic.Config.Caching = false;
compareLogic.Config.UseHashCodeIdentifier = true;
compareLogic.Config.MembersToIgnore = new List<string>();

compareLogic.Config.CompareProperties = true;
ComparisonResult result = null;
try
{
if (compareLogic != null)
result = compareLogic.Compare(object1, object2);
}
catch
{
return false;
}
return result?.AreEqual ?? false;
//THIS IS WHAT PREVENTS THE STACK OVERFLOW
compareLogic.Config.UseHashCodeIdentifier = true;

ComparisonResult result = compareLogic.Compare(object1, object2);
return result.AreEqual;
}
}
}
7 changes: 7 additions & 0 deletions Compare-NET-Objects-Tests/TestClasses/OverrideNewType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace KellermanSoftware.CompareNetObjectsTests.TestClasses
{
public class OverrideNewType : BaseNewType
{
public new string MyProperty1 { get; set; }
}
}
24 changes: 13 additions & 11 deletions Compare-NET-Objects/TypeComparers/PropertyComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,19 @@ private void CompareProperty(CompareParms parms, PropertyEntity info, List<Prope
return;

//If we ignore types then we must get correct PropertyInfo object
PropertyEntity secondObjectInfo = GetSecondObjectInfo(info, object2Properties);
PropertyEntity secondObjectInfo;

if (parms.Config.IgnoreObjectTypes)
{
secondObjectInfo =
object2Properties.FirstOrDefault(x => x.Name == info.Name && x.PropertyType == info.PropertyType)
?? object2Properties.FirstOrDefault(x => x.Name == info.Name);
}
else
{
secondObjectInfo =
object2Properties.FirstOrDefault(x => x.Name == info.Name && x.PropertyType == info.PropertyType);
}

//If the property does not exist, and we are ignoring the object types, skip it - unless we have set IgnoreMissingProperties = true
if ((parms.Config.IgnoreObjectTypes || parms.Config.IgnoreConcreteTypes) && secondObjectInfo == null && parms.Config.IgnoreMissingProperties)
Expand Down Expand Up @@ -127,16 +139,6 @@ private void CompareProperty(CompareParms parms, PropertyEntity info, List<Prope
_rootComparer.Compare(childParms);
}

private static PropertyEntity GetSecondObjectInfo(PropertyEntity info, List<PropertyEntity> object2Properties)
{
foreach (var object2Property in object2Properties)
{
if (info.Name == object2Property.Name)
return object2Property;
}

return null;
}

private static List<PropertyEntity> GetCurrentProperties(CompareParms parms, object objectValue, Type objectType)
{
Expand Down

0 comments on commit 110466f

Please sign in to comment.