Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions TUnit.Assertions.Tests/Bugs/Tests3489.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
namespace TUnit.Assertions.Tests.Bugs;

/// <summary>
/// Tests for issue #3489: IsEquivalentTo does not work properly with nested collections in records
/// </summary>
public class Tests3489
{
[Test]
public async Task IsEquivalentTo_WithNestedCollections_InRecords_ShouldSucceed()
{
// Arrange - Create identical hierarchical structures
var actual = new List<Person>
{
new()
{
Name = "Parent1",
Children =
[
new Person { Name = "Child1", Children = [] },
new Person { Name = "Child2", Children = [] }
]
},
new()
{
Name = "Parent2",
Children = [new Person { Name = "Child3", Children = [] }]
}
};

var expected = new List<Person>
{
new()
{
Name = "Parent1",
Children =
[
new Person { Name = "Child1", Children = [] },
new Person { Name = "Child2", Children = [] }
]
},
new()
{
Name = "Parent2",
Children = [new Person { Name = "Child3", Children = [] }]
}
};

// Act & Assert - Should recognize structural equivalence despite different List instances
await Assert.That(actual).IsEquivalentTo(expected);
}

[Test]
public async Task IsEquivalentTo_WithNestedCollections_InRecords_DifferentData_ShouldFail()
{
// Arrange
var actual = new List<Person>
{
new()
{
Name = "Parent1",
Children = [new Person { Name = "Child1", Children = [] }]
}
};

var expected = new List<Person>
{
new()
{
Name = "Parent1",
Children = [new Person { Name = "DifferentChild", Children = [] }]
}
};

// Act & Assert - Should fail when actual data is different
var exception = await Assert.ThrowsAsync<TUnitAssertionException>(
async () => await Assert.That(actual).IsEquivalentTo(expected));

await Assert.That(exception).IsNotNull();
}

private sealed record Person
{
public required string Name { get; init; }
public required List<Person> Children { get; init; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ public bool Equals(T? x, T? y)
return EqualityComparer<T>.Default.Equals(x, y);
}

if (typeof(IEquatable<T>).IsAssignableFrom(type))
{
return ((IEquatable<T>)x).Equals(y);
}

return CompareStructurally(x, y, new HashSet<object>(new ReferenceEqualityComparer()));
}

Expand Down
Loading