CSharpCodeFixVerifier.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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.Verifiers;
  8. namespace Godot.SourceGenerators.Tests;
  9. public static class CSharpCodeFixVerifier<TCodeFix, TAnalyzer>
  10. where TCodeFix : CodeFixProvider, new()
  11. where TAnalyzer : DiagnosticAnalyzer, new()
  12. {
  13. public class Test : CSharpCodeFixTest<TAnalyzer, TCodeFix, XUnitVerifier>
  14. {
  15. public Test()
  16. {
  17. ReferenceAssemblies = Constants.Net80;
  18. SolutionTransforms.Add((Solution solution, ProjectId projectId) =>
  19. {
  20. Project project = solution.GetProject(projectId)!
  21. .AddMetadataReference(Constants.GodotSharpAssembly.CreateMetadataReference());
  22. return project.Solution;
  23. });
  24. }
  25. }
  26. public static Task Verify(string sources, string fixedSources)
  27. {
  28. return MakeVerifier(sources, fixedSources).RunAsync();
  29. }
  30. public static Test MakeVerifier(string source, string results)
  31. {
  32. var verifier = new Test();
  33. verifier.TestCode = File.ReadAllText(Path.Combine(Constants.SourceFolderPath, source));
  34. verifier.FixedCode = File.ReadAllText(Path.Combine(Constants.GeneratedSourceFolderPath, results));
  35. verifier.TestState.AnalyzerConfigFiles.Add(("/.globalconfig", $"""
  36. is_global = true
  37. build_property.GodotProjectDir = {Constants.ExecutingAssemblyPath}
  38. """));
  39. return verifier;
  40. }
  41. }