Skip to content

Commit

Permalink
fix: custom name fetch on null instances (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
maranmaran authored Jul 18, 2023
1 parent 1cf3b9f commit 0d4597c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 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.1.2</Version>
<Version>2.1.3</Version>
<Authors>Marko Urh</Authors>
<Company>Perun</Company>
<Copyright>Copyright (c) 2023 Marko Urh and other authors.</Copyright>
Expand Down
27 changes: 22 additions & 5 deletions source/Perun.Differ.Tests/DifferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,36 @@ 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 entity = new AutoFaker<SimpleTypes>().UseSeed(1).Generate();
var diffsRight = DifferDotNet.Diff(null, entity).ToList();
var diffsLeft = DifferDotNet.Diff(entity, null).ToList();
Assert.NotEmpty(diffsRight);
Assert.NotEmpty(diffsLeft);

var entity2 = new AutoFaker<ComplexType>().UseSeed(1).Generate();
diffsRight = DifferDotNet.Diff(null, entity2).ToList();
diffsLeft = DifferDotNet.Diff(entity2, null).ToList();
Assert.NotEmpty(diffsRight);
Assert.NotEmpty(diffsLeft);

var entity3 = new AutoFaker<SimpleIterableTypes>().UseSeed(1).Generate();
diffsRight = DifferDotNet.Diff(null, entity3).ToList();
diffsLeft = DifferDotNet.Diff(entity3, null).ToList();
Assert.NotEmpty(diffsRight);
Assert.NotEmpty(diffsLeft);

var entity4 = new AutoFaker<ComplexIterableTypes>().UseSeed(1).Generate();
diffsRight = DifferDotNet.Diff(null, entity4).ToList();
diffsLeft = DifferDotNet.Diff(entity4, null).ToList();
Assert.NotEmpty(diffsRight);
Assert.NotEmpty(diffsLeft);

entity4 = new ComplexIterableTypes { Array = null };
diffsNone = DifferDotNet.Diff(entity4, entity4).ToList();
Assert.Empty(diffsNone);
}

[Fact]
Expand Down Expand Up @@ -691,6 +694,20 @@ public void CustomNameDefined_Reflection_RegisteredInDiff()
Assert.Equal("a", diff.FieldName);
}

[Fact]
public void CustomNameDefined_Reflection_NullInstance_ReturnsDiff()
{
var faker = new AutoFaker<CustomNameNullValueReflectionModelNested>();
var faker2 = new AutoFaker<CustomNameNullValueReflectionModel>();

var left = new CustomNameNullValueReflectionModelNested() { Items = null };
var right = faker.UseSeed(2).Generate();

var diff = DifferDotNet.Diff(left, right);

Assert.NotEmpty(diff);
}

[Fact]
public void CustomNameDefined_RegisteredInDiff()
{
Expand Down
17 changes: 16 additions & 1 deletion source/Perun.Differ.Tests/TestTypes/CustomNameModels.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Differ.DotNet.Tests.TestTypes
using System.Collections.Generic;

namespace Differ.DotNet.Tests.TestTypes
{
internal class CustomNameModel
{
Expand All @@ -16,6 +18,19 @@ internal class CustomNameReflectionModel
public string B => "MyCustomName";
}

internal class CustomNameNullValueReflectionModel
{
[DiffPropertyName("B", fromPropertyValue: true)]
public double? A { get; set; }

public string B => "MyCustomName";
}

internal class CustomNameNullValueReflectionModelNested
{
public List<CustomNameNullValueReflectionModel> Items { get; set; }
}

internal class CustomNameNestedModel
{
[DiffPropertyName("BCustomName")]
Expand Down
25 changes: 19 additions & 6 deletions source/Perun.Differ/DifferDotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,15 @@ private static string GetCustomPropertyName<T>(Type type, T left, T right, Prope
if (attr is null)
{
return null;
}

if (attr.FromPropertyValue == false)
{
return attr.Name;
}

var segments = attr.Name.Split('.');
var nestedProp = prop;
var nestedProp = prop;
var nestedType = type;
object nestedLeft = left;
object nestedRight = right;
Expand All @@ -152,12 +157,20 @@ private static string GetCustomPropertyName<T>(Type type, T left, T right, Prope

if (i < segments.Length - 1)
{
nestedLeft = nestedProp.GetValue(nestedLeft);
nestedRight = nestedProp.GetValue(nestedRight);
}
}
nestedLeft = TryGetValue(nestedProp, nestedLeft);
nestedRight = TryGetValue(nestedProp, nestedRight);
}
}

var customName = (TryGetValue(nestedProp, nestedRight) ?? TryGetValue(nestedProp, nestedLeft))?.ToString();

return (nestedProp.GetValue(nestedRight) ?? nestedProp.GetValue(nestedLeft))?.ToString();
return customName ?? attr.Name;
}

[CanBeNull]
private static object TryGetValue(PropertyInfo prop, object obj)
{
return obj is not null ? prop.GetValue(obj) : null;
}

private static bool HandleIterable<T>(
Expand Down

0 comments on commit 0d4597c

Please sign in to comment.