Skip to content

[Bug]: [GenerateAssertion] returning AssertionResult<T> of a value type generates code that does not compile #7792

Closed

Description

@JohnVerheij

Bug report checklist

  • I've searched existing issues to ensure this isn't a duplicate
  • I've read the documentation and this isn't expected behavior
  • I'm using the latest version of TUnit

Description

A [GenerateAssertion] method that returns AssertionResult<T> for a value type makes the build fail in the generated file. The generated assertion always declares its result field as T?. For AssertionResult<int> that field is int?, which the awaiter cannot return as int (CS0266). For AssertionResult<int?> the field becomes int??, which does not parse (CS1519). Reference-type results work.

Expected Behavior

Both assertions compile, and awaiting them gives the int and the int?.

Actual Behavior

The generated *.GeneratedAssertions.g.cs files do not compile: private int? _result; with return _result!; in the awaiter for AssertionResult<int>, and private int?? _result; for AssertionResult<int?>.

Steps to Reproduce

public static partial class ParsingAssertions
{
    [GenerateAssertion(ExpectationMessage = "to parse as an integer")]
    public static AssertionResult<int> ParsesAsInt(this string value)
        => int.TryParse(value, out var number)
            ? AssertionResult<int>.Passed(number)
            : AssertionResult.Failed($"{value} is not an integer");
}

public static partial class NullableAssertions
{
    [GenerateAssertion(ExpectationMessage = "to have a length")]
    public static AssertionResult<int?> LengthOrNull(this string value)
        => AssertionResult<int?>.Passed(value.Length == 0 ? null : value.Length);
}

// int number = await Assert.That("42").ParsesAsInt();

TUnit Version

1.85.7

.NET Version

net10.0

Operating System

Windows

IDE / Test Runner

dotnet CLI (dotnet test / dotnet run)

Error Output / Stack Trace

ParsingAssertions.GeneratedAssertions.g.cs(62,16): error CS0266: Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)
NullableAssertions.GeneratedAssertions.g.cs(17,16): error CS1519: Invalid token '??' in a member declaration

Additional Context

I have a small fix with tests and will open a PR.

IDE-Specific Issue?

  • I've confirmed this issue occurs when running via dotnet test or dotnet run, not just in my IDE

Activity

  1. added
    P2Medium: edge case, mode divergence, flaky test, or workaround exists
    on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium: edge case, mode divergence, flaky test, or workaround existsbugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions