-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSA1125UnitTests.cs
More file actions
363 lines (334 loc) · 14.7 KB
/
Copy pathSA1125UnitTests.cs
File metadata and controls
363 lines (334 loc) · 14.7 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright (c) Contributors to the New StyleCop Analyzers project.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable disable
namespace StyleCop.Analyzers.Test.ReadabilityRules
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.ReadabilityRules;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopDiagnosticVerifier<StyleCop.Analyzers.ReadabilityRules.SA1125UseShorthandForNullableTypes>;
/// <summary>
/// This class contains unit tests for <see cref="SA1125UseShorthandForNullableTypes"/>.
/// </summary>
public class SA1125UnitTests
{
/// <summary>
/// This is a regression test for "SA1125 UseShorthandForNullableTypes incorrectly reported in XML comment".
/// </summary>
/// <param name="form">The source code for the content of a <c>cref</c> attribute referencing
/// <see cref="Nullable{T}"/> in an XML documentation comment.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("Nullable{T}")]
[InlineData("System.Nullable{T}")]
[InlineData("global::System.Nullable{T}")]
[WorkItem(385, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/385")]
public async Task TestSeeAlsoNullableAsync(string form)
{
string template = @"
namespace System
{{
/// <seealso cref=""{0}""/>
class ClassName
{{
}}
}}
";
string testCode = string.Format(template, form);
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// This is a regression test for "SA1125 UseShorthandForNullableTypes incorrectly reported for member in XML
/// comment".
/// </summary>
/// <param name="form">The source code for the content of a <c>cref</c> attribute referencing
/// <see cref="Nullable{T}"/> in an XML documentation comment.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("Nullable{T}")]
[InlineData("System.Nullable{T}")]
[InlineData("global::System.Nullable{T}")]
[WorkItem(638, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/638")]
public async Task TestSeeAlsoNullableValueAsync(string form)
{
string template = @"
namespace System
{{
/// <seealso cref=""{0}.Value""/>
class ClassName
{{
}}
}}
";
string testCode = string.Format(template, form);
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// This is a regression test for "SA1125 UseShorthandForNullableTypes incorrectly reported in XML comment".
/// </summary>
/// <param name="longForm">The source code for the long form of a <c>cref</c> attribute referencing
/// an instantiation of <see cref="Nullable{T}"/> in an XML documentation comment (e.g. for the parameter type
/// in a reference to <see cref="Enumerable.Average(IEnumerable{int?})"/>.</param>
/// <param name="shortForm">The source code for the shorthand form of a <c>cref</c> attribute referencing
/// an instantiation of <see cref="Nullable{T}"/> in an XML documentation comment (e.g. for the parameter type
/// in a reference to <see cref="Enumerable.Average(IEnumerable{int?})"/>.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("Nullable{int}", "int?")]
[InlineData("System.Nullable{int}", "int?")]
[InlineData("global::System.Nullable{int}", "int?")]
[WorkItem(385, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/385")]
public async Task TestSeeAlsoNullableShorthandAsync(string longForm, string shortForm)
{
string template = @"
using System.Collections.Generic;
using System.Linq;
namespace System
{{
/// <seealso cref=""Enumerable.Average(IEnumerable{{{0}}})""/>
class ClassName
{{
}}
}}
";
string testCode = string.Format(template, longForm);
string fixedCode = string.Format(template, shortForm);
DiagnosticResult expected = Diagnostic().WithLocation(6, 55);
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(true);
await VerifyCSharpDiagnosticAsync(fixedCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// This is a regression test for "SA1125 UseShorthandForNullableTypes incorrectly reported in typeof()".
/// </summary>
/// <param name="longForm">The source code for the long form of a <c>cref</c> attribute referencing
/// an instantiation of <see cref="Nullable{T}"/> in a <c>typeof</c> expression.</param>
/// <param name="shortForm">The source code for the shorthand form of a <c>cref</c> attribute referencing
/// an instantiation of <see cref="Nullable{T}"/> in a <c>typeof</c> expression. If no shorthand form is
/// available, this argument should be the same as <paramref name="longForm"/>.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("Nullable<int>", "int?")]
[InlineData("System.Nullable<int>", "int?")]
[InlineData("global::System.Nullable<int>", "int?")]
[InlineData("Nullable<T>", "T?")]
[InlineData("System.Nullable<T>", "T?")]
[InlineData("global::System.Nullable<T>", "T?")]
[InlineData("Nullable<>", "Nullable<>")]
[InlineData("System.Nullable<>", "System.Nullable<>")]
[InlineData("global::System.Nullable<>", "global::System.Nullable<>")]
[WorkItem(386, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/386")]
public async Task TestTypeOfNullableAsync(string longForm, string shortForm)
{
string template = @"
namespace System
{{
class ClassName<T>
where T : struct
{{
Type nullableType = typeof({0});
}}
}}
";
string testCode = string.Format(template, longForm);
string fixedCode = string.Format(template, shortForm);
if (testCode != fixedCode)
{
DiagnosticResult expected = Diagnostic().WithLocation(7, 36);
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(true);
}
await VerifyCSharpDiagnosticAsync(fixedCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
[Theory]
[InlineData("Nullable<int>", "int?")]
[InlineData("System.Nullable<int>", "int?")]
[InlineData("global::System.Nullable<int>", "int?")]
[InlineData("Nullable<T>", "T?")]
[InlineData("System.Nullable<T>", "T?")]
[InlineData("global::System.Nullable<T>", "T?")]
public async Task TestNullableFieldAsync(string longForm, string shortForm)
{
string template = @"
namespace System
{{
class ClassName<T>
where T : struct
{{
{0} nullableField;
}}
}}
";
string testCode = string.Format(template, longForm);
string fixedCode = string.Format(template, shortForm);
DiagnosticResult expected = Diagnostic().WithLocation(7, 9);
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(true);
await VerifyCSharpDiagnosticAsync(fixedCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
[Theory]
[InlineData("Nullable<int>", "int?")]
[InlineData("System.Nullable<int>", "int?")]
[InlineData("global::System.Nullable<int>", "int?")]
[InlineData("Nullable<T>", "T?")]
[InlineData("System.Nullable<T>", "T?")]
[InlineData("global::System.Nullable<T>", "T?")]
public async Task TestDefaultNullableValueAsync(string longForm, string shortForm)
{
string template = @"
namespace System
{{
class ClassName<T>
where T : struct
{{
void MethodName()
{{
var nullableValue = default({0});
}}
}}
}}
";
string testCode = string.Format(template, longForm);
string fixedCode = string.Format(template, shortForm);
DiagnosticResult expected = Diagnostic().WithLocation(9, 41);
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(true);
await VerifyCSharpDiagnosticAsync(fixedCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// This is a regression test for "SA1125 UseShorthandForNullableTypes incorrectly reported in <c>nameof</c>
/// expression".
/// </summary>
/// <param name="form">The source code for the content of a <c>nameof</c> expression referencing
/// <see cref="Nullable{T}"/>.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("Nullable<int>")]
[InlineData("System.Nullable<int>")]
[InlineData("global::System.Nullable<int>")]
[WorkItem(637, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/637")]
public async Task TestNameOfNullableAsync(string form)
{
string template = @"
namespace System
{{
class ClassName<T>
where T : struct
{{
string nullableName = nameof({0});
}}
}}
";
string testCode = string.Format(template, form);
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// This is a regression test for "SA1125 UseShorthandForNullableTypes incorrectly reported for static access
/// through Nullable<int>".
/// </summary>
/// <remarks>
/// <para>This special case of instance access through <c>Nullable<int></c> was mentioned in a
/// comment.</para>
/// </remarks>
/// <param name="form">The source code for the content of a <c>nameof</c> expression referencing
/// <see cref="Nullable{T}"/>.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("Nullable<int>")]
[InlineData("System.Nullable<int>")]
[InlineData("global::System.Nullable<int>")]
[WorkItem(636, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/636")]
public async Task TestNameOfNullableValueAsync(string form)
{
string template = @"
namespace System
{{
class ClassName<T>
where T : struct
{{
string nullableName = nameof({0}.Value);
}}
}}
";
string testCode = string.Format(template, form);
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// This is a regression test for "SA1125 UseShorthandForNullableTypes incorrectly reported for static access
/// through Nullable<int>".
/// </summary>
/// <param name="form">The source code for an instantiation of <see cref="Nullable{T}"/> which does not use the
/// shorthand syntax.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("Nullable<int>")]
[InlineData("System.Nullable<int>")]
[InlineData("global::System.Nullable<int>")]
[WorkItem(636, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/636")]
public async Task TestAccessObjectEqualThroughNullableAsync(string form)
{
string template = @"
namespace System
{{
class ClassName<T>
where T : struct
{{
bool equal = {0}.Equals(null, null);
}}
}}
";
string testCode = string.Format(template, form);
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
[Theory]
[InlineData("Nullable<int>", "int?")]
[InlineData("System.Nullable<int>", "int?")]
[InlineData("global::System.Nullable<int>", "int?")]
[InlineData("Nullable<T>", "T?")]
[InlineData("System.Nullable<T>", "T?")]
[InlineData("global::System.Nullable<T>", "T?")]
public async Task TestNameOfListOfNullableAsync(string longForm, string shortForm)
{
string template = @"
using System.Collections.Generic;
namespace System
{{
class ClassName<T>
where T : struct
{{
string nullableName = nameof(List<{0}>);
}}
}}
";
string testCode = string.Format(template, longForm);
string fixedCode = string.Format(template, shortForm);
DiagnosticResult expected = Diagnostic().WithLocation(8, 43);
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(true);
await VerifyCSharpDiagnosticAsync(fixedCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
// This is a regression test for issue 2284.
[Theory]
[InlineData("@Nullable<int>", "int?")]
[InlineData("System.@Nullable<int>", "int?")]
[InlineData("global::System.@Nullable<int>", "int?")]
public async Task TestNullableFieldWithAtSignPrefixInTypeAsync(string longForm, string shortForm)
{
string template = @"
namespace System
{{
class ClassName<T>
where T : struct
{{
{0} nullableField;
}}
}}
";
string testCode = string.Format(template, longForm);
string fixedCode = string.Format(template, shortForm);
DiagnosticResult expected = Diagnostic().WithLocation(7, 9);
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(true);
await VerifyCSharpDiagnosticAsync(fixedCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
}
}