Skip to content
This repository was archived by the owner on Nov 8, 2018. It is now read-only.

Commit 4555834

Browse files
committed
Merge pull request #13 from sharwell/initial-analyzers
Initial analyzers
2 parents 17115f8 + 23628e2 commit 4555834

15 files changed

+1204
-0
lines changed

AsyncUsageAnalyzers/AsyncUsageAnalyzers.Test/AsyncUsageAnalyzers.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@
122122
</Reference>
123123
</ItemGroup>
124124
<ItemGroup>
125+
<Compile Include="Naming\AvoidAsyncSuffixUnitTests.cs" />
126+
<Compile Include="Naming\UseAsyncSuffixUnitTests.cs" />
127+
<Compile Include="Reliability\AvoidAsyncVoidUnitTests.cs" />
125128
<Compile Include="Verifiers\CodeFixVerifier.cs" />
126129
<Compile Include="Verifiers\DiagnosticVerifier.cs" />
127130
<Compile Include="Helpers\CodeFixVerifier.Helper.cs" />
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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 AvoidAsyncSuffixUnitTests : CodeFixVerifier
12+
{
13+
[Fact]
14+
public async Task TestReturnVoidAsync()
15+
{
16+
string testCode = @"
17+
class ClassName
18+
{
19+
void FirstMethod() { }
20+
void SecondMethodAsync() { }
21+
}
22+
";
23+
string fixedCode = @"
24+
class ClassName
25+
{
26+
void FirstMethod() { }
27+
void SecondMethod() { }
28+
}
29+
";
30+
31+
DiagnosticResult expected = CSharpDiagnostic().WithArguments("SecondMethodAsync").WithLocation(5, 10);
32+
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
33+
await VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
34+
await VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
35+
}
36+
37+
[Fact]
38+
public async Task TestAsyncReturnVoidAsync()
39+
{
40+
string testCode = @"
41+
class ClassName
42+
{
43+
async void FirstMethod() { }
44+
async void SecondMethodAsync() { }
45+
}
46+
";
47+
48+
await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
49+
}
50+
51+
[Fact]
52+
public async Task TestEventHandlerReturnVoidAsync()
53+
{
54+
string testCode = @"
55+
using System;
56+
class ClassName
57+
{
58+
void FirstMethod(object sender, EventArgs e) { }
59+
void SecondMethodAsync(object sender, EventArgs e) { }
60+
}
61+
";
62+
string fixedCode = @"
63+
using System;
64+
class ClassName
65+
{
66+
void FirstMethod(object sender, EventArgs e) { }
67+
void SecondMethod(object sender, EventArgs e) { }
68+
}
69+
";
70+
71+
DiagnosticResult expected = CSharpDiagnostic().WithArguments("SecondMethodAsync").WithLocation(6, 10);
72+
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
73+
await VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
74+
await VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
75+
}
76+
77+
[Fact]
78+
public async Task TestAsyncEventHandlerReturnVoidAsync()
79+
{
80+
string testCode = @"
81+
using System;
82+
class ClassName
83+
{
84+
async void FirstMethod(object sender, EventArgs e) { }
85+
async void SecondMethodAsync(object sender, EventArgs e) { }
86+
}
87+
";
88+
89+
await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
90+
}
91+
92+
[Fact]
93+
public async Task TestAsyncReturnTaskAsync()
94+
{
95+
string testCode = @"
96+
using System.Threading.Tasks;
97+
class ClassName
98+
{
99+
async Task FirstMethod() { }
100+
async Task SecondMethodAsync() { }
101+
}
102+
";
103+
104+
await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
105+
}
106+
107+
[Fact]
108+
public async Task TestAsyncReturnGenericTaskAsync()
109+
{
110+
string testCode = @"
111+
using System.Threading.Tasks;
112+
class ClassName
113+
{
114+
async Task<int> FirstMethod() { return 3; }
115+
async Task<int> SecondMethodAsync() { return 3; }
116+
}
117+
";
118+
119+
await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
120+
}
121+
122+
[Fact]
123+
public async Task TestReturnTaskAsync()
124+
{
125+
string testCode = @"
126+
using System.Threading.Tasks;
127+
class ClassName
128+
{
129+
Task FirstMethod() { return Task.FromResult(3); }
130+
Task SecondMethodAsync() { return Task.FromResult(3); }
131+
}
132+
";
133+
134+
await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
135+
}
136+
137+
[Fact]
138+
public async Task TestReturnGenericTaskAsync()
139+
{
140+
string testCode = @"
141+
using System.Threading.Tasks;
142+
class ClassName
143+
{
144+
Task<int> FirstMethod() { return Task.FromResult(3); }
145+
Task<int> SecondMethodAsync() { return Task.FromResult(3); }
146+
}
147+
";
148+
149+
await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
150+
}
151+
152+
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
153+
{
154+
return new AvoidAsyncSuffixAnalyzer();
155+
}
156+
157+
protected override CodeFixProvider GetCSharpCodeFixProvider()
158+
{
159+
return new AvoidAsyncSuffixCodeFixProvider();
160+
}
161+
}
162+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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

Comments
 (0)