Skip to content

Commit 1f3f59a

Browse files
Copilotthomhurst
andcommitted
Fix fallback path to preserve action lambda for unrecognized constraints
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
1 parent 36c3108 commit 1f3f59a

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

TUnit.Analyzers.CodeFixers/NUnitMigrationCodeFixProvider.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,12 @@ private ExpressionSyntax ConvertNUnitThrows(InvocationExpressionSyntax invocatio
667667
}
668668

669669
// Fallback for unsupported Throws patterns
670-
return CreateTUnitAssertion("Throws", invocation.ArgumentList.Arguments[0].Expression);
670+
// If we have 2+ arguments, it's a constraint-based form where arg[1] is the action
671+
// Otherwise, it's a single-argument form where arg[0] is the action
672+
var fallbackArg = invocation.ArgumentList.Arguments.Count >= 2
673+
? invocation.ArgumentList.Arguments[1].Expression
674+
: invocation.ArgumentList.Arguments[0].Expression;
675+
return CreateTUnitAssertion("Throws", fallbackArg);
671676
}
672677

673678
/// <summary>

TUnit.Analyzers.Tests/NUnitMigrationAnalyzerTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,61 @@ public async Task TestMethod()
18221822
);
18231823
}
18241824

1825+
[Test]
1826+
public async Task NUnit_ThrowsAsync_WithUnrecognizedConstraint_PreservesAction()
1827+
{
1828+
// Test that unrecognized constraint patterns still preserve the action lambda
1829+
// This tests the fallback path in ConvertNUnitThrows
1830+
await CodeFixer.VerifyCodeFixAsync(
1831+
"""
1832+
using NUnit.Framework;
1833+
using System;
1834+
1835+
{|#0:public class MyClass|}
1836+
{
1837+
[Test]
1838+
public void TestMethod()
1839+
{
1840+
// Using Is.InstanceOf which is not recognized by TryExtractTypeFromConstraint
1841+
Assert.ThrowsAsync(Is.InstanceOf<ArgumentException>(), async () => await SomeMethod());
1842+
}
1843+
1844+
private async System.Threading.Tasks.Task SomeMethod()
1845+
{
1846+
await System.Threading.Tasks.Task.Delay(1);
1847+
throw new ArgumentException();
1848+
}
1849+
}
1850+
""",
1851+
Verifier.Diagnostic(Rules.NUnitMigration).WithLocation(0),
1852+
"""
1853+
using System;
1854+
using System.Threading.Tasks;
1855+
using TUnit.Core;
1856+
using TUnit.Assertions;
1857+
using static TUnit.Assertions.Assert;
1858+
using TUnit.Assertions.Extensions;
1859+
1860+
public class MyClass
1861+
{
1862+
[Test]
1863+
public async Task TestMethod()
1864+
{
1865+
// Using Is.InstanceOf which is not recognized by TryExtractTypeFromConstraint
1866+
await Assert.That(async () => await SomeMethod()).Throws();
1867+
}
1868+
1869+
private async System.Threading.Tasks.Task SomeMethod()
1870+
{
1871+
await System.Threading.Tasks.Task.Delay(1);
1872+
throw new ArgumentException();
1873+
}
1874+
}
1875+
""",
1876+
ConfigureNUnitTest
1877+
);
1878+
}
1879+
18251880
private static void ConfigureNUnitTest(Verifier.Test test)
18261881
{
18271882
test.TestState.AdditionalReferences.Add(typeof(NUnit.Framework.TestAttribute).Assembly);

0 commit comments

Comments
 (0)