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

Commit 4893308

Browse files
committed
Don't check names of overriding/implementing methods
Fixes #30
1 parent d7cc75a commit 4893308

File tree

4 files changed

+440
-0
lines changed

4 files changed

+440
-0
lines changed

AsyncUsageAnalyzers/AsyncUsageAnalyzers.Test/Naming/AvoidAsyncSuffixUnitTests.cs

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,193 @@ void SecondMethod() { }
3838
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
3939
}
4040

41+
[Fact]
42+
public async Task TestInheritedReturnVoidAsync()
43+
{
44+
string testCode = @"
45+
class BaseName
46+
{
47+
protected virtual void MethodAsync() { }
48+
}
49+
class ClassName : BaseName
50+
{
51+
protected override void MethodAsync() { }
52+
}
53+
";
54+
string fixedCode = @"
55+
class BaseName
56+
{
57+
protected virtual void Method() { }
58+
}
59+
class ClassName : BaseName
60+
{
61+
protected override void Method() { }
62+
}
63+
";
64+
65+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("MethodAsync").WithLocation(4, 28);
66+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
67+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
68+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
69+
}
70+
71+
[Fact]
72+
public async Task TestExplicitInterfaceReturnVoidAsync()
73+
{
74+
string testCode = @"
75+
interface InterfaceName
76+
{
77+
void MethodAsync();
78+
}
79+
class ClassName : InterfaceName
80+
{
81+
void InterfaceName.MethodAsync() { }
82+
}
83+
";
84+
string fixedCode = @"
85+
interface InterfaceName
86+
{
87+
void Method();
88+
}
89+
class ClassName : InterfaceName
90+
{
91+
void InterfaceName.Method() { }
92+
}
93+
";
94+
95+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("MethodAsync").WithLocation(4, 10);
96+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
97+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
98+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
99+
}
100+
101+
[Fact]
102+
public async Task TestExplicitInterfaceReturnVoidPlusExtraAsync()
103+
{
104+
string testCode = @"
105+
interface InterfaceName
106+
{
107+
void MethodAsync();
108+
}
109+
class ClassName : InterfaceName
110+
{
111+
void MethodAsync() { }
112+
void InterfaceName.MethodAsync() { }
113+
}
114+
";
115+
string fixedCode = @"
116+
interface InterfaceName
117+
{
118+
void Method();
119+
}
120+
class ClassName : InterfaceName
121+
{
122+
void Method() { }
123+
void InterfaceName.Method() { }
124+
}
125+
";
126+
127+
DiagnosticResult[] expected =
128+
{
129+
this.CSharpDiagnostic().WithArguments("MethodAsync").WithLocation(4, 10),
130+
this.CSharpDiagnostic().WithArguments("MethodAsync").WithLocation(8, 10)
131+
};
132+
133+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
134+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
135+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
136+
}
137+
138+
[Fact]
139+
public async Task TestImplicitInterfaceReturnVoidAsync()
140+
{
141+
string testCode = @"
142+
interface InterfaceName
143+
{
144+
void MethodAsync();
145+
}
146+
class ClassName : InterfaceName
147+
{
148+
public void MethodAsync() { }
149+
}
150+
";
151+
string fixedCode = @"
152+
interface InterfaceName
153+
{
154+
void Method();
155+
}
156+
class ClassName : InterfaceName
157+
{
158+
public void Method() { }
159+
}
160+
";
161+
162+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("MethodAsync").WithLocation(4, 10);
163+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
164+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
165+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
166+
}
167+
168+
[Fact]
169+
public async Task TestImplicitGenericInterfaceReturnVoidAsync()
170+
{
171+
string testCode = @"
172+
interface InterfaceName<T>
173+
{
174+
void MethodAsync(T value);
175+
}
176+
class ClassName : InterfaceName<int>
177+
{
178+
public void MethodAsync(int value) { }
179+
}
180+
";
181+
string fixedCode = @"
182+
interface InterfaceName<T>
183+
{
184+
void Method(T value);
185+
}
186+
class ClassName : InterfaceName<int>
187+
{
188+
public void Method(int value) { }
189+
}
190+
";
191+
192+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("MethodAsync").WithLocation(4, 10);
193+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
194+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
195+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
196+
}
197+
198+
[Fact]
199+
public async Task TestImplicitGenericInterfaceMethodReturnVoidAsync()
200+
{
201+
string testCode = @"
202+
interface InterfaceName
203+
{
204+
void MethodAsync<T>(T value);
205+
}
206+
class ClassName : InterfaceName
207+
{
208+
public void MethodAsync<T>(T value) { }
209+
}
210+
";
211+
string fixedCode = @"
212+
interface InterfaceName
213+
{
214+
void Method<T>(T value);
215+
}
216+
class ClassName : InterfaceName
217+
{
218+
public void Method<T>(T value) { }
219+
}
220+
";
221+
222+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("MethodAsync").WithLocation(4, 10);
223+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
224+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
225+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
226+
}
227+
41228
[Fact]
42229
public async Task TestAsyncReturnVoidAsync()
43230
{

AsyncUsageAnalyzers/AsyncUsageAnalyzers.Test/Naming/UseAsyncSuffixUnitTests.cs

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,205 @@ class ClassName
121121
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
122122
}
123123

124+
[Fact]
125+
public async Task TestInheritedReturnTaskAsync()
126+
{
127+
string testCode = @"
128+
using System.Threading.Tasks;
129+
class BaseName
130+
{
131+
protected virtual Task Method() { return Task.FromResult(3); }
132+
}
133+
class ClassName : BaseName
134+
{
135+
protected override Task Method() { return Task.FromResult(3); }
136+
}
137+
";
138+
string fixedCode = @"
139+
using System.Threading.Tasks;
140+
class BaseName
141+
{
142+
protected virtual Task MethodAsync() { return Task.FromResult(3); }
143+
}
144+
class ClassName : BaseName
145+
{
146+
protected override Task MethodAsync() { return Task.FromResult(3); }
147+
}
148+
";
149+
150+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("Method").WithLocation(5, 28);
151+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
152+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
153+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
154+
}
155+
156+
[Fact]
157+
public async Task TestExplicitInterfaceReturnTaskAsync()
158+
{
159+
string testCode = @"
160+
using System.Threading.Tasks;
161+
interface InterfaceName
162+
{
163+
Task Method();
164+
}
165+
class ClassName : InterfaceName
166+
{
167+
Task InterfaceName.Method() { return Task.FromResult(3); }
168+
}
169+
";
170+
string fixedCode = @"
171+
using System.Threading.Tasks;
172+
interface InterfaceName
173+
{
174+
Task MethodAsync();
175+
}
176+
class ClassName : InterfaceName
177+
{
178+
Task InterfaceName.MethodAsync() { return Task.FromResult(3); }
179+
}
180+
";
181+
182+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("Method").WithLocation(5, 10);
183+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
184+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
185+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
186+
}
187+
188+
[Fact]
189+
public async Task TestExplicitInterfaceReturnTaskPlusExtraAsync()
190+
{
191+
string testCode = @"
192+
using System.Threading.Tasks;
193+
interface InterfaceName
194+
{
195+
Task Method();
196+
}
197+
class ClassName : InterfaceName
198+
{
199+
Task Method() { return Task.FromResult(3); }
200+
Task InterfaceName.Method() { return Task.FromResult(3); }
201+
}
202+
";
203+
string fixedCode = @"
204+
using System.Threading.Tasks;
205+
interface InterfaceName
206+
{
207+
Task MethodAsync();
208+
}
209+
class ClassName : InterfaceName
210+
{
211+
Task MethodAsync() { return Task.FromResult(3); }
212+
Task InterfaceName.MethodAsync() { return Task.FromResult(3); }
213+
}
214+
";
215+
216+
DiagnosticResult[] expected =
217+
{
218+
this.CSharpDiagnostic().WithArguments("Method").WithLocation(5, 10),
219+
this.CSharpDiagnostic().WithArguments("Method").WithLocation(9, 10)
220+
};
221+
222+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
223+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
224+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
225+
}
226+
227+
[Fact]
228+
public async Task TestImplicitInterfaceReturnTaskAsync()
229+
{
230+
string testCode = @"
231+
using System.Threading.Tasks;
232+
interface InterfaceName
233+
{
234+
Task Method();
235+
}
236+
class ClassName : InterfaceName
237+
{
238+
public Task Method() { return Task.FromResult(3); }
239+
}
240+
";
241+
string fixedCode = @"
242+
using System.Threading.Tasks;
243+
interface InterfaceName
244+
{
245+
Task MethodAsync();
246+
}
247+
class ClassName : InterfaceName
248+
{
249+
public Task MethodAsync() { return Task.FromResult(3); }
250+
}
251+
";
252+
253+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("Method").WithLocation(5, 10);
254+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
255+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
256+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
257+
}
258+
259+
[Fact]
260+
public async Task TestImplicitGenericInterfaceReturnTaskAsync()
261+
{
262+
string testCode = @"
263+
using System.Threading.Tasks;
264+
interface InterfaceName<T>
265+
{
266+
Task Method(T value);
267+
}
268+
class ClassName : InterfaceName<int>
269+
{
270+
public Task Method(int value) { return Task.FromResult(value); }
271+
}
272+
";
273+
string fixedCode = @"
274+
using System.Threading.Tasks;
275+
interface InterfaceName<T>
276+
{
277+
Task MethodAsync(T value);
278+
}
279+
class ClassName : InterfaceName<int>
280+
{
281+
public Task MethodAsync(int value) { return Task.FromResult(value); }
282+
}
283+
";
284+
285+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("Method").WithLocation(5, 10);
286+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
287+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
288+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
289+
}
290+
291+
[Fact]
292+
public async Task TestImplicitGenericInterfaceMethodReturnTaskAsync()
293+
{
294+
string testCode = @"
295+
using System.Threading.Tasks;
296+
interface InterfaceName
297+
{
298+
Task Method<T>(T value);
299+
}
300+
class ClassName : InterfaceName
301+
{
302+
public Task Method<T>(T value) { return Task.FromResult(value); }
303+
}
304+
";
305+
string fixedCode = @"
306+
using System.Threading.Tasks;
307+
interface InterfaceName
308+
{
309+
Task MethodAsync<T>(T value);
310+
}
311+
class ClassName : InterfaceName
312+
{
313+
public Task MethodAsync<T>(T value) { return Task.FromResult(value); }
314+
}
315+
";
316+
317+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("Method").WithLocation(5, 10);
318+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
319+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
320+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
321+
}
322+
124323
[Fact]
125324
public async Task TestReturnGenericTaskAsync()
126325
{

0 commit comments

Comments
 (0)