Skip to content

Commit

Permalink
fix: object equals (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
maranmaran authored Jul 19, 2023
1 parent 0d4597c commit 83f53f3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 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.3</Version>
<Version>2.1.4</Version>
<Authors>Marko Urh</Authors>
<Company>Perun</Company>
<Copyright>Copyright (c) 2023 Marko Urh and other authors.</Copyright>
Expand Down
44 changes: 44 additions & 0 deletions source/Perun.Differ.Tests/DifferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using AutoBogus;
using Differ.DotNet.Tests.TestTypes;
using Microsoft.VisualBasic;
Expand Down Expand Up @@ -289,6 +290,49 @@ public void ComplexNestedIterable_ListIdDefined_DeleteFirstItem_DeleteDiffRather
Assert.Null(diff[1].LeftValue);
}

[Fact]
public void ComplexNestedIterable_ListIdDefined_MutateArray_DeleteDiffRatherThanUpdateDiff()
{
var faker = new AutoFaker<ComplexWithId>();

var left = new ComplexIterableWithId
{
Iterable = faker.Generate(10)
};

var right = new ComplexIterableWithId
{
Iterable = left.Iterable.Select(x => new ComplexWithId(x.Id, x.Value)).ToList()
};

right.Iterable.RemoveAt(5);
right.Iterable.RemoveAt(0);
right.Iterable.Add(faker.Generate());

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

Assert.Equivalent(3, diff.Length);
}

[Fact]
public void ComplexDeeplyNestedIterableWithId()
{
var faker = new AutoFaker<ComplexDeeplyNestedIterableWithId>();
var faker2 = new AutoFaker<ComplexDeeplyNestedIterableWithId.Level1>();

var left = faker.Generate();
var right = JsonSerializer.Deserialize<ComplexDeeplyNestedIterableWithId>(JsonSerializer.Serialize(left));

right.Iterable[0].Iterable.RemoveAt(0); // 3 changes
right.Iterable[1].Iterable.RemoveAt(1); // 3 changes
right.Iterable[2].Iterable.RemoveAt(2); // 3 changes
right.Iterable.Add(faker2.Generate()); // 3*3 changes

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

Assert.Equivalent(3 + 3 + 3 + 3 * 3, diff.Length);
}

[Fact]
public void ComplexIterable_Diffs()
{
Expand Down
24 changes: 24 additions & 0 deletions source/Perun.Differ.Tests/TestTypes/IterableModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ public class ComplexNestedIterableWithId
public List<List<ComplexWithId>> Iterable { get; set; }
}

public class ComplexDeeplyNestedIterableWithId
{
[DiffCollectionId("IdLevel1")]
public List<Level1> Iterable { get; set; }

public class Level1
{
[IgnoreInDiff]
public string IdLevel1 { get; set; }

[DiffCollectionId("IdLevel2")]
public List<Level2> Iterable { get; set; }
}

public class Level2
{
[IgnoreInDiff]
public string IdLevel2 { get; set; }

[DiffCollectionId("Id")]
public List<ComplexWithId> Iterable { get; set; }
}
}

public record ComplexWithId
{
public ComplexWithId(string id, string value)
Expand Down
2 changes: 1 addition & 1 deletion source/Perun.Differ/DifferDotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ DiffActions actions
var keyL = curL != null ? idProperty.GetValue(curL) : null;
var keyR = curR != null ? idProperty.GetValue(curR) : null;

if (keyL != keyR)
if (!Equals(keyL, keyR))
{
var nextIdx = curL != null ? l : r;
var nextR = curL != null ? null : curR;
Expand Down

0 comments on commit 83f53f3

Please sign in to comment.