|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp8.OrderingRules |
5 | 5 | { |
| 6 | + using System.Threading; |
| 7 | + using System.Threading.Tasks; |
| 8 | + using Microsoft.CodeAnalysis.Testing; |
6 | 9 | using StyleCop.Analyzers.Test.CSharp7.OrderingRules; |
| 10 | + using Xunit; |
| 11 | + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< |
| 12 | + StyleCop.Analyzers.OrderingRules.SA1206DeclarationKeywordsMustFollowOrder, |
| 13 | + StyleCop.Analyzers.OrderingRules.SA1206CodeFixProvider>; |
7 | 14 |
|
8 | 15 | public partial class SA1206CodeFixProviderCSharp8UnitTests : SA1206CodeFixProviderCSharp7UnitTests |
9 | 16 | { |
| 17 | + [Fact] |
| 18 | + [WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")] |
| 19 | + public async Task VerifyReadonlyKeywordReorderingInStructMethodAsync() |
| 20 | + { |
| 21 | + var testCode = @" |
| 22 | +public struct S |
| 23 | +{ |
| 24 | + readonly {|#0:public|} void M() |
| 25 | + { |
| 26 | + } |
| 27 | +}"; |
| 28 | + |
| 29 | + var fixedCode = @" |
| 30 | +public struct S |
| 31 | +{ |
| 32 | + public readonly void M() |
| 33 | + { |
| 34 | + } |
| 35 | +}"; |
| 36 | + |
| 37 | + var expected = Diagnostic().WithLocation(0).WithArguments("public", "readonly"); |
| 38 | + await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + [WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")] |
| 43 | + public async Task VerifyReadonlyKeywordReorderingInStructPropertyAsync() |
| 44 | + { |
| 45 | + var testCode = @" |
| 46 | +public struct S |
| 47 | +{ |
| 48 | + readonly {|#0:internal|} int Value { get; } |
| 49 | +}"; |
| 50 | + |
| 51 | + var fixedCode = @" |
| 52 | +public struct S |
| 53 | +{ |
| 54 | + internal readonly int Value { get; } |
| 55 | +}"; |
| 56 | + |
| 57 | + var expected = Diagnostic().WithLocation(0).WithArguments("internal", "readonly"); |
| 58 | + await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + [WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")] |
| 63 | + public async Task VerifyReadonlyKeywordReorderingWithOtherModifiersAsync() |
| 64 | + { |
| 65 | + var testCode = @" |
| 66 | +public struct S |
| 67 | +{ |
| 68 | + readonly override {|#0:public|} string ToString() => string.Empty; |
| 69 | +}"; |
| 70 | + |
| 71 | + var fixedCode = @" |
| 72 | +public struct S |
| 73 | +{ |
| 74 | + public readonly override string ToString() => string.Empty; |
| 75 | +}"; |
| 76 | + |
| 77 | + var expected = Diagnostic().WithLocation(0).WithArguments("public", "override"); |
| 78 | + await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + [WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")] |
| 83 | + public async Task VerifyReadonlyKeywordReorderingWithAccessAndStaticAsync() |
| 84 | + { |
| 85 | + var testCode = @" |
| 86 | +public struct S |
| 87 | +{ |
| 88 | + readonly {|#0:static|} {|#1:public|} int {|#2:M|}() => 0; |
| 89 | +}"; |
| 90 | + |
| 91 | + var fixedCode = @" |
| 92 | +public struct S |
| 93 | +{ |
| 94 | + public static readonly int {|#2:M|}() => 0; |
| 95 | +}"; |
| 96 | + |
| 97 | + var test = new CSharpTest |
| 98 | + { |
| 99 | + TestCode = testCode, |
| 100 | + FixedCode = fixedCode, |
| 101 | + ExpectedDiagnostics = |
| 102 | + { |
| 103 | + Diagnostic().WithLocation(0).WithArguments("static", "readonly"), |
| 104 | + Diagnostic().WithLocation(1).WithArguments("public", "readonly"), |
| 105 | + |
| 106 | + // /0/Test0.cs(4,32): error CS8657: Static member 'S.M()' cannot be marked 'readonly'. |
| 107 | + DiagnosticResult.CompilerError("CS8657").WithLocation(2).WithArguments("S.M()"), |
| 108 | + }, |
| 109 | + }; |
| 110 | + |
| 111 | + await test.RunAsync(CancellationToken.None).ConfigureAwait(false); |
| 112 | + } |
10 | 113 | } |
11 | 114 | } |
0 commit comments