Skip to content

Commit

Permalink
Add support for CancelAfter on TestFixture class
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred-brands committed Jan 22, 2024
1 parent bb0263c commit 70dd521
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ public void ShortName(int first, int second)

#if NUNIT4
[Test]
public void AnalyzeWhenNumberOfParametersMatchExcludingImplicitSuppliedCancellationToken()
public void AnalyzeWhenNumberOfParametersMatchExcludingImplicitSuppliedCancellationTokenDueToCancelAfterOnMethod()
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
[TestFixture]
Expand All @@ -734,6 +734,33 @@ static IEnumerable<int> TestData(int first, int second, int third)
RoslynAssert.Valid(analyzer, testCode);
}

[Test]
public void AnalyzeWhenNumberOfParametersMatchExcludingImplicitSuppliedCancellationTokenDueToCancelAfterOnClass()
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
[TestFixture]
[CancelAfter(100)]
public class AnalyzeWhenNumberOfParametersMatch
{
[TestCaseSource(nameof(TestData), new object[] { 1, 3, 5 })]
public void ShortName(int number, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
Assert.Ignore(""Cancelled"");
Assert.That(number, Is.GreaterThanOrEqualTo(0));
}
static IEnumerable<int> TestData(int first, int second, int third)
{
yield return first;
yield return second;
yield return third;
}
}", additionalUsings: "using System.Collections.Generic;using System.Threading;");

RoslynAssert.Valid(analyzer, testCode);
}

[Test]
public void AnalyzeWhenNumberOfParametersDoesNotMatchNoParametersExpectedNoImplicitSuppliedCancellationToken()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,18 +758,39 @@ public void TestWithGenericParameter<T>(T arg1) { }

#if NUNIT4
[Test]
public void AnalyzeWhenTestMethodHasImplicitlySuppliedCancellationTokenParameter()
public void AnalyzeWhenTestMethodHasImplicitlySuppliedCancellationTokenParameterDueToCancelAfterOnMethod()
{
var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
[TestCase(100)]
[TestCase(100)]
[CancelAfter(50)]
public async Task InfiniteLoopWithCancelAfter(int delayInMs, CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(delayInMs, cancellationToken).ConfigureAwait(false);
}
}", "using System.Threading;");

RoslynAssert.Valid(this.analyzer, testCode);
}

[Test]
public void AnalyzeWhenTestMethodHasImplicitlySuppliedCancellationTokenParameterDueToCancelAfterOnClass()
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
[TestFixture]
[CancelAfter(50)]
public async Task InfiniteLoopWithCancelAfter(int delayInMs, CancellationToken cancellationToken)
public class TestClass
{
while (!cancellationToken.IsCancellationRequested)
[TestCase(100)]
public async Task InfiniteLoopWithCancelAfter(int delayInMs, CancellationToken cancellationToken)
{
await Task.Delay(delayInMs, cancellationToken).ConfigureAwait(false);
while (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(delayInMs, cancellationToken).ConfigureAwait(false);
}
}
}", "using System.Threading;");
}", "using System.Threading;");

RoslynAssert.Valid(this.analyzer, testCode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ public void M(int p)

#if NUNIT4
[Test]
public void WhenTestMethodHasImplicitlySuppliedCancellationTokenParameter()
public void WhenTestMethodHasImplicitlySuppliedCancellationTokenParameterDueToCancelAfterOnMethod()
{
var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
[Test]
Expand All @@ -554,6 +554,27 @@ public async Task InfiniteLoopWith50msCancelAfter(CancellationToken cancellation
RoslynAssert.Valid(analyzer, testCode);
}

[Test]
public void WhenTestMethodHasImplicitlySuppliedCancellationTokenParameterDueToCancelAfterOnClass()
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
[TestFixture]
[CancelAfter(50)]
public class TestClass
{
[Test]
public async Task InfiniteLoopWith50msCancelAfter(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
}
}
}", "using System.Threading;");

RoslynAssert.Valid(analyzer, testCode);
}

[Test]
public void AnalyzeWhenSimpleTestMethodHasParameterSuppliedByValuesAndCancelAfterAttributes()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ private static void AnalyzeAttribute(
IMethodSymbol? testMethod = context.SemanticModel.GetDeclaredSymbol(testMethodDeclaration);
if (testMethod is not null)
{
var hasCancelAfterAttribute = testMethod.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, cancelAfterType));
var hasCancelAfterAttribute = testMethod.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, cancelAfterType)) ||
testMethod.ContainingType.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, cancelAfterType));

var (methodRequiredParameters, methodOptionalParameters, methodParamsParameters) = testMethod.GetParameterCounts(hasCancelAfterAttribute, cancellationTokenType);

Expand Down
5 changes: 3 additions & 2 deletions src/nunit.analyzers/TestCaseUsage/TestCaseUsageAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ private static void AnalyzeMethod(
if (methodAttributes.Length == 0)
return;

var hasCancelAfterAttribute = methodAttributes.Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, cancelAfterType));
var hasCancelAfterAttribute = methodAttributes.Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, cancelAfterType)) ||
methodSymbol.ContainingType.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, cancelAfterType));

var testCaseAttributes = methodSymbol.GetAttributes()
var testCaseAttributes = methodAttributes
.Where(a => a.ApplicationSyntaxReference is not null
&& SymbolEqualityComparer.Default.Equals(a.AttributeClass, testCaseType));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ private static void AnalyzeMethod(
if (!hasITestBuilderAttribute)
{
var testAttribute = methodAttributes.SingleOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, testType));
var hasCancelAfterAttribute = methodAttributes.Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, cancelAfterType));
var hasCancelAfterAttribute = methodAttributes.Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, cancelAfterType)) ||
methodSymbol.ContainingType.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, cancelAfterType));

if (testAttribute is not null)
{
Expand Down

0 comments on commit 70dd521

Please # to comment.