-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathTestHelpers.cs
More file actions
152 lines (130 loc) · 4.66 KB
/
Copy pathTestHelpers.cs
File metadata and controls
152 lines (130 loc) · 4.66 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Reflection;
using System.Text;
using Microsoft.Azure.Functions.Worker;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.DurableTask.Generators.Tests.Utils;
static class TestHelpers
{
public static Task RunTestAsync<TSourceGenerator>(
string expectedFileName,
string inputSource,
string? expectedOutputSource,
bool isDurableFunctions) where TSourceGenerator : IIncrementalGenerator, new()
{
CSharpSourceGeneratorVerifier<TSourceGenerator>.Test test = new()
{
TestState =
{
Sources = { inputSource },
AdditionalReferences =
{
// Durable Task SDK
typeof(TaskActivityContext).Assembly,
},
},
};
// Only add generated source if expectedOutputSource is not null
if (expectedOutputSource != null)
{
test.TestState.GeneratedSources.Add(
(typeof(TSourceGenerator), expectedFileName, SourceText.From(expectedOutputSource, Encoding.UTF8, SourceHashAlgorithm.Sha256)));
}
if (isDurableFunctions)
{
// Durable Functions code generation is triggered by the presence of the
// Durable Functions worker extension for .NET Isolated.
Assembly functionsWorkerAbstractions = typeof(FunctionAttribute).Assembly;
test.TestState.AdditionalReferences.Add(functionsWorkerAbstractions);
Assembly functionsWorkerCore = typeof(FunctionContext).Assembly;
test.TestState.AdditionalReferences.Add(functionsWorkerCore);
// OrchestrationTriggerAttribute and ActivityTriggerAttribute are in the DurableTask extension
Assembly durableExtension = typeof(OrchestrationTriggerAttribute).Assembly;
test.TestState.AdditionalReferences.Add(durableExtension);
Assembly dependencyInjection = typeof(ActivatorUtilities).Assembly;
test.TestState.AdditionalReferences.Add(dependencyInjection);
}
return test.RunAsync();
}
public static string WrapAndFormat(string generatedClassName, string methodList, bool isDurableFunctions = false)
{
string formattedMethodList = IndentLines(spaces: 8, methodList);
string usings = @"
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DurableTask.Internal;";
if (isDurableFunctions)
{
usings += @"
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;";
}
return $@"
// <auto-generated/>
#nullable enable
{usings}
namespace Microsoft.DurableTask
{{
public static class {generatedClassName}
{{
{formattedMethodList.TrimStart()}
}}
}}
".TrimStart();
}
static string IndentLines(int spaces, string multilineText)
{
string indent = new(' ', spaces);
StringBuilder sb = new();
foreach (string line in multilineText.Trim().Split(Environment.NewLine))
{
if (line.Length > 0)
{
sb.Append(indent);
}
sb.AppendLine(line);
}
return sb.ToString().TrimEnd();
}
internal static object DeIndent(string code, int spacesToRemove)
{
StringBuilder sb = new(code.Length);
foreach (string line in code.Split(Environment.NewLine))
{
int charsToSkip = Math.Min(spacesToRemove, line.Length);
sb.AppendLine(line[charsToSkip..]);
}
return sb.ToString();
}
internal static string GetDefaultInputType(string inputType)
{
static bool IsValueType(string typeExpression)
{
// This list is obviously incomplete, but should be enhanced as necessary for testing
switch (typeExpression)
{
case "int":
case "float":
case "double":
case "byte":
case "Guid":
case "TimeSpan":
case "DateTime":
case "DateTimeOffset":
return true;
default:
Type? runtimeType = Type.GetType(typeExpression, throwOnError: false);
return runtimeType != null && runtimeType.IsValueType;
}
}
if (inputType.StartsWith("(") || inputType.EndsWith('?') || IsValueType(inputType))
{
return inputType;
}
return inputType + "?";
}
}