public static void VerifyRefactoring( string source, string[] additionalSources, string newSource, IEnumerable <TextSpan> spans, CodeRefactoringProvider refactoringProvider, string language, string equivalenceKey = null, bool allowNewCompilerDiagnostics = false) { Document document = WorkspaceFactory.CreateDocument(source, additionalSources, language); foreach (TextSpan span in spans.OrderByDescending(f => f.Start)) { document = VerifyRefactoring( document: document, span: span, refactoringProvider: refactoringProvider, equivalenceKey: equivalenceKey, allowNewCompilerDiagnostics: allowNewCompilerDiagnostics); } string actual = document.ToSimplifiedAndFormattedFullString(); Assert.Equal(newSource, actual); }
public static void VerifyNoRefactoring( string source, TextSpan span, CodeRefactoringProvider refactoringProvider, string language, string equivalenceKey = null) { Document document = WorkspaceFactory.CreateDocument(source, language); DiagnosticVerifier.VerifyNoCompilerError(document); List <CodeAction> actions = null; var context = new CodeRefactoringContext( document, span, codeAction => { if (equivalenceKey == null || string.Equals(codeAction.EquivalenceKey, equivalenceKey, StringComparison.Ordinal)) { (actions ?? (actions = new List <CodeAction>())).Add(codeAction); } }, CancellationToken.None); refactoringProvider.ComputeRefactoringsAsync(context).Wait(); Assert.True(actions == null, $"Expected no code refactoring, actual: {actions?.Count ?? 0}"); }
public static void VerifyNoFix( string source, DiagnosticAnalyzer analyzer, CodeFixProvider fixProvider, string language) { Document document = WorkspaceFactory.CreateDocument(source, language); DiagnosticVerifier.VerifyNoCompilerError(document); foreach (Diagnostic diagnostic in DiagnosticUtility.GetSortedDiagnostics(document, analyzer)) { List <CodeAction> actions = null; var context = new CodeFixContext( document, diagnostic, (a, _) => (actions ?? (actions = new List <CodeAction>())).Add(a), CancellationToken.None); fixProvider.RegisterCodeFixesAsync(context).Wait(); Assert.True(actions == null, $"Expected no code fix, actual: {actions.Count}."); } }
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 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); }