summaryrefslogtreecommitdiffstats
path: root/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpAnalyzerVerifier.cs
blob: e3e7373b2e1bebd25423d25eafd24577d3f14bae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;
using Microsoft.CodeAnalysis.Text;

namespace Godot.SourceGenerators.Tests;

public static class CSharpAnalyzerVerifier<TAnalyzer>
where TAnalyzer : DiagnosticAnalyzer, new()
{
    public class Test : CSharpAnalyzerTest<TAnalyzer, XUnitVerifier>
    {
        public Test()
        {
            ReferenceAssemblies = ReferenceAssemblies.Net.Net60;

            SolutionTransforms.Add((Solution solution, ProjectId projectId) =>
            {
                Project project =
                    solution.GetProject(projectId)!.AddMetadataReference(Constants.GodotSharpAssembly
                        .CreateMetadataReference());

                return project.Solution;
            });
        }
    }

    public static Task Verify(string sources, params DiagnosticResult[] expected)
    {
        return MakeVerifier(new string[] { sources }, expected).RunAsync();
    }

    public static Test MakeVerifier(ICollection<string> sources, params DiagnosticResult[] expected)
    {
        var verifier = new Test();

        verifier.TestState.AnalyzerConfigFiles.Add(("/.globalconfig", $"""
        is_global = true
        build_property.GodotProjectDir = {Constants.ExecutingAssemblyPath}
        """));

        verifier.TestState.Sources.AddRange(sources.Select(source =>
        {
            return (source, SourceText.From(File.ReadAllText(Path.Combine(Constants.SourceFolderPath, source))));
        }));

        verifier.ExpectedDiagnostics.AddRange(expected);
        return verifier;
    }
}