/// <summary> /// Get the expected diagnostics and cleaned sources. /// </summary> /// <param name="analyzerId">The descriptor diagnosticId that is expected to produce diagnostics.</param> /// <param name="message">The expected message for the diagnostics, can be null.</param> /// <param name="code">The code with errors indicated.</param> /// <returns>An instance of <see cref="DiagnosticsAndSources"/>.</returns> public static DiagnosticsAndSources CreateFromCodeWithErrorsIndicated(string analyzerId, string message, IReadOnlyList <string> code) { if (analyzerId == null) { throw new ArgumentNullException(nameof(analyzerId)); } var diagnostics = new List <ExpectedDiagnostic>(); var cleanedSources = new List <string>(); foreach (var source in code) { var positions = CodeReader.FindDiagnosticsPositions(source).ToArray(); if (positions.Length == 0) { cleanedSources.Add(source); continue; } cleanedSources.Add(source.Replace("↓", string.Empty)); var fileName = CodeReader.FileName(source); diagnostics.AddRange(positions.Select(p => new ExpectedDiagnostic(analyzerId, message, new FileLinePositionSpan(fileName, p, p)))); } return(new DiagnosticsAndSources(diagnostics, cleanedSources)); }
/// <summary> /// Create a new instance of <see cref="ExpectedDiagnostic"/> /// </summary> /// <param name="diagnosticId">The expected diagnostic id</param> /// <param name="message">The expected message.</param> /// <param name="codeWithErrorsIndicated">The code with error position indicated..</param> /// <param name="cleanedSources"><paramref name="codeWithErrorsIndicated"/> without errors indicated.</param> /// <returns>A new instance of <see cref="ExpectedDiagnostic"/></returns> public static IReadOnlyList <ExpectedDiagnostic> CreateManyFromCodeWithErrorsIndicated(string diagnosticId, string message, string codeWithErrorsIndicated, out string cleanedSources) { var positions = CodeReader.FindDiagnosticsPositions(codeWithErrorsIndicated).ToArray(); if (positions.Length == 0) { throw new ArgumentException("Expected one error position indicated, was zero.", nameof(codeWithErrorsIndicated)); } cleanedSources = codeWithErrorsIndicated.Replace("↓", string.Empty); var fileName = CodeReader.FileName(codeWithErrorsIndicated); return(positions.Select(p => new ExpectedDiagnostic(diagnosticId, message, new FileLinePositionSpan(fileName, p, p))) .ToArray()); }
/// <summary> /// Create a new instance of <see cref="ExpectedDiagnostic"/> with position /// </summary> /// <param name="codeWithErrorsIndicated">The code with error position indicated..</param> /// <param name="cleanedSources"><paramref name="codeWithErrorsIndicated"/> without errors indicated.</param> /// <returns>A new instance of <see cref="ExpectedDiagnostic"/></returns> public ExpectedDiagnostic WithPositionFromCodeWithErrorsIndicated(string codeWithErrorsIndicated, out string cleanedSources) { var positions = CodeReader.FindDiagnosticsPositions(codeWithErrorsIndicated).ToArray(); if (positions.Length == 0) { throw new ArgumentException("Expected one error position indicated, was zero.", nameof(codeWithErrorsIndicated)); } if (positions.Length > 1) { throw new ArgumentException($"Expected one error position indicated, was {positions.Length}.", nameof(codeWithErrorsIndicated)); } cleanedSources = codeWithErrorsIndicated.Replace("↓", string.Empty); var fileName = CodeReader.FileName(codeWithErrorsIndicated); var position = positions[0]; return(new ExpectedDiagnostic(this.Id, this.Message, new FileLinePositionSpan(fileName, position, position))); }