Skip to content

Commit 604cfb5

Browse files
committed
Convert documentation rules to the new test helpers
1 parent 45a4680 commit 604cfb5

46 files changed

Lines changed: 1389 additions & 1675 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ namespace StyleCop.Analyzers.Test.CSharp7.DocumentationRules
88
using StyleCop.Analyzers.DocumentationRules;
99
using StyleCop.Analyzers.Test.DocumentationRules;
1010
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.DocumentationRules.PropertySummaryDocumentationAnalyzer,
13+
StyleCop.Analyzers.DocumentationRules.PropertySummaryDocumentationCodeFixProvider>;
1114

1215
public class SA1623CSharp7UnitTests : SA1623UnitTests
1316
{
@@ -71,11 +74,8 @@ public class TestClass
7174
}}
7275
";
7376

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);
77+
var expected = Diagnostic(PropertySummaryDocumentationAnalyzer.SA1623Descriptor).WithLocation(9, 7 + accessibility.Length + type.Length).WithArguments(expectedArgument);
78+
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
7979
}
8080
}
8181
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ namespace StyleCop.Analyzers.Test.CSharp7.DocumentationRules
88
using StyleCop.Analyzers.DocumentationRules;
99
using StyleCop.Analyzers.Test.DocumentationRules;
1010
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.DocumentationRules.PropertySummaryDocumentationAnalyzer,
13+
StyleCop.Analyzers.DocumentationRules.PropertySummaryDocumentationCodeFixProvider>;
1114

1215
public class SA1624CSharp7UnitTests : SA1624UnitTests
1316
{
@@ -69,11 +72,8 @@ public class TestClass
6972
}}
7073
";
7174

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);
75+
var expected = Diagnostic(PropertySummaryDocumentationAnalyzer.SA1624Descriptor).WithLocation(9, 7 + accessibility.Length + type.Length).WithArguments(expectedArgument1, expectedArgument2);
76+
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
7777
}
7878
}
7979
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/FileHeaderTestBase.cs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
66
using System.Collections.Generic;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
using Microsoft.CodeAnalysis.Diagnostics;
9+
using Microsoft.CodeAnalysis;
1010
using StyleCop.Analyzers.DocumentationRules;
11+
using StyleCop.Analyzers.Test.Verifiers;
1112
using TestHelper;
1213
using Xunit;
1314

1415
/// <summary>
1516
/// Base class for file header related unit tests.
1617
/// </summary>
17-
public abstract class FileHeaderTestBase : CodeFixVerifier
18+
public abstract class FileHeaderTestBase
1819
{
19-
private const string TestSettings = @"
20+
private const string DefaultTestSettings = @"
2021
{
2122
""settings"": {
2223
""documentationRules"": {
@@ -27,6 +28,9 @@ public abstract class FileHeaderTestBase : CodeFixVerifier
2728
}
2829
";
2930

31+
protected static DiagnosticResult[] EmptyDiagnosticResults
32+
=> StyleCopCodeFixVerifier<FileHeaderAnalyzers, FileHeaderCodeFixProvider>.EmptyDiagnosticResults;
33+
3034
/// <summary>
3135
/// Verifies that a file header with an autogenerated comment will not produce a diagnostic message.
3236
/// </summary>
@@ -86,22 +90,40 @@ namespace Bar
8690
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
8791
}
8892

89-
/// <inheritdoc/>
90-
protected override IEnumerable<string> GetDisabledDiagnostics()
91-
{
92-
yield return FileHeaderAnalyzers.SA1639Descriptor.Id;
93-
}
93+
protected static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor)
94+
=> StyleCopCodeFixVerifier<FileHeaderAnalyzers, FileHeaderCodeFixProvider>.Diagnostic(descriptor);
9495

95-
/// <inheritdoc/>
96-
protected override string GetSettings()
97-
{
98-
return TestSettings;
99-
}
96+
protected virtual string GetSettings()
97+
=> DefaultTestSettings;
98+
99+
protected virtual IEnumerable<string> GetDisabledDiagnostics()
100+
=> new[] { FileHeaderAnalyzers.SA1639Descriptor.Id };
100101

101-
/// <inheritdoc/>
102-
protected sealed override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
102+
protected Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult expected, CancellationToken cancellationToken)
103+
=> this.VerifyCSharpFixAsync(source, new[] { expected }, fixedSource: null, cancellationToken);
104+
105+
protected Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
106+
=> this.VerifyCSharpFixAsync(source, expected, fixedSource: null, cancellationToken);
107+
108+
protected Task VerifyCSharpFixAsync(string source, DiagnosticResult expected, string fixedSource, CancellationToken cancellationToken)
109+
=> this.VerifyCSharpFixAsync(source, new[] { expected }, fixedSource, cancellationToken);
110+
111+
protected Task VerifyCSharpFixAsync(string source, DiagnosticResult[] expected, string fixedSource, CancellationToken cancellationToken)
112+
=> this.VerifyCSharpFixAsync(source, expected, fixedSource, EmptyDiagnosticResults, cancellationToken);
113+
114+
protected Task VerifyCSharpFixAsync(string source, DiagnosticResult[] expected, string fixedSource, DiagnosticResult[] remainingDiagnostics, CancellationToken cancellationToken)
103115
{
104-
yield return new FileHeaderAnalyzers();
116+
var test = new StyleCopCodeFixVerifier<FileHeaderAnalyzers, FileHeaderCodeFixProvider>.CSharpTest
117+
{
118+
TestCode = source,
119+
FixedCode = fixedSource,
120+
Settings = this.GetSettings(),
121+
};
122+
123+
test.ExpectedDiagnostics.AddRange(expected);
124+
test.RemainingDiagnostics.AddRange(remainingDiagnostics);
125+
test.DisabledDiagnostics.AddRange(this.GetDisabledDiagnostics());
126+
return test.RunAsync(cancellationToken);
105127
}
106128
}
107129
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/InheritdocCodeFixProviderUnitTests.cs

Lines changed: 74 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,24 @@
33

44
namespace StyleCop.Analyzers.Test.DocumentationRules
55
{
6-
using System.Collections.Generic;
76
using System.Threading;
87
using System.Threading.Tasks;
98
using Microsoft.CodeAnalysis;
10-
using Microsoft.CodeAnalysis.CodeFixes;
11-
using Microsoft.CodeAnalysis.Diagnostics;
129
using StyleCop.Analyzers.DocumentationRules;
13-
using TestHelper;
1410
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.DocumentationRules.SA1600ElementsMustBeDocumented,
13+
StyleCop.Analyzers.DocumentationRules.InheritdocCodeFixProvider>;
1514

1615
/// <summary>
1716
/// This class contains unit tests for <see cref="InheritdocCodeFixProvider"/>.
1817
/// </summary>
19-
public class InheritdocCodeFixProviderUnitTests : CodeFixVerifier
18+
public class InheritdocCodeFixProviderUnitTests
2019
{
2120
private static readonly DiagnosticDescriptor SA1600 = new SA1600ElementsMustBeDocumented().SupportedDiagnostics[0];
2221
private static readonly DiagnosticDescriptor CS1591 =
2322
new DiagnosticDescriptor(nameof(CS1591), "Title", "Missing XML comment for publicly visible type or member '{0}'", "Category", DiagnosticSeverity.Error, AnalyzerConstants.EnabledByDefault);
2423

25-
private DiagnosticDescriptor descriptor = SA1600;
26-
2724
[Theory]
2825
[InlineData(false, null, "string TestMember { get; set; }")]
2926
[InlineData(false, null, "string TestMember() { return null; }")]
@@ -66,27 +63,32 @@ public override {memberData}
6663
}}
6764
";
6865

69-
if (compilerWarning)
70-
{
71-
this.descriptor = CS1591;
72-
}
66+
var descriptor = compilerWarning ? CS1591 : SA1600;
7367

74-
DiagnosticResult[] expected =
68+
var test = new CSharpTest
7569
{
76-
this.CSharpDiagnostic(this.descriptor).WithArguments("ParentClass").WithLocation(2, 14),
77-
this.CSharpDiagnostic(this.descriptor).WithArguments("ChildClass").WithLocation(10, 14),
78-
this.CSharpDiagnostic(this.descriptor).WithArguments(memberName).WithLocation(12, 40),
70+
TestCode = testCode,
71+
ExpectedDiagnostics =
72+
{
73+
Diagnostic(descriptor).WithArguments("ParentClass").WithLocation(2, 14),
74+
Diagnostic(descriptor).WithArguments("ChildClass").WithLocation(10, 14),
75+
Diagnostic(descriptor).WithArguments(memberName).WithLocation(12, 40),
76+
},
77+
FixedCode = fixedCode,
78+
RemainingDiagnostics =
79+
{
80+
Diagnostic(descriptor).WithArguments("ParentClass").WithLocation(2, 14),
81+
Diagnostic(descriptor).WithArguments("ChildClass").WithLocation(10, 14),
82+
},
7983
};
8084

81-
DiagnosticResult[] expectedFixed =
85+
if (compilerWarning)
8286
{
83-
this.CSharpDiagnostic(this.descriptor).WithArguments("ParentClass").WithLocation(2, 14),
84-
this.CSharpDiagnostic(this.descriptor).WithArguments("ChildClass").WithLocation(10, 14),
85-
};
87+
test.DisabledDiagnostics.Add(SA1600.Id);
88+
test.SolutionTransforms.Add(SetCompilerDocumentationWarningToError);
89+
}
8690

87-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
88-
await this.VerifyCSharpDiagnosticAsync(fixedCode, expectedFixed, CancellationToken.None).ConfigureAwait(false);
89-
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
91+
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
9092
}
9193

9294
[Theory]
@@ -131,27 +133,32 @@ public class ChildClass : IParent
131133
}}
132134
";
133135

134-
if (compilerWarning)
135-
{
136-
this.descriptor = CS1591;
137-
}
136+
var descriptor = compilerWarning ? CS1591 : SA1600;
138137

139-
DiagnosticResult[] expected =
138+
var test = new CSharpTest
140139
{
141-
this.CSharpDiagnostic(this.descriptor).WithArguments("IParent").WithLocation(2, 18),
142-
this.CSharpDiagnostic(this.descriptor).WithArguments("ChildClass").WithLocation(10, 14),
143-
this.CSharpDiagnostic(this.descriptor).WithArguments(memberName).WithLocation(12, 31),
140+
TestCode = testCode,
141+
ExpectedDiagnostics =
142+
{
143+
Diagnostic(descriptor).WithArguments("IParent").WithLocation(2, 18),
144+
Diagnostic(descriptor).WithArguments("ChildClass").WithLocation(10, 14),
145+
Diagnostic(descriptor).WithArguments(memberName).WithLocation(12, 31),
146+
},
147+
FixedCode = fixedCode,
148+
RemainingDiagnostics =
149+
{
150+
Diagnostic(descriptor).WithArguments("IParent").WithLocation(2, 18),
151+
Diagnostic(descriptor).WithArguments("ChildClass").WithLocation(10, 14),
152+
},
144153
};
145154

146-
DiagnosticResult[] expectedFixed =
155+
if (compilerWarning)
147156
{
148-
this.CSharpDiagnostic(this.descriptor).WithArguments("IParent").WithLocation(2, 18),
149-
this.CSharpDiagnostic(this.descriptor).WithArguments("ChildClass").WithLocation(10, 14),
150-
};
157+
test.DisabledDiagnostics.Add(SA1600.Id);
158+
test.SolutionTransforms.Add(SetCompilerDocumentationWarningToError);
159+
}
151160

152-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
153-
await this.VerifyCSharpDiagnosticAsync(fixedCode, expectedFixed, CancellationToken.None).ConfigureAwait(false);
154-
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
161+
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
155162
}
156163

157164
[Theory]
@@ -176,15 +183,19 @@ public class ChildClass : ParentClass
176183
}}
177184
";
178185

179-
DiagnosticResult[] expected =
186+
await new CSharpTest
180187
{
181-
this.CSharpDiagnostic(this.descriptor).WithLocation(2, 14),
182-
this.CSharpDiagnostic(this.descriptor).WithLocation(10, 14),
183-
this.CSharpDiagnostic(this.descriptor).WithLocation(12, 35),
184-
};
185-
186-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
187-
await this.VerifyCSharpFixAsync(testCode, testCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
188+
TestCode = testCode,
189+
ExpectedDiagnostics =
190+
{
191+
Diagnostic(SA1600).WithLocation(2, 14),
192+
Diagnostic(SA1600).WithLocation(10, 14),
193+
Diagnostic(SA1600).WithLocation(12, 35),
194+
},
195+
FixedCode = testCode,
196+
NumberOfIncrementalIterations = 1,
197+
NumberOfFixAllIterations = 1,
198+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
188199
}
189200

190201
[Theory]
@@ -209,59 +220,30 @@ public class ChildClass : ParentClass
209220
}}
210221
";
211222

212-
var fixedCode = testCode;
213-
214-
DiagnosticResult[] expected =
223+
await new CSharpTest
215224
{
216-
this.CSharpDiagnostic(this.descriptor).WithLocation(2, 14),
217-
this.CSharpDiagnostic(this.descriptor).WithLocation(10, 14),
218-
this.CSharpDiagnostic(this.descriptor).WithLocation(12, 35),
219-
};
220-
221-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
222-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
223-
}
224-
225-
/// <inheritdoc/>
226-
protected override IEnumerable<string> GetDisabledDiagnostics()
227-
{
228-
if (this.descriptor == CS1591)
229-
{
230-
yield return SA1600.Id;
231-
}
225+
TestCode = testCode,
226+
ExpectedDiagnostics =
227+
{
228+
Diagnostic(SA1600).WithLocation(2, 14),
229+
Diagnostic(SA1600).WithLocation(10, 14),
230+
Diagnostic(SA1600).WithLocation(12, 35),
231+
},
232+
FixedCode = testCode,
233+
NumberOfIncrementalIterations = 1,
234+
NumberOfFixAllIterations = 1,
235+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
232236
}
233237

234-
/// <inheritdoc/>
235-
protected override Project ApplyCompilationOptions(Project project)
238+
private static Solution SetCompilerDocumentationWarningToError(Solution solution, ProjectId projectId)
236239
{
237-
project = base.ApplyCompilationOptions(project);
238-
239-
if (this.descriptor == CS1591)
240-
{
241-
var supportedDiagnosticsSpecificOptions = new Dictionary<string, ReportDiagnostic>();
242-
supportedDiagnosticsSpecificOptions.Add(CS1591.Id, ReportDiagnostic.Error);
240+
var project = solution.GetProject(projectId);
243241

244-
// update the project compilation options
245-
var modifiedSpecificDiagnosticOptions = project.CompilationOptions.SpecificDiagnosticOptions.SetItem(CS1591.Id, ReportDiagnostic.Error);
246-
var modifiedCompilationOptions = project.CompilationOptions.WithSpecificDiagnosticOptions(modifiedSpecificDiagnosticOptions);
242+
// update the project compilation options
243+
var modifiedSpecificDiagnosticOptions = project.CompilationOptions.SpecificDiagnosticOptions.SetItem(CS1591.Id, ReportDiagnostic.Error);
244+
var modifiedCompilationOptions = project.CompilationOptions.WithSpecificDiagnosticOptions(modifiedSpecificDiagnosticOptions);
247245

248-
Solution solution = project.Solution.WithProjectCompilationOptions(project.Id, modifiedCompilationOptions);
249-
project = solution.GetProject(project.Id);
250-
}
251-
252-
return project;
253-
}
254-
255-
/// <inheritdoc/>
256-
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
257-
{
258-
yield return new SA1600ElementsMustBeDocumented();
259-
}
260-
261-
/// <inheritdoc/>
262-
protected override CodeFixProvider GetCSharpCodeFixProvider()
263-
{
264-
return new InheritdocCodeFixProvider();
246+
return solution.WithProjectCompilationOptions(projectId, modifiedCompilationOptions);
265247
}
266248
}
267249
}

0 commit comments

Comments
 (0)