private static void RunFixAllProvider(DiagnosticAnalyzer diagnosticAnalyzer, CodeFixProvider codeFixProvider, string codeFixTitle, FixAllProvider fixAllProvider, Document document, ParseOptions parseOption, string pathToExpected) { var currentDocument = document; CalculateDiagnosticsAndCode(diagnosticAnalyzer, currentDocument, parseOption, out var diagnostics, out var actualCode); diagnostics.Should().NotBeEmpty(); var fixAllDiagnosticProvider = new FixAllDiagnosticProvider( codeFixProvider.FixableDiagnosticIds.ToHashSet(), (doc, ids, ct) => Task.FromResult( DiagnosticVerifier.GetDiagnostics( currentDocument.Project.GetCompilationAsync(ct).Result, diagnosticAnalyzer, CompilationErrorBehavior.Ignore)), // ToDo: Is that the right decision? null); var fixAllContext = new FixAllContext(currentDocument, codeFixProvider, FixAllScope.Document, codeFixTitle, codeFixProvider.FixableDiagnosticIds, fixAllDiagnosticProvider, CancellationToken.None); var codeActionToExecute = fixAllProvider.GetFixAsync(fixAllContext).Result; codeActionToExecute.Should().NotBeNull(); currentDocument = ApplyCodeFix(currentDocument, codeActionToExecute); CalculateDiagnosticsAndCode(diagnosticAnalyzer, currentDocument, parseOption, out diagnostics, out actualCode); AreEqualIgnoringLineEnding(pathToExpected, actualCode); }
public DocumentState(DiagnosticAnalyzer diagnosticAnalyzer, Document document, ParseOptions parseOption) { var project = document.Project; if (parseOption != null) { project = project.WithParseOptions(parseOption); } Compilation = project.GetCompilationAsync().Result; Diagnostics = DiagnosticVerifier.GetDiagnostics(Compilation, diagnosticAnalyzer, CompilationErrorBehavior.Ignore).ToImmutableArray(); ActualCode = document.GetSyntaxRootAsync().Result.GetText().ToString(); }
private static void CalculateDiagnosticsAndCode(DiagnosticAnalyzer diagnosticAnalyzer, Document document, ParseOptions parseOption, out List <Diagnostic> diagnostics, out string actualCode) { var project = document.Project; if (parseOption != null) { project = project.WithParseOptions(parseOption); } diagnostics = DiagnosticVerifier.GetDiagnostics(project.GetCompilationAsync().Result, diagnosticAnalyzer).ToList(); actualCode = document.GetSyntaxRootAsync().Result.GetText().ToString(); }