CodeFixVerifier.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // Most of the code in this file comes from the default Roslyn Analyzer project template
  4. using System.Globalization;
  5. using Microsoft.CodeAnalysis;
  6. using Microsoft.CodeAnalysis.CodeActions;
  7. using Microsoft.CodeAnalysis.CodeFixes;
  8. using Microsoft.CodeAnalysis.Diagnostics;
  9. using Microsoft.CodeAnalysis.Formatting;
  10. namespace TestHelper;
  11. /// <summary>
  12. /// Superclass of all Unit tests made for diagnostics with codefixes.
  13. /// Contains methods used to verify correctness of codefixes
  14. /// </summary>
  15. public abstract partial class CodeFixVerifier : DiagnosticVerifier
  16. {
  17. /// <summary>
  18. /// Returns the codefix being tested (C#) - to be implemented in non-abstract class
  19. /// </summary>
  20. /// <returns>The CodeFixProvider to be used for CSharp code</returns>
  21. protected virtual CodeFixProvider GetCSharpCodeFixProvider()
  22. {
  23. return null;
  24. }
  25. /// <summary>
  26. /// Returns the codefix being tested (VB) - to be implemented in non-abstract class
  27. /// </summary>
  28. /// <returns>The CodeFixProvider to be used for VisualBasic code</returns>
  29. protected virtual CodeFixProvider GetBasicCodeFixProvider()
  30. {
  31. return null;
  32. }
  33. /// <summary>
  34. /// Called to test a C# codefix when applied on the inputted string as a source
  35. /// </summary>
  36. /// <param name="oldSource">A class in the form of a string before the CodeFix was applied to it</param>
  37. /// <param name="newSource">A class in the form of a string after the CodeFix was applied to it</param>
  38. /// <param name="codeFixIndex">Index determining which codefix to apply if there are multiple</param>
  39. /// <param name="allowNewCompilerDiagnostics">A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied</param>
  40. protected void VerifyCSharpFix(string oldSource, string newSource, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false)
  41. {
  42. VerifyFix(LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer(), GetCSharpCodeFixProvider(), oldSource, newSource, codeFixIndex, allowNewCompilerDiagnostics);
  43. }
  44. /// <summary>
  45. /// Called to test a VB codefix when applied on the inputted string as a source
  46. /// </summary>
  47. /// <param name="oldSource">A class in the form of a string before the CodeFix was applied to it</param>
  48. /// <param name="newSource">A class in the form of a string after the CodeFix was applied to it</param>
  49. /// <param name="codeFixIndex">Index determining which codefix to apply if there are multiple</param>
  50. /// <param name="allowNewCompilerDiagnostics">A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied</param>
  51. protected void VerifyBasicFix(string oldSource, string newSource, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false)
  52. {
  53. VerifyFix(LanguageNames.VisualBasic, GetBasicDiagnosticAnalyzer(), GetBasicCodeFixProvider(), oldSource, newSource, codeFixIndex, allowNewCompilerDiagnostics);
  54. }
  55. /// <summary>
  56. /// General verifier for codefixes.
  57. /// Creates a Document from the source string, then gets diagnostics on it and applies the relevant codefixes.
  58. /// Then gets the string after the codefix is applied and compares it with the expected result.
  59. /// Note: If any codefix causes new diagnostics to show up, the test fails unless allowNewCompilerDiagnostics is set to true.
  60. /// </summary>
  61. /// <param name="language">The language the source code is in</param>
  62. /// <param name="analyzer">The analyzer to be applied to the source code</param>
  63. /// <param name="codeFixProvider">The codefix to be applied to the code wherever the relevant Diagnostic is found</param>
  64. /// <param name="oldSource">A class in the form of a string before the CodeFix was applied to it</param>
  65. /// <param name="newSource">A class in the form of a string after the CodeFix was applied to it</param>
  66. /// <param name="codeFixIndex">Index determining which codefix to apply if there are multiple</param>
  67. /// <param name="allowNewCompilerDiagnostics">A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied</param>
  68. private void VerifyFix(string language, DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, string oldSource, string newSource, int? codeFixIndex, bool allowNewCompilerDiagnostics)
  69. {
  70. var document = CreateDocument(oldSource, language);
  71. var analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, new[] { document });
  72. var compilerDiagnostics = GetCompilerDiagnostics(document);
  73. var attempts = analyzerDiagnostics.Length;
  74. for (int i = 0; i < attempts; ++i)
  75. {
  76. var actions = new List<CodeAction>();
  77. var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None);
  78. codeFixProvider.RegisterCodeFixesAsync(context).Wait();
  79. if (!actions.Any())
  80. {
  81. break;
  82. }
  83. if (codeFixIndex != null)
  84. {
  85. document = ApplyFix(document, actions.ElementAt((int)codeFixIndex));
  86. break;
  87. }
  88. document = ApplyFix(document, actions.ElementAt(0));
  89. analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, new[] { document });
  90. var newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document));
  91. //check if applying the code fix introduced any new compiler diagnostics
  92. if (!allowNewCompilerDiagnostics && newCompilerDiagnostics.Any())
  93. {
  94. // Format and get the compiler diagnostics again so that the locations make sense in the output
  95. document = document.WithSyntaxRoot(Formatter.Format(document.GetSyntaxRootAsync().Result, Formatter.Annotation, document.Project.Solution.Workspace));
  96. newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document));
  97. Assert.True(false,
  98. string.Format(
  99. CultureInfo.InvariantCulture,
  100. "Fix introduced new compiler diagnostics:\r\n{0}\r\n\r\nNew document:\r\n{1}\r\n",
  101. string.Join("\r\n", newCompilerDiagnostics.Select(d => d.ToString())),
  102. document.GetSyntaxRootAsync().Result.ToFullString()));
  103. }
  104. //check if there are analyzer diagnostics left after the code fix
  105. if (!analyzerDiagnostics.Any())
  106. {
  107. break;
  108. }
  109. }
  110. //after applying all of the code fixes, compare the resulting string to the inputted one
  111. var actual = GetStringFromDocument(document);
  112. Assert.Equal(newSource, actual);
  113. }
  114. }