Debian Codegraph @

modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpSourceGeneratorVerifier.cs

raw symbols SCIP
1 using System.Collections.Generic;
2 using System.IO;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using Microsoft.CodeAnalysis;
7 using Microsoft.CodeAnalysis.CSharp.Testing;
8 using Microsoft.CodeAnalysis.Testing;
9 using Microsoft.CodeAnalysis.Text;
10
11 namespace Godot.SourceGenerators.Tests;
12
13 public static class CSharpSourceGeneratorVerifier<TSourceGenerator>
14 where TSourceGenerator : ISourceGenerator, new()
15 {
16 public class Test : CSharpSourceGeneratorTest<TSourceGenerator, DefaultVerifier>
17 {
18 public Test()
19 {
20 ReferenceAssemblies = Constants.Net80;
21
22 SolutionTransforms.Add((Solution solution, ProjectId projectId) =>
23 {
24 Project project = solution.GetProject(projectId)!
25 .AddMetadataReference(Constants.GodotSharpAssembly.CreateMetadataReference());
26
27 return project.Solution;
28 });
29 }
30 }
31
32 public static Task Verify(string source, params string[] generatedSources)
33 {
34 return Verify(new string[] { source }, generatedSources);
35 }
36
37 public static Task VerifyNoCompilerDiagnostics(string source, params string[] generatedSources)
38 {
39 return VerifyNoCompilerDiagnostics(new string[] { source }, generatedSources);
40 }
41
42 public static Task Verify(ICollection<string> sources, params string[] generatedSources)
43 {
44 return MakeVerifier(sources, generatedSources).RunAsync();
45 }
46
47 public static Task VerifyNoCompilerDiagnostics(ICollection<string> sources, params string[] generatedSources)
48 {
49 var verifier = MakeVerifier(sources, generatedSources);
50 verifier.CompilerDiagnostics = CompilerDiagnostics.None;
51 return verifier.RunAsync();
52 }
53
54 public static Test MakeVerifier(ICollection<string> sources, ICollection<string> generatedSources)
55 {
56 var verifier = new Test();
57
58 verifier.TestState.AnalyzerConfigFiles.Add(("/.globalconfig", $"""
59 is_global = true
60 build_property.GodotProjectDir = {Constants.ExecutingAssemblyPath}
61 """));
62
63 verifier.TestState.Sources.AddRange(sources.Select(source => (
64 source,
65 SourceText.From(File.ReadAllText(Path.Combine(Constants.SourceFolderPath, source)))
66 )));
67
68 verifier.TestState.GeneratedSources.AddRange(generatedSources.Select(generatedSource => (
69 FullGeneratedSourceName(generatedSource),
70 SourceText.From(File.ReadAllText(Path.Combine(Constants.GeneratedSourceFolderPath, generatedSource)), Encoding.UTF8)
71 )));
72
73 return verifier;
74 }
75
76 private static string FullGeneratedSourceName(string name)
77 {
78 var generatorType = typeof(TSourceGenerator);
79 return Path.Combine(generatorType.Namespace!, generatorType.FullName!, name);
80 }
81 }