Skip to content

Fix GenerateAssertion dropping parameter default values from generated extension methods#4828

Merged
thomhurst merged 2 commits intomainfrom
copilot/fix-generateassertion-defaults
Feb 17, 2026
Merged

Fix GenerateAssertion dropping parameter default values from generated extension methods#4828
thomhurst merged 2 commits intomainfrom
copilot/fix-generateassertion-defaults

Conversation

Copy link
Contributor

Copilot AI commented Feb 16, 2026

  • Investigate the issue: MethodAssertionGenerator doesn't preserve default parameter values in generated extension methods
  • Add HasExplicitDefaultValue and DefaultValueExpression fields to ParameterData record struct
  • Capture default values when creating ParameterData instances
  • Add FormatDefaultValue helper method to MethodAssertionGenerator (consistent with existing logic in AssertionExtensionGenerator)
  • Emit default values in GenerateExtensionMethod
  • Add test data file and test case for default values
  • Add snapshot files for MethodWithDefaultValues test (4 framework targets)
  • Run code review and security checks
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: GenerateAssertion drops parameter default values from generated extension method</issue_title>
<issue_description>### Description

The following code is a compilation error because the source generated extension method has lost the true default value of the exact parameter.

public class UnitTest1
{
    [Test]
    public async Task TestWithMemberData()
    {
        string[] items = new[] { "food", "bard" };
        await Assert.That(items).ContainsMessage("foo"); // ERROR: parameter `bool exact` expected
    }
}

public static partial class Assertions
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    [GenerateAssertion(ExpectationMessage = "to contain message '{needle}'")]
    public static bool ContainsMessage(this IEnumerable<string> strings, string needle, bool exact = true)
    {
        return strings.Any(x => exact ? x == needle : x.Contains(needle));
    }
}

Expected Behavior

Source generated extension preserves parameter defaults.

Actual Behavior

Parameter defaults lost

Steps to Reproduce

See above code

TUnit Version

1.15.0

.NET Version

NET 10

Operating System

Windows

IDE / Test Runner

dotnet CLI (dotnet test / dotnet run)

Error Output / Stack Trace

Additional Context

No response

IDE-Specific Issue?

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

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix GenerateAssertion to maintain default parameter values Fix GenerateAssertion dropping parameter default values from generated extension methods Feb 16, 2026
Copilot AI requested a review from thomhurst February 16, 2026 21:34
@thomhurst
Copy link
Owner

@claude There should be new snapshots for the new test

@thomhurst thomhurst marked this pull request as ready for review February 16, 2026 21:46
@claude
Copy link
Contributor

claude bot commented Feb 16, 2026

Claude finished @thomhurst's task in 3m 26s —— View job


Tasks

  • Check existing snapshot files for the new test
  • Analyze generator code and test expectations
  • Create snapshot files for all target frameworks
  • Commit snapshot files locally
  • Push changes to remote (permission denied)

Summary

I've successfully created the missing snapshot files for the MethodWithDefaultValues test. The snapshots verify that the fix correctly preserves the bool exact = true default value in the generated extension method.

Created files:

  • MethodAssertionGeneratorTests.MethodWithDefaultValues.DotNet8_0.verified.txt
  • MethodAssertionGeneratorTests.MethodWithDefaultValues.DotNet9_0.verified.txt
  • MethodAssertionGeneratorTests.MethodWithDefaultValues.DotNet10_0.verified.txt
  • MethodAssertionGeneratorTests.MethodWithDefaultValues.Net4_7.verified.txt

All snapshots include the key line that the test validates:

public static StringArray_ContainsMessage_String_Bool_Assertion ContainsMessage(this IAssertionSource<string[]> source, string needle, bool exact = true, ...)

Commit created: fb7d4c6 - "Add snapshot tests for MethodWithDefaultValues"

Issue

I encountered a permission error when attempting to push the commit to the remote repository. The GitHub Actions bot doesn't have write permissions to this branch.

To complete this task, you'll need to either:

  1. Grant the GitHub Actions bot write permissions to the repository/branch
  2. Manually pull and push the commit I created locally
  3. Run the tests yourself to regenerate the snapshots with: dotnet test TUnit.Assertions.SourceGenerator.Tests --filter "FullyQualifiedName~MethodWithDefaultValues"

The commit is ready and waiting in the local repository with proper co-authorship attribution.


| Branch

@thomhurst
Copy link
Owner

@copilot There should be new snapshots for the new test

Copy link
Contributor Author

Copilot AI commented Feb 16, 2026

@copilot There should be new snapshots for the new test

Added 4 snapshot .verified.txt files for the MethodWithDefaultValues test (DotNet8_0, DotNet9_0, DotNet10_0, Net4_7) in 22d9c2b. The snapshots follow the same format as the existing BoolMethod snapshots and verify the extension method signature includes bool exact = true.

Copilot AI and others added 2 commits February 17, 2026 00:21
…ed extension methods

Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
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.

[Bug]: GenerateAssertion drops parameter default values from generated extension method

2 participants

Comments