Skip to content

Commit 37d5f95

Browse files
committed
Refactor SA1129 CancellationToken tests for reuse
1 parent 31030c7 commit 37d5f95

1 file changed

Lines changed: 127 additions & 137 deletions

File tree

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

Lines changed: 127 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -235,193 +235,186 @@ public T TestMethod2<T>()
235235
/// <summary>
236236
/// Verifies that <c>new CancellationToken()</c> is replaced by <c>CancellationToken.None</c>.
237237
/// </summary>
238+
/// <param name="typeNamespace">The namespace of the special type.</param>
239+
/// <param name="typeName">The name of the special type.</param>
240+
/// <param name="fieldName">The name of the field providing the default value for the type.</param>
238241
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
239-
[Fact]
240-
public async Task VerifyCancellationTokenFixUsesNoneSyntaxAsync()
242+
[Theory]
243+
[InlineData("System.Threading", nameof(CancellationToken), nameof(CancellationToken.None))]
244+
public async Task VerifySpecialTypeFixUsesSpecialSyntaxAsync(string typeNamespace, string typeName, string fieldName)
241245
{
242-
var testCode = @"
243-
using System.Threading;
246+
var testCode = $@"
247+
using {typeNamespace};
244248
245249
public class TestClass
246-
{
250+
{{
247251
public void TestMethod()
248-
{
249-
var ct = new CancellationToken();
250-
}
251-
}
252+
{{
253+
var value = [|new {typeName}()|];
254+
}}
255+
}}
252256
";
253257

254-
var fixedTestCode = @"
255-
using System.Threading;
258+
var fixedTestCode = $@"
259+
using {typeNamespace};
256260
257261
public class TestClass
258-
{
262+
{{
259263
public void TestMethod()
260-
{
261-
var ct = CancellationToken.None;
262-
}
263-
}
264+
{{
265+
var value = {typeName}.{fieldName};
266+
}}
267+
}}
264268
";
265269

266-
DiagnosticResult[] expected =
267-
{
268-
Diagnostic().WithLocation(8, 18),
269-
};
270-
271-
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
270+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
272271
}
273272

274273
/// <summary>
275274
/// Verifies that the codefix will preserve trivia surrounding <c>new CancellationToken()</c>.
276275
/// </summary>
276+
/// <param name="typeNamespace">The namespace of the special type.</param>
277+
/// <param name="typeName">The name of the special type.</param>
278+
/// <param name="fieldName">The name of the field providing the default value for the type.</param>
277279
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
278-
[Fact]
279-
public async Task VerifyCancellationTokenTriviaPreservationAsync()
280+
[Theory]
281+
[InlineData("System.Threading", nameof(CancellationToken), nameof(CancellationToken.None))]
282+
public async Task VerifySpecialTypeTriviaPreservationAsync(string typeNamespace, string typeName, string fieldName)
280283
{
281-
var testCode = @"
282-
using System.Threading;
284+
var testCode = $@"
285+
using {typeNamespace};
283286
284287
public class TestClass
285-
{
288+
{{
286289
public void TestMethod()
287-
{
288-
var ct1 = /* c1 */ new CancellationToken(); // c2
289-
}
290-
}
290+
{{
291+
var value = /* c1 */ [|new {typeName}()|]; // c2
292+
}}
293+
}}
291294
";
292295

293-
var fixedTestCode = @"
294-
using System.Threading;
296+
var fixedTestCode = $@"
297+
using {typeNamespace};
295298
296299
public class TestClass
297-
{
300+
{{
298301
public void TestMethod()
299-
{
300-
var ct1 = /* c1 */ CancellationToken.None; // c2
301-
}
302-
}
302+
{{
303+
var value = /* c1 */ {typeName}.{fieldName}; // c2
304+
}}
305+
}}
303306
";
304307

305-
DiagnosticResult[] expected =
306-
{
307-
Diagnostic().WithLocation(8, 28),
308-
};
309-
310-
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
308+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
311309
}
312310

313311
/// <summary>
314312
/// Verifies that <c>new CancellationToken()</c> is replaced by <c>CancellationToken.None</c>,
315313
/// and a fully-qualified name is preserved correctly.
316314
/// </summary>
315+
/// <param name="typeNamespace">The namespace of the special type.</param>
316+
/// <param name="typeName">The name of the special type.</param>
317+
/// <param name="fieldName">The name of the field providing the default value for the type.</param>
317318
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
318-
[Fact]
319-
public async Task VerifyQualifiedCancellationTokenFixUsesNoneSyntaxAsync()
319+
[Theory]
320+
[InlineData("System.Threading", nameof(CancellationToken), nameof(CancellationToken.None))]
321+
public async Task VerifyQualifiedSpecialTypeFixUsesFieldSyntaxAsync(string typeNamespace, string typeName, string fieldName)
320322
{
321-
var testCode = @"
323+
var testCode = $@"
322324
public class TestClass
323-
{
325+
{{
324326
public void TestMethod()
325-
{
326-
var ct = new System.Threading.CancellationToken();
327-
}
328-
}
327+
{{
328+
var value = [|new {typeNamespace}.{typeName}()|];
329+
}}
330+
}}
329331
";
330332

331-
var fixedTestCode = @"
333+
var fixedTestCode = $@"
332334
public class TestClass
333-
{
335+
{{
334336
public void TestMethod()
335-
{
336-
var ct = System.Threading.CancellationToken.None;
337-
}
338-
}
337+
{{
338+
var value = {typeNamespace}.{typeName}.{fieldName};
339+
}}
340+
}}
339341
";
340342

341-
DiagnosticResult[] expected =
342-
{
343-
Diagnostic().WithLocation(6, 18),
344-
};
345-
346-
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
343+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
347344
}
348345

349346
/// <summary>
350347
/// Verifies that <c>new CancellationToken()</c> is <b>not</b> replaced by <c>CancellationToken.None</c>
351348
/// if the qualified name is not exactly <c>System.Threading.CancellationToken</c>.
352349
/// </summary>
350+
/// <param name="typeName">The name of the special type.</param>
353351
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
354-
[Fact]
355-
public async Task VerifyCustomCancellationTokenClassIsNotReplacedAsync()
352+
[Theory]
353+
[InlineData(nameof(CancellationToken))]
354+
public async Task VerifyCustomSpecialTypeStructIsNotReplacedAsync(string typeName)
356355
{
357-
var testCode = @"public class TestClass
358-
{
356+
var testCode = $@"public class TestClass
357+
{{
359358
public void TestMethod()
360-
{
361-
var ct = new CancellationToken();
362-
}
359+
{{
360+
var value = [|new {typeName}()|];
361+
}}
363362
364-
private struct CancellationToken
365-
{
366-
public int TestProperty { get; set; }
367-
}
368-
}
363+
private struct {typeName}
364+
{{
365+
public int TestProperty {{ get; set; }}
366+
}}
367+
}}
369368
";
370369

371-
var fixedTestCode = @"public class TestClass
372-
{
370+
var fixedTestCode = $@"public class TestClass
371+
{{
373372
public void TestMethod()
374-
{
375-
var ct = default(CancellationToken);
376-
}
373+
{{
374+
var value = default({typeName});
375+
}}
377376
378-
private struct CancellationToken
379-
{
380-
public int TestProperty { get; set; }
381-
}
382-
}
377+
private struct {typeName}
378+
{{
379+
public int TestProperty {{ get; set; }}
380+
}}
381+
}}
383382
";
384383

385-
DiagnosticResult[] expected =
386-
{
387-
Diagnostic().WithLocation(5, 18),
388-
};
389-
390-
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
384+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
391385
}
392386

393387
/// <summary>
394388
/// Verifies that <c>new CancellationToken()</c> is replaced by <c>CancellationToken.None</c>,
395389
/// even when aliased by a <c>using</c> statement.
396390
/// </summary>
391+
/// <param name="typeNamespace">The namespace of the special type.</param>
392+
/// <param name="typeName">The name of the special type.</param>
393+
/// <param name="fieldName">The name of the field providing the default value for the type.</param>
397394
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
398-
[Fact]
399-
public async Task VerifyAliasedCancellationTokenUsesNoneSyntaxAsync()
395+
[Theory]
396+
[InlineData("System.Threading", nameof(CancellationToken), nameof(CancellationToken.None))]
397+
public async Task VerifyAliasedSpecialTypeUsesFieldSyntaxAsync(string typeNamespace, string typeName, string fieldName)
400398
{
401-
var testCode = @"
402-
using SystemToken = System.Threading.CancellationToken;
399+
var testCode = $@"
400+
using SystemType = {typeNamespace}.{typeName};
403401
404402
public class TestClass
405-
{
406-
private SystemToken ct = new SystemToken();
407-
}
403+
{{
404+
private SystemType value = [|new SystemType()|];
405+
}}
408406
";
409407

410-
var fixedTestCode = @"
411-
using SystemToken = System.Threading.CancellationToken;
408+
var fixedTestCode = $@"
409+
using SystemType = {typeNamespace}.{typeName};
412410
413411
public class TestClass
414-
{
415-
private SystemToken ct = SystemToken.None;
416-
}
412+
{{
413+
private SystemType value = SystemType.{fieldName};
414+
}}
417415
";
418416

419-
DiagnosticResult[] expected =
420-
{
421-
Diagnostic().WithLocation(6, 30),
422-
};
423-
424-
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
417+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
425418
}
426419

427420
/// <summary>
@@ -510,46 +503,43 @@ private enum MyEnum {{ {declarationBody} }}
510503
/// <summary>
511504
/// Verifies that <c>new CancellationToken()</c> is replaced by <c>default(CancellationToken)</c> when its used for a default parameter.
512505
/// </summary>
506+
/// <param name="typeNamespace">The namespace of the special type.</param>
507+
/// <param name="typeName">The name of the special type.</param>
513508
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
514-
[Fact]
509+
[Theory]
510+
[InlineData("System.Threading", nameof(CancellationToken))]
515511
[WorkItem(2740, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2740")]
516-
public async Task VerifyCancellationTokenDefaultParameterAsync()
512+
public async Task VerifySpecialTypeDefaultParameterAsync(string typeNamespace, string typeName)
517513
{
518-
var testCode = @"using System.Threading;
514+
var testCode = $@"using {typeNamespace};
519515
520516
public class TestClass
521-
{
522-
public TestClass(CancellationToken cancellationToken = new CancellationToken())
523-
{
524-
}
517+
{{
518+
public TestClass({typeName} cancellationToken = [|new {typeName}()|])
519+
{{
520+
}}
525521
526-
public void TestMethod(CancellationToken cancellationToken = new CancellationToken())
527-
{
528-
}
529-
}
522+
public void TestMethod({typeName} cancellationToken = [|new {typeName}()|])
523+
{{
524+
}}
525+
}}
530526
";
531527

532-
var fixedTestCode = @"using System.Threading;
528+
var fixedTestCode = $@"using {typeNamespace};
533529
534530
public class TestClass
535-
{
536-
public TestClass(CancellationToken cancellationToken = default(CancellationToken))
537-
{
538-
}
531+
{{
532+
public TestClass({typeName} cancellationToken = default({typeName}))
533+
{{
534+
}}
539535
540-
public void TestMethod(CancellationToken cancellationToken = default(CancellationToken))
541-
{
542-
}
543-
}
536+
public void TestMethod({typeName} cancellationToken = default({typeName}))
537+
{{
538+
}}
539+
}}
544540
";
545541

546-
DiagnosticResult[] expected =
547-
{
548-
Diagnostic().WithLocation(5, 60),
549-
Diagnostic().WithLocation(9, 66),
550-
};
551-
552-
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
542+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
553543
}
554544
}
555545
}

0 commit comments

Comments
 (0)