Skip to content

Commit 16f640e

Browse files
authored
Merge pull request #686 from manfred-brands/issue685_TestCaseSource
Fix case where TestCaseSource and no CancellationToken parameter
2 parents 9330fc4 + 09b905c commit 16f640e

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/nunit.analyzers.tests/TestCaseSourceUsage/TestCaseSourceUsesStringAnalyzerTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,33 @@ static IEnumerable<int> TestData(int first, int second, int third)
761761
RoslynAssert.Valid(analyzer, testCode);
762762
}
763763

764+
[Test]
765+
public void AnalyzeWhenNumberOfParametersMatchNoImplicitlySuppliedCancellationTokenParameter()
766+
{
767+
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
768+
[TestFixture]
769+
public class AnalyzeWhenNumberOfParametersMatch
770+
{
771+
[TestCaseSource(nameof(TestData), new object[] { 1, 3, 5 })]
772+
[CancelAfter(10)]
773+
public void ShortName(int number)
774+
{
775+
if (TestContext.CurrentContext.CancellationToken.IsCancellationRequested)
776+
Assert.Ignore(""Cancelled"");
777+
Assert.That(number, Is.GreaterThanOrEqualTo(0));
778+
}
779+
780+
static IEnumerable<int> TestData(int first, int second, int third)
781+
{
782+
yield return first;
783+
yield return second;
784+
yield return third;
785+
}
786+
}", additionalUsings: "using System.Collections.Generic;");
787+
788+
RoslynAssert.Valid(analyzer, testCode);
789+
}
790+
764791
[Test]
765792
public void AnalyzeWhenNumberOfParametersDoesNotMatchNoParametersExpectedNoImplicitSuppliedCancellationToken()
766793
{

src/nunit.analyzers/TestCaseSourceUsage/TestCaseSourceUsesStringAnalyzer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,13 @@ private static void AnalyzeAttribute(
268268
}
269269
else
270270
{
271-
if (methodRequiredParameters + methodOptionalParameters != (hasCancelAfterAttribute ? 2 : 1))
271+
if (methodRequiredParameters != 1)
272272
{
273273
context.ReportDiagnostic(Diagnostic.Create(
274274
mismatchInNumberOfTestMethodParameters,
275275
syntaxNode.GetLocation(),
276276
1,
277-
methodRequiredParameters + methodOptionalParameters));
277+
methodRequiredParameters));
278278
}
279279
else
280280
{

0 commit comments

Comments
 (0)