private static string GetDiagnosticDescription(DiagnosticDescription d, int indentDepth) { return(new string(' ', 4 * indentDepth) + d.ToString()); }
public static string GetAssertText(DiagnosticDescription[] expected, IEnumerable <Diagnostic> actual) { const int CSharp = 1; const int VisualBasic = 2; var language = actual.Any() && actual.First().Id.StartsWith("CS", StringComparison.Ordinal) ? CSharp : VisualBasic; var includeDiagnosticMessagesAsComments = (language == CSharp); int indentDepth = (language == CSharp) ? 4 : 1; if (IsSortedOrEmpty(expected)) { // If this is a new test (empty expectations) or a test that's already sorted, // we sort the actual diagnostics to minimize diff noise as diagnostics change. actual = Sort(actual); } var assertText = new StringBuilder(); assertText.AppendLine(); // write out the error baseline as method calls int i; assertText.AppendLine("Expected:"); var expectedText = ArrayBuilder <string> .GetInstance(); foreach (var d in expected) { expectedText.Add(GetDiagnosticDescription(d, indentDepth)); } GetCommaSeparatedLines(assertText, expectedText); // write out the actual results as method calls (copy/paste this to update baseline) assertText.AppendLine("Actual:"); var actualText = ArrayBuilder <string> .GetInstance(); var e = actual.GetEnumerator(); for (i = 0; e.MoveNext(); i++) { Diagnostic d = e.Current; string message = d.ToString(); if (Regex.Match(message, @"{\d+}").Success) { Assert.True(false, "Diagnostic messages should never contain unsubstituted placeholders.\n " + message); } if (i > 0) { assertText.AppendLine(","); } if (includeDiagnosticMessagesAsComments) { Indent(assertText, indentDepth); assertText.Append("// "); assertText.AppendLine(d.ToString()); var l = d.Location; if (l.IsInSource) { Indent(assertText, indentDepth); assertText.Append("// "); assertText.AppendLine(l.SourceTree.GetText().Lines.GetLineFromPosition(l.SourceSpan.Start).ToString()); } } var description = new DiagnosticDescription(d, errorCodeOnly: false); var diffDescription = description; var idx = Array.IndexOf(expected, description); if (idx != -1) { diffDescription = expected[idx]; } assertText.Append(GetDiagnosticDescription(description, indentDepth)); actualText.Add(GetDiagnosticDescription(diffDescription, indentDepth)); } if (i > 0) { assertText.AppendLine(); } assertText.AppendLine("Diff:"); assertText.Append(DiffUtil.DiffReport(expectedText, actualText, separator: Environment.NewLine)); actualText.Free(); expectedText.Free(); return(assertText.ToString()); }