private void VerifyCSharp(Dictionary <string, string> sources, DiagnosticResult[] expectedResults, params FixResult[] fixResults) { var analyzer = GetCSharpDiagnosticAnalyzer(); var fix = GetCSharpCodeFixProvider(); var originalProject = CreateProject(sources); var diagnostics = GetDiagnostics(originalProject, analyzer); VerifyDiagnosticResults(diagnostics, analyzer, expectedResults); foreach (var fixResult in fixResults) { var project = ApplyFix(originalProject, analyzer, fix, fixResult.Index); var expectedSources = fixResult.ExpectedSources; if (expectedSources == null || !expectedSources.Any()) { return; } var actualSources = new Dictionary <string, string>(); foreach (var doc in project.Documents) { var code = CodeFixVerifier.GetStringFromDocument(doc); actualSources.Add(doc.Name, code); } foreach (var expected in expectedSources.Keys) { var e = expected.Replace(".csx", "").Replace(".cs", ""); Assert.True(actualSources.ContainsKey(e + ".cs") || actualSources.ContainsKey(e + ".csx"), $"File {e}.cs is required but not generated."); } foreach (var item in actualSources) { var actual = item.Value; var e = item.Key.Replace(".csx", "").Replace(".cs", ""); if (expectedSources.TryGetValue(e + ".cs", out var expected) || expectedSources.TryGetValue(e + ".csx", out expected)) { Assert.Equal(expected, actual); } else { Assert.True(false); } } } }
/// <summary> /// General verifier for codefixes. /// Creates a Document from the source string, then gets diagnostics on it and applies the relevant codefixes. /// Then gets the string after the codefix is applied and compares it with the expected result. /// Note: If any codefix causes new diagnostics to show up, the test fails unless allowNewCompilerDiagnostics is set to true. /// </summary> /// <param name="language">The language the source code is in</param> /// <param name="analyzer">The analyzer to be applied to the source code</param> /// <param name="codeFixProvider">The codefix to be applied to the code wherever the relevant Diagnostic is found</param> /// <param name="oldSource">A class in the form of a string before the CodeFix was applied to it</param> /// <param name="newSource">A class in the form of a string after the CodeFix was applied to it</param> /// <param name="codeFixIndex">Index determining which codefix to apply if there are multiple</param> /// <param name="allowNewCompilerDiagnostics">A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied</param> private void VerifyFix(string language, DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, string oldSource, string newSource, int?codeFixIndex, bool allowNewCompilerDiagnostics) { var document = DiagnosticVerifier.CreateDocument(oldSource, language); var analyzerDiagnostics = DiagnosticVerifier.GetSortedDiagnosticsFromDocuments(analyzer, new[] { document }); var compilerDiagnostics = CodeFixVerifier.GetCompilerDiagnostics(document); var attempts = analyzerDiagnostics.Length; for (int i = 0; i < attempts; ++i) { var actions = new List <CodeAction>(); var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None); codeFixProvider.RegisterCodeFixesAsync(context).Wait(); if (!actions.Any()) { break; } if (codeFixIndex != null) { document = CodeFixVerifier.ApplyFix(document, actions.ElementAt((int)codeFixIndex)); break; } document = CodeFixVerifier.ApplyFix(document, actions.ElementAt(0)); analyzerDiagnostics = DiagnosticVerifier.GetSortedDiagnosticsFromDocuments(analyzer, new[] { document }); var newCompilerDiagnostics = CodeFixVerifier.GetNewDiagnostics(compilerDiagnostics, CodeFixVerifier.GetCompilerDiagnostics(document)); //check if applying the code fix introduced any new compiler diagnostics if (!allowNewCompilerDiagnostics && newCompilerDiagnostics.Any()) { // Format and get the compiler diagnostics again so that the locations make sense in the output document = document.WithSyntaxRoot(Formatter.Format(document.GetSyntaxRootAsync().Result, Formatter.Annotation, document.Project.Solution.Workspace)); newCompilerDiagnostics = CodeFixVerifier.GetNewDiagnostics(compilerDiagnostics, CodeFixVerifier.GetCompilerDiagnostics(document)); Assert.True(false, string.Format("Fix introduced new compiler diagnostics:\r\n{0}\r\n\r\nNew document:\r\n{1}\r\n", string.Join("\r\n", newCompilerDiagnostics.Select(d => d.ToString())), document.GetSyntaxRootAsync().Result.ToFullString())); } //check if there are analyzer diagnostics left after the code fix if (!analyzerDiagnostics.Any()) { break; } } //after applying all of the code fixes, compare the resulting string to the inputted one var actual = CodeFixVerifier.GetStringFromDocument(document); Assert.Equal(newSource, actual); }