Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

feat(assertions): add nullable value type assertions for IsDefault and IsNotDefault - #3475

#3475
Merged
thomhurst merged 3 commits into
mainthomhurst/TUnit:mainfrom
bug/3472thomhurst/TUnit:bug/3472Copy head branch name to clipboard
Oct 22, 2025
Merged

feat(assertions): add nullable value type assertions for IsDefault and IsNotDefault#3475
thomhurst merged 3 commits into
mainthomhurst/TUnit:mainfrom
bug/3472thomhurst/TUnit:bug/3472Copy head branch name to clipboard

Conversation

@thomhurst

Copy link
Copy Markdown
Owner

Fixes #3472

@claude

claude Bot commented Oct 22, 2025

Copy link
Copy Markdown
Contributor

Claude finished @thomhurst's task —— View job


Code Review Complete ✅

Todo List:

  • Read and understand the CLAUDE.md guidelines
  • Review the modified assertion files and understand the changes
  • Check test coverage and quality
  • Verify public API snapshot updates
  • Assess compliance with TUnit's dual-mode requirements
  • Evaluate performance implications
  • Check for potential bugs and security issues
  • Provide comprehensive review feedback

Code Review Feedback

Positive Aspects

  1. Excellent Type Safety & Design: The implementation provides proper type-safe assertions for different scenarios:

    • IsDefaultAssertion<T> for value types (where T : struct)
    • IsDefaultNullableAssertion<T> for nullable value types (T? where T : struct)
    • IsDefaultReferenceAssertion<T> for reference types (where T : class)
  2. Comprehensive Test Coverage: The test suite in DefaultAssertionTests.cs covers all scenarios:

    • Value types (int, DateTime) with default and non-default values
    • Reference types (string, object) with null and non-null values
    • Nullable value types (bool?, int?, DateTime?) with null and HasValue scenarios
  3. Proper Public API Management: All snapshots were correctly updated across all target frameworks (.NET 4.7, 8.0, 9.0, 10.0), indicating proper API versioning.

  4. Clear Documentation: Good XML documentation explains the purpose and usage of each assertion type.

  5. Thread Safety Verification: The STA thread tests demonstrate that delegate assertions properly preserve thread affinity, which was important for issue Asserted Action runs on a different thread / Missing IsDefault assertion #3472.

TUnit Compliance

Dual-Mode Implementation: ✅ VERIFIED

  • The implementation correctly uses the [AssertionExtension] attribute system
  • Source generator (AssertionExtensionGenerator) automatically creates extension methods for both source-gen and reflection modes
  • No manual extension methods needed - follows TUnit's architecture perfectly

⚠️ Minor Issues & Suggestions

  1. Nullable Suppression Warnings (lines 97, 130, 232, 266):

    if (EqualityComparer<TValue>.Default.Equals(value!, default!))

    The null-forgiving operators (!) suppress valid nullable warnings. Consider:

    if (EqualityComparer<TValue>.Default.Equals(value, default(TValue)))
  2. Redundant Exception Handling: Lines 90-95 in IsDefaultAssertion<T> and similar patterns:

    var exception = metadata.Exception;
    if (exception != null)
    {
        return Task.FromResult(AssertionResult.Failed($"threw {exception.GetType().Name}"));
    }

    This exception handling appears in all assertion types but may be redundant since these are value assertions, not delegate assertions. The exception should typically be null unless there's an evaluation error.

  3. Performance Consideration: The use of EqualityComparer<T>.Default.Equals() is appropriate and performant, but consider caching the comparer for hot paths if needed.

🔍 Security & Bugs

No Security Concerns: ✅ The code doesn't expose any sensitive information or create security vulnerabilities.

No Bugs Identified: ✅ The implementation logic is sound:

  • Correctly uses !value.HasValue for nullable default checks
  • Properly uses EqualityComparer<T>.Default.Equals() for type-safe equality
  • Error messages are informative and consistent

📈 Performance Assessment

Performance Impact: ✅ MINIMAL

  • Uses efficient EqualityComparer<T>.Default.Equals()
  • No allocations in happy path beyond error message strings
  • Leverages source generation for zero-reflection overhead
  • Follows TUnit's performance-first design principles

🎯 Overall Assessment

This is a high-quality implementation that:

Recommendation: APPROVE with consideration of the minor nullable suppression suggestions above.


@thomhurst
thomhurst enabled auto-merge (squash) October 22, 2025 14:37
@thomhurst
thomhurst merged commit f41d69c into main Oct 22, 2025
12 of 13 checks passed
@thomhurst
thomhurst deleted the bug/3472 branch October 22, 2025 15:03
This was referenced Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Asserted Action runs on a different thread / Missing IsDefault assertion

1 participant

Morty Proxy This is a proxified and sanitized view of the page, visit original site.