/// <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.FindLinePositions(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.FindLinePositions(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)));
        }
示例#3
0
        /// <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 is null)
            {
                throw new ArgumentNullException(nameof(analyzerId));
            }

            if (code is null)
            {
                throw new ArgumentNullException(nameof(code));
            }

            var diagnostics    = new List <ExpectedDiagnostic>();
            var cleanedSources = new List <string>();

            foreach (var source in code)
            {
                var positions = CodeReader.FindLinePositions(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))));
            }

            if (diagnostics.Count == 0)
            {
                throw new InvalidOperationException("Expected code to have at least one error position indicated with '↓'");
            }

            return(new DiagnosticsAndSources(diagnostics, cleanedSources));
        }