-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathTestGenerators.cs
125 lines (101 loc) · 4.38 KB
/
TestGenerators.cs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
namespace Roslyn.Test.Utilities.TestGenerators
{
internal class SingleFileTestGenerator : ISourceGenerator
{
private readonly string _content;
private readonly string _hintName;
public SingleFileTestGenerator(string content, string hintName = "generatedFile")
{
_content = content;
_hintName = hintName;
}
public void Execute(GeneratorExecutionContext context)
{
context.AddSource(this._hintName, SourceText.From(_content, Encoding.UTF8));
}
public void Initialize(GeneratorInitializationContext context)
{
}
}
internal class SingleFileTestGenerator2 : SingleFileTestGenerator
{
public SingleFileTestGenerator2(string content, string hintName = "generatedFile") : base(content, hintName)
{
}
}
internal class CallbackGenerator : ISourceGenerator
{
private readonly Action<GeneratorInitializationContext> _onInit;
private readonly Action<GeneratorExecutionContext> _onExecute;
private readonly string? _source;
public CallbackGenerator(Action<GeneratorInitializationContext> onInit, Action<GeneratorExecutionContext> onExecute, string? source = "")
{
_onInit = onInit;
_onExecute = onExecute;
_source = source;
}
public void Execute(GeneratorExecutionContext context)
{
_onExecute(context);
if (!string.IsNullOrWhiteSpace(_source))
{
context.AddSource("source", SourceText.From(_source, Encoding.UTF8));
}
}
public void Initialize(GeneratorInitializationContext context) => _onInit(context);
}
internal class CallbackGenerator2 : CallbackGenerator
{
public CallbackGenerator2(Action<GeneratorInitializationContext> onInit, Action<GeneratorExecutionContext> onExecute, string? source = "") : base(onInit, onExecute, source)
{
}
}
internal class InMemoryAdditionalText : AdditionalText
{
private readonly SourceText _content;
public InMemoryAdditionalText(string path, string content)
{
Path = path;
_content = SourceText.From(content, Encoding.UTF8);
}
public override string Path { get; }
public override SourceText GetText(CancellationToken cancellationToken = default) => _content;
}
internal sealed class PipelineCallbackGenerator : IIncrementalGenerator
{
private readonly Action<IncrementalGeneratorInitializationContext> _registerPipelineCallback;
public PipelineCallbackGenerator(Action<IncrementalGeneratorInitializationContext> registerPipelineCallback)
{
_registerPipelineCallback = registerPipelineCallback;
}
public void Initialize(IncrementalGeneratorInitializationContext context) => _registerPipelineCallback(context);
}
internal sealed class PipelineCallbackGenerator2 : IIncrementalGenerator
{
private readonly Action<IncrementalGeneratorInitializationContext> _registerPipelineCallback;
public PipelineCallbackGenerator2(Action<IncrementalGeneratorInitializationContext> registerPipelineCallback)
{
_registerPipelineCallback = registerPipelineCallback;
}
public void Initialize(IncrementalGeneratorInitializationContext context) => _registerPipelineCallback(context);
}
internal sealed class IncrementalAndSourceCallbackGenerator : CallbackGenerator, IIncrementalGenerator
{
private readonly Action<IncrementalGeneratorInitializationContext> _onInit;
public IncrementalAndSourceCallbackGenerator(Action<GeneratorInitializationContext> onInit, Action<GeneratorExecutionContext> onExecute, Action<IncrementalGeneratorInitializationContext> onIncrementalInit)
: base(onInit, onExecute)
{
_onInit = onIncrementalInit;
}
public void Initialize(IncrementalGeneratorInitializationContext context) => _onInit(context);
}
}