Fix All().Satisfy() compile error with nullable value types#4685
Fix All().Satisfy() compile error with nullable value types#4685
Conversation
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. Review Summary: This PR cleanly fixes the compilation error with nullable value types in
The changes are focused, well-tested, and maintain the project's quality standards. |
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. The PR correctly fixes the type compatibility issue with nullable value types in All().Satisfy() by:
The changes are well-structured and follow TUnit's development guidelines. |
Use generic TAssertion constraint (where TAssertion : IAssertion) on Satisfy methods instead of widening to IAssertion? directly. This preserves type safety while allowing type-transforming assertions like IsNotNull() on double? (which returns Assertion<double>, not Assertion<double?>). - Add TAssertion type parameter to CollectionAllSatisfyHelper.Satisfy - Store IAssertion? internally in assertion classes - Fix NotNullAssertion.CheckAsync to handle exceptions from nullable value type Map operations - Add tests for All().Satisfy() with double? types - Update public API snapshots Fixes #4684 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
968c3f7 to
c175f4f
Compare
All().Satisfy()fails to compile when the mapped property is a nullable value type (e.g.,double?). TheSatisfylambda expectsAssertion<TMapped>?as return type, butIsNotNull()ondouble?returnsNotNullAssertion<double>(i.e.,Assertion<double>), which is not assignable toAssertion<double?>.Approach
Uses a generic
TAssertiontype parameter withwhere TAssertion : IAssertionconstraint on theSatisfymethods. This preserves type safety (the compiler still enforces that the lambda returns an assertion) while allowing type-transforming assertions likeIsNotNull()on nullable value types.Changes
CollectionAssertions.cs: AddedTAssertiontype parameter withIAssertionconstraint toCollectionAllSatisfyHelper.Satisfyoverloads. The Satisfy methods wrap the typed lambda intoFunc<..., IAssertion?>when constructing the assertion classes. Internal storage inCollectionAllSatisfyAssertionandCollectionAllSatisfyMappedAssertionchanged fromFunc<..., Assertion<T>?>toFunc<..., IAssertion?>.NullAssertion.cs:NotNullAssertion.CheckAsyncnow checksmetadata.Exception— required because the nullable-to-non-nullableMapthrowsInvalidOperationExceptionon null, and the exception was being silently ignored sincedefault(double)is never null.SatisfiesTests.cs: Added tests forAll().Satisfy()withdouble?members (both pass and fail cases, direct and mapped)..verified.txtfiles.Fixes #4684