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

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

上级 f27dd680
......@@ -1131,7 +1131,7 @@ End Class
}
[Fact]
public async Task SuppressDuplicateAnalyzerExceptionDiagnostics()
public async Task AnalyzerExceptionDiagnosticsWithDifferentContext()
{
var exceptionDiagnostics = new HashSet<Diagnostic>();
......@@ -1149,13 +1149,15 @@ public class C2
new[] { new ThrowExceptionForEachNamedTypeAnalyzer() },
onAnalyzerException: (ex, a, d) => exceptionDiagnostics.Add(d));
exceptionDiagnostics.Verify(
Diagnostic("AD0001", null)
var diagnostic = Diagnostic("AD0001", null)
.WithArguments(
"Microsoft.CodeAnalysis.UnitTests.Diagnostics.SuppressMessageAttributeTests+ThrowExceptionForEachNamedTypeAnalyzer",
"System.Exception",
"ThrowExceptionAnalyzer exception")
.WithLocation(1, 1));
.WithLocation(1, 1);
// expect 3 different diagnostics with 3 different contexts.
exceptionDiagnostics.Verify(diagnostic, diagnostic, diagnostic);
}
#endregion
......
......@@ -91,29 +91,39 @@ public string GetContext()
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.
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();
}
private string GetValueText(SyntaxNode node)
private string GetFlattenedNodeText(SyntaxNode node)
{
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(" ");
}
sb.Append(token.ToString());
lastEnd = token.Span.End;
if (sb.Length > cutoff)
{
break;
}
}
// get actual text without creating texts for all sub nodes.
return RemoveNewLines(node.GetText().ToString(new TextSpan(node.Span.Start - node.FullSpan.Start, cutoff))) + " ...";
}
private string RemoveNewLines(string text)
{
return text.Replace(Environment.NewLine, @"\r\n");
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.
先完成此消息的编辑!
想要评论请 注册