Skip to content

Commit

Permalink
Bugfix/add null side support (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
maranmaran authored Jun 19, 2023
1 parent 3525772 commit c294d79
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 36 deletions.
2 changes: 1 addition & 1 deletion source/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>2.0.2</Version>
<Version>2.0.3</Version>
<Authors>Marko Urh</Authors>
<Company>Perun</Company>
<Copyright>Copyright (c) 2023 Marko Urh and other authors.</Copyright>
Expand Down
32 changes: 32 additions & 0 deletions source/Perun.Differ.Tests/DifferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,38 @@ public void Complex_Diffs()
}
}

[Fact]
public void Nulls_Diffs()
{
var entity = new AutoFaker<SimpleTypes>().UseSeed(1).Generate();
var entity2 = new AutoFaker<ComplexType>().UseSeed(1).Generate();
var entity3 = new AutoFaker<SimpleIterableTypes>().UseSeed(1).Generate();
var entity4 = new AutoFaker<ComplexIterableTypes>().UseSeed(1).Generate();

var diffsNone = DifferDotNet.Diff<SimpleTypes>(null, null).ToList();
Assert.Empty(diffsNone);

var diffsRight = DifferDotNet.Diff(null, entity).ToList();
var diffsLeft = DifferDotNet.Diff(entity, null).ToList();
Assert.NotEmpty(diffsRight);
Assert.NotEmpty(diffsLeft);

diffsRight = DifferDotNet.Diff(null, entity2).ToList();
diffsLeft = DifferDotNet.Diff(entity2, null).ToList();
Assert.NotEmpty(diffsRight);
Assert.NotEmpty(diffsLeft);

diffsRight = DifferDotNet.Diff(null, entity3).ToList();
diffsLeft = DifferDotNet.Diff(entity3, null).ToList();
Assert.NotEmpty(diffsRight);
Assert.NotEmpty(diffsLeft);

diffsRight = DifferDotNet.Diff(null, entity4).ToList();
diffsLeft = DifferDotNet.Diff(entity4, null).ToList();
Assert.NotEmpty(diffsRight);
Assert.NotEmpty(diffsLeft);
}

[Fact]
public void NestedComplex_Diffs()
{
Expand Down
59 changes: 29 additions & 30 deletions source/Perun.Differ/DifferDotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ private static DiffCollection DiffRecursive<T>(
string path,
string customPath,
Type type,
T oldObj,
T newObj,
T leftObj,
T rightObj,
DiffCollection diffs,
DiffActions actions
)
{
if (HandleSimpleType(path, customPath, type, oldObj, newObj, diffs, actions))
if (HandleSimpleType(path, customPath, type, leftObj, rightObj, diffs, actions))
{
return diffs;
}

if (HandleIterable(path, customPath, type, oldObj, newObj, diffs, actions))
if (HandleIterable(path, customPath, type, leftObj, rightObj, diffs, actions))
{
return diffs;
}

HandleComplex(path, customPath, type, oldObj, newObj, diffs, actions);
HandleComplex(path, customPath, type, leftObj, rightObj, diffs, actions);

return diffs;
}
Expand All @@ -63,7 +63,7 @@ private static bool HandleComplex<T>(
string path,
string customPath,
Type type,
T oldObj, T newObj,
T leftObj, T rightObj,
DiffCollection diffs,
DiffActions actions
)
Expand Down Expand Up @@ -107,8 +107,8 @@ DiffActions actions
fullPath + ".",
customFullPath + ".",
prop.PropertyType,
oldObj != null ? prop.GetValue(oldObj) : null,
newObj != null ? prop.GetValue(newObj) : null,
leftObj != null ? prop.GetValue(leftObj) : null,
rightObj != null ? prop.GetValue(rightObj) : null,
diffs,
actions
);
Expand All @@ -123,7 +123,7 @@ private static bool HandleIterable<T>(
string path,
string customPath,
Type type,
T oldObj, T newObj,
T leftObj, T rightObj,
DiffCollection diffs,
DiffActions actions
)
Expand All @@ -133,20 +133,20 @@ DiffActions actions
return false;
}

var oldArray = (oldObj as IEnumerable)?.GetEnumerator().ToArray() ?? Array.Empty<object>();
var newArray = (newObj as IEnumerable)?.GetEnumerator().ToArray() ?? Array.Empty<object>();
var leftArr = (leftObj as IEnumerable)?.GetEnumerator().ToArray() ?? Array.Empty<object>();
var rightArr = (rightObj as IEnumerable)?.GetEnumerator().ToArray() ?? Array.Empty<object>();

for (var i = 0; i < Math.Max(oldArray.Length, newArray.Length); i++)
for (var i = 0; i < Math.Max(leftArr.Length, rightArr.Length); i++)
{
var currentOld = i < oldArray.Length ? oldArray[i] : null;
var currentNew = i < newArray.Length ? newArray[i] : null;
var curL = i < leftArr.Length ? leftArr[i] : null;
var curR = i < rightArr.Length ? rightArr[i] : null;

DiffRecursive(
path + $"{i}.",
customPath + $"{i}.",
type.GetIterableType() ?? currentOld?.GetType() ?? currentNew?.GetType(),
currentOld,
currentNew,
type.GetIterableType() ?? curL?.GetType() ?? curR?.GetType(),
curL,
curR,
diffs,
actions
);
Expand All @@ -159,7 +159,7 @@ private static bool HandleSimpleType<T>(
string path,
string customPath,
Type type,
T oldObj, T newObj,
T leftObj, T rightObj,
DiffCollection diffs,
DiffActions actions
)
Expand All @@ -172,26 +172,25 @@ DiffActions actions
var diff = new Difference(
path.Substring(0, path.Length - 1),
customPath.Substring(0, customPath.Length - 1),
oldObj,
newObj
leftObj,
rightObj
);

if (actions.HasFlag(DiffActions.Keep))
{
diffs.KeepDiffs.Add(diff);
}

if (actions.HasFlag(DiffActions.Ignore))

var exitCond = actions.HasFlag(DiffActions.Ignore)
|| leftObj == null && rightObj == null
|| diffs.Diffs.ContainsKey(diff.FullPath)
|| leftObj?.Equals(rightObj) == true;
if (exitCond)
{
return true;
}

var hasDiff = !oldObj?.Equals(newObj) ?? false;
if (hasDiff && !diffs.Diffs.ContainsKey(diff.FullPath))
{
diffs.Diffs.Add(diff.FullPath, diff);
}

}

diffs.Diffs.Add(diff.FullPath, diff);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public sealed class Difference
public string FieldPath { get; set; }
public string FieldName { get; set; }

public object LeftValue { get; set; }
public object RightValue { get; set; }

public string CustomFullPath { get; set; }
public string CustomFieldPath { get; set; }
public string CustomFieldName { get; set; }


public object LeftValue { get; set; }
public object RightValue { get; set; }

public Difference()
{
}
Expand All @@ -34,7 +34,7 @@ public Difference(string fullPath, string customFullPath, object leftValue, obje
{
(CustomFullPath, CustomFieldName, CustomFieldPath) = SetPath(customFullPath);
}
}
}

private (string, string, string) SetPath(string fullPath)
{
Expand Down

0 comments on commit c294d79

Please sign in to comment.