Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage improvements #3675

Merged
merged 15 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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()
{
Expand All @@ -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()
{
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 TestFieldInNativeClassMethodAsync()
bjornhellander marked this conversation as resolved.
Show resolved Hide resolved
{
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 TestVariableNamesInNativeClassMethodAsync()
bjornhellander marked this conversation as resolved.
Show resolved Hide resolved
{
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 class Foo

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