From 58dbaedefb752b8feb19ef18e6ac52d67fc9ff13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sat, 24 Jun 2023 16:17:20 +0200 Subject: [PATCH 01/14] Improve coverage for SA1305 by adding test for a variable in a native methods class --- .../NamingRules/SA1305UnitTests.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1305UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1305UnitTests.cs index fb10c4f40..09c465d71 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1305UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1305UnitTests.cs @@ -62,6 +62,21 @@ public void TestMethod() await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); } + [Fact] + public async Task TestVariableNamesInNativeClassMethodAsync() + { + var testCode = @" +public class TypeNameNativeMethods +{ + public void MethodName() + { + bool abX; + } +}"; + + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } + [Fact] public async Task TestInvalidFieldNamesAreReportedAsync() { From 2406656b5b8ebd353630e947455a8ada20a3c4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sat, 24 Jun 2023 16:31:46 +0200 Subject: [PATCH 02/14] Improve coverage for SA1304 by adding test for a field in a native methods class --- .../NamingRules/SA1304UnitTests.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1304UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1304UnitTests.cs index 75d7314dc..b68a150e8 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1304UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1304UnitTests.cs @@ -61,6 +61,17 @@ public async Task TestProtectedReadonlyFieldStartingWithUpperCaseAsync() await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); } + [Fact] + public async Task TestFieldInNativeClassMethodAsync() + { + var testCode = @"public class FooNativeMethods +{ + internal readonly string bar = ""baz""; +}"; + + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } + [Fact] public async Task TestInternalReadonlyFieldStartingWithLowerCaseAsync() { From 487935c8e3fb6d98e828dc14b918c25b5de19286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sat, 24 Jun 2023 16:58:25 +0200 Subject: [PATCH 03/14] Improve coverage for SA1300 by changing a test to verify a property (instead of a field) in a native methods class, since this analyzer does not handle fields --- .../NamingRules/SA1300CSharp10UnitTests.cs | 2 +- .../StyleCop.Analyzers.Test/NamingRules/SA1300UnitTests.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/NamingRules/SA1300CSharp10UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/NamingRules/SA1300CSharp10UnitTests.cs index 41ee9feee..e29dc4c69 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/NamingRules/SA1300CSharp10UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/NamingRules/SA1300CSharp10UnitTests.cs @@ -58,7 +58,7 @@ public async Task TestAllowedLowerCaseFileScopedNamespaceIsNotReportedAsync() } [Fact] - public async Task TestLowerCaseComlicatedFileScopedNamespaceAsync() + public async Task TestLowerCaseComplicatedFileScopedNamespaceAsync() { var testCode = @"namespace {|#0:test|}.{|#1:foo|}.{|#2:bar|};"; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1300UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1300UnitTests.cs index 2146b87cd..f306feac4 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1300UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1300UnitTests.cs @@ -71,7 +71,7 @@ public async Task TestAllowedLowerCaseNamespaceIsNotReportedAsync() } [Fact] - public async Task TestLowerCaseComlicatedNamespaceAsync() + public async Task TestLowerCaseComplicatedNamespaceAsync() { var testCode = @"namespace test.foo.bar { @@ -789,7 +789,7 @@ public async Task TestNativeMethodsExceptionAsync() { var testCode = @"public class TestNativeMethods { -public string test; +public string test { get; set; } }"; await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); From 88cf7a175cd9509b0de54fa12360ba63da011947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sun, 25 Jun 2023 07:41:23 +0200 Subject: [PATCH 04/14] Improve coverage for SA1600 by adding tests for partial types --- .../DocumentationRules/SA1600UnitTests.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1600UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1600UnitTests.cs index a786b3f8c..491c38fbf 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1600UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1600UnitTests.cs @@ -46,12 +46,28 @@ public async Task TestClassWithoutDocumentationAsync() await this.TestTypeWithoutDocumentationAsync("class", false).ConfigureAwait(false); } + [Fact] + public async Task TestPartialClassWithoutDocumentationAsync() + { + await this.TestTypeDeclarationDocumentationAsync("class", "partial", false, false).ConfigureAwait(false); + await this.TestTypeDeclarationDocumentationAsync("class", "internal partial", false, false).ConfigureAwait(false); + await this.TestTypeDeclarationDocumentationAsync("class", "public partial", false, false).ConfigureAwait(false); + } + [Fact] public async Task TestStructWithoutDocumentationAsync() { await this.TestTypeWithoutDocumentationAsync("struct", false).ConfigureAwait(false); } + [Fact] + public async Task TestPartialStructWithoutDocumentationAsync() + { + await this.TestTypeDeclarationDocumentationAsync("struct", "partial", false, false).ConfigureAwait(false); + await this.TestTypeDeclarationDocumentationAsync("struct", "internal partial", false, false).ConfigureAwait(false); + await this.TestTypeDeclarationDocumentationAsync("struct", "public partial", false, false).ConfigureAwait(false); + } + [Fact] public async Task TestEnumWithoutDocumentationAsync() { @@ -64,6 +80,14 @@ public async Task TestInterfaceWithoutDocumentationAsync() await this.TestTypeWithoutDocumentationAsync("interface", true).ConfigureAwait(false); } + [Fact] + public async Task TestPartialInterfaceWithoutDocumentationAsync() + { + await this.TestTypeDeclarationDocumentationAsync("interface", "partial", false, false).ConfigureAwait(false); + await this.TestTypeDeclarationDocumentationAsync("interface", "internal partial", false, false).ConfigureAwait(false); + await this.TestTypeDeclarationDocumentationAsync("interface", "public partial", false, false).ConfigureAwait(false); + } + [Fact] public async Task TestClassWithDocumentationAsync() { From db6febe33f559695fb0ec4711912f67f0f90d755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Wed, 28 Jun 2023 06:53:38 +0200 Subject: [PATCH 05/14] Improve coverage for SA1201 by adding tests with record structs. --- .../OrderingRules/SA1201CSharp10UnitTests.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1201CSharp10UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1201CSharp10UnitTests.cs index 61b745521..7b2719c51 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1201CSharp10UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1201CSharp10UnitTests.cs @@ -51,5 +51,42 @@ public struct {|#1:FooStruct|} { } await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); } + + [Fact] + public async Task TestOuterOrderWithRecordStructCorrectOrderAsync() + { + var testCode = @"namespace Foo { } +public delegate void bar(); +public enum TestEnum { } +public interface IFoo { } +public record struct FooStruct { } +public class FooClass { } +"; + + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpDiagnosticAsync("namespace OuterNamespace { " + testCode + " }", DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } + + [Fact] + public async Task TestOuterOrderWithRecordStructWrongOrderAsync() + { + var testCode = @" +namespace Foo { } +public enum TestEnum { } +public delegate void {|#0:bar|}(); +public interface IFoo { } +public class FooClass { } +public record struct {|#1:FooStruct|} { } +"; + + var expected = new[] + { + Diagnostic().WithLocation(0).WithArguments("delegate", "enum"), + Diagnostic().WithLocation(1).WithArguments("record struct", "class"), + }; + + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpDiagnosticAsync("namespace OuterNamespace { " + testCode + " }", expected, CancellationToken.None).ConfigureAwait(false); + } } } From eb324ac0b1e028302701b208b518bf9b0aafa4cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Thu, 29 Jun 2023 20:00:01 +0200 Subject: [PATCH 06/14] Improve coverage for SA1404 by adding test for other attributes besides suppressions --- .../MaintainabilityRules/SA1404UnitTests.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1404UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1404UnitTests.cs index 038030a92..6981afc5e 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1404UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1404UnitTests.cs @@ -410,5 +410,19 @@ public void Bar() }; await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); } + + [Fact] + public async Task TestOtherAttributeAsync() + { + var testCode = @"public class Foo +{ + [System.Obsolete(""Method is obsolete!"")] + public void Bar() + { + } +}"; + + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } } } From 120a6a5a8fe577651847c9c428c0604cac3089de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Fri, 30 Jun 2023 17:24:33 +0200 Subject: [PATCH 07/14] Improve coverage for SA1404 by adding test for full attribute name --- .../MaintainabilityRules/SA1404UnitTests.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1404UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1404UnitTests.cs index 6981afc5e..fa93d26ae 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1404UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1404UnitTests.cs @@ -16,17 +16,19 @@ namespace StyleCop.Analyzers.Test.MaintainabilityRules public class SA1404UnitTests { - [Fact] - public async Task TestSuppressionWithStringLiteralAsync() + [Theory] + [InlineData("SuppressMessage")] + [InlineData("SuppressMessageAttribute")] + public async Task TestSuppressionWithStringLiteralAsync(string attributeName) { - var testCode = @"public class Foo -{ - [System.Diagnostics.CodeAnalysis.SuppressMessage(null, null, Justification = ""a justification"")] + var testCode = $@"public class Foo +{{ + [System.Diagnostics.CodeAnalysis.{attributeName}(null, null, Justification = ""a justification"")] public void Bar() - { + {{ - } -}"; + }} +}}"; await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); } From 7885adbdf430e2f69ff2e8b93e5408bc2115f65f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sat, 1 Jul 2023 08:22:44 +0200 Subject: [PATCH 08/14] Improve coverage for SA1213 by adding test with only one accessor --- .../OrderingRules/SA1213UnitTests.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1213UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1213UnitTests.cs index 108d17801..a02ee3d21 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1213UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1213UnitTests.cs @@ -242,5 +242,23 @@ public event EventHandler NameChanged await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); } + + [Fact] + public async Task TestOnlyAddAccessorAsync() + { + var testCode = @" +using System; +public class Foo +{ + private EventHandler nameChanged; + + public event EventHandler {|CS0065:NameChanged|} + { + add { this.nameChanged += value; } + } +}"; + + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } } } From 92d66c600194849ff1de72500474de84abf11bd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sat, 1 Jul 2023 10:38:58 +0200 Subject: [PATCH 09/14] Improve coverage for SA1110 by adding tests for attributes with qualified names --- .../ReadabilityRules/SA1110UnitTests.cs | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1110UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1110UnitTests.cs index 9e1832e8a..913e715f7 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1110UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1110UnitTests.cs @@ -603,31 +603,33 @@ public void Bar() await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); } - [Fact] - public async Task TestAttributeOpeningParenthesisOnTheNextLineAsync() + [Theory] + [InlineData("Conditional")] + [InlineData("System.Diagnostics.Conditional")] + public async Task TestAttributeOpeningParenthesisOnTheNextLineAsync(string attributeName) { - var testCode = @" + var testCode = $@" using System.Diagnostics; public class Foo -{ -[Conditional -(""DEBUG""), Conditional +{{ +[{attributeName} +(""DEBUG""), {attributeName} (""TEST1"")] public void Baz() - { - } -}"; - var fixedCode = @" + {{ + }} +}}"; + var fixedCode = $@" using System.Diagnostics; public class Foo -{ -[Conditional( -""DEBUG""), Conditional( +{{ +[{attributeName}( +""DEBUG""), {attributeName}( ""TEST1"")] public void Baz() - { - } -}"; + {{ + }} +}}"; DiagnosticResult[] expected = { @@ -638,19 +640,21 @@ public void Baz() await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } - [Fact] - public async Task TestAttributeOpeningParenthesisOnTheSameLineAsync() + [Theory] + [InlineData("Conditional")] + [InlineData("System.Diagnostics.Conditional")] + public async Task TestAttributeOpeningParenthesisOnTheSameLineAsync(string attributeName) { - var testCode = @" + var testCode = $@" using System.Diagnostics; public class Foo -{ - [Conditional(""DEBUG""), Conditional(""TEST1"")] +{{ + [{attributeName}(""DEBUG""), {attributeName}(""TEST1"")] public void Baz() - { + {{ - } -}"; + }} +}}"; await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); } From 551d825b24d02a5a85a66afb29882ca573762a32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sun, 13 Aug 2023 08:09:04 +0200 Subject: [PATCH 10/14] Improve coverage for SA1010 by adding test for creation of implicitly typed arrays --- .../SpacingRules/SA1010UnitTests.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1010UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1010UnitTests.cs index 317f3f0f9..2917f06d4 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1010UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1010UnitTests.cs @@ -216,5 +216,23 @@ public void TestMethod(IDictionary items) var expected = Diagnostic(DescriptorNotPreceded).WithLocation(8, 62); await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false); } + + // NOTE: This case is handled by SA1026, so it's intentionally allowed here + [Fact] + public async Task TestImplicitlyTypedArrayCreationAsync() + { + var testCode = @" +namespace TestNamespace +{ + public class TestClass + { + public void TestMethod() + { + var x = new [] { 0, 1 }; + } + } +}"; + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } } } From 5a680811a7f6bfa08a60ce6717078c688bc200c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sat, 11 Nov 2023 11:52:12 +0100 Subject: [PATCH 11/14] Improve coverage for AccessLevelHelper by adding tests for the GetAccessLevel method --- .../HelperTests/AccessLevelHelperTests.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/HelperTests/AccessLevelHelperTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/HelperTests/AccessLevelHelperTests.cs index 5c3ef3598..ccf5da2eb 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/HelperTests/AccessLevelHelperTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/HelperTests/AccessLevelHelperTests.cs @@ -6,7 +6,9 @@ namespace StyleCop.Analyzers.Test.HelperTests { using System; + using System.Linq; using Microsoft.CodeAnalysis; + using Microsoft.CodeAnalysis.CSharp; using StyleCop.Analyzers.Helpers; using Xunit; @@ -50,5 +52,36 @@ public void TestCombineEffectiveAccessibility() Assert.Equal(Accessibility.Private, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Private, Accessibility.Public)); Assert.Equal(Accessibility.Public, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Public, Accessibility.Public)); } + + [Fact] + public void TestGetAccessLevel() + { + Check(AccessLevel.NotSpecified); + Check(AccessLevel.NotSpecified, SyntaxKind.PartialKeyword); + + Check(AccessLevel.Private, SyntaxKind.PrivateKeyword); + Check(AccessLevel.Private, SyntaxKind.OverrideKeyword, SyntaxKind.PrivateKeyword); + + Check(AccessLevel.PrivateProtected, SyntaxKind.PrivateKeyword, SyntaxKind.ProtectedKeyword); + Check(AccessLevel.PrivateProtected, SyntaxKind.ProtectedKeyword, SyntaxKind.PrivateKeyword); + + Check(AccessLevel.Protected, SyntaxKind.ProtectedKeyword); + Check(AccessLevel.Protected, SyntaxKind.VirtualKeyword, SyntaxKind.ProtectedKeyword); + + Check(AccessLevel.ProtectedInternal, SyntaxKind.ProtectedKeyword, SyntaxKind.InternalKeyword); + Check(AccessLevel.ProtectedInternal, SyntaxKind.InternalKeyword, SyntaxKind.ProtectedKeyword); + + Check(AccessLevel.Internal, SyntaxKind.InternalKeyword); + Check(AccessLevel.Internal, SyntaxKind.AbstractKeyword, SyntaxKind.InternalKeyword); + + Check(AccessLevel.Public, SyntaxKind.PublicKeyword); + Check(AccessLevel.Public, SyntaxKind.AsyncKeyword, SyntaxKind.PublicKeyword); + + static void Check(AccessLevel expectedAccessLevel, params SyntaxKind[] tokenKinds) + { + var tokenList = SyntaxFactory.TokenList(tokenKinds.Select(SyntaxFactory.Token)); + Assert.Equal(expectedAccessLevel, AccessLevelHelper.GetAccessLevel(tokenList)); + } + } } } From 5fd6a8fcf11a6f89f4b2320c6bfe54d8a5f242f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Wed, 15 Nov 2023 20:01:06 +0100 Subject: [PATCH 12/14] Improve coverage for SA1131 by updating tests so they pass for the right reason --- .../ReadabilityRules/SA1131UnitTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1131UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1131UnitTests.cs index 72cd1013f..c6d55f217 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1131UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1131UnitTests.cs @@ -373,7 +373,7 @@ public class TypeName public void Test() {{ int j = 6; - bool b = j {@operator} i; + bool b = i {@operator} j; }} }}"; await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); @@ -396,7 +396,7 @@ public class TypeName public void Test() {{ int j = 6; - bool b = j {@operator} i; + bool b = i {@operator} j; }} }}"; await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); @@ -419,7 +419,7 @@ public class TypeName public void Test() {{ int j = 6; - bool b = j {@operator} i; + bool b = i {@operator} j; }} }}"; await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); From baebf75a3d9a734161eb43e34e4a52554a6a921d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Tue, 19 Dec 2023 21:14:04 +0100 Subject: [PATCH 13/14] Fix review comments --- .../StyleCop.Analyzers.Test/NamingRules/SA1304UnitTests.cs | 2 +- .../StyleCop.Analyzers.Test/NamingRules/SA1305UnitTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1304UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1304UnitTests.cs index b68a150e8..89616ceb8 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1304UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1304UnitTests.cs @@ -62,7 +62,7 @@ public async Task TestProtectedReadonlyFieldStartingWithUpperCaseAsync() } [Fact] - public async Task TestFieldInNativeClassMethodAsync() + public async Task TestFieldInNativeMethodsClassAsync() { var testCode = @"public class FooNativeMethods { diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1305UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1305UnitTests.cs index 09c465d71..5736deac5 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1305UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1305UnitTests.cs @@ -63,7 +63,7 @@ public void TestMethod() } [Fact] - public async Task TestVariableNamesInNativeClassMethodAsync() + public async Task TestVariableNamesInNativeMethodsClassAsync() { var testCode = @" public class TypeNameNativeMethods From fbeef562e2c07bb535d14ab82c29b47dafaf549f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Tue, 19 Dec 2023 21:38:30 +0100 Subject: [PATCH 14/14] Generalize tests for SA1600 concerning partial types --- .../DocumentationRules/SA1600UnitTests.cs | 27 +++++-------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1600UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1600UnitTests.cs index 7fd1fc16d..4e5561632 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1600UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1600UnitTests.cs @@ -56,28 +56,13 @@ public async Task TestBaseTypeWithDocumentationAsync(string type) await this.TestTypeWithDocumentationAsync(type).ConfigureAwait(false); } - [Fact] - public async Task TestPartialClassWithoutDocumentationAsync() - { - await this.TestTypeDeclarationDocumentationAsync("class", "partial", false, false).ConfigureAwait(false); - await this.TestTypeDeclarationDocumentationAsync("class", "internal partial", false, false).ConfigureAwait(false); - await this.TestTypeDeclarationDocumentationAsync("class", "public partial", false, false).ConfigureAwait(false); - } - - [Fact] - public async Task TestPartialStructWithoutDocumentationAsync() - { - await this.TestTypeDeclarationDocumentationAsync("struct", "partial", false, false).ConfigureAwait(false); - await this.TestTypeDeclarationDocumentationAsync("struct", "internal partial", false, false).ConfigureAwait(false); - await this.TestTypeDeclarationDocumentationAsync("struct", "public partial", false, false).ConfigureAwait(false); - } - - [Fact] - public async Task TestPartialInterfaceWithoutDocumentationAsync() + [Theory] + [MemberData(nameof(CommonMemberData.TypeDeclarationKeywords), MemberType = typeof(CommonMemberData))] + public async Task TestPartialTypeWithoutDocumentationAsync(string type) { - await this.TestTypeDeclarationDocumentationAsync("interface", "partial", false, false).ConfigureAwait(false); - await this.TestTypeDeclarationDocumentationAsync("interface", "internal partial", false, false).ConfigureAwait(false); - await this.TestTypeDeclarationDocumentationAsync("interface", "public partial", false, false).ConfigureAwait(false); + await this.TestTypeDeclarationDocumentationAsync(type, "partial", false, false).ConfigureAwait(false); + await this.TestTypeDeclarationDocumentationAsync(type, "internal partial", false, false).ConfigureAwait(false); + await this.TestTypeDeclarationDocumentationAsync(type, "public partial", false, false).ConfigureAwait(false); } [Fact]