/// <summary> /// General method that gets a collection of actual <see cref="Diagnostic"/>s found in the source after the /// analyzer is run, then verifies each of them. /// </summary> /// <param name="sources">An array of strings to create source documents from to run the analyzers on.</param> /// <param name="language">The language of the classes represented by the source strings.</param> /// <param name="analyzers">The analyzers to be run on the source code.</param> /// <param name="expected">A collection of <see cref="DiagnosticResult"/>s that should appear after the analyzer /// is run on the sources.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param> /// <param name="filenames">The filenames or null if the default filename should be used</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> private async Task VerifyDiagnosticsAsync(string[] sources, string language, ImmutableArray<DiagnosticAnalyzer> analyzers, DiagnosticResult[] expected, CancellationToken cancellationToken, string[] filenames) { VerifyDiagnosticResults(await this.GetSortedDiagnosticsAsync(sources, language, analyzers, cancellationToken, filenames).ConfigureAwait(false), analyzers, expected); // If filenames is null we want to test for exclusions too if (filenames == null) { // Also check if the analyzer honors exclusions if (expected.Any(x => x.Id.StartsWith("SA", StringComparison.Ordinal) || x.Id.StartsWith("SX", StringComparison.Ordinal))) { // We want to look at non-stylecop diagnostics only. We also insert a new line at the beginning // so we have to move all diagnostic location down by one line var expectedResults = expected .Where(x => !x.Id.StartsWith("SA", StringComparison.Ordinal) && !x.Id.StartsWith("SX", StringComparison.Ordinal)) .Select(x => x.WithLineOffset(1)) .ToArray(); VerifyDiagnosticResults(await this.GetSortedDiagnosticsAsync(sources.Select(x => " // <auto-generated>\r\n" + x).ToArray(), language, analyzers, cancellationToken, null).ConfigureAwait(false), analyzers, expectedResults); } } }
/// <summary> /// General method that gets a collection of actual <see cref="Diagnostic"/>s found in the source after the /// analyzer is run, then verifies each of them. /// </summary> /// <param name="sources">An array of strings to create source documents from to run the analyzers on.</param> /// <param name="language">The language of the classes represented by the source strings.</param> /// <param name="analyzers">The analyzers to be run on the source code.</param> /// <param name="expected">A collection of <see cref="DiagnosticResult"/>s that should appear after the analyzer /// is run on the sources.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param> /// <param name="filenames">The filenames or null if the default filename should be used</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> private async Task VerifyDiagnosticsAsync(string[] sources, string language, ImmutableArray<DiagnosticAnalyzer> analyzers, DiagnosticResult[] expected, CancellationToken cancellationToken, string[] filenames) { VerifyDiagnosticResults(await this.GetSortedDiagnosticsAsync(sources, language, analyzers, cancellationToken, filenames).ConfigureAwait(false), analyzers, expected); // If filenames is null we want to test for exclusions too if (filenames == null) { // Also check if the analyzer honors exclusions if (expected.Any(IsSubjectToExclusion)) { // Diagnostics reported by the compiler and analyzer diagnostics which don't have a location will // still be reported. We also insert a new line at the beginning so we have to move all diagnostic // locations which have a specific position down by one line. var expectedResults = expected .Where(x => !IsSubjectToExclusion(x)) .Select(x => x.WithLineOffset(1)) .ToArray(); VerifyDiagnosticResults(await this.GetSortedDiagnosticsAsync(sources.Select(x => " // <auto-generated>\r\n" + x).ToArray(), language, analyzers, cancellationToken, null).ConfigureAwait(false), analyzers, expectedResults); } } }