-
Notifications
You must be signed in to change notification settings - Fork 457
/
UsePropertyInsteadOfCountMethodWhenAvailableTests.cs
335 lines (322 loc) · 14.5 KB
/
UsePropertyInsteadOfCountMethodWhenAvailableTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Dynamic;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.NetCore.Analyzers.Runtime;
using Xunit;
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier<
Microsoft.NetCore.Analyzers.Performance.UsePropertyInsteadOfCountMethodWhenAvailableAnalyzer,
Microsoft.NetCore.CSharp.Analyzers.Performance.CSharpUsePropertyInsteadOfCountMethodWhenAvailableFixer>;
using VerifyVB = Test.Utilities.VisualBasicCodeFixVerifier<
Microsoft.NetCore.Analyzers.Performance.UsePropertyInsteadOfCountMethodWhenAvailableAnalyzer,
Microsoft.NetCore.VisualBasic.Analyzers.Performance.BasicUsePropertyInsteadOfCountMethodWhenAvailableFixer>;
namespace Microsoft.NetCore.Analyzers.Performance.UnitTests
{
public static partial class UsePropertyInsteadOfCountMethodWhenAvailableTests
{
[Fact]
public static Task CSharp_ImmutableArray_Tests()
=> VerifyCS.VerifyCodeFixAsync(
$@"using System;
using System.Linq;
public static class C
{{
public static System.Collections.Immutable.ImmutableArray<int> GetData() => default;
public static int M() => {{|{UsePropertyInsteadOfCountMethodWhenAvailableAnalyzer.RuleId}:GetData().Count()|}};
public static int N() => {{|{UsePropertyInsteadOfCountMethodWhenAvailableAnalyzer.RuleId}:GetData().Count()|}};
}}
",
$@"using System;
using System.Linq;
public static class C
{{
public static System.Collections.Immutable.ImmutableArray<int> GetData() => default;
public static int M() => GetData().Length;
public static int N() => GetData().Length;
}}
");
[Theory]
[InlineData("string[]", nameof(Array.Length))]
[InlineData("System.Collections.Immutable.ImmutableArray<int>", nameof(ImmutableArray<int>.Length))]
[InlineData("System.Collections.Generic.List<int>", nameof(List<int>.Count))]
[InlineData("System.Collections.Generic.IList<int>", nameof(IList<int>.Count))]
[InlineData("System.Collections.Generic.ICollection<int>", nameof(ICollection<int>.Count))]
public static Task CSharp_Fixed(string type, string propertyName)
=> VerifyCS.VerifyCodeFixAsync(
$@"using System;
using System.Linq;
public static class C
{{
public static {type} GetData() => default;
public static int M() => GetData().Count();
}}
",
VerifyCS.Diagnostic(UsePropertyInsteadOfCountMethodWhenAvailableAnalyzer.RuleId)
.WithLocation(6, 30)
.WithArguments(propertyName),
$@"using System;
using System.Linq;
public static class C
{{
public static {type} GetData() => default;
public static int M() => GetData().{propertyName};
}}
");
[Theory]
[InlineData("string()", nameof(Array.Length))]
[InlineData("System.Collections.Immutable.ImmutableArray(Of Integer)", nameof(ImmutableArray<int>.Length))]
public static Task Basic_Fixed(string type, string propertyName)
=> VerifyVB.VerifyCodeFixAsync(
$@"Imports System
Imports System.Linq
Public Module M
Public Function GetData() As {type}
Return Nothing
End Function
Public Function F() As Integer
Return GetData().Count()
End Function
End Module
",
VerifyCS.Diagnostic(UsePropertyInsteadOfCountMethodWhenAvailableAnalyzer.RuleId)
.WithLocation(8, 16)
.WithArguments(propertyName),
$@"Imports System
Imports System.Linq
Public Module M
Public Function GetData() As {type}
Return Nothing
End Function
Public Function F() As Integer
Return GetData().{propertyName}
End Function
End Module
");
[Theory]
[InlineData("System.Collections.Generic.IEnumerable<int>")]
public static Task CSharp_NoDiagnostic(string type)
=> VerifyCS.VerifyAnalyzerAsync(
$@"using System;
using System.Linq;
public static class C
{{
public static {type} GetData() => default;
public static int M() => GetData().Count();
}}
");
[Theory]
[InlineData("System.Collections.Generic.List(Of Integer)")]
[InlineData("System.Collections.Generic.IList(Of Integer)")]
[InlineData("System.Collections.Generic.ICollection(Of Integer)")]
[InlineData("System.Collections.Generic.IEnumerable(Of Integer)")]
public static Task Basic_NoDiagnostic(string type)
=> VerifyVB.VerifyAnalyzerAsync(
$@"Imports System
Imports System.Linq
Public Module M
Public Function GetData() As {type}
Return Nothing
End Function
Public Function F() As Integer
Return GetData().Count()
End Function
End Module
");
[Fact]
public static Task CSharp_ICollectionOfTImplementerWithImplicitCount_Fixed()
=> VerifyCS.VerifyCodeFixAsync(
$@"using System;
using System.Linq;
public class T : global::System.Collections.Generic.ICollection<string>
{{
public int Count => throw new NotImplementedException();
public bool IsReadOnly => throw new NotImplementedException();
public void Add(string item) => throw new NotImplementedException();
public void Clear() => throw new NotImplementedException();
public bool Contains(string item) => throw new NotImplementedException();
public void CopyTo(string[] array, int arrayIndex) => throw new NotImplementedException();
public global::System.Collections.Generic.IEnumerator<string> GetEnumerator() => throw new NotImplementedException();
public bool Remove(string item) => throw new NotImplementedException();
global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => throw new NotImplementedException();
}}
public static class C
{{
public static T GetData() => default;
public static int M() => GetData().Count();
}}
",
VerifyCS.Diagnostic(UsePropertyInsteadOfCountMethodWhenAvailableAnalyzer.RuleId)
.WithLocation(18, 30)
.WithArguments("Count"),
$@"using System;
using System.Linq;
public class T : global::System.Collections.Generic.ICollection<string>
{{
public int Count => throw new NotImplementedException();
public bool IsReadOnly => throw new NotImplementedException();
public void Add(string item) => throw new NotImplementedException();
public void Clear() => throw new NotImplementedException();
public bool Contains(string item) => throw new NotImplementedException();
public void CopyTo(string[] array, int arrayIndex) => throw new NotImplementedException();
public global::System.Collections.Generic.IEnumerator<string> GetEnumerator() => throw new NotImplementedException();
public bool Remove(string item) => throw new NotImplementedException();
global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => throw new NotImplementedException();
}}
public static class C
{{
public static T GetData() => default;
public static int M() => GetData().Count;
}}
");
[Fact]
public static Task CSharp_ICollectionImplementerWithImplicitCount_Fixed()
=> VerifyCS.VerifyCodeFixAsync(
$@"using System;
using System.Linq;
public class T :
global::System.Collections.Generic.IEnumerable<string>,
global::System.Collections.ICollection
{{
public int Count => throw new NotImplementedException();
bool global::System.Collections.ICollection.IsSynchronized => throw new NotImplementedException();
object global::System.Collections.ICollection.SyncRoot => throw new NotImplementedException();
void global::System.Collections.ICollection.CopyTo(global::System.Array array, int arrayIndex) => throw new NotImplementedException();
public global::System.Collections.Generic.IEnumerator<string> GetEnumerator() => throw new NotImplementedException();
global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => throw new NotImplementedException();
}}
public static class C
{{
public static T GetData() => default;
public static int M() => GetData().Count();
}}
",
VerifyCS.Diagnostic(UsePropertyInsteadOfCountMethodWhenAvailableAnalyzer.RuleId)
.WithLocation(17, 30)
.WithArguments("Count"),
$@"using System;
using System.Linq;
public class T :
global::System.Collections.Generic.IEnumerable<string>,
global::System.Collections.ICollection
{{
public int Count => throw new NotImplementedException();
bool global::System.Collections.ICollection.IsSynchronized => throw new NotImplementedException();
object global::System.Collections.ICollection.SyncRoot => throw new NotImplementedException();
void global::System.Collections.ICollection.CopyTo(global::System.Array array, int arrayIndex) => throw new NotImplementedException();
public global::System.Collections.Generic.IEnumerator<string> GetEnumerator() => throw new NotImplementedException();
global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => throw new NotImplementedException();
}}
public static class C
{{
public static T GetData() => default;
public static int M() => GetData().Count;
}}
");
[Fact]
public static Task CSharp_ICollectionOfTImplementerWithExplicitCount_NoDiagnostic()
=> VerifyCS.VerifyAnalyzerAsync(
$@"using System;
using System.Linq;
public class T : global::System.Collections.Generic.ICollection<string>
{{
int global::System.Collections.Generic.ICollection<string>.Count => throw new NotImplementedException();
bool global::System.Collections.Generic.ICollection<string>.IsReadOnly => throw new NotImplementedException();
void global::System.Collections.Generic.ICollection<string>.Add(string item) => throw new NotImplementedException();
void global::System.Collections.Generic.ICollection<string>.Clear() => throw new NotImplementedException();
bool global::System.Collections.Generic.ICollection<string>.Contains(string item) => throw new NotImplementedException();
void global::System.Collections.Generic.ICollection<string>.CopyTo(string[] array, int arrayIndex) => throw new NotImplementedException();
public global::System.Collections.Generic.IEnumerator<string> GetEnumerator() => throw new NotImplementedException();
global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => throw new NotImplementedException();
bool global::System.Collections.Generic.ICollection<string>.Remove(string item) => throw new NotImplementedException();
}}
public static class C
{{
public static T GetData() => default;
public static int M() => GetData().Count();
}}
");
[Fact]
public static Task CSharp_InterfaceShadowingICollectionOfT_NoDiagnostic()
=> VerifyCS.VerifyAnalyzerAsync(
@"using System;
using System.Linq;
public interface I : global::System.Collections.Generic.ICollection<string>
{
new int Count { get; }
}
public static class C
{
public static I GetData() => default;
public static int M() => GetData().Count();
}
");
[Fact]
public static Task CSharp_InterfaceShadowingICollection_NoDiagnostic()
=> VerifyCS.VerifyAnalyzerAsync(
@"using System;
using System.Linq;
public interface I :
global::System.Collections.Generic.IEnumerable<string>,
global::System.Collections.ICollection
{
new int Count { get; }
}
public static class C
{
public static I GetData() => default;
public static int M() => GetData().Count();
}
");
[Fact]
public static Task CSharp_ClassShadowingICollectionOfT_NoDiagnostic()
=> VerifyCS.VerifyAnalyzerAsync(
$@"using System;
using System.Linq;
public class T : global::System.Collections.Generic.ICollection<string>
{{
public int Count => throw new NotImplementedException();
int global::System.Collections.Generic.ICollection<string>.Count => throw new NotImplementedException();
bool global::System.Collections.Generic.ICollection<string>.IsReadOnly => throw new NotImplementedException();
void global::System.Collections.Generic.ICollection<string>.Add(string item) => throw new NotImplementedException();
void global::System.Collections.Generic.ICollection<string>.Clear() => throw new NotImplementedException();
bool global::System.Collections.Generic.ICollection<string>.Contains(string item) => throw new NotImplementedException();
void global::System.Collections.Generic.ICollection<string>.CopyTo(string[] array, int arrayIndex) => throw new NotImplementedException();
bool global::System.Collections.Generic.ICollection<string>.Remove(string item) => throw new NotImplementedException();
public global::System.Collections.Generic.IEnumerator<string> GetEnumerator() => throw new NotImplementedException();
global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => throw new NotImplementedException();
}}
public static class C
{{
public static T GetData() => default;
public static int M() => GetData().Count();
}}
");
[Fact]
public static Task CSharp_ClassShadowingICollection_NoDiagnostic()
=> VerifyCS.VerifyAnalyzerAsync(
$@"using System;
using System.Linq;
public class T :
global::System.Collections.Generic.IEnumerable<string>,
global::System.Collections.ICollection
{{
public int Count => throw new NotImplementedException();
int global::System.Collections.ICollection.Count => throw new NotImplementedException();
bool global::System.Collections.ICollection.IsSynchronized => throw new NotImplementedException();
object global::System.Collections.ICollection.SyncRoot => throw new NotImplementedException();
void global::System.Collections.ICollection.CopyTo(global::System.Array array, int arrayIndex) => throw new NotImplementedException();
public global::System.Collections.Generic.IEnumerator<string> GetEnumerator() => throw new NotImplementedException();
global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => throw new NotImplementedException();
}}
public static class C
{{
public static T GetData() => default;
public static int M() => GetData().Count();
}}
");
}
}