提交 08358fb1 编写于 作者: H Heejae Chang

fixed unit test failure due to behavior change of AD001 description.

上级 f27dd680
...@@ -1131,7 +1131,7 @@ End Class ...@@ -1131,7 +1131,7 @@ End Class
} }
[Fact] [Fact]
public async Task SuppressDuplicateAnalyzerExceptionDiagnostics() public async Task AnalyzerExceptionDiagnosticsWithDifferentContext()
{ {
var exceptionDiagnostics = new HashSet<Diagnostic>(); var exceptionDiagnostics = new HashSet<Diagnostic>();
...@@ -1149,13 +1149,15 @@ public class C2 ...@@ -1149,13 +1149,15 @@ public class C2
new[] { new ThrowExceptionForEachNamedTypeAnalyzer() }, new[] { new ThrowExceptionForEachNamedTypeAnalyzer() },
onAnalyzerException: (ex, a, d) => exceptionDiagnostics.Add(d)); onAnalyzerException: (ex, a, d) => exceptionDiagnostics.Add(d));
exceptionDiagnostics.Verify( var diagnostic = Diagnostic("AD0001", null)
Diagnostic("AD0001", null)
.WithArguments( .WithArguments(
"Microsoft.CodeAnalysis.UnitTests.Diagnostics.SuppressMessageAttributeTests+ThrowExceptionForEachNamedTypeAnalyzer", "Microsoft.CodeAnalysis.UnitTests.Diagnostics.SuppressMessageAttributeTests+ThrowExceptionForEachNamedTypeAnalyzer",
"System.Exception", "System.Exception",
"ThrowExceptionAnalyzer exception") "ThrowExceptionAnalyzer exception")
.WithLocation(1, 1)); .WithLocation(1, 1);
// expect 3 different diagnostics with 3 different contexts.
exceptionDiagnostics.Verify(diagnostic, diagnostic, diagnostic);
} }
#endregion #endregion
......
...@@ -91,29 +91,39 @@ public string GetContext() ...@@ -91,29 +91,39 @@ public string GetContext()
if (_node != null) if (_node != null)
{ {
var text = _tree?.GetText();
var lineSpan = text?.Lines?.GetLinePositionSpan(_node.Span);
// can't use Kind since that is language specific. instead will output typename. // can't use Kind since that is language specific. instead will output typename.
sb.AppendLine($"{nameof(SyntaxNode)}: \"{GetValueText(_node)}\" {_node.GetType().Name}@{_node.Span.ToString()}"); sb.AppendLine($"{nameof(SyntaxNode)}: {GetFlattenedNodeText(_node)} [{_node.GetType().Name}]@{_node.Span.ToString()} {(lineSpan.HasValue ? lineSpan.Value.ToString() : string.Empty)}");
} }
return sb.ToString(); return sb.ToString();
} }
private string GetValueText(SyntaxNode node) private string GetFlattenedNodeText(SyntaxNode node)
{ {
const int cutoff = 30; const int cutoff = 30;
if (node.Span.Length < cutoff) var lastEnd = node.Span.Start;
var sb = new StringBuilder();
foreach (var token in node.DescendantTokens(descendIntoTrivia: false))
{ {
return RemoveNewLines(node.ToString()); if (token.Span.Start - lastEnd > 0)
{
sb.Append(" ");
} }
// get actual text without creating texts for all sub nodes. sb.Append(token.ToString());
return RemoveNewLines(node.GetText().ToString(new TextSpan(node.Span.Start - node.FullSpan.Start, cutoff))) + " ..."; lastEnd = token.Span.End;
}
private string RemoveNewLines(string text) if (sb.Length > cutoff)
{ {
return text.Replace(Environment.NewLine, @"\r\n"); break;
}
}
return sb.ToString() + (sb.Length > cutoff ? " ..." : string.Empty);
} }
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册