|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules |
5 | 5 | { |
| 6 | + using System.Threading; |
| 7 | + using System.Threading.Tasks; |
| 8 | + using Microsoft.CodeAnalysis.Testing; |
6 | 9 | using StyleCop.Analyzers.Test.CSharp7.SpacingRules; |
| 10 | + using Xunit; |
| 11 | + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< |
| 12 | + StyleCop.Analyzers.SpacingRules.SA1018NullableTypeSymbolsMustNotBePrecededBySpace, |
| 13 | + StyleCop.Analyzers.SpacingRules.SA1018CodeFixProvider>; |
7 | 14 |
|
8 | 15 | public partial class SA1018CSharp8UnitTests : SA1018CSharp7UnitTests |
9 | 16 | { |
| 17 | + /// <summary> |
| 18 | + /// Verifies that nullable reference type annotations are handled without diagnostics when spaced correctly. |
| 19 | + /// </summary> |
| 20 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 21 | + [Fact] |
| 22 | + [WorkItem(3006, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3006")] |
| 23 | + public async Task TestNullableReferenceTypesNoDiagnosticsAsync() |
| 24 | + { |
| 25 | + const string testCode = @"#nullable enable |
| 26 | +
|
| 27 | +class TestClass |
| 28 | +{ |
| 29 | + private string? field; |
| 30 | + private string?[] array; |
| 31 | + private System.Collections.Generic.List<string?>? list; |
| 32 | +
|
| 33 | + public string? Property { get; set; } |
| 34 | +
|
| 35 | + public void TestMethod(string? parameter, out string? output) |
| 36 | + { |
| 37 | + output = parameter; |
| 38 | + } |
| 39 | +
|
| 40 | + public (string? first, string? second) TupleMethod() |
| 41 | + { |
| 42 | + return (null, null); |
| 43 | + } |
| 44 | +} |
| 45 | +"; |
| 46 | + |
| 47 | + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Verifies that spacing diagnostics are reported for nullable reference types preceded by whitespace. |
| 52 | + /// </summary> |
| 53 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 54 | + [Fact] |
| 55 | + [WorkItem(3006, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3006")] |
| 56 | + public async Task TestNullableReferenceTypeSpacingAsync() |
| 57 | + { |
| 58 | + var testCode = @"#nullable enable |
| 59 | +
|
| 60 | +class TestClass |
| 61 | +{ |
| 62 | + string {|#0:?|} field1; |
| 63 | + string /* comment */ {|#1:?|} field2; |
| 64 | + string |
| 65 | +{|#2:?|} field3; |
| 66 | + string |
| 67 | +/* comment */ |
| 68 | +{|#3:?|} field4; |
| 69 | +} |
| 70 | +"; |
| 71 | + |
| 72 | + var fixedCode = @"#nullable enable |
| 73 | +
|
| 74 | +class TestClass |
| 75 | +{ |
| 76 | + string? field1; |
| 77 | + string/* comment */? field2; |
| 78 | + string? field3; |
| 79 | + string/* comment */? field4; |
| 80 | +} |
| 81 | +"; |
| 82 | + |
| 83 | + DiagnosticResult[] expected = |
| 84 | + { |
| 85 | + Diagnostic().WithLocation(0), |
| 86 | + Diagnostic().WithLocation(1), |
| 87 | + Diagnostic().WithLocation(2), |
| 88 | + Diagnostic().WithLocation(3), |
| 89 | + }; |
| 90 | + await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); |
| 91 | + } |
10 | 92 | } |
11 | 93 | } |
0 commit comments