Skip to content

Commit e4650ca

Browse files
committed
Add expression-bodied property accessor tests for SA1623 and SA1624
1 parent 726a739 commit e4650ca

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/DocumentationRules/SA1623CSharp7UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp7.DocumentationRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using StyleCop.Analyzers.DocumentationRules;
69
using StyleCop.Analyzers.Test.DocumentationRules;
10+
using Xunit;
711

812
public class SA1623CSharp7UnitTests : SA1623UnitTests
913
{
14+
/// <summary>
15+
/// Verifies that property documentation that does not start with the appropriate text will result in a
16+
/// diagnostic. This test extends
17+
/// <see cref="SA1623UnitTests.VerifyDocumentationWithWrongStartingTextWillProduceDiagnosticAsync"/> for the
18+
/// purpose of testing expression-bodied property accessors.
19+
/// </summary>
20+
/// <param name="accessibility">The accessibility of the property.</param>
21+
/// <param name="type">The type to use for the property.</param>
22+
/// <param name="accessors">The accessors for the property.</param>
23+
/// <param name="expectedArgument">The expected argument for the diagnostic message.</param>
24+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
25+
[Theory]
26+
[InlineData("public", "int", "{ get => 0; set => this.field = value; }", "Gets or sets")]
27+
[InlineData("public", "int", "{ get => 0; protected set => this.field = value; }", "Gets or sets")]
28+
[InlineData("public", "int", "{ get => 0; protected internal set => this.field = value; }", "Gets or sets")]
29+
[InlineData("public", "int", "{ get => 0; internal set => this.field = value; }", "Gets")]
30+
[InlineData("public", "int", "{ get => 0; private set => this.field = value; }", "Gets")]
31+
[InlineData("public", "int", "{ get => 0; }", "Gets")]
32+
[InlineData("public", "int", "{ set => this.field = value; }", "Sets")]
33+
[InlineData("public", "int", "{ internal get => 0; set => this.field = value; }", "Sets")]
34+
[InlineData("public", "int", "{ private get => 0; set => this.field = value; }", "Sets")]
35+
[InlineData("public", "bool", "{ get => true; set => this.field = value; }", "Gets or sets a value indicating whether")]
36+
[InlineData("public", "bool", "{ get => true; }", "Gets a value indicating whether")]
37+
[InlineData("public", "bool", "{ get => true; private set => this.field = value; }", "Gets a value indicating whether")]
38+
[InlineData("public", "bool", "{ set => this.field = value; }", "Sets a value indicating whether")]
39+
[InlineData("public", "bool", "{ private get => false; set => this.field = value; }", "Sets a value indicating whether")]
40+
[InlineData("protected", "int", "{ get => 0; private set => this.field = value; }", "Gets")]
41+
[InlineData("protected", "int", "{ private get => 0; set => this.field = value; }", "Sets")]
42+
[InlineData("protected internal", "int", "{ get => 0; internal set => this.field = value; }", "Gets")]
43+
[InlineData("protected internal", "int", "{ get => 0; private set => this.field = value; }", "Gets")]
44+
[InlineData("protected internal", "int", "{ internal get => 0; set => this.field = value; }", "Sets")]
45+
[InlineData("protected internal", "int", "{ private get => 0; set => this.field = value; }", "Sets")]
46+
[InlineData("internal", "int", "{ get => 0; private set => this.field = value; }", "Gets")]
47+
[InlineData("internal", "int", "{ private get => 0; set => this.field = value; }", "Sets")]
48+
public async Task VerifyDocumentationWithWrongStartingTextWillProduceDiagnosticWithExpressionBodiedAccessorsAsync(string accessibility, string type, string accessors, string expectedArgument)
49+
{
50+
var testCode = $@"
51+
public class TestClass
52+
{{
53+
private object field;
54+
55+
/// <summary>
56+
/// The first test value.
57+
/// </summary>
58+
{accessibility} {type} TestProperty {accessors}
59+
}}
60+
";
61+
62+
var fixedTestCode = $@"
63+
public class TestClass
64+
{{
65+
private object field;
66+
67+
/// <summary>
68+
/// {expectedArgument} the first test value.
69+
/// </summary>
70+
{accessibility} {type} TestProperty {accessors}
71+
}}
72+
";
73+
74+
var expected = this.CSharpDiagnostic(PropertySummaryDocumentationAnalyzer.SA1623Descriptor).WithLocation(9, 7 + accessibility.Length + type.Length).WithArguments(expectedArgument);
75+
76+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
77+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
78+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
79+
}
1080
}
1181
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/DocumentationRules/SA1624CSharp7UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp7.DocumentationRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using StyleCop.Analyzers.DocumentationRules;
69
using StyleCop.Analyzers.Test.DocumentationRules;
10+
using Xunit;
711

812
public class SA1624CSharp7UnitTests : SA1624UnitTests
913
{
14+
/// <summary>
15+
/// Verifies that documentation that starts with the proper text for multiple expression-bodied accessors will
16+
/// produce a diagnostic when one of the accessors has reduced visibility.
17+
/// </summary>
18+
/// <param name="accessibility">The accessibility of the property.</param>
19+
/// <param name="type">The type to use for the property.</param>
20+
/// <param name="accessors">The accessors for the property.</param>
21+
/// <param name="summaryPrefix">The prefix to use in the summary text.</param>
22+
/// <param name="expectedArgument1">The first expected argument for the diagnostic.</param>
23+
/// <param name="expectedArgument2">The second expected argument for the diagnostic.</param>
24+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
25+
[Theory(DisplayName = "Expression-bodied Property Accessor Findings")]
26+
[InlineData("public", "int", "get => 0; internal set => this.field = value;", "Gets or sets", "get", "Gets")]
27+
[InlineData("public", "int", "get => 0; private set => this.field = value;", "Gets or sets", "get", "Gets")]
28+
[InlineData("public", "int", "internal get => 0; set => this.field = value;", "Gets or sets", "set", "Sets")]
29+
[InlineData("public", "int", "private get => 0; set => this.field = value;", "Gets or sets", "set", "Sets")]
30+
[InlineData("public", "bool", "get => false; private set => this.field = value;", "Gets or sets a value indicating whether", "get", "Gets a value indicating whether")]
31+
[InlineData("public", "bool", "private get => false; set => this.field = value;", "Gets or sets a value indicating whether", "set", "Sets a value indicating whether")]
32+
[InlineData("protected", "int", "get => 0; private set => this.field = value;", "Gets or sets", "get", "Gets")]
33+
[InlineData("protected", "int", "private get => 0; set => this.field = value;", "Gets or sets", "set", "Sets")]
34+
[InlineData("protected internal", "int", "get => 0; internal set => this.field = value;", "Gets or sets", "get", "Gets")]
35+
[InlineData("protected internal", "int", "get => 0; private set => this.field = value;", "Gets or sets", "get", "Gets")]
36+
[InlineData("protected internal", "int", "internal get => 0; set => this.field = value;", "Gets or sets", "set", "Sets")]
37+
[InlineData("protected internal", "int", "private get => 0; set => this.field = value;", "Gets or sets", "set", "Sets")]
38+
[InlineData("internal", "int", "get => 0; private set => this.field = value;", "Gets or sets", "get", "Gets")]
39+
[InlineData("internal", "int", "private get => 0; set => this.field = value;", "Gets or sets", "set", "Sets")]
40+
public async Task VerifyThatInvalidDocumentationWillReportDiagnosticWithExpressionBodiedAccessorsAsync(string accessibility, string type, string accessors, string summaryPrefix, string expectedArgument1, string expectedArgument2)
41+
{
42+
var testCode = $@"
43+
public class TestClass
44+
{{
45+
private object field;
46+
47+
/// <summary>
48+
/// {summaryPrefix} the test property.
49+
/// </summary>
50+
{accessibility} {type} TestProperty
51+
{{
52+
{accessors}
53+
}}
54+
}}
55+
";
56+
57+
var fixedTestCode = $@"
58+
public class TestClass
59+
{{
60+
private object field;
61+
62+
/// <summary>
63+
/// {expectedArgument2} the test property.
64+
/// </summary>
65+
{accessibility} {type} TestProperty
66+
{{
67+
{accessors}
68+
}}
69+
}}
70+
";
71+
72+
var expected = this.CSharpDiagnostic(PropertySummaryDocumentationAnalyzer.SA1624Descriptor).WithLocation(9, 7 + accessibility.Length + type.Length).WithArguments(expectedArgument1, expectedArgument2);
73+
74+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
75+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
76+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
77+
}
1078
}
1179
}

0 commit comments

Comments
 (0)