Skip to content

Commit 9636fdb

Browse files
committed
Improved test coverage for edge cases
1 parent 28c3905 commit 9636fdb

File tree

2 files changed

+111
-4
lines changed

2 files changed

+111
-4
lines changed

DocumentationAnalyzers/DocumentationAnalyzers.CodeFixes/StyleRules/DOC103CodeFixProvider.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,31 @@ internal class DOC103CodeFixProvider : CodeFixProvider
2626
public override FixAllProvider GetFixAllProvider()
2727
=> CustomFixAllProviders.BatchFixer;
2828

29-
public override Task RegisterCodeFixesAsync(CodeFixContext context)
29+
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
3030
{
31+
SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
32+
3133
foreach (var diagnostic in context.Diagnostics)
3234
{
3335
if (!FixableDiagnosticIds.Contains(diagnostic.Id))
3436
{
3537
continue;
3638
}
3739

40+
SyntaxToken token = root.FindToken(diagnostic.Location.SourceSpan.Start, findInsideTrivia: true);
41+
if (!token.IsKind(SyntaxKind.XmlEntityLiteralToken))
42+
{
43+
// Could be an unrelated CS1570 error.
44+
return;
45+
}
46+
3847
context.RegisterCodeFix(
3948
CodeAction.Create(
4049
StyleResources.DOC103CodeFix,
41-
token => GetTransformedDocumentAsync(context.Document, diagnostic, token),
50+
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken),
4251
nameof(DOC103CodeFixProvider)),
4352
diagnostic);
4453
}
45-
46-
return SpecializedTasks.CompletedTask;
4754
}
4855

4956
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)

DocumentationAnalyzers/DocumentationAnalyzers.Test/StyleRules/DOC103UnitTests.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,105 @@ class TestClass
110110
CompilerDiagnostics = CompilerDiagnostics.Warnings,
111111
}.RunAsync();
112112
}
113+
114+
[Fact]
115+
public async Task TestHtmlEntityReplacementInInvalidXmlAsync()
116+
{
117+
var testCode = @"
118+
/// <summary>
119+
/// From A&rarr;B.
120+
/// <p>
121+
/// An unterminated second paragraph...
122+
/// </summary>
123+
class TestClass
124+
{
125+
}
126+
";
127+
var fixedCode = @"
128+
/// <summary>
129+
/// From A→B.
130+
/// <p>
131+
/// An unterminated second paragraph...
132+
/// </summary>
133+
class TestClass
134+
{
135+
}
136+
";
137+
138+
await new CSharpCodeFixTest<DOC103UseUnicodeCharacters, DOC103CodeFixProvider, XUnitVerifier>
139+
{
140+
TestState =
141+
{
142+
Sources = { testCode },
143+
ExpectedDiagnostics =
144+
{
145+
DiagnosticResult.CompilerWarning("CS1570").WithSpan(3, 11, 3, 11).WithMessage("XML comment has badly formed XML -- 'Reference to undefined entity 'rarr'.'"),
146+
DiagnosticResult.CompilerWarning("CS1570").WithSpan(6, 7, 6, 14).WithMessage("XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'p'.'"),
147+
DiagnosticResult.CompilerWarning("CS1570").WithSpan(7, 1, 7, 1).WithMessage("XML comment has badly formed XML -- 'Expected an end tag for element 'summary'.'"),
148+
},
149+
},
150+
FixedState =
151+
{
152+
Sources = { fixedCode },
153+
ExpectedDiagnostics =
154+
{
155+
DiagnosticResult.CompilerWarning("CS1570").WithSpan(6, 7, 6, 14).WithMessage("XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'p'.'"),
156+
DiagnosticResult.CompilerWarning("CS1570").WithSpan(7, 1, 7, 1).WithMessage("XML comment has badly formed XML -- 'Expected an end tag for element 'summary'.'"),
157+
},
158+
},
159+
CompilerDiagnostics = CompilerDiagnostics.Warnings,
160+
}.RunAsync();
161+
}
162+
163+
[Fact]
164+
public async Task TestNoCodeFixForRequiredEntityAsync()
165+
{
166+
var testCode = @"
167+
/// <summary>
168+
/// Processing for <c>&lt;code&gt;</c> elements.
169+
/// </summary>
170+
class TestClass
171+
{
172+
}
173+
";
174+
var fixedCode = testCode;
175+
176+
await Verify.VerifyCodeFixAsync(testCode, fixedCode);
177+
}
178+
179+
[Fact]
180+
public async Task TestNoCodeFixForInvalidXmlAsync()
181+
{
182+
var testCode = @"
183+
/// <summary>
184+
/// From A to B.
185+
/// <p>
186+
/// An unterminated second paragraph...
187+
/// </summary>
188+
class TestClass
189+
{
190+
}
191+
";
192+
var fixedCode = testCode;
193+
194+
await new CSharpCodeFixTest<DOC103UseUnicodeCharacters, DOC103CodeFixProvider, XUnitVerifier>
195+
{
196+
TestState =
197+
{
198+
Sources = { testCode },
199+
ExpectedDiagnostics =
200+
{
201+
DiagnosticResult.CompilerWarning("CS1570").WithSpan(6, 7, 6, 14).WithMessage("XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'p'.'"),
202+
DiagnosticResult.CompilerWarning("CS1570").WithSpan(7, 1, 7, 1).WithMessage("XML comment has badly formed XML -- 'Expected an end tag for element 'summary'.'"),
203+
},
204+
},
205+
FixedState =
206+
{
207+
Sources = { fixedCode },
208+
InheritanceMode = StateInheritanceMode.AutoInheritAll,
209+
},
210+
CompilerDiagnostics = CompilerDiagnostics.Warnings,
211+
}.RunAsync();
212+
}
113213
}
114214
}

0 commit comments

Comments
 (0)