-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSA1139UnitTests.cs
More file actions
319 lines (301 loc) · 11.2 KB
/
Copy pathSA1139UnitTests.cs
File metadata and controls
319 lines (301 loc) · 11.2 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
// 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.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.ReadabilityRules;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.ReadabilityRules.SA1139UseLiteralSuffixNotationInsteadOfCasting,
StyleCop.Analyzers.ReadabilityRules.SA1139CodeFixProvider>;
/// <summary>
/// This class contains unit tests for SA1139.
/// </summary>
/// <seealso cref="SA1139UseLiteralSuffixNotationInsteadOfCasting" />
/// <seealso cref="SA1139CodeFixProvider" />
public class SA1139UnitTests
{
/// <summary>
/// Verifies that using literals does not produce diagnostic.
/// </summary>
/// <param name="literalType">The type which is checked.</param>
/// <param name="literalSuffix">The literal's suffix.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("int", "")]
[InlineData("long", "L")]
[InlineData("long", "l")]
[InlineData("ulong", "UL")]
[InlineData("ulong", "Ul")]
[InlineData("ulong", "uL")]
[InlineData("ulong", "ul")]
[InlineData("ulong", "LU")]
[InlineData("ulong", "lU")]
[InlineData("ulong", "Lu")]
[InlineData("ulong", "lu")]
[InlineData("uint", "U")]
[InlineData("uint", "u")]
[InlineData("float", "F")]
[InlineData("float", "f")]
[InlineData("double", "D")]
[InlineData("double", "d")]
[InlineData("decimal", "M")]
[InlineData("decimal", "m")]
public async Task TestUsingLiteralsDoesNotProduceDiagnosticAsync(string literalType, string literalSuffix)
{
var testCode = $@"
class ClassName
{{
{literalType} x = 1{literalSuffix};
public void Method()
{{
var x = 1{literalSuffix};
}}
}}
";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that using casts produces diagnostic and correct code fix.
/// </summary>
/// <param name="literalType">The type which is checked.</param>
/// <param name="castedLiteral">The literal that is casted.</param>
/// <param name="correctLiteral">A literal representing result of a cast.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("long", "1", "1L")]
[InlineData("long", "+1", "+1L")]
[InlineData("long", "-1", "-1L")]
[InlineData("long", "(+1)", "(+1L)")]
[InlineData("long", "(-1)", "(-1L)")]
[InlineData("long", "(1)", "(1L)")]
[InlineData("long", "(-(1))", "(-(1L))")]
[InlineData("ulong", "1", "1UL")]
[InlineData("ulong", "+1", "+1UL")]
[InlineData("uint", "1", "1U")]
[InlineData("uint", "+1", "+1U")]
[InlineData("float", "1", "1F")]
[InlineData("float", "+1", "+1F")]
[InlineData("float", "-1", "-1F")]
[InlineData("float", "1.0", "1.0F")]
[InlineData("float", "-1e-10", "-1e-10F")]
[InlineData("double", "1", "1D")]
[InlineData("double", "+1", "+1D")]
[InlineData("double", "-1", "-1D")]
[InlineData("decimal", "1", "1M")]
[InlineData("decimal", "+1", "+1M")]
[InlineData("decimal", "-1", "-1M")]
[InlineData("ulong", "1L", "1UL")]
[InlineData("ulong", "+1L", "+1UL")]
[InlineData("ulong", "1l", "1UL")]
[InlineData("ulong", "1U", "1UL")]
[InlineData("ulong", "1u", "1UL")]
[InlineData("long", "0xF", "0xFL")]
[InlineData("long", "0x1", "0x1L")]
[InlineData("ulong", "0x1", "0x1UL")]
[InlineData("long", "-0x1", "-0x1L")]
public async Task TestUsingCastsProducesDiagnosticAndCorrectCodefixAsync(string literalType, string castedLiteral, string correctLiteral)
{
var testCode = $@"
class ClassName
{{
{literalType} x = ({literalType}){castedLiteral};
public object Method()
{{
var y = ({literalType}){castedLiteral};
return y;
}}
}}
";
var fixedCode = $@"
class ClassName
{{
{literalType} x = {correctLiteral};
public object Method()
{{
var y = {correctLiteral};
return y;
}}
}}
";
DiagnosticResult[] expectedDiagnosticResult =
{
Diagnostic().WithLocation(4, 10 + literalType.Length),
Diagnostic().WithLocation(8, 17),
};
await VerifyCSharpFixAsync(testCode, expectedDiagnosticResult, fixedCode, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that redundant cast does not trigger diagnostic.
/// </summary>
/// <param name="literal">A literal that is casted.</param>
/// <param name="type">A type that literal is casted on.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("1", "int")]
[InlineData("1L", "long")]
[InlineData("1l", "long")]
[InlineData("1UL", "ulong")]
[InlineData("1Ul", "ulong")]
[InlineData("1uL", "ulong")]
[InlineData("1ul", "ulong")]
[InlineData("1LU", "ulong")]
[InlineData("1Lu", "ulong")]
[InlineData("1lU", "ulong")]
[InlineData("1lu", "ulong")]
[InlineData("1U", "uint")]
[InlineData("1u", "uint")]
[InlineData("1F", "float")]
[InlineData("1f", "float")]
[InlineData("1D", "double")]
[InlineData("1d", "double")]
[InlineData("1M", "decimal")]
[InlineData("1m", "decimal")]
public async Task TestDoNotRaportDiagnositcOnRedundantCastAsync(string literal, string type)
{
var testCode = $@"
class ClassName
{{
public void Method()
{{
var x = ({type}){literal};
}}
}}
";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that other types of casts should not produces diagnostics.
/// </summary>
/// <param name="correctCastExpression">A legal cast that should not trigger diagnostic.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("(long)~1")]
[InlineData("(long)(~1)")]
[InlineData("(bool)true")]
[InlineData("(bool)(false)")]
[InlineData("(long)~+1")]
[InlineData("(long)(~+1)")]
[InlineData("unchecked((int)0x80000000)")]
[InlineData("unchecked((int)0xFFFFFFFF)")]
[InlineData("unchecked((ulong)-1)")]
[InlineData("unchecked((byte)-1)")]
[InlineData("(sbyte)-1")]
[InlineData("(int)-1")]
[InlineData("unchecked((ulong)-1L)")]
public async Task TestOtherTypesOfCastsShouldNotTriggerDiagnosticAsync(string correctCastExpression)
{
var testCode = $@"
class ClassName
{{
public void Method()
{{
var x = {correctCastExpression};
}}
}}
";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that diagnostics is not produced when error CS0221 is reported.
/// </summary>
/// <param name="type">A type that a literal is casted on.</param>
/// <param name="castedLiteral">A literal that is casted.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("ulong", "-1")]
public async Task TestCodeTriggeringCs0221ShouldNotTriggerDiagnosticAsync(string type, string castedLiteral)
{
var testCode = $@"
class ClassName
{{
public void Method()
{{
var x = ({type}){castedLiteral};
}}
}}
";
DiagnosticResult[] expectedDiagnosticResult =
{
DiagnosticResult.CompilerError("CS0221")
.WithMessage($"Constant value '{castedLiteral}' cannot be converted to a '{type}' (use 'unchecked' syntax to override)")
.WithLocation(6, 17),
};
await VerifyCSharpDiagnosticAsync(testCode, expectedDiagnosticResult, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that casts in unchecked environment do not get replaced with incorrect values.
/// </summary>
/// <param name="castExpression">A cast which can be performed in unchecked environment.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("(ulong)-1L")]
[InlineData("(int)1000000000000000000L")]
[InlineData("(int)0xFFFFFFFFFFFFFFFFL")]
[InlineData("(uint)0xFFFFFFFFFFFFFFFFL")]
public async Task TestCastsInUncheckedEnviromentShouldPreserveValueAsync(string castExpression)
{
var testCode = $@"
class ClassName
{{
public void Method()
{{
unchecked
{{
var x = {castExpression};
}}
var y = unchecked({castExpression});
}}
}}
";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that the codefix will not insert extraneous spaces.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(2901, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2901")]
public async Task TestCodeFixDoesNotAddExtraneousSpacesAsync()
{
var testCode = @"
public class TestClass
{
public void TestMethod()
{
var x = (long)64 * 1024;
var y = (double)64 /* test */ * 1024;
var z = (long) /* test */ 64 * 1024;
var a = (double)64 // dividend
/ (double)4; // divisor
}
}
";
var fixedCode = @"
public class TestClass
{
public void TestMethod()
{
var x = 64L * 1024;
var y = 64D /* test */ * 1024;
var z = /* test */ 64L * 1024;
var a = 64D // dividend
/ 4D; // divisor
}
}
";
DiagnosticResult[] expectedDiagnosticResult =
{
Diagnostic().WithLocation(6, 17),
Diagnostic().WithLocation(7, 17),
Diagnostic().WithLocation(8, 17),
Diagnostic().WithLocation(9, 17),
Diagnostic().WithLocation(10, 15),
};
await VerifyCSharpFixAsync(testCode, expectedDiagnosticResult, fixedCode, CancellationToken.None).ConfigureAwait(true);
}
}
}