Skip to content

Commit 45449ed

Browse files
committed
Improved testcoverage by adding testcases and performing tweaks
1 parent b306648 commit 45449ed

11 files changed

Lines changed: 463 additions & 68 deletions

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1619UnitTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,23 @@ public partial class TestClass<T>
165165
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
166166
}
167167

168+
/// <summary>
169+
/// Verifies that a generic partial type without a summary tag in the included documentation will not produce diagnostics.
170+
/// </summary>
171+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
172+
[Fact]
173+
public async Task TestGenericPartialTypeWithoutSummaryInIncludedDocumentationAsync()
174+
{
175+
var testCode = @"
176+
/// <include file='ClassWithoutSummary.xml' path='/TestClass/*'/>
177+
public partial class TestClass<T>
178+
{
179+
}
180+
";
181+
182+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
183+
}
184+
168185
/// <summary>
169186
/// Verifies that a generic partial type without a typeparam in included documentation will flag.
170187
/// </summary>
@@ -212,6 +229,12 @@ protected override Project ApplyCompilationOptions(Project project)
212229
";
213230
resolver.XmlReferences.Add("ClassWithTypeparamDoc.xml", contentClassWithTypeparamDoc);
214231

232+
string contentClassWithoutSummary = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
233+
<TestClass>
234+
</TestClass>
235+
";
236+
resolver.XmlReferences.Add("ClassWithoutSummary.xml", contentClassWithoutSummary);
237+
215238
string contentClassWithoutTypeparamDoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
216239
<TestClass>
217240
<summary>Test class</summary>

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1622UnitTests.cs

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
77
using System.Threading;
88
using System.Threading.Tasks;
99
using Analyzers.DocumentationRules;
10+
using Helpers;
11+
using Microsoft.CodeAnalysis;
1012
using Microsoft.CodeAnalysis.Diagnostics;
1113
using TestHelper;
1214
using Xunit;
1315

1416
/// <summary>
15-
/// This class contains unit tests for <see cref="SA1622GenericTypeParameterDocumentationMustHaveText"/>.
17+
/// This class contains unit tests for SA1622.
1618
/// </summary>
1719
public class SA1622UnitTests : DiagnosticVerifier
1820
{
@@ -217,6 +219,142 @@ public async Task TestTypesWithEmptyParams2Async(string declaration)
217219
await this.VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), expected, CancellationToken.None).ConfigureAwait(false);
218220
}
219221

222+
/// <summary>
223+
/// Verifies that a class with valid typeparameter tags in included documentation will not produce diagnostics.
224+
/// </summary>
225+
/// <param name="typeText">The type specific test text.</param>
226+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
227+
[Theory]
228+
[MemberData(nameof(Types))]
229+
public async Task TestTypesWithValidTypeparameterInIncludedDocumentationAsync(string typeText)
230+
{
231+
var testCode = @"
232+
/// <include file='TypeWithTypeparamsDoc.xml' path='/Foo/*'/>
233+
public ##
234+
";
235+
236+
await this.VerifyCSharpDiagnosticAsync(testCode.Replace("##", typeText), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
237+
}
238+
239+
/// <summary>
240+
/// Verifies that a class with empty typeparameter tags in included documentation will produce diagnostics.
241+
/// </summary>
242+
/// <param name="typeText">The type specific test text.</param>
243+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
244+
[Theory]
245+
[MemberData(nameof(Types))]
246+
public async Task TestTypesWithEmptyTypeparameterInIncludedDocumentationAsync(string typeText)
247+
{
248+
var testCode = @"
249+
/// <include file='TypeWithEmptyTypeparamsDoc.xml' path='/Foo/*'/>
250+
public ##
251+
";
252+
DiagnosticResult[] expected =
253+
{
254+
this.CSharpDiagnostic(GenericTypeParameterDocumentationAnalyzer.SA1622Descriptor).WithLocation(2, 5),
255+
this.CSharpDiagnostic(GenericTypeParameterDocumentationAnalyzer.SA1622Descriptor).WithLocation(2, 5),
256+
};
257+
258+
await this.VerifyCSharpDiagnosticAsync(testCode.Replace("##", typeText), expected, CancellationToken.None).ConfigureAwait(false);
259+
}
260+
261+
/// <summary>
262+
/// Verifies that a method with valid typeparameter tags in the included documentation will produce no diagnostics.
263+
/// </summary>
264+
/// <param name="memberText">The member declaration text that will be used.</param>
265+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
266+
[Theory]
267+
[MemberData(nameof(Members))]
268+
public async Task TestMemberWithValidTypeparameterInIncludedDocumentationAsync(string memberText)
269+
{
270+
var testCode = @"
271+
/// <summary>Test class</summary>
272+
public class TestClass
273+
{
274+
/// <include file='MethodWithTypeparamsDoc.xml' path='/TestClass/Foo/*'/>
275+
public ##
276+
}
277+
";
278+
279+
await this.VerifyCSharpDiagnosticAsync(testCode.Replace("##", memberText), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
280+
}
281+
282+
/// <summary>
283+
/// Verifies that a method with empty typeparameter tags in the included documentation will produce no diagnostics.
284+
/// </summary>
285+
/// <param name="memberText">The member declaration text that will be used.</param>
286+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
287+
[Theory]
288+
[MemberData(nameof(Members))]
289+
public async Task TestMemberWithEmptyTypeparameterInIncludedDocumentationAsync(string memberText)
290+
{
291+
var testCode = @"
292+
/// <summary>Test class</summary>
293+
public class TestClass
294+
{
295+
/// <include file='MethodWithEmptyTypeparamsDoc.xml' path='/TestClass/Foo/*'/>
296+
public ##
297+
}
298+
";
299+
300+
DiagnosticResult[] expected =
301+
{
302+
this.CSharpDiagnostic(GenericTypeParameterDocumentationAnalyzer.SA1622Descriptor).WithLocation(5, 9),
303+
this.CSharpDiagnostic(GenericTypeParameterDocumentationAnalyzer.SA1622Descriptor).WithLocation(5, 9),
304+
};
305+
306+
await this.VerifyCSharpDiagnosticAsync(testCode.Replace("##", memberText), expected, CancellationToken.None).ConfigureAwait(false);
307+
}
308+
309+
protected override Project ApplyCompilationOptions(Project project)
310+
{
311+
var resolver = new TestXmlReferenceResolver();
312+
313+
string contentTypeWithTypeparamDoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
314+
<Foo>
315+
<summary>Test class</summary>
316+
<typeparam name=""Ta"">Param 1</typeparam>
317+
<typeparam name=""Tb"">Param 2</typeparam>
318+
</Foo>
319+
";
320+
resolver.XmlReferences.Add("TypeWithTypeparamsDoc.xml", contentTypeWithTypeparamDoc);
321+
322+
string contentTypeWithEmptyTypeparamDoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
323+
<Foo>
324+
<summary>Test class</summary>
325+
<typeparam name=""Ta""></typeparam>
326+
<typeparam name=""Tb""/>
327+
</Foo>
328+
";
329+
resolver.XmlReferences.Add("TypeWithEmptyTypeparamsDoc.xml", contentTypeWithEmptyTypeparamDoc);
330+
331+
string contentMethodWithTypeparamDoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
332+
<TestClass>
333+
<Foo>
334+
<summary>Test class</summary>
335+
<typeparam name=""Ta"">Param 1</typeparam>
336+
<typeparam name=""Tb"">Param 2</typeparam>
337+
</Foo>
338+
</TestClass>
339+
";
340+
resolver.XmlReferences.Add("MethodWithTypeparamsDoc.xml", contentMethodWithTypeparamDoc);
341+
342+
string contentMethodWithEmptyTypeparamDoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
343+
<TestClass>
344+
<Foo>
345+
<summary>Test class</summary>
346+
<typeparam name=""Ta""/>
347+
<typeparam name=""Tb""></typeparam>
348+
</Foo>
349+
</TestClass>
350+
";
351+
resolver.XmlReferences.Add("MethodWithEmptyTypeparamsDoc.xml", contentMethodWithEmptyTypeparamDoc);
352+
353+
project = base.ApplyCompilationOptions(project);
354+
project = project.WithCompilationOptions(project.CompilationOptions.WithXmlReferenceResolver(resolver));
355+
return project;
356+
}
357+
220358
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
221359
{
222360
yield return new GenericTypeParameterDocumentationAnalyzer();

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1627UnitTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,44 @@ public class ClassName
146146
await this.VerifyCSharpDiagnosticAsync(testCode.Replace("$$", element), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
147147
}
148148

149+
/// <summary>
150+
/// Checks an element with a custom (unsupported) empty element does not give an error.
151+
/// </summary>
152+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
153+
[Fact]
154+
public async Task TestMemberWithCustomElementAsync()
155+
{
156+
var testCode = @"
157+
/// <summary>
158+
/// Class name.
159+
/// </summary>
160+
public class ClassName
161+
{
162+
/// <summary>
163+
/// Join together two strings.
164+
/// </summary>
165+
/// <param name=""first"">First string.</param>
166+
/// <param name=""second"">Second string.</param>
167+
/// <custom1/>
168+
public string JoinStrings(string first, string second) { return first + second; }
169+
}";
170+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
171+
}
172+
173+
/// <summary>
174+
/// Verifies that an orphaned include documentation statement does not produce diagnostics.
175+
/// </summary>
176+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
177+
[Fact]
178+
public async Task TestOrphanedIncludedDocumentationAsync()
179+
{
180+
var testCode = @"
181+
/// <include file='AllFilled.xml' path='/TestClass/TestMethod/*'/>
182+
";
183+
184+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
185+
}
186+
149187
/// <summary>
150188
/// Verifies that member with valid elements in the included documentation does not produce diagnostics.
151189
/// </summary>

0 commit comments

Comments
 (0)