Skip to content

Commit 861c3d1

Browse files
committed
Implement CR feedback
1 parent b1c9b25 commit 861c3d1

9 files changed

Lines changed: 282 additions & 64 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1201UnitTests.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ public enum TestEnum { }
132132
public string TestProperty { get; set; }
133133
public struct TestStruct { }
134134
public void TestMethod () { }
135-
public string this[string arg] { get { return ""foo""; } set { } }
136135
public class TestClass { }
136+
public string this[string arg] { get { return ""foo""; } set { } }
137137
}
138138
";
139139
var expected = new[]
@@ -143,7 +143,7 @@ public class TestClass { }
143143
this.CSharpDiagnostic().WithLocation(11, 5).WithArguments("conversion", "operator"),
144144
this.CSharpDiagnostic().WithLocation(12, 19).WithArguments("property", "conversion"),
145145
this.CSharpDiagnostic().WithLocation(14, 17).WithArguments("method", "struct"),
146-
this.CSharpDiagnostic().WithLocation(15, 19).WithArguments("indexer", "method")
146+
this.CSharpDiagnostic().WithLocation(16, 19).WithArguments("indexer", "class")
147147
};
148148

149149
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
@@ -187,8 +187,8 @@ public enum TestEnum { }
187187
public string TestProperty { get; set; }
188188
public struct TestStruct { }
189189
public void TestMethod () { }
190-
public string this[string arg] { get { return ""foo""; } set { } }
191190
public class TestClass { }
191+
public string this[string arg] { get { return ""foo""; } set { } }
192192
}
193193
";
194194
var expected = new[]
@@ -197,7 +197,7 @@ public class TestClass { }
197197
this.CSharpDiagnostic().WithLocation(10, 5).WithArguments("conversion", "operator"),
198198
this.CSharpDiagnostic().WithLocation(11, 19).WithArguments("property", "conversion"),
199199
this.CSharpDiagnostic().WithLocation(13, 17).WithArguments("method", "struct"),
200-
this.CSharpDiagnostic().WithLocation(14, 19).WithArguments("indexer", "method")
200+
this.CSharpDiagnostic().WithLocation(15, 19).WithArguments("indexer", "class")
201201
};
202202

203203
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
@@ -229,14 +229,18 @@ public async Task TestTypeMemberOrderWrongOrderInterfaceAsync()
229229
{
230230
string testCode = @"public interface OuterType
231231
{
232-
event System.Action TestEvent;
233232
string TestProperty { get; set; }
233+
event System.Action TestEvent;
234234
void TestMethod ();
235235
string this[string arg] { get; set; }
236236
}
237237
";
238238

239-
var expected = this.CSharpDiagnostic().WithLocation(6, 12).WithArguments("indexer", "method");
239+
DiagnosticResult[] expected =
240+
{
241+
this.CSharpDiagnostic().WithLocation(4, 5).WithArguments("event", "property"),
242+
this.CSharpDiagnostic().WithLocation(6, 12).WithArguments("indexer", "method")
243+
};
240244

241245
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
242246

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1202UnitTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,10 @@ public void B()
532532

533533
var fixedCode = @"public class TestClass
534534
{
535-
536535
public void B()
537536
{
538537
}
538+
539539
private static void A()
540540
{
541541
}
@@ -740,10 +740,10 @@ public MyClass(int a)
740740
var fixedCode = @"
741741
class MyClass
742742
{
743-
744743
public MyClass(int a)
745744
{
746745
}
746+
747747
private MyClass()
748748
{
749749
}
@@ -904,10 +904,10 @@ public interface TestInterface
904904
905905
public class TestClass : TestInterface
906906
{
907-
908907
void TestInterface.SomeMethod()
909908
{
910909
}
910+
911911
private static void ExampleMethod()
912912
{
913913
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1203UnitTests.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
namespace StyleCop.Analyzers.Test.OrderingRules
55
{
66
using System.Collections.Generic;
7-
using System.Collections.Immutable;
87
using System.Threading;
98
using System.Threading.Tasks;
10-
using Microsoft.CodeAnalysis;
119
using Microsoft.CodeAnalysis.CodeFixes;
1210
using Microsoft.CodeAnalysis.Diagnostics;
1311
using StyleCop.Analyzers.OrderingRules;
@@ -275,12 +273,12 @@ public async Task TestCodeFixAsync()
275273

276274
var fixedTestCode = @"public class Foo
277275
{
278-
279-
public const string After2 = ""test"";
280276
private const string Before1 = ""test"";
281277
282278
public const string Before2 = ""test"";
283279
280+
public const string After2 = ""test"";
281+
284282
private const string After1 = ""test"";
285283
286284
private int field1;
@@ -325,12 +323,12 @@ public async Task TestCodeFixWithCommentsAsync()
325323

326324
var fixedTestCode = @"public class Foo
327325
{
328-
329-
public const string After2 = ""test"";
330326
private const string Before1 = ""test"";
331327
332328
public const string Before2 = ""test"";
333329
330+
public const string After2 = ""test"";
331+
334332
//Comment on this field
335333
private const string After1 = ""test"";
336334
@@ -447,19 +445,12 @@ protected override CodeFixProvider GetCSharpCodeFixProvider()
447445
return new ElementOrderCodeFixProvider();
448446
}
449447

450-
protected override Solution CreateSolution(ProjectId projectId, string language)
448+
protected override IEnumerable<string> GetDisabledDiagnostics()
451449
{
452-
Solution solution = base.CreateSolution(projectId, language);
453450
if (this.suppressSA1202)
454451
{
455-
Project project = solution.GetProject(projectId);
456-
CompilationOptions options = project.CompilationOptions;
457-
ImmutableDictionary<string, ReportDiagnostic> specificOptions = options.SpecificDiagnosticOptions;
458-
options = options.WithSpecificDiagnosticOptions(specificOptions.Add(SA1202ElementsMustBeOrderedByAccess.DiagnosticId, ReportDiagnostic.Suppress));
459-
solution = solution.WithProjectCompilationOptions(projectId, options);
452+
yield return SA1202ElementsMustBeOrderedByAccess.DiagnosticId;
460453
}
461-
462-
return solution;
463454
}
464455
}
465456
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1204UnitTests.cs

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,74 @@ class TestClass1 { }
376376
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
377377
}
378378

379+
/// <summary>
380+
/// Verifies that the analyzer will properly handle wrongly ordered classes with a file header.
381+
/// </summary>
382+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
383+
[Fact]
384+
public async Task TestWronglyOrderedClassesWithFileHeaderAsync()
385+
{
386+
var testCode = @"// Test header
387+
388+
public class TestClass1 { }
389+
public static class TestClass2 { }
390+
";
391+
392+
var expected = this.CSharpDiagnostic().WithLocation(4, 21).WithArguments("public", "classes");
393+
394+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
395+
396+
var fixedCode = @"// Test header
397+
398+
public static class TestClass2 { }
399+
public class TestClass1 { }
400+
";
401+
402+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
403+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
404+
}
405+
406+
/// <summary>
407+
/// Verifies that the analyzer will properly handle wrongly ordered classes with a file header and XMN comments.
408+
/// </summary>
409+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
410+
[Fact]
411+
public async Task TestWronglyOrderedClassesWithFileHeaderAndXmlCommentsAsync()
412+
{
413+
var testCode = @"// Test header
414+
415+
/// <summary>
416+
/// Test comment 1
417+
/// </summary>
418+
public class TestClass1 { }
419+
420+
/// <summary>
421+
/// Test comment 2
422+
/// </summary>
423+
public static class TestClass2 { }
424+
";
425+
426+
var expected = this.CSharpDiagnostic().WithLocation(11, 21).WithArguments("public", "classes");
427+
428+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
429+
430+
var fixedCode = @"// Test header
431+
432+
/// <summary>
433+
/// Test comment 2
434+
/// </summary>
435+
public static class TestClass2 { }
436+
437+
/// <summary>
438+
/// Test comment 1
439+
/// </summary>
440+
public class TestClass1 { }
441+
";
442+
443+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
444+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
445+
}
446+
379447
/// <summary>
380448
/// Verifies that the analyzer will properly handle ordering within a namespace.
381449
/// </summary>
@@ -429,7 +497,7 @@ class MyClass2
429497
static MyClass2()
430498
{
431499
}
432-
500+
433501
public MyClass2()
434502
{
435503
}
@@ -443,10 +511,10 @@ public MyClass2()
443511
var fixedCode = @"
444512
class MyClass1
445513
{
446-
447514
static MyClass1()
448515
{
449516
}
517+
450518
public MyClass1()
451519
{
452520
}
@@ -457,7 +525,7 @@ class MyClass2
457525
static MyClass2()
458526
{
459527
}
460-
528+
461529
public MyClass2()
462530
{
463531
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1214UnitTests.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,8 @@ public class FooInner
243243
var fixTestCode = @"
244244
public class Foo
245245
{
246-
247246
public static readonly int u = 5;
248247
249-
public static readonly int j = 0;
250248
public string s = ""qwe"";
251249
private static readonly int i = 0;
252250
@@ -256,17 +254,47 @@ public void Ff() {}
256254
257255
public class FooInner
258256
{
259-
private static readonly int e = 1;
260257
private int aa = 0;
261258
public static readonly int t = 2;
259+
private static readonly int e = 1;
262260
private static int z = 999;
263261
}
262+
263+
public static readonly int j = 0;
264264
}";
265+
265266
await this.VerifyCSharpDiagnosticAsync(fixTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
266-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
267267
await this.VerifyCSharpFixAsync(testCode, fixTestCode).ConfigureAwait(false);
268268
}
269269

270+
[Fact]
271+
public async Task TestStaticReadonlyPrecededByClassAsync()
272+
{
273+
var testCode = @"
274+
public class Foo
275+
{
276+
public static readonly int u = 5;
277+
public string s = ""qwe"";
278+
private static readonly int i = 0;
279+
280+
public void Ff() {}
281+
282+
public static string s2 = ""qwe"";
283+
284+
public class FooInner
285+
{
286+
private int aa = 0;
287+
public static readonly int t = 2;
288+
private static readonly int e = 1;
289+
private static int z = 999;
290+
}
291+
292+
public static readonly int j = 0;
293+
}";
294+
295+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
296+
}
297+
270298
[Fact]
271299
public async Task TestStaticFollowedByReadOnlyAtDifferentAccessLevelAsync()
272300
{

0 commit comments

Comments
 (0)