CSharpAnalyzerVerifier.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.CodeAnalysis;
  6. using Microsoft.CodeAnalysis.CSharp;
  7. using Microsoft.CodeAnalysis.CSharp.Testing;
  8. using Microsoft.CodeAnalysis.Diagnostics;
  9. using Microsoft.CodeAnalysis.Testing;
  10. using Microsoft.CodeAnalysis.Testing.Verifiers;
  11. using Microsoft.CodeAnalysis.Text;
  12. namespace Godot.SourceGenerators.Tests;
  13. public static class CSharpAnalyzerVerifier<TAnalyzer>
  14. where TAnalyzer : DiagnosticAnalyzer, new()
  15. {
  16. public const LanguageVersion LangVersion = LanguageVersion.CSharp11;
  17. public class Test : CSharpAnalyzerTest<TAnalyzer, XUnitVerifier>
  18. {
  19. public Test()
  20. {
  21. ReferenceAssemblies = Constants.Net80;
  22. SolutionTransforms.Add((Solution solution, ProjectId projectId) =>
  23. {
  24. Project project =
  25. solution.GetProject(projectId)!.AddMetadataReference(Constants.GodotSharpAssembly
  26. .CreateMetadataReference()).WithParseOptions(new CSharpParseOptions(LangVersion));
  27. return project.Solution;
  28. });
  29. }
  30. }
  31. public static Task Verify(string sources, params DiagnosticResult[] expected)
  32. {
  33. return MakeVerifier(new string[] { sources }, expected).RunAsync();
  34. }
  35. public static Test MakeVerifier(ICollection<string> sources, params DiagnosticResult[] expected)
  36. {
  37. var verifier = new Test();
  38. verifier.TestState.AnalyzerConfigFiles.Add(("/.globalconfig", $"""
  39. is_global = true
  40. build_property.GodotProjectDir = {Constants.ExecutingAssemblyPath}
  41. """));
  42. verifier.TestState.Sources.AddRange(sources.Select(source =>
  43. {
  44. return (source, SourceText.From(File.ReadAllText(Path.Combine(Constants.SourceFolderPath, source))));
  45. }));
  46. verifier.ExpectedDiagnostics.AddRange(expected);
  47. return verifier;
  48. }
  49. }