Skip to content

Commit 6665d36

Browse files
committed
Add unit test for SA1129 and aliased CancellationToken
1 parent 6d769b1 commit 6665d36

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1129UnitTests.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public void TestMethod()
279279
}
280280

281281
/// <summary>
282-
/// Verifies that the codefix will preserve trivia surrounding <c>new CancellationToken()</c>
282+
/// Verifies that the codefix will preserve trivia surrounding <c>new CancellationToken()</c>.
283283
/// </summary>
284284
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
285285
[Fact]
@@ -403,6 +403,42 @@ private struct CancellationToken
403403
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
404404
}
405405

406+
/// <summary>
407+
/// Verifies that <c>new CancellationToken()</c> is replaced by <c>CancellationToken.None</c>,
408+
/// even when aliased by a <c>using</c> statement.
409+
/// </summary>
410+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
411+
[Fact]
412+
public async Task VerifyAliasedCancellationTokenUsesNoneSyntaxAsync()
413+
{
414+
var testCode = @"
415+
using SystemToken = System.Threading.CancellationToken;
416+
417+
public class TestClass
418+
{
419+
private SystemToken ct = new SystemToken();
420+
}
421+
";
422+
423+
var fixedTestCode = @"
424+
using SystemToken = System.Threading.CancellationToken;
425+
426+
public class TestClass
427+
{
428+
private SystemToken ct = SystemToken.None;
429+
}
430+
";
431+
432+
DiagnosticResult[] expected =
433+
{
434+
this.CSharpDiagnostic().WithLocation(6, 30),
435+
};
436+
437+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
438+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
439+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
440+
}
441+
406442
/// <inheritdoc/>
407443
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
408444
{

0 commit comments

Comments
 (0)