TUnit icon indicating copy to clipboard operation
TUnit copied to clipboard

IsStructurallyEqualTo

Open AnthonyLloyd opened this issue 3 months ago • 2 comments

Hi,

I'm converting CsCheck over to use TUnit. Good so far and looking forward to the AOT.

I have one issue with assrting arrays have the same values (but can be different collections like the framework generated ones). IsEquivalentTo doesn't check the order by default and is also giving me an error when trying to publish AOT.

error IL2026: Using member 'TUnit.Assertions.Extensions.IsEquivalentToAssertionExtensions.IsEquivalentTo<TCollection, TItem>(IAssertionSource<TCollection>, IEnumerable<TItem>, CollectionOrdering, String, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Collection equivalency uses structural comparison for complex objects, which requires reflection and is not compatible with AOT.

I think I need IsStructurallyEqualTo but I don't think it's implemented. Any other hints for an extension method that can check all the element are the same.

Thanks

AnthonyLloyd avatar Nov 29 '25 17:11 AnthonyLloyd

For checking collections with ordering, you can use:

await TUnitAssert.That(listA).IsEquivalentTo(listB, CollectionOrdering.Matching);

For AOT structural equivalency, you'll need to supply for own custom comparer. As TUnit doesn't know what it'll be comparing, it has to use reflection. So await TUnitAssert.That(objA).IsEqualTo(objB, CustomComparer);

thomhurst avatar Nov 29 '25 19:11 thomhurst

There's also this for easily creating your own assertions too: https://tunit.dev/docs/assertions/extensibility/source-generator-assertions/#method-level-generation-generateassertion

thomhurst avatar Nov 29 '25 20:11 thomhurst

Thanks. Creating assertions looks good.

AnthonyLloyd avatar Dec 06 '25 09:12 AnthonyLloyd