Debian Codegraph @

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

raw symbols SCIP
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.Text;
11
12 namespace Godot.SourceGenerators.Tests;
13
14 public static class CSharpAnalyzerVerifier<TAnalyzer>
15 where TAnalyzer : DiagnosticAnalyzer, new()
16 {
17 public const LanguageVersion LangVersion = LanguageVersion.CSharp11;
18
19 public class Test : CSharpAnalyzerTest<TAnalyzer, DefaultVerifier>
20 {
21 public Test()
22 {
23 ReferenceAssemblies = Constants.Net80;
24
25 SolutionTransforms.Add((Solution solution, ProjectId projectId) =>
26 {
27 Project project =
28 solution.GetProject(projectId)!.AddMetadataReference(Constants.GodotSharpAssembly
29 .CreateMetadataReference()).WithParseOptions(new CSharpParseOptions(LangVersion));
30
31 return project.Solution;
32 });
33 }
34 }
35
36 public static Task Verify(string sources, params DiagnosticResult[] expected)
37 {
38 return MakeVerifier(new string[] { sources }, expected).RunAsync();
39 }
40
41 public static Test MakeVerifier(ICollection<string> sources, params DiagnosticResult[] expected)
42 {
43 var verifier = new Test();
44
45 verifier.TestState.AnalyzerConfigFiles.Add(("/.globalconfig", $"""
46 is_global = true
47 build_property.GodotProjectDir = {Constants.ExecutingAssemblyPath}
48 """));
49
50 verifier.TestState.Sources.AddRange(sources.Select(source =>
51 {
52 return (source, SourceText.From(File.ReadAllText(Path.Combine(Constants.SourceFolderPath, source))));
53 }));
54
55 verifier.ExpectedDiagnostics.AddRange(expected);
56 return verifier;
57 }
58 }