Skip to content

Commit

Permalink
Merge pull request #3675 from bjornhellander/feature/coverage
Browse files Browse the repository at this point in the history
Coverage improvements
  • Loading branch information
sharwell authored Dec 19, 2023
2 parents d91d674 + fbeef56 commit 36cc2cc
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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|};";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public async Task TestBaseTypeWithDocumentationAsync(string type)
await this.TestTypeWithDocumentationAsync(type).ConfigureAwait(false);
}

[Theory]
[MemberData(nameof(CommonMemberData.TypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
public async Task TestPartialTypeWithoutDocumentationAsync(string type)
{
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]
public async Task TestDelegateWithoutDocumentationAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -410,5 +412,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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async Task TestAllowedLowerCaseNamespaceIsNotReportedAsync()
}

[Fact]
public async Task TestLowerCaseComlicatedNamespaceAsync()
public async Task TestLowerCaseComplicatedNamespaceAsync()
{
var testCode = @"namespace test.foo.bar
{
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ public async Task TestProtectedReadonlyFieldStartingWithUpperCaseAsync()
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestFieldInNativeMethodsClassAsync()
{
var testCode = @"public class FooNativeMethods
{
internal readonly string bar = ""baz"";
}";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestInternalReadonlyFieldStartingWithLowerCaseAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ public void TestMethod()
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestVariableNamesInNativeMethodsClassAsync()
{
var testCode = @"
public class TypeNameNativeMethods
{
public void MethodName()
{
bool abX;
}
}";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestInvalidFieldNamesAreReportedAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
{
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,23 @@ public void TestMethod(IDictionary<ulong, string> 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);
}
}
}

0 comments on commit 36cc2cc

Please sign in to comment.