-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratorDriverExtensions.cs
More file actions
61 lines (54 loc) · 1.9 KB
/
Copy pathGeneratorDriverExtensions.cs
File metadata and controls
61 lines (54 loc) · 1.9 KB
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
57
58
59
60
61
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
namespace Cutout.Tests.Extensions;
public sealed class TestAdditionalFile(string path, SourceText text) : AdditionalText
{
public override string Path => $"<global namespace>.{path}";
public override SourceText GetText(CancellationToken cancellationToken)
{
return text;
}
}
public static class GeneratorDriverExtensions
{
internal static GeneratorDriver BuildDriver(
this string? source,
ImmutableArray<AdditionalText> additionalTexts
)
{
var compilation = CSharpCompilation.Create(
"name",
source != null ? [CSharpSyntaxTree.ParseText(source)] : [],
[
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(TemplateAttribute).Assembly.Location),
],
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
var generator = new TemplateSourceGenerator();
var driver = CSharpGeneratorDriver.Create(generator).AddAdditionalTexts(additionalTexts);
return driver.RunGenerators(compilation);
}
internal static Task VerifyTemplate(
this string? source,
[CallerFilePath] string sourceFile = ""
)
{
var driver = $$"""
using System;
using Cutout;
internal static class Test
{
{{source}}
}
""".BuildDriver([]);
return Verify(driver, sourceFile: sourceFile).IgnoreStandardSupportCode();
}
internal static SettingsTask IgnoreStandardSupportCode(this SettingsTask settings)
{
return settings.IgnoreGeneratedResult(x => x.HintName is "TemplateAttribute.g.cs");
}
}