|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp9.ReadabilityRules |
5 | 5 | { |
| 6 | + using System.Threading; |
| 7 | + using System.Threading.Tasks; |
| 8 | + using Microsoft.CodeAnalysis.Testing; |
6 | 9 | using StyleCop.Analyzers.Test.CSharp8.ReadabilityRules; |
| 10 | + using StyleCop.Analyzers.Test.Helpers; |
| 11 | + using Xunit; |
| 12 | + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< |
| 13 | + StyleCop.Analyzers.ReadabilityRules.SA1111ClosingParenthesisMustBeOnLineOfLastParameter, |
| 14 | + StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>; |
7 | 15 |
|
8 | 16 | public partial class SA1111CSharp9UnitTests : SA1111CSharp8UnitTests |
9 | 17 | { |
| 18 | + [Theory] |
| 19 | + [MemberData(nameof(CommonMemberData.TypeKeywordsWhichSupportPrimaryConstructors), MemberType = typeof(CommonMemberData))] |
| 20 | + [WorkItem(3785, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3785")] |
| 21 | + public async Task TestPrimaryConstructorWithParameterAsync(string typeKeyword) |
| 22 | + { |
| 23 | + var testCode = $@" |
| 24 | +{typeKeyword} Foo(int x |
| 25 | + {{|#0:)|}} |
| 26 | +{{ |
| 27 | +}}"; |
| 28 | + |
| 29 | + var fixedCode = $@" |
| 30 | +{typeKeyword} Foo(int x) |
| 31 | +{{ |
| 32 | +}}"; |
| 33 | + |
| 34 | + var expected = this.GetExpectedResultTestPrimaryConstructorWithParameter(); |
| 35 | + await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); |
| 36 | + } |
| 37 | + |
| 38 | + [Theory] |
| 39 | + [MemberData(nameof(CommonMemberData.TypeKeywordsWhichSupportPrimaryConstructors), MemberType = typeof(CommonMemberData))] |
| 40 | + [WorkItem(3785, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3785")] |
| 41 | + public async Task TestPrimaryConstructorWithoutParameterAsync(string typeKeyword) |
| 42 | + { |
| 43 | + var testCode = $@" |
| 44 | +{typeKeyword} Foo( |
| 45 | + ) |
| 46 | +{{ |
| 47 | +}}"; |
| 48 | + |
| 49 | + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 50 | + } |
| 51 | + |
| 52 | + [Theory] |
| 53 | + [MemberData(nameof(CommonMemberData.ReferenceTypeKeywordsWhichSupportPrimaryConstructors), MemberType = typeof(CommonMemberData))] |
| 54 | + [WorkItem(3785, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3785")] |
| 55 | + public async Task TestPrimaryConstructorBaseListWithArgumentsAsync(string typeKeyword) |
| 56 | + { |
| 57 | + var testCode = $@" |
| 58 | +{typeKeyword} Foo(int x) |
| 59 | +{{ |
| 60 | +}} |
| 61 | +
|
| 62 | +{typeKeyword} Bar(int x) : Foo(x |
| 63 | + {{|#0:)|}} |
| 64 | +{{ |
| 65 | +}}"; |
| 66 | + |
| 67 | + var fixedCode = $@" |
| 68 | +{typeKeyword} Foo(int x) |
| 69 | +{{ |
| 70 | +}} |
| 71 | +
|
| 72 | +{typeKeyword} Bar(int x) : Foo(x) |
| 73 | +{{ |
| 74 | +}}"; |
| 75 | + |
| 76 | + var expected = this.GetExpectedResultTestPrimaryConstructorBaseList(); |
| 77 | + await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); |
| 78 | + } |
| 79 | + |
| 80 | + [Theory] |
| 81 | + [MemberData(nameof(CommonMemberData.ReferenceTypeKeywordsWhichSupportPrimaryConstructors), MemberType = typeof(CommonMemberData))] |
| 82 | + [WorkItem(3785, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3785")] |
| 83 | + public async Task TestPrimaryConstructorBaseListWithoutArgumentsAsync(string typeKeyword) |
| 84 | + { |
| 85 | + var testCode = $@" |
| 86 | +{typeKeyword} Foo() |
| 87 | +{{ |
| 88 | +}} |
| 89 | +
|
| 90 | +{typeKeyword} Bar(int x) : Foo( |
| 91 | + ) |
| 92 | +{{ |
| 93 | +}}"; |
| 94 | + |
| 95 | + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 96 | + } |
| 97 | + |
| 98 | + protected virtual DiagnosticResult[] GetExpectedResultTestPrimaryConstructorWithParameter() |
| 99 | + { |
| 100 | + return new[] |
| 101 | + { |
| 102 | + // Diagnostic issued twice because of https://github.com/dotnet/roslyn/issues/53136 |
| 103 | + Diagnostic().WithLocation(0), |
| 104 | + Diagnostic().WithLocation(0), |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + protected virtual DiagnosticResult[] GetExpectedResultTestPrimaryConstructorBaseList() |
| 109 | + { |
| 110 | + return new[] |
| 111 | + { |
| 112 | + // Diagnostic issued twice because of https://github.com/dotnet/roslyn/issues/70488 |
| 113 | + Diagnostic().WithLocation(0), |
| 114 | + Diagnostic().WithLocation(0), |
| 115 | + }; |
| 116 | + } |
10 | 117 | } |
11 | 118 | } |
0 commit comments