private void VerifyDiagnostics( IEnumerable <Diagnostic> actualDiagnostics, IEnumerable <Diagnostic> expectedDiagnostics, bool checkAdditionalLocations, CancellationToken cancellationToken = default) { int expectedCount = 0; int actualCount = 0; using (IEnumerator <Diagnostic> expectedEnumerator = expectedDiagnostics.OrderBy(f => f, DiagnosticComparer.SpanStart).GetEnumerator()) using (IEnumerator <Diagnostic> actualEnumerator = actualDiagnostics.OrderBy(f => f, DiagnosticComparer.SpanStart).GetEnumerator()) { if (!expectedEnumerator.MoveNext()) { throw new InvalidOperationException($"'{nameof(expectedDiagnostics)}' contains no elements."); } do { cancellationToken.ThrowIfCancellationRequested(); expectedCount++; Diagnostic expectedDiagnostic = expectedEnumerator.Current; if (SupportedDiagnostics.IndexOf(expectedDiagnostic.Descriptor, DiagnosticDescriptorComparer.Id) == -1) { Assert.True(false, $"Diagnostic \"{expectedDiagnostic.Id}\" is not supported by analyzer(s) {string.Join(", ", Analyzers.Select(f => f.GetType().Name))}."); } if (actualEnumerator.MoveNext()) { actualCount++; VerifyDiagnostic(actualEnumerator.Current, expectedDiagnostic, checkAdditionalLocations: checkAdditionalLocations); } else { while (expectedEnumerator.MoveNext()) { expectedCount++; } Assert.True(false, $"Mismatch between number of diagnostics returned, expected: {expectedCount} actual: {actualCount}{actualDiagnostics.ToDebugString()}"); } } while (expectedEnumerator.MoveNext()); if (actualEnumerator.MoveNext()) { actualCount++; while (actualEnumerator.MoveNext()) { actualCount++; } Assert.True(false, $"Mismatch between number of diagnostics returned, expected: {expectedCount} actual: {actualCount}{actualDiagnostics.ToDebugString()}"); } } }
public async Task VerifyNoDiagnosticAsync( string source, string[] additionalSources = null, CodeVerificationOptions options = null, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); if (SupportedDiagnostics.IndexOf(Descriptor, DiagnosticDescriptorComparer.Id) == -1) { Assert.True(false, $"Diagnostic \"{Descriptor.Id}\" is not supported by analyzer \"{Analyzer.GetType().Name}\"."); } using (Workspace workspace = new AdhocWorkspace()) { Project project = WorkspaceFactory.AddProject(workspace.CurrentSolution, options); Document document = WorkspaceFactory.AddDocument(project, source, additionalSources ?? Array.Empty <string>()); Compilation compilation = await document.Project.GetCompilationAsync(cancellationToken).ConfigureAwait(false); ImmutableArray <Diagnostic> compilerDiagnostics = compilation.GetDiagnostics(cancellationToken); if (options == null) { options = Options; } VerifyCompilerDiagnostics(compilerDiagnostics, options); if (options.EnableDiagnosticsDisabledByDefault) { compilation = compilation.EnsureEnabled(Descriptor); } ImmutableArray <Diagnostic> analyzerDiagnostics = await compilation.GetAnalyzerDiagnosticsAsync(Analyzer, DiagnosticComparer.SpanStart, cancellationToken).ConfigureAwait(false); foreach (Diagnostic diagnostic in analyzerDiagnostics) { if (string.Equals(diagnostic.Id, Descriptor.Id, StringComparison.Ordinal)) { Assert.True(false, $"No diagnostic expected{analyzerDiagnostics.Where(f => string.Equals(f.Id, Descriptor.Id, StringComparison.Ordinal)).ToDebugString()}"); } } } }
public async Task VerifyNoDiagnosticAsync( string source, IEnumerable <string> additionalSources = null, CodeVerificationOptions options = null, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); options ??= Options; if (SupportedDiagnostics.IndexOf(Descriptor, DiagnosticDescriptorComparer.Id) == -1) { Assert.True(false, $"Diagnostic \"{Descriptor.Id}\" is not supported by analyzer(s) {string.Join(", ", Analyzers.Select(f => f.GetType().Name))}."); } using (Workspace workspace = new AdhocWorkspace()) { Project project = WorkspaceFactory.AddProject(workspace.CurrentSolution, options); Document document = WorkspaceFactory.AddDocument(project, source, additionalSources); Compilation compilation = await document.Project.GetCompilationAsync(cancellationToken).ConfigureAwait(false); ImmutableArray <Diagnostic> compilerDiagnostics = compilation.GetDiagnostics(cancellationToken); VerifyCompilerDiagnostics(compilerDiagnostics, options); compilation = UpdateCompilation(compilation); ImmutableArray <Diagnostic> analyzerDiagnostics = await compilation.GetAnalyzerDiagnosticsAsync(Analyzers, DiagnosticComparer.SpanStart, cancellationToken).ConfigureAwait(false); foreach (Diagnostic diagnostic in analyzerDiagnostics) { if (string.Equals(diagnostic.Id, Descriptor.Id, StringComparison.Ordinal)) { Assert.True(false, $"No diagnostic expected{analyzerDiagnostics.Where(f => string.Equals(f.Id, Descriptor.Id, StringComparison.Ordinal)).ToDebugString()}"); } } } }