Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,59 @@

namespace StyleCop.Analyzers.Test.CSharp8.OrderingRules
{
using System.Threading;
using System.Threading.Tasks;
using StyleCop.Analyzers.Test.CSharp7.OrderingRules;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.OrderingRules.SA1202ElementsMustBeOrderedByAccess,
StyleCop.Analyzers.OrderingRules.ElementOrderCodeFixProvider>;

public class SA1202CSharp8UnitTests : SA1202CSharp7UnitTests
{
/// <summary>
/// Verifies that the analyzer will properly handle property access levels.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestPropertiesOfInterfaceAsync()
{
var testCode = @"public interface TestClass
{
private string TestProperty1 { get { return """"; } set { } }
protected string {|#0:TestProperty2|} { get; set; }
protected internal string {|#1:TestProperty3|} { get; set; }
internal string {|#2:TestProperty4|} { get; set; }
public string {|#3:TestProperty5|} { get; set; }
string TestProperty0 { get; set; }
}
";

var fixedCode = @"public interface TestClass
{
public string TestProperty5 { get; set; }
string TestProperty0 { get; set; }
internal string TestProperty4 { get; set; }
protected internal string TestProperty3 { get; set; }
protected string TestProperty2 { get; set; }
private string TestProperty1 { get { return """"; } set { } }
}
";

await new CSharpTest
{
TestCode = testCode,
ExpectedDiagnostics =
{
Diagnostic().WithLocation(0).WithArguments("protected", "private"),
Diagnostic().WithLocation(1).WithArguments("protected internal", "protected"),
Diagnostic().WithLocation(2).WithArguments("internal", "protected internal"),
Diagnostic().WithLocation(3).WithArguments("public", "internal"),
},
FixedCode = fixedCode,
NumberOfIncrementalIterations = 5,
NumberOfFixAllIterations = 2,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,37 @@ public static IEnumerable<object[]> DataTypeDeclarationKeywords
}
}

public static IEnumerable<object[]> ReferenceTypeDeclarationKeywords
{
get
{
yield return new[] { "class" };

if (LightupHelpers.SupportsCSharp9)
{
yield return new[] { "record" };
}

if (LightupHelpers.SupportsCSharp10)
{
yield return new[] { "record class" };
}
}
}

public static IEnumerable<object[]> ValueTypeDeclarationKeywords
{
get
{
yield return new[] { "struct" };

if (LightupHelpers.SupportsCSharp10)
{
yield return new[] { "record struct" };
}
}
}

public static IEnumerable<object[]> RecordTypeDeclarationKeywords
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.Test.OrderingRules
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.OrderingRules;
using StyleCop.Analyzers.Test.Helpers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.OrderingRules.SA1202ElementsMustBeOrderedByAccess,
Expand Down Expand Up @@ -139,18 +140,20 @@ public class TestClass2 { }
/// <summary>
/// Verifies that the analyzer will properly handle property access levels.
/// </summary>
/// <param name="keyword">The keyword used to declare the type.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestPropertiesAsync()
[Theory]
[MemberData(nameof(CommonMemberData.ReferenceTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
public async Task TestPropertiesOfClassAsync(string keyword)
{
var testCode = @"public class TestClass
{
private string TestProperty1 { get; set; }
protected string TestProperty2 { get; set; }
protected internal string TestProperty3 { get; set; }
internal string TestProperty4 { get; set; }
public string TestProperty5 { get; set; }
}
var testCode = $@"public {keyword} TestClass
{{
private string TestProperty1 {{ get; set; }}
protected string TestProperty2 {{ get; set; }}
protected internal string TestProperty3 {{ get; set; }}
internal string TestProperty4 {{ get; set; }}
public string TestProperty5 {{ get; set; }}
}}
";

DiagnosticResult[] expected =
Expand All @@ -161,14 +164,48 @@ public async Task TestPropertiesAsync()
Diagnostic().WithLocation(7, 19).WithArguments("public", "internal"),
};

var fixedCode = @"public class TestClass
{
public string TestProperty5 { get; set; }
internal string TestProperty4 { get; set; }
protected internal string TestProperty3 { get; set; }
protected string TestProperty2 { get; set; }
private string TestProperty1 { get; set; }
}
var fixedCode = $@"public {keyword} TestClass
{{
public string TestProperty5 {{ get; set; }}
internal string TestProperty4 {{ get; set; }}
protected internal string TestProperty3 {{ get; set; }}
protected string TestProperty2 {{ get; set; }}
private string TestProperty1 {{ get; set; }}
}}
";

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies that the analyzer will properly handle property access levels.
/// </summary>
/// <param name="keyword">The keyword used to declare the type.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[MemberData(nameof(CommonMemberData.ValueTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
public async Task TestPropertiesOfStructAsync(string keyword)
{
var testCode = $@"public {keyword} TestClass
{{
private string TestProperty1 {{ get; set; }}
internal string {{|#0:TestProperty4|}} {{ get; set; }}
public string {{|#1:TestProperty5|}} {{ get; set; }}
}}
";

DiagnosticResult[] expected =
{
Diagnostic().WithLocation(0).WithArguments("internal", "private"),
Diagnostic().WithLocation(1).WithArguments("public", "internal"),
};

var fixedCode = $@"public {keyword} TestClass
{{
public string TestProperty5 {{ get; set; }}
internal string TestProperty4 {{ get; set; }}
private string TestProperty1 {{ get; set; }}
}}
";

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ internal static AccessLevel GetAccessLevelForOrdering(SyntaxNode member, SyntaxT
{
accessibility = AccessLevel.Internal;
}
else if (member.Parent.IsKind(SyntaxKind.InterfaceDeclaration))
{
accessibility = AccessLevel.Public;
}
else
{
accessibility = AccessLevel.Private;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ internal class SA1202ElementsMustBeOrderedByAccess : DiagnosticAnalyzer
private static readonly DiagnosticDescriptor Descriptor =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.OrderingRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);

private static readonly ImmutableArray<SyntaxKind> TypeDeclarationKinds =
ImmutableArray.Create(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration);

private static readonly ImmutableHashSet<SyntaxKind> MemberKinds = ImmutableHashSet.Create(
SyntaxKind.DelegateDeclaration,
SyntaxKind.EnumDeclaration,
Expand Down Expand Up @@ -89,7 +86,7 @@ public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(CompilationUnitAction, SyntaxKind.CompilationUnit);
context.RegisterSyntaxNodeAction(BaseNamespaceDeclarationAction, SyntaxKinds.BaseNamespaceDeclaration);
context.RegisterSyntaxNodeAction(TypeDeclarationAction, TypeDeclarationKinds);
context.RegisterSyntaxNodeAction(TypeDeclarationAction, SyntaxKinds.TypeDeclaration);
});
}

Expand Down