|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules |
5 | 5 | { |
| 6 | + using System; |
| 7 | + using System.Collections.Generic; |
6 | 8 | using System.Threading; |
7 | 9 | using System.Threading.Tasks; |
| 10 | + |
| 11 | + using Microsoft.CodeAnalysis; |
| 12 | + using Microsoft.CodeAnalysis.CodeFixes; |
| 13 | + using Microsoft.CodeAnalysis.CSharp; |
| 14 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 15 | + using Microsoft.CodeAnalysis.Testing; |
| 16 | + using Microsoft.CodeAnalysis.Testing.Verifiers; |
| 17 | + |
8 | 18 | using StyleCop.Analyzers.Test.CSharp7.SpacingRules; |
| 19 | + |
9 | 20 | using Xunit; |
| 21 | + |
10 | 22 | using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< |
11 | 23 | StyleCop.Analyzers.SpacingRules.SA1013ClosingBracesMustBeSpacedCorrectly, |
12 | 24 | StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>; |
@@ -62,5 +74,54 @@ public void TestMethod(object value) |
62 | 74 | var expected = Diagnostic().WithSpan(14, 37, 14, 38).WithArguments(string.Empty, "followed"); |
63 | 75 | await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); |
64 | 76 | } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Validates that a closing brace followed by a null-forgiving operator does not require a space. |
| 80 | + /// </summary> |
| 81 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 82 | + [Fact] |
| 83 | + [WorkItem(3172, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3172")] |
| 84 | + public async Task TestCloseBraceWithNullForgivingOperatorAsync() |
| 85 | + { |
| 86 | + const string testCode = @" |
| 87 | +public class Foo |
| 88 | +{ |
| 89 | + public void TestMethod() |
| 90 | + { |
| 91 | + var test = new[] |
| 92 | + { |
| 93 | + new { Value = default(string) }, |
| 94 | + new { Value = ""a"" }!, |
| 95 | + new { Value = ""b"" } !, |
| 96 | + }; |
| 97 | + } |
| 98 | +} |
| 99 | +"; |
| 100 | + |
| 101 | + var test = new NullableCSharpAnalyzerTest |
| 102 | + { |
| 103 | + TestCode = testCode, |
| 104 | + }; |
| 105 | + |
| 106 | + await test.RunAsync(CancellationToken.None).ConfigureAwait(false); |
| 107 | + } |
| 108 | + |
| 109 | + private class NullableCSharpAnalyzerTest : AnalyzerTest<XUnitVerifier> |
| 110 | + { |
| 111 | + public override string Language => LanguageNames.CSharp; |
| 112 | + |
| 113 | + protected override string DefaultFileExt => "cs"; |
| 114 | + |
| 115 | + protected override CompilationOptions CreateCompilationOptions() |
| 116 | + => new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true, nullableContextOptions: NullableContextOptions.Enable); |
| 117 | + |
| 118 | + protected override ParseOptions CreateParseOptions() |
| 119 | + => new CSharpParseOptions(LanguageVersion.CSharp8); |
| 120 | + |
| 121 | + protected override IEnumerable<DiagnosticAnalyzer> GetDiagnosticAnalyzers() |
| 122 | + { |
| 123 | + yield return new StyleCop.Analyzers.SpacingRules.SA1013ClosingBracesMustBeSpacedCorrectly(); |
| 124 | + } |
| 125 | + } |
65 | 126 | } |
66 | 127 | } |
0 commit comments