public async Task VerifyNoDiagnosticAsync( string source, string[] additionalSources = null, CodeVerificationOptions options = null, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); if (!Analyzer.Supports(Descriptor)) { 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.EnableDiagnosticsDisabledByDefault(Analyzer); } 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 static void VerifyNoDiagnostic( IEnumerable <string> sources, DiagnosticDescriptor descriptor, DiagnosticAnalyzer analyzer, string language) { Assert.True(analyzer.Supports(descriptor), $"Diagnostic \"{descriptor.Id}\" is not supported by analyzer \"{analyzer.GetType().Name}\"."); IEnumerable <Document> documents = WorkspaceFactory.CreateDocuments(sources, language); VerifyNoCompilerError(documents); Diagnostic[] diagnostics = DiagnosticUtility.GetSortedDiagnostics(documents, analyzer); Assert.True(diagnostics.Length == 0 || diagnostics.All(f => !string.Equals(f.Id, descriptor.Id, StringComparison.Ordinal)), $"No diagnostic expected{diagnostics.Where(f => string.Equals(f.Id, descriptor.Id, StringComparison.Ordinal)).ToDebugString()}"); }
public static void VerifyNoFix( string source, CodeFixProvider fixProvider, string language, string equivalenceKey = null) { Document document = WorkspaceFactory.CreateDocument(source, language); foreach (Diagnostic compilerDiagnostic in document.GetCompilerDiagnostics()) { var context = new CodeFixContext( document, compilerDiagnostic, (a, _) => Assert.True(equivalenceKey != null && !string.Equals(a.EquivalenceKey, equivalenceKey, StringComparison.Ordinal), "Expected no code fix."), CancellationToken.None); fixProvider.RegisterCodeFixesAsync(context).Wait(); } }
public static void VerifyRefactoring( string source, string newSource, TextSpan span, CodeRefactoringProvider refactoringProvider, string language, string equivalenceKey = null, bool allowNewCompilerDiagnostics = false) { Document document = WorkspaceFactory.CreateDocument(source, language); document = VerifyRefactoring( document: document, span: span, refactoringProvider: refactoringProvider, equivalenceKey: equivalenceKey, allowNewCompilerDiagnostics: allowNewCompilerDiagnostics); string actual = document.ToSimplifiedAndFormattedFullString(); Assert.Equal(newSource, actual); }
public static void VerifyFix( string source, string newSource, string diagnosticId, CodeFixProvider fixProvider, string language, string equivalenceKey = null) { Assert.True(fixProvider.FixableDiagnosticIds.Contains(diagnosticId), $"Code fix provider '{fixProvider.GetType().Name}' cannot fix diagnostic '{diagnosticId}'."); Document document = WorkspaceFactory.CreateDocument(source, language); ImmutableArray <Diagnostic> compilerDiagnostics = document.GetCompilerDiagnostics(); while (compilerDiagnostics.Length > 0) { Diagnostic diagnostic = null; foreach (Diagnostic compilerDiagnostic in compilerDiagnostics) { if (string.Equals(compilerDiagnostic.Id, diagnosticId, StringComparison.Ordinal)) { diagnostic = compilerDiagnostic; break; } } if (diagnostic == null) { break; } List <CodeAction> actions = null; var context = new CodeFixContext( document, diagnostic, (a, _) => { if (equivalenceKey == null || string.Equals(a.EquivalenceKey, equivalenceKey, StringComparison.Ordinal)) { (actions ?? (actions = new List <CodeAction>())).Add(a); } }, CancellationToken.None); fixProvider.RegisterCodeFixesAsync(context).Wait(); if (actions == null) { break; } document = document.ApplyCodeAction(actions[0]); compilerDiagnostics = document.GetCompilerDiagnostics(); } string actual = document.ToSimplifiedAndFormattedFullString(); Assert.Equal(newSource, actual); }
public async Task VerifyDiagnosticAsync( string source, IEnumerable <Diagnostic> expectedDiagnostics, string[] additionalSources = null, CodeVerificationOptions options = null, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); 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.EnableDiagnosticsDisabledByDefault(Analyzer); } ImmutableArray <Diagnostic> diagnostics = await compilation.GetAnalyzerDiagnosticsAsync(Analyzer, DiagnosticComparer.SpanStart, cancellationToken).ConfigureAwait(false); if (diagnostics.Length > 0 && Analyzer.SupportedDiagnostics.Length > 1) { VerifyDiagnostics(FilterDiagnostics(diagnostics), expectedDiagnostics, cancellationToken); } else { VerifyDiagnostics(diagnostics, expectedDiagnostics, cancellationToken); } } IEnumerable <Diagnostic> FilterDiagnostics(ImmutableArray <Diagnostic> diagnostics) { foreach (Diagnostic diagnostic in diagnostics) { bool success = false; foreach (Diagnostic expectedDiagnostic in expectedDiagnostics) { if (DiagnosticComparer.Id.Equals(diagnostic, expectedDiagnostic)) { success = true; break; } } if (success) { yield return(diagnostic); } } } }
public static void VerifyFix( string source, string newSource, DiagnosticAnalyzer analyzer, CodeFixProvider fixProvider, string language, bool allowNewCompilerDiagnostics = false) { Assert.True(fixProvider.CanFixAny(analyzer.SupportedDiagnostics), $"Code fix provider '{fixProvider.GetType().Name}' cannot fix any diagnostic supported by analyzer '{analyzer}'."); Document document = WorkspaceFactory.CreateDocument(source, language); ImmutableArray <Diagnostic> compilerDiagnostics = document.GetCompilerDiagnostics(); DiagnosticVerifier.VerifyNoCompilerError(compilerDiagnostics); Diagnostic[] analyzerDiagnostics = DiagnosticUtility.GetSortedDiagnostics(document, analyzer); while (analyzerDiagnostics.Length > 0) { Diagnostic diagnostic = null; foreach (Diagnostic analyzerDiagnostic in analyzerDiagnostics) { if (fixProvider.FixableDiagnosticIds.Contains(analyzerDiagnostic.Id)) { diagnostic = analyzerDiagnostic; break; } } if (diagnostic == null) { break; } List <CodeAction> actions = null; var context = new CodeFixContext( document, diagnostic, (a, _) => (actions ?? (actions = new List <CodeAction>())).Add(a), CancellationToken.None); fixProvider.RegisterCodeFixesAsync(context).Wait(); if (actions == null) { break; } document = document.ApplyCodeAction(actions[0]); if (!allowNewCompilerDiagnostics) { DiagnosticVerifier.VerifyNoNewCompilerDiagnostics(document, compilerDiagnostics); } analyzerDiagnostics = DiagnosticUtility.GetSortedDiagnostics(document, analyzer); } string actual = document.ToSimplifiedAndFormattedFullString(); Assert.Equal(newSource, actual); }
public async Task VerifyRefactoringAsync( string source, string expected, TextSpan span, string[] additionalSources = null, string equivalenceKey = null, CodeVerificationOptions options = null, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); using (Workspace workspace = new AdhocWorkspace()) { Project project = WorkspaceFactory.AddProject(workspace.CurrentSolution, options); Document document = WorkspaceFactory.AddDocument(project, source, additionalSources ?? Array.Empty <string>()); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); ImmutableArray <Diagnostic> compilerDiagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken); if (options == null) { options = Options; } VerifyCompilerDiagnostics(compilerDiagnostics, options); CodeAction action = null; var context = new CodeRefactoringContext( document, span, a => { if (equivalenceKey == null || string.Equals(a.EquivalenceKey, equivalenceKey, StringComparison.Ordinal)) { if (action == null) { action = a; } } }, CancellationToken.None); await RefactoringProvider.ComputeRefactoringsAsync(context).ConfigureAwait(false); Assert.True(action != null, "No code refactoring has been registered."); document = await document.ApplyCodeActionAsync(action).ConfigureAwait(false); semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); ImmutableArray <Diagnostic> newCompilerDiagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken); VerifyCompilerDiagnostics(newCompilerDiagnostics, options); if (!options.AllowNewCompilerDiagnostics) { VerifyNoNewCompilerDiagnostics(compilerDiagnostics, newCompilerDiagnostics, options); } string actual = await document.ToFullStringAsync(simplify : true, format : true).ConfigureAwait(false); Assert.Equal(expected, actual); } }