CSharpCodeFixVerifier.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.IO;
  2. using System.Threading.Tasks;
  3. using Microsoft.CodeAnalysis;
  4. using Microsoft.CodeAnalysis.CodeFixes;
  5. using Microsoft.CodeAnalysis.CSharp.Testing;
  6. using Microsoft.CodeAnalysis.Diagnostics;
  7. using Microsoft.CodeAnalysis.Testing;
  8. using Microsoft.CodeAnalysis.Testing.Verifiers;
  9. namespace Godot.SourceGenerators.Tests;
  10. public static class CSharpCodeFixVerifier<TCodeFix, TAnalyzer>
  11. where TCodeFix : CodeFixProvider, new()
  12. where TAnalyzer : DiagnosticAnalyzer, new()
  13. {
  14. public class Test : CSharpCodeFixTest<TAnalyzer, TCodeFix, XUnitVerifier>
  15. {
  16. public Test()
  17. {
  18. ReferenceAssemblies = ReferenceAssemblies.Net.Net60;
  19. SolutionTransforms.Add((Solution solution, ProjectId projectId) =>
  20. {
  21. Project project = solution.GetProject(projectId)!
  22. .AddMetadataReference(Constants.GodotSharpAssembly.CreateMetadataReference());
  23. return project.Solution;
  24. });
  25. }
  26. }
  27. public static Task Verify(string sources, string fixedSources)
  28. {
  29. return MakeVerifier(sources, fixedSources).RunAsync();
  30. }
  31. public static Test MakeVerifier(string source, string results)
  32. {
  33. var verifier = new Test();
  34. verifier.TestCode = File.ReadAllText(Path.Combine(Constants.SourceFolderPath, source));
  35. verifier.FixedCode = File.ReadAllText(Path.Combine(Constants.GeneratedSourceFolderPath, results));
  36. verifier.TestState.AnalyzerConfigFiles.Add(("/.globalconfig", $"""
  37. is_global = true
  38. build_property.GodotProjectDir = {Constants.ExecutingAssemblyPath}
  39. """));
  40. return verifier;
  41. }
  42. }