Skip to content

Commit 40992ba

Browse files
Added test cases for checking the default behaviour of SA1402.
1 parent d1ef224 commit 40992ba

9 files changed

Lines changed: 144 additions & 42 deletions

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1402ForBlockDeclarationUnitTestsBase.cs

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public abstract class SA1402ForBlockDeclarationUnitTestsBase : FileMayOnlyContai
1717
{
1818
public override bool SupportsCodeFix => true;
1919

20-
private bool ConfigureAsNonTopLevelType { get; set; } = false;
20+
protected SA1402SettingsConfiguration SettingsConfiguration { get; set; } = SA1402SettingsConfiguration.ConfigureAsTopLevelType;
21+
22+
protected abstract bool IsConfiguredAsTopLevelTypeByDefault { get; }
2123

2224
[Fact]
2325
public async Task TestTwoGenericElementsAsync()
@@ -53,7 +55,7 @@ public async Task TestTwoGenericElementsAsync()
5355
[Fact]
5456
public async Task TestTwoElementsWithRuleDisabledAsync()
5557
{
56-
this.ConfigureAsNonTopLevelType = true;
58+
this.SettingsConfiguration = SA1402SettingsConfiguration.ConfigureAsNonTopLevelType;
5759

5860
var testCode = @"%1 Foo
5961
{
@@ -67,6 +69,46 @@ public async Task TestTwoElementsWithRuleDisabledAsync()
6769
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
6870
}
6971

72+
[Fact]
73+
public async Task TestTwoElementsWithDefaultRuleConfigurationAsync()
74+
{
75+
this.SettingsConfiguration = SA1402SettingsConfiguration.KeepDefaultConfiguration;
76+
77+
var testCode = @"%1 Foo
78+
{
79+
}
80+
%1 Bar
81+
{
82+
}";
83+
84+
var fixedCode = new[]
85+
{
86+
@"%1 Foo
87+
{
88+
}
89+
",
90+
@"%1 Bar
91+
{
92+
}"
93+
};
94+
95+
testCode = testCode.Replace("%1", this.Keyword);
96+
fixedCode = fixedCode.Select(c => c.Replace("%1", this.Keyword)).ToArray();
97+
98+
if (this.IsConfiguredAsTopLevelTypeByDefault)
99+
{
100+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(4, this.Keyword.Length + 2);
101+
102+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
103+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
104+
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
105+
}
106+
else
107+
{
108+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
109+
}
110+
}
111+
70112
[Fact]
71113
public async Task TestPartialTypesAsync()
72114
{
@@ -155,24 +197,7 @@ public async Task TestNestedTypesAsync()
155197

156198
protected override string GetSettings()
157199
{
158-
var keywords = new List<string> { "class", "interface", "struct", "enum", "delegate" };
159-
if (this.ConfigureAsNonTopLevelType)
160-
{
161-
keywords.Remove(this.Keyword);
162-
}
163-
164-
var keywordsStr = string.Join(", ", keywords.Select(x => "\"" + x + "\""));
165-
166-
var settings = $@"
167-
{{
168-
""settings"": {{
169-
""maintainabilityRules"": {{
170-
""topLevelTypes"": [{keywordsStr}]
171-
}}
172-
}}
173-
}}";
174-
175-
return settings;
200+
return this.SettingsConfiguration.GetSettings(this.Keyword);
176201
}
177202

178203
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1402ForClassUnitTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ namespace StyleCop.Analyzers.Test.MaintainabilityRules
66
public class SA1402ForClassUnitTests : SA1402ForBlockDeclarationUnitTestsBase
77
{
88
public override string Keyword => "class";
9+
10+
protected override bool IsConfiguredAsTopLevelTypeByDefault => true;
911
}
1012
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1402ForDelegateUnitTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,19 @@ public async Task TestTwoGenericElementsAsync()
6767
[Fact]
6868
public async Task TestTwoElementsWithRuleDisabledAsync()
6969
{
70-
this.ConfigureAsNonTopLevelType = true;
70+
this.SettingsConfiguration = SA1402SettingsConfiguration.ConfigureAsNonTopLevelType;
71+
72+
var testCode = @"public delegate void Foo();
73+
public delegate void Bar();
74+
";
75+
76+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
77+
}
78+
79+
[Fact]
80+
public async Task TestTwoElementsWithDefaultRuleConfigurationAsync()
81+
{
82+
this.SettingsConfiguration = SA1402SettingsConfiguration.KeepDefaultConfiguration;
7183

7284
var testCode = @"public delegate void Foo();
7385
public delegate void Bar();

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1402ForEnumUnitTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,25 @@ enum Bar
6161
[Fact]
6262
public async Task TestTwoElementsWithRuleDisabledAsync()
6363
{
64-
this.ConfigureAsNonTopLevelType = true;
64+
this.SettingsConfiguration = SA1402SettingsConfiguration.ConfigureAsNonTopLevelType;
65+
66+
var testCode = @"enum Foo
67+
{
68+
A, B, C
69+
}
70+
enum Bar
71+
{
72+
D, E
73+
}
74+
";
75+
76+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
77+
}
78+
79+
[Fact]
80+
public async Task TestTwoElementsWithDefaultRuleConfigurationAsync()
81+
{
82+
this.SettingsConfiguration = SA1402SettingsConfiguration.KeepDefaultConfiguration;
6583

6684
var testCode = @"enum Foo
6785
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1402ForInterfaceUnitTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ namespace StyleCop.Analyzers.Test.MaintainabilityRules
66
public class SA1402ForInterfaceUnitTests : SA1402ForBlockDeclarationUnitTestsBase
77
{
88
public override string Keyword => "interface";
9+
10+
protected override bool IsConfiguredAsTopLevelTypeByDefault => false;
911
}
1012
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1402ForNonBlockDeclarationUnitTestsBase.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace StyleCop.Analyzers.Test.MaintainabilityRules
55
{
66
using System.Collections.Generic;
7-
using System.Linq;
87
using Microsoft.CodeAnalysis.CodeFixes;
98
using Microsoft.CodeAnalysis.Diagnostics;
109
using StyleCop.Analyzers.MaintainabilityRules;
@@ -14,28 +13,11 @@ public abstract class SA1402ForNonBlockDeclarationUnitTestsBase : CodeFixVerifie
1413
{
1514
public abstract string Keyword { get; }
1615

17-
protected bool ConfigureAsNonTopLevelType { get; set; } = false;
16+
protected SA1402SettingsConfiguration SettingsConfiguration { get; set; } = SA1402SettingsConfiguration.ConfigureAsTopLevelType;
1817

1918
protected override string GetSettings()
2019
{
21-
var keywords = new List<string> { "class", "interface", "struct", "enum", "delegate" };
22-
if (this.ConfigureAsNonTopLevelType)
23-
{
24-
keywords.Remove(this.Keyword);
25-
}
26-
27-
var keywordsStr = string.Join(", ", keywords.Select(x => "\"" + x + "\""));
28-
29-
var settings = $@"
30-
{{
31-
""settings"": {{
32-
""maintainabilityRules"": {{
33-
""topLevelTypes"": [{keywordsStr}]
34-
}}
35-
}}
36-
}}";
37-
38-
return settings;
20+
return this.SettingsConfiguration.GetSettings(this.Keyword);
3921
}
4022

4123
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1402ForStructUnitTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ namespace StyleCop.Analyzers.Test.MaintainabilityRules
66
public class SA1402ForStructUnitTests : SA1402ForBlockDeclarationUnitTestsBase
77
{
88
public override string Keyword => "struct";
9+
10+
protected override bool IsConfiguredAsTopLevelTypeByDefault => false;
911
}
1012
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.MaintainabilityRules
5+
{
6+
using System.Collections.Generic;
7+
using System.Diagnostics.CodeAnalysis;
8+
using System.Linq;
9+
10+
public enum SA1402SettingsConfiguration
11+
{
12+
/// <summary>
13+
/// Provide no custom settings
14+
/// </summary>
15+
KeepDefaultConfiguration,
16+
17+
/// <summary>
18+
/// Provide custom settings that configure the tested type as being a top level type
19+
/// </summary>
20+
ConfigureAsTopLevelType,
21+
22+
/// <summary>
23+
/// Provide custom settings that configure the tested type as not being a top level type
24+
/// </summary>
25+
ConfigureAsNonTopLevelType,
26+
}
27+
28+
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1649:File name must match first type name.", Justification = "Extension method for enum above.")]
29+
public static class SA1402SettingsConfigurationExtensions
30+
{
31+
public static string GetSettings(this SA1402SettingsConfiguration configuration, string keyword)
32+
{
33+
if (configuration == SA1402SettingsConfiguration.KeepDefaultConfiguration)
34+
{
35+
return null;
36+
}
37+
38+
var keywords = new List<string> { "class", "interface", "struct", "enum", "delegate" };
39+
if (configuration == SA1402SettingsConfiguration.ConfigureAsNonTopLevelType)
40+
{
41+
keywords.Remove(keyword);
42+
}
43+
44+
var keywordsStr = string.Join(", ", keywords.Select(x => "\"" + x + "\""));
45+
46+
var settings = $@"
47+
{{
48+
""settings"": {{
49+
""maintainabilityRules"": {{
50+
""topLevelTypes"": [{keywordsStr}]
51+
}}
52+
}}
53+
}}";
54+
55+
return settings;
56+
}
57+
}
58+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@
250250
<Compile Include="MaintainabilityRules\SA1402ForInterfaceUnitTests.cs" />
251251
<Compile Include="MaintainabilityRules\SA1402ForClassUnitTests.cs" />
252252
<Compile Include="MaintainabilityRules\SA1402ForBlockDeclarationUnitTestsBase.cs" />
253+
<Compile Include="MaintainabilityRules\SA1402SettingsConfiguration.cs" />
253254
<Compile Include="MaintainabilityRules\SA1403UnitTests.cs" />
254255
<Compile Include="MaintainabilityRules\SA1404UnitTests.cs" />
255256
<Compile Include="MaintainabilityRules\SA1405UnitTests.cs" />

0 commit comments

Comments
 (0)