Fix IsEquivalentTo to respect IEquatable<T> implementations #4082
+397
−12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
IsEquivalentTofails for collections containing types implementingIEquatable<T>(e.g.,Vector2,Uri,CultureInfo). The structural comparer attempts to reflect over properties requiring parameters (like indexers), causing "Parameter count mismatch" exceptions.Solution: Modified
TypeHelper.IsPrimitiveOrWellKnownTypeto detectIEquatable<T>implementations:IEquatable<T>: Always use theirEqualsmethodIEquatable<T>: UseEqualsif not compiler-generatedImplementation details:
IsCompilerGeneratedTypeto detect records viaEqualityContractpropertytypeof(IEquatable<>)for type-safe interface detection[DynamicallyAccessedMembers]for AOT/trimmingRelated Issue
(Issue linking handled by system)
Type of Change
Checklist
Required
TUnit-Specific Requirements
TUnit.Core.SourceGenerator) - N/A (assertions only)TUnit.Engine) - N/A (assertions only)TUnit.Core.SourceGenerator.Testsand/orTUnit.PublicAPItests.received.txtfiles and accepted them as.verified.txt.verified.txtfiles[DynamicallyAccessedMembers]annotationsdotnet publish -p:PublishAot=trueTesting
dotnet test)Additional Notes
Test coverage:
IEquatableCollectionTests.cs: CoversVector2,Vector3,Uri,CultureInfo,DateTimeOffset, custom typesIssueReproductionTest.cs: Exact scenario from issueIEquatableWorkaroundTests.cs: Verifies explicit comparer still worksVerified: 1379 tests pass on .NET 8.0 and 9.0. No breaking changes to records or structural comparison behavior.
Original prompt
This section details on the original issue you should resolve
<issue_title>[Bug]:
IsEquivalentTodoes not respectIEquatable<T></issue_title><issue_description>### Description
When using
await Assert.That(array).IsEquivalentTo(array2);on a type which implement
IEquatable<T>, TUnit does not use theIEquatable<T>to check item is equal or not.Expected Behavior
Test should pass.
Actual Behavior
Test failed.
Steps to Reproduce
TUnit Version
1.5.60
.NET Version
.NET9.0
Operating System
Windows
IDE / Test Runner
JetBrains Rider
Error Output / Stack Trace
Additional Context
If I change
await Assert.That(array).IsEquivalentTo(array2);to
await Assert.That(array).IsEquivalentTo(array2,EqualityComparer<Vector2>.Default);test will pass.
There are a lot of type (
Uri,CultureInfo,Vector3,Matrix3x2,etc...) has this issue, not justVector2.Additionally,
IComparable<T>should be considered as well.IDE-Specific Issue?
dotnet testordotnet run, not just in my IDE</issue_description>Comments on the Issue (you are @copilot in this section)
IsEquivalentTodoes not respectIEquatable<T>#4081💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.