Skip to content

Commit

Permalink
Add tests based on code coverage information
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Sep 21, 2018
1 parent 1a7e90a commit 9e41513
Show file tree
Hide file tree
Showing 9 changed files with 389 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace DocumentationAnalyzers.StyleRules
{
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using DocumentationAnalyzers.Helpers;
Expand All @@ -27,10 +28,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
foreach (var diagnostic in context.Diagnostics)
{
if (!FixableDiagnosticIds.Contains(diagnostic.Id))
{
continue;
}
Debug.Assert(FixableDiagnosticIds.Contains(diagnostic.Id), "Assertion failed: FixableDiagnosticIds.Contains(diagnostic.Id)");

context.RegisterCodeFix(
CodeAction.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace DocumentationAnalyzers.StyleRules
{
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using DocumentationAnalyzers.Helpers;
Expand All @@ -27,10 +28,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
foreach (var diagnostic in context.Diagnostics)
{
if (!FixableDiagnosticIds.Contains(diagnostic.Id))
{
continue;
}
Debug.Assert(FixableDiagnosticIds.Contains(diagnostic.Id), "Assertion failed: FixableDiagnosticIds.Contains(diagnostic.Id)");

context.RegisterCodeFix(
CodeAction.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace DocumentationAnalyzers.StyleRules
{
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using DocumentationAnalyzers.Helpers;
Expand All @@ -27,10 +28,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
foreach (var diagnostic in context.Diagnostics)
{
if (!FixableDiagnosticIds.Contains(diagnostic.Id))
{
continue;
}
Debug.Assert(FixableDiagnosticIds.Contains(diagnostic.Id), "Assertion failed: FixableDiagnosticIds.Contains(diagnostic.Id)");

context.RegisterCodeFix(
CodeAction.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ namespace DocumentationAnalyzers.StyleRules
{
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using DocumentationAnalyzers.Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(DOC107CodeFixProvider))]
Expand All @@ -27,10 +29,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
foreach (var diagnostic in context.Diagnostics)
{
if (!FixableDiagnosticIds.Contains(diagnostic.Id))
{
continue;
}
Debug.Assert(FixableDiagnosticIds.Contains(diagnostic.Id), "Assertion failed: FixableDiagnosticIds.Contains(diagnostic.Id)");

context.RegisterCodeFix(
CodeAction.Create(
Expand All @@ -49,7 +48,9 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
var xmlElement = (XmlElementSyntax)root.FindNode(diagnostic.Location.SourceSpan, findInsideTrivia: true, getInnermostNodeForTie: true);

var newXmlElement = XmlSyntaxFactory.EmptyElement(XmlCommentHelper.SeeXmlTag)
.AddAttributes(XmlSyntaxFactory.TextAttribute("cref", xmlElement.Content.ToFullString()))
.AddAttributes(XmlSyntaxFactory.TextAttribute(
"cref",
SyntaxFactory.Token(SyntaxTriviaList.Empty, SyntaxKind.XmlTextLiteralToken, xmlElement.Content.ToFullString(), xmlElement.Content.ToFullString(), SyntaxTriviaList.Empty)))
.WithTriviaFrom(xmlElement);

return document.WithSyntaxRoot(root.ReplaceNode(xmlElement, newXmlElement));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,74 @@ public async Task TestNonKeywordsAsync(string keyword)
class TestClass
{{
}}
";

await Verify.VerifyAnalyzerAsync(testCode);
}

[Fact]
public async Task TestNonDiagnosticPotentialCasesAsync()
{
// These cases could qualify for this diagnostic, but currently do not.
var testCode = @"
class TestClass
{
/// <summary>
/// The keyword is <c>&#97;wait</c>.
/// The keyword is <c> true</c>.
/// The keyword is <c>true </c>.
/// The keyword is <c> true </c>.
/// The keyword is <c>true
/// </c>.
/// The keyword is <c>
/// true</c>.
/// The keyword is <c>
/// true
/// </c>.
/// </summary>
void Method<a>()
{
}
}
";

await Verify.VerifyAnalyzerAsync(testCode);
}

[Fact]
public async Task TestNonDiagnosticCasesAsync()
{
// These cases *shouldn't* qualify for this diagnostic.
var testCode = @"
class TestClass
{
/// <summary>
/// The keyword is <c>not-keyword</c>.
/// The keyword is <c>True</c>.
/// The keyword is <c>true&gt;</c>.
/// The keyword is <c>&gt;true</c>.
/// The keyword is <c><em>true</em></c>.
/// The keyword is <c><em>true</em>true</c>.
/// </summary>
void Method<a>()
{
}
}
";

await Verify.VerifyAnalyzerAsync(testCode);
}

[Fact]
public async Task TestNonCodeAsync()
{
var testCode = @"
/// <summary>
/// The keyword is <p:c>true</p:c>.
/// </summary>
class TestClass
{
}
";

await Verify.VerifyAnalyzerAsync(testCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ void Method(int a)
await Verify.VerifyCodeFixAsync(testCode, fixedCode);
}

[Fact]
public async Task TestParameterNameEncodedAsync()
{
var testCode = @"
class TestClass
{
/// <summary>
/// Consider a parameter [|<c>&#97;</c>|].
/// </summary>
void Method(int a)
{
}
}
";
var fixedCode = @"
class TestClass
{
/// <summary>
/// Consider a parameter <paramref name=""&#97;""/>.
/// </summary>
void Method(int a)
{
}
}
";

await Verify.VerifyCodeFixAsync(testCode, fixedCode);
}

[Fact]
public async Task TestParameterNameMatchesKeywordAsync()
{
Expand Down Expand Up @@ -70,5 +99,75 @@ void Method(int @true)

await Verify.VerifyCodeFixAsync(testCode, fixedCode);
}

[Fact]
public async Task TestNonDiagnosticPotentialCasesAsync()
{
// These cases could qualify for this diagnostic, but currently do not.
var testCode = @"
class TestClass
{
/// <summary>
/// Consider a parameter <c> a</c>.
/// Consider a parameter <c>a </c>.
/// Consider a parameter <c> a </c>.
/// Consider a parameter <c>a
/// </c>.
/// Consider a parameter <c>
/// a</c>.
/// Consider a parameter <c>
/// a
/// </c>.
/// </summary>
void Method(int a)
{
}
}
";

await Verify.VerifyAnalyzerAsync(testCode);
}

[Fact]
public async Task TestNonDiagnosticCasesAsync()
{
// These cases *shouldn't* qualify for this diagnostic.
var testCode = @"
class TestClass
{
/// <summary>
/// Consider a parameter <c>b</c>.
/// Consider a parameter <c>A</c>.
/// Consider a parameter <c>a&gt;</c>.
/// Consider a parameter <c>&gt;a</c>.
/// Consider a parameter <c><em>a</em></c>.
/// Consider a parameter <c><em>a</em>a</c>.
/// </summary>
void Method(int a)
{
}
}
";

await Verify.VerifyAnalyzerAsync(testCode);
}

[Fact]
public async Task TestNonCodeAsync()
{
var testCode = @"
class TestClass
{
/// <summary>
/// Consider a parameter <p:c>a</p:c>.
/// </summary>
void Method(int a)
{
}
}
";

await Verify.VerifyAnalyzerAsync(testCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ void Method<a>()
await Verify.VerifyCodeFixAsync(testCode, fixedCode);
}

[Fact]
public async Task TestTypeParameterNameEncodedAsync()
{
var testCode = @"
class TestClass
{
/// <summary>
/// Consider a type parameter [|<c>&#97;</c>|].
/// </summary>
void Method<a>()
{
}
}
";
var fixedCode = @"
class TestClass
{
/// <summary>
/// Consider a type parameter <typeparamref name=""&#97;""/>.
/// </summary>
void Method<a>()
{
}
}
";

await Verify.VerifyCodeFixAsync(testCode, fixedCode);
}

[Fact]
public async Task TestTypeParameterNameMatchesKeywordAsync()
{
Expand Down Expand Up @@ -70,5 +99,75 @@ void Method<@true>()

await Verify.VerifyCodeFixAsync(testCode, fixedCode);
}

[Fact]
public async Task TestNonDiagnosticPotentialCasesAsync()
{
// These cases could qualify for this diagnostic, but currently do not.
var testCode = @"
class TestClass
{
/// <summary>
/// Consider a type parameter <c> a</c>.
/// Consider a type parameter <c>a </c>.
/// Consider a type parameter <c> a </c>.
/// Consider a type parameter <c>a
/// </c>.
/// Consider a type parameter <c>
/// a</c>.
/// Consider a type parameter <c>
/// a
/// </c>.
/// </summary>
void Method<a>()
{
}
}
";

await Verify.VerifyAnalyzerAsync(testCode);
}

[Fact]
public async Task TestNonDiagnosticCasesAsync()
{
// These cases *shouldn't* qualify for this diagnostic.
var testCode = @"
class TestClass
{
/// <summary>
/// Consider a type parameter <c>b</c>.
/// Consider a type parameter <c>A</c>.
/// Consider a type parameter <c>a&gt;</c>.
/// Consider a type parameter <c>&gt;a</c>.
/// Consider a type parameter <c><em>a</em></c>.
/// Consider a type parameter <c><em>a</em>a</c>.
/// </summary>
void Method<a>()
{
}
}
";

await Verify.VerifyAnalyzerAsync(testCode);
}

[Fact]
public async Task TestNonCodeAsync()
{
var testCode = @"
class TestClass
{
/// <summary>
/// Consider a type parameter <p:c>a</p:c>.
/// </summary>
void Method<a>()
{
}
}
";

await Verify.VerifyAnalyzerAsync(testCode);
}
}
}
Loading

0 comments on commit 9e41513

Please sign in to comment.