|
| 1 | +namespace AsyncUsageAnalyzers.Test.Naming |
| 2 | +{ |
| 3 | + using System.Threading; |
| 4 | + using System.Threading.Tasks; |
| 5 | + using AsyncUsageAnalyzers.Naming; |
| 6 | + using Microsoft.CodeAnalysis.CodeFixes; |
| 7 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 8 | + using TestHelper; |
| 9 | + using Xunit; |
| 10 | + |
| 11 | + public class UseAsyncSuffixUnitTests : CodeFixVerifier |
| 12 | + { |
| 13 | + [Fact] |
| 14 | + public async Task TestAsyncReturnVoidAsync() |
| 15 | + { |
| 16 | + string testCode = @" |
| 17 | +class ClassName |
| 18 | +{ |
| 19 | + async void FirstMethod() { } |
| 20 | + async void SecondMethodAsync() { } |
| 21 | +} |
| 22 | +"; |
| 23 | + |
| 24 | + await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public async Task TestAsyncEventHandlerReturnVoidAsync() |
| 29 | + { |
| 30 | + string testCode = @" |
| 31 | +using System; |
| 32 | +class ClassName |
| 33 | +{ |
| 34 | + async void FirstMethod(object sender, EventArgs e) { } |
| 35 | + async void SecondMethodAsync(object sender, EventArgs e) { } |
| 36 | +} |
| 37 | +"; |
| 38 | + |
| 39 | + await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public async Task TestAsyncReturnTaskAsync() |
| 44 | + { |
| 45 | + string testCode = @" |
| 46 | +using System.Threading.Tasks; |
| 47 | +class ClassName |
| 48 | +{ |
| 49 | + async Task FirstMethod() { } |
| 50 | + async Task SecondMethodAsync() { } |
| 51 | +} |
| 52 | +"; |
| 53 | + string fixedCode = @" |
| 54 | +using System.Threading.Tasks; |
| 55 | +class ClassName |
| 56 | +{ |
| 57 | + async Task FirstMethodAsync() { } |
| 58 | + async Task SecondMethodAsync() { } |
| 59 | +} |
| 60 | +"; |
| 61 | + |
| 62 | + DiagnosticResult expected = CSharpDiagnostic().WithArguments("FirstMethod").WithLocation(5, 16); |
| 63 | + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 64 | + await VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 65 | + await VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public async Task TestAsyncReturnGenericTaskAsync() |
| 70 | + { |
| 71 | + string testCode = @" |
| 72 | +using System.Threading.Tasks; |
| 73 | +class ClassName |
| 74 | +{ |
| 75 | + async Task<int> FirstMethod() { return 3; } |
| 76 | + async Task<int> SecondMethodAsync() { return 3; } |
| 77 | +} |
| 78 | +"; |
| 79 | + string fixedCode = @" |
| 80 | +using System.Threading.Tasks; |
| 81 | +class ClassName |
| 82 | +{ |
| 83 | + async Task<int> FirstMethodAsync() { return 3; } |
| 84 | + async Task<int> SecondMethodAsync() { return 3; } |
| 85 | +} |
| 86 | +"; |
| 87 | + |
| 88 | + DiagnosticResult expected = CSharpDiagnostic().WithArguments("FirstMethod").WithLocation(5, 21); |
| 89 | + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 90 | + await VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 91 | + await VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public async Task TestReturnTaskAsync() |
| 96 | + { |
| 97 | + string testCode = @" |
| 98 | +using System.Threading.Tasks; |
| 99 | +class ClassName |
| 100 | +{ |
| 101 | + Task FirstMethod() { return Task.FromResult(3); } |
| 102 | + Task SecondMethodAsync() { return Task.FromResult(3); } |
| 103 | +} |
| 104 | +"; |
| 105 | + string fixedCode = @" |
| 106 | +using System.Threading.Tasks; |
| 107 | +class ClassName |
| 108 | +{ |
| 109 | + Task FirstMethodAsync() { return Task.FromResult(3); } |
| 110 | + Task SecondMethodAsync() { return Task.FromResult(3); } |
| 111 | +} |
| 112 | +"; |
| 113 | + |
| 114 | + DiagnosticResult expected = CSharpDiagnostic().WithArguments("FirstMethod").WithLocation(5, 10); |
| 115 | + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 116 | + await VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 117 | + await VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false); |
| 118 | + } |
| 119 | + |
| 120 | + [Fact] |
| 121 | + public async Task TestReturnGenericTaskAsync() |
| 122 | + { |
| 123 | + string testCode = @" |
| 124 | +using System.Threading.Tasks; |
| 125 | +class ClassName |
| 126 | +{ |
| 127 | + Task<int> FirstMethod() { return Task.FromResult(3); } |
| 128 | + Task<int> SecondMethodAsync() { return Task.FromResult(3); } |
| 129 | +} |
| 130 | +"; |
| 131 | + string fixedCode = @" |
| 132 | +using System.Threading.Tasks; |
| 133 | +class ClassName |
| 134 | +{ |
| 135 | + Task<int> FirstMethodAsync() { return Task.FromResult(3); } |
| 136 | + Task<int> SecondMethodAsync() { return Task.FromResult(3); } |
| 137 | +} |
| 138 | +"; |
| 139 | + |
| 140 | + DiagnosticResult expected = CSharpDiagnostic().WithArguments("FirstMethod").WithLocation(5, 15); |
| 141 | + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 142 | + await VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 143 | + await VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false); |
| 144 | + } |
| 145 | + |
| 146 | + protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() |
| 147 | + { |
| 148 | + return new UseAsyncSuffixAnalyzer(); |
| 149 | + } |
| 150 | + |
| 151 | + protected override CodeFixProvider GetCSharpCodeFixProvider() |
| 152 | + { |
| 153 | + return new UseAsyncSuffixCodeFixProvider(); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments