Skip to content

Commit

Permalink
Merge pull request #3694 from sharwell/sa1202-more-types
Browse files Browse the repository at this point in the history
Update SA1202 to support interfaces (C# 8) and records (C# 9, 10)
  • Loading branch information
sharwell committed Sep 12, 2023
2 parents b209e4e + 4ba311b commit 890236e
Show file tree
Hide file tree
Showing 18 changed files with 302 additions and 179 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.CSharp10.NamingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp9.NamingRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.NamingRules.SA1313ParameterNamesMustBeginWithLowerCaseLetter,
Expand Down Expand Up @@ -42,12 +40,7 @@ public R(int a, int b)
}}
";

await new CSharpTest()
{
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet60,
TestCode = testCode,
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace StyleCop.Analyzers.Test.CSharp10.ReadabilityRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp9.ReadabilityRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.ReadabilityRules.SA1101PrefixLocalCallsWithThis,
Expand All @@ -29,11 +29,7 @@ public bool Method(Test arg)
}
}";

await new CSharpTest()
{
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet60,
TestCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace StyleCop.Analyzers.Test.CSharp11.OrderingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp10.OrderingRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.OrderingRules.SA1206DeclarationKeywordsMustFollowOrder,
Expand Down Expand Up @@ -43,17 +43,13 @@ internal struct SomeStruct
public required int Field;
}";

await new CSharpTest()
DiagnosticResult[] expected =
{
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet70,
TestCode = testCode,
FixedCode = fixedCode,
ExpectedDiagnostics =
{
Diagnostic().WithLocation(0).WithArguments("public", "required"),
Diagnostic().WithLocation(1).WithArguments("public", "required"),
},
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
Diagnostic().WithLocation(0).WithArguments("public", "required"),
Diagnostic().WithLocation(1).WithArguments("public", "required"),
};

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp10.SpacingRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;

using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
Expand Down Expand Up @@ -40,11 +40,7 @@ public void TestMethod(List<int> x)
}}
";

await new CSharpTest()
{
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50,
TestCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp10.SpacingRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.SpacingRules.SA1012OpeningBracesMustBeSpacedCorrectly,
Expand Down Expand Up @@ -40,20 +40,16 @@ void M(string[] a)
}
";

await new CSharpTest()
DiagnosticResult[] expected =
{
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50,
TestCode = testCode,
ExpectedDiagnostics =
{
// Opening brace should not be preceded by a space
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
// Opening brace should not be preceded by a space
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),

// Opening brace should be preceded by a space
Diagnostic().WithLocation(1).WithArguments(string.Empty, "preceded"),
},
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
// Opening brace should be preceded by a space
Diagnostic().WithLocation(1).WithArguments(string.Empty, "preceded"),
};

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
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
@@ -1,15 +1,12 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.CSharp9.NamingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.NamingRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.NamingRules.SA1300ElementMustBeginWithUpperCaseLetter,
Expand Down Expand Up @@ -40,15 +37,7 @@ public R(int a, int b)
}
";

var test = new CSharpTest()
{
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50,
TestCode = testCode,
FixedCode = fixedCode,
};
var expectedDiagnostics = this.GetExpectedResultTestPositionalRecord1();
test.TestState.ExpectedDiagnostics.AddRange(expectedDiagnostics);
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
await VerifyCSharpFixAsync(testCode, this.GetExpectedResultTestPositionalRecord1(), fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
Expand All @@ -74,12 +63,7 @@ public R(int a, int b)
}
";

await new CSharpTest()
{
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50,
TestCode = testCode,
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

protected virtual DiagnosticResult[] GetExpectedResultTestPositionalRecord1()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.CSharp9.NamingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.NamingRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.NamingRules.SA1313ParameterNamesMustBeginWithLowerCaseLetter,
Expand Down Expand Up @@ -41,12 +39,7 @@ public R(int a, int b)
}
";

await new CSharpTest()
{
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50,
TestCode = testCode,
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace StyleCop.Analyzers.Test.CSharp9.ReadabilityRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.ReadabilityRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.ReadabilityRules.SA1101PrefixLocalCallsWithThis,
Expand All @@ -31,11 +31,7 @@ public A UpdateA(A value)
}
}";

await new CSharpTest()
{
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50,
TestCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.CSharp9.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.SpacingRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;
using static StyleCop.Analyzers.SpacingRules.SA1023DereferenceAndAccessOfSymbolsMustBeSpacedCorrectly;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
Expand Down Expand Up @@ -68,21 +66,17 @@ public async Task TestFunctionPointerTypeInvalidSpacingAsync()
}
";

await new CSharpTest()
DiagnosticResult[] expected =
{
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50,
TestCode = testCode,
FixedCode = fixedCode,
ExpectedDiagnostics =
{
Diagnostic(DescriptorNotPreceded).WithLocation(0),
Diagnostic(DescriptorNotFollowed).WithLocation(1),
Diagnostic(DescriptorNotPreceded).WithLocation(2),
Diagnostic(DescriptorFollowed).WithLocation(3),
Diagnostic(DescriptorNotPreceded).WithLocation(4),
Diagnostic(DescriptorFollowed).WithLocation(5),
},
}.RunAsync().ConfigureAwait(false);
Diagnostic(DescriptorNotPreceded).WithLocation(0),
Diagnostic(DescriptorNotFollowed).WithLocation(1),
Diagnostic(DescriptorNotPreceded).WithLocation(2),
Diagnostic(DescriptorFollowed).WithLocation(3),
Diagnostic(DescriptorNotPreceded).WithLocation(4),
Diagnostic(DescriptorFollowed).WithLocation(5),
};

await VerifyCSharpFixAsync(testCode, expected, fixedCode, 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

0 comments on commit 890236e

Please sign in to comment.