public static string GetAssertText(DiagnosticDescription[] expected, IEnumerable <Diagnostic> actual) { const int CSharp = 1; const int VisualBasic = 2; var language = actual.Any() && actual.First() is CSDiagnostic ? CSharp : VisualBasic; var includeDiagnosticMessagesAsComments = (language == CSharp); int indentDepth = (language == CSharp) ? 4 : 1; var includeDefaultSeverity = expected.Any() && expected.All(d => d.DefaultSeverity != null); var includeEffectiveSeverity = expected.Any() && expected.All(d => d.EffectiveSeverity != null); 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, includeDefaultSeverity, includeEffectiveSeverity); 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()); }
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; StringBuilder assertText = new StringBuilder(); assertText.AppendLine(); // write out the error baseline as method calls int i; assertText.AppendLine("Expected:"); var expectedText = new StringBuilder(); for (i = 0; i < expected.Length; i++) { var d = expected[i]; AppendDiagnosticDescription(expectedText, d, indentDepth); if (i < expected.Length - 1) { expectedText.Append(","); } expectedText.AppendLine(); } assertText.Append(expectedText); // write out the actual results as method calls (copy/paste this to update baseline) assertText.AppendLine("Actual:"); var actualText = new StringBuilder(); var e = actual.GetEnumerator(); for (i = 0; e.MoveNext(); i++) { var 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(","); actualText.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]; } AppendDiagnosticDescription(assertText, description, indentDepth); AppendDiagnosticDescription(actualText, diffDescription, indentDepth); } if (i > 0) { assertText.AppendLine(); actualText.AppendLine(); } assertText.AppendLine("Diff:"); assertText.Append(DiffUtil.DiffReport(expectedText.ToString(), actualText.ToString())); return(assertText.ToString()); }
public static string GetAssertText(DiagnosticDescription[] expected, IEnumerable <Diagnostic> actual) { var includeCompilerOutput = false; var includeDiagnosticMessagesAsComments = false; const int CSharp = 1; const int VisualBasic = 2; var language = actual.Any() && actual.First().Id.StartsWith("CS") ? CSharp : VisualBasic; if (language == CSharp) { includeDiagnosticMessagesAsComments = true; } StringBuilder assertText = new StringBuilder(); assertText.AppendLine(); // Write out the 'command line compiler output' including squiggles (easy to read debugging info in the case of a failure). // This will be useful for VB, because we can't do the inline comments. if (includeCompilerOutput) { assertText.AppendLine("Compiler output:"); foreach (var d in actual) { Indent(assertText, 1); assertText.AppendLine(d.ToString()); var location = d.Location; var lineText = location.SourceTree.GetText().Lines.GetLineFromPosition(location.SourceSpan.Start).ToString(); assertText.AppendLine(lineText); var span = location.GetMappedLineSpan(); var startPosition = span.StartLinePosition; var endPosition = span.EndLinePosition; assertText.Append(' ', startPosition.Character); var endCharacter = (startPosition.Line == endPosition.Line) ? endPosition.Character : lineText.Length; assertText.Append('~', endCharacter - startPosition.Character); assertText.AppendLine(); } } // write out the error baseline as method calls int i; assertText.AppendLine("Expected:"); var expectedText = new StringBuilder(); for (i = 0; i < expected.Length; i++) { var d = expected[i]; AppendDiagnosticDescription(expectedText, d); if (i < expected.Length - 1) { expectedText.Append(","); } expectedText.AppendLine(); } assertText.Append(expectedText); // write out the actual results as method calls (copy/paste this to update baseline) assertText.AppendLine("Actual:"); var actualText = new StringBuilder(); var e = actual.GetEnumerator(); for (i = 0; e.MoveNext(); i++) { var 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(","); actualText.AppendLine(","); } if (includeDiagnosticMessagesAsComments) { Indent(assertText, 1); assertText.Append("// "); assertText.AppendLine(d.ToString()); var l = d.Location; if (l.IsInSource) { Indent(assertText, 1); assertText.Append("// "); assertText.AppendLine(l.SourceTree.GetText().Lines.GetLineFromPosition(l.SourceSpan.Start).ToString()); } } var description = new DiagnosticDescription(d, errorCodeOnly: false, showPosition: true); AppendDiagnosticDescription(assertText, description); AppendDiagnosticDescription(actualText, description); } if (i > 0) { assertText.AppendLine(); actualText.AppendLine(); } assertText.AppendLine("Diff:"); assertText.Append(DiffUtil.DiffReport(expectedText.ToString(), actualText.ToString())); return(assertText.ToString()); }