/// <summary> /// Writes the diagnostic and the offending code. /// </summary> /// <returns>A string for use in assert exception.</returns> internal static string ToErrorString(this Diagnostic diagnostic, string padding = "") { var idAndPosition = diagnostic.Location.GetMappedLineSpan(); var code = diagnostic.Location.SourceTree?.GetText(CancellationToken.None).ToString() ?? string.Empty; var line = CodeReader.GetLineWithErrorIndicated(code, idAndPosition.StartLinePosition); return($"{padding}{diagnostic.Id} {diagnostic.GetMessage(CultureInfo.InvariantCulture)}\r\n" + $"{padding} at line {idAndPosition.StartLinePosition.Line} and character {idAndPosition.StartLinePosition.Character} in file {idAndPosition.Path} | {line.TrimStart(' ')}"); }
/// <summary> /// Writes the diagnostic and the offending code. /// </summary> /// <returns>A string for use in assert exception</returns> internal static string ToString(this Diagnostic diagnostic, IReadOnlyList <string> sources) { var idAndPosition = diagnostic.Location.GetMappedLineSpan(); var match = sources.SingleOrDefault(x => CodeReader.FileName(x) == idAndPosition.Path); var line = match != null?CodeReader.GetLineWithErrorIndicated(match, idAndPosition.StartLinePosition) : string.Empty; return($"{diagnostic.Id} {diagnostic.GetMessage(CultureInfo.InvariantCulture)}\r\n" + $" at line {idAndPosition.StartLinePosition.Line} and character {idAndPosition.StartLinePosition.Character} in file {idAndPosition.Path} | {line.TrimStart(' ')}"); }
public static void GetLineWithErrorIndicated(int line, int character, string expected) { var code = @"using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace N { class CodeReaderTests { } }"; Assert.AreEqual(expected, CodeReader.GetLineWithErrorIndicated(code, new LinePosition(line, character))); }