Skip to content

Commit 2119cc7

Browse files
authored
Merge pull request #4032 from sharwell/readonly-members
Add tests for readonly members in C# 8
2 parents b72b3f7 + 83f51e0 commit 2119cc7

File tree

19 files changed

+689
-3
lines changed

19 files changed

+689
-3
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/DocumentationRules/SA1649CSharp8UnitTests.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,114 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp8.DocumentationRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.DocumentationRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.CustomDiagnosticVerifier<StyleCop.Analyzers.DocumentationRules.SA1649FileNameMustMatchTypeName>;
712

813
public partial class SA1649CSharp8UnitTests : SA1649CSharp7UnitTests
914
{
15+
[Fact]
16+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
17+
public async Task VerifyReadonlyMembersDoNotAffectCorrectFileNameAsync()
18+
{
19+
var testCode = @"
20+
namespace TestNamespace
21+
{
22+
public readonly struct TestType
23+
{
24+
private readonly int value;
25+
26+
public TestType(int value)
27+
{
28+
this.value = value;
29+
}
30+
31+
public readonly int GetValue() => this.value;
32+
33+
public int Property
34+
{
35+
readonly get => this.value;
36+
set => _ = value;
37+
}
38+
39+
public int this[int index]
40+
{
41+
readonly get => this.value + index;
42+
set => _ = value - index;
43+
}
44+
}
45+
}
46+
";
47+
48+
await VerifyCSharpDiagnosticAsync(fileName: "TestType.cs", testCode, testSettings: null, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
49+
}
50+
51+
[Fact]
52+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
53+
public async Task VerifyWrongFileNameWithReadonlyMembersAsync()
54+
{
55+
var testCode = @"
56+
namespace TestNamespace
57+
{
58+
public readonly struct {|#0:TestType|}
59+
{
60+
private readonly int value;
61+
62+
public TestType(int value)
63+
{
64+
this.value = value;
65+
}
66+
67+
public readonly int GetValue() => this.value;
68+
69+
public int Property
70+
{
71+
readonly get => this.value;
72+
set => _ = value;
73+
}
74+
75+
public int this[int index]
76+
{
77+
readonly get => this.value + index;
78+
set => _ = value - index;
79+
}
80+
}
81+
}
82+
";
83+
var fixedCode = @"
84+
namespace TestNamespace
85+
{
86+
public readonly struct TestType
87+
{
88+
private readonly int value;
89+
90+
public TestType(int value)
91+
{
92+
this.value = value;
93+
}
94+
95+
public readonly int GetValue() => this.value;
96+
97+
public int Property
98+
{
99+
readonly get => this.value;
100+
set => _ = value;
101+
}
102+
103+
public int this[int index]
104+
{
105+
readonly get => this.value + index;
106+
set => _ = value - index;
107+
}
108+
}
109+
}
110+
";
111+
112+
var expectedDiagnostic = Diagnostic().WithLocation(0);
113+
await VerifyCSharpFixAsync(oldFileName: "WrongFileName.cs", testCode, StyleCopSettings, expectedDiagnostic, newFileName: "TestType.cs", fixedCode, CancellationToken.None).ConfigureAwait(false);
114+
}
10115
}
11116
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/MaintainabilityRules/SA1401CSharp8UnitTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,44 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp8.MaintainabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopDiagnosticVerifier<
12+
StyleCop.Analyzers.MaintainabilityRules.SA1401FieldsMustBePrivate>;
713

814
public partial class SA1401CSharp8UnitTests : SA1401CSharp7UnitTests
915
{
16+
[Theory]
17+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
18+
[InlineData("public")]
19+
[InlineData("internal")]
20+
[InlineData("protected")]
21+
[InlineData("protected internal")]
22+
public async Task TestClassWithReadonlyFieldAsync(string accessModifier)
23+
{
24+
var testCode = $@"public class Foo
25+
{{
26+
{accessModifier} readonly string [|bar|];
27+
}}";
28+
29+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
30+
}
31+
32+
[Fact]
33+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
34+
public async Task VerifyReadonlyStructMembersAreIgnoredAsync()
35+
{
36+
var testCode = @"
37+
public struct S
38+
{
39+
public readonly int Property { get; }
40+
public readonly void Method() { }
41+
}";
42+
43+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
44+
}
1045
}
1146
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/NamingRules/SA1304CSharp8UnitTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,54 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp8.NamingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.NamingRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.NamingRules.SA1304NonPrivateReadonlyFieldsMustBeginWithUpperCaseLetter,
13+
StyleCop.Analyzers.NamingRules.RenameToUpperCaseCodeFixProvider>;
714

815
public partial class SA1304CSharp8UnitTests : SA1304CSharp7UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
19+
public async Task TestReadonlyMembersDoNotTriggerAsync()
20+
{
21+
var testCode = @"public struct TestStruct
22+
{
23+
public readonly void Method() { }
24+
public int Property { readonly get; set; }
25+
}";
26+
27+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
28+
}
29+
30+
[Fact]
31+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
32+
public async Task TestPublicReadonlyFieldHandledBySA1307Async()
33+
{
34+
var testCode = @"public struct TestStruct
35+
{
36+
public readonly int bar;
37+
public readonly void Method() { }
38+
}";
39+
40+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
41+
}
42+
43+
[Fact]
44+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
45+
public async Task TestPublicStaticReadonlyFieldHandledBySA1311Async()
46+
{
47+
var testCode = @"public struct TestStruct
48+
{
49+
public static readonly int bar;
50+
public readonly void Method() { }
51+
}";
52+
53+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
54+
}
1055
}
1156
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/NamingRules/SA1307CSharp8UnitTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,70 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp8.NamingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.NamingRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.NamingRules.SA1307AccessibleFieldsMustBeginWithUpperCaseLetter,
13+
StyleCop.Analyzers.NamingRules.RenameToUpperCaseCodeFixProvider>;
714

815
public partial class SA1307CSharp8UnitTests : SA1307CSharp7UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
19+
public async Task TestReadonlyFieldInStructAsync()
20+
{
21+
var testCode = @"public struct TestStruct
22+
{
23+
public readonly int {|#0:bar|};
24+
public readonly void Method() { }
25+
}";
26+
27+
var fixedCode = @"public struct TestStruct
28+
{
29+
public readonly int Bar;
30+
public readonly void Method() { }
31+
}";
32+
33+
DiagnosticResult expected = Diagnostic().WithLocation(0).WithArguments("bar");
34+
35+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
36+
}
37+
38+
[Fact]
39+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
40+
public async Task TestStaticReadonlyFieldInStructAsync()
41+
{
42+
var testCode = @"public struct TestStruct
43+
{
44+
public static readonly int {|#0:bar|};
45+
public readonly void Method() { }
46+
}";
47+
48+
var fixedCode = @"public struct TestStruct
49+
{
50+
public static readonly int Bar;
51+
public readonly void Method() { }
52+
}";
53+
54+
DiagnosticResult expected = Diagnostic().WithLocation(0).WithArguments("bar");
55+
56+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
57+
}
58+
59+
[Fact]
60+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
61+
public async Task TestReadonlyMembersWithoutFieldsAsync()
62+
{
63+
var testCode = @"public struct TestStruct
64+
{
65+
public readonly void Method() { }
66+
public int Property { readonly get; set; }
67+
}";
68+
69+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
70+
}
1071
}
1172
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/NamingRules/SA1311CSharp8UnitTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,47 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp8.NamingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.NamingRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.NamingRules.SA1311StaticReadonlyFieldsMustBeginWithUpperCaseLetter,
13+
StyleCop.Analyzers.NamingRules.RenameToUpperCaseCodeFixProvider>;
714

815
public partial class SA1311CSharp8UnitTests : SA1311CSharp7UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
19+
public async Task TestStaticReadonlyFieldInStructWithReadonlyMembersAsync()
20+
{
21+
var testCode = @"public struct TestStruct
22+
{
23+
public static readonly string [|bar|] = string.Empty;
24+
public readonly void Method() { }
25+
}";
26+
27+
var fixedCode = @"public struct TestStruct
28+
{
29+
public static readonly string Bar = string.Empty;
30+
public readonly void Method() { }
31+
}";
32+
33+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
34+
}
35+
36+
[Fact]
37+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
38+
public async Task TestReadonlyMembersWithoutStaticReadonlyFieldsAsync()
39+
{
40+
var testCode = @"public struct TestStruct
41+
{
42+
public readonly void Method() { }
43+
public int Property { readonly get; set; }
44+
}";
45+
46+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
47+
}
1048
}
1149
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/NamingRules/SX1309CSharp8UnitTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,62 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp8.NamingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.NamingRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.NamingRules.SX1309FieldNamesMustBeginWithUnderscore,
13+
StyleCop.Analyzers.NamingRules.SX1309CodeFixProvider>;
714

815
public partial class SX1309CSharp8UnitTests : SX1309CSharp7UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
19+
public async Task TestReadonlyMembersDoNotTriggerAsync()
20+
{
21+
var testCode = @"public struct TestStruct
22+
{
23+
public readonly void Method() { }
24+
public int Property { readonly get; set; }
25+
}";
26+
27+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
28+
}
29+
30+
[Fact]
31+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
32+
public async Task TestPrivateReadonlyFieldRequiresUnderscoreAsync()
33+
{
34+
var testCode = @"public struct TestStruct
35+
{
36+
private readonly int {|#0:value|};
37+
public readonly void Method() { }
38+
}";
39+
40+
var fixedCode = @"public struct TestStruct
41+
{
42+
private readonly int _value;
43+
public readonly void Method() { }
44+
}";
45+
46+
DiagnosticResult expected = Diagnostic().WithLocation(0).WithArguments("value");
47+
48+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
49+
}
50+
51+
[Fact]
52+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
53+
public async Task TestStaticReadonlyFieldIgnoredAsync()
54+
{
55+
var testCode = @"public struct TestStruct
56+
{
57+
private static readonly int value = 0;
58+
public readonly void Method() { }
59+
}";
60+
61+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
62+
}
1063
}
1164
}

0 commit comments

Comments
 (0)