You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: [GenerateAssertion] returning AssertionResult<T> of a value type generates code that does not compile #7792
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
publicstaticpartialclassParsingAssertions{[GenerateAssertion(ExpectationMessage="to parse as an integer")]publicstaticAssertionResult<int>ParsesAsInt(thisstringvalue)=>int.TryParse(value,outvarnumber)?AssertionResult<int>.Passed(number):AssertionResult.Failed($"{value} is not an integer");}publicstaticpartialclassNullableAssertions{[GenerateAssertion(ExpectationMessage="to have a length")]publicstaticAssertionResult<int?>LengthOrNull(thisstringvalue)=>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