You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CodeFixVerifier.cs 7.1 KiB

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