-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngineOptionsAndSourceTests.cs
More file actions
256 lines (217 loc) · 11.4 KB
/
Copy pathEngineOptionsAndSourceTests.cs
File metadata and controls
256 lines (217 loc) · 11.4 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using AuroraScript.Core;
using AuroraScript.Source;
using AuroraScript.Tests.Infrastructure;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace AuroraScript.Tests;
public sealed class EngineOptionsAndSourceTests
{
[Fact]
public void RejectsNullOptions()
{
Assert.Throws<AuroraException>(() => new AuroraEngine(null!));
}
[Fact]
public void FluentOptionsReturnNewConfiguredInstances()
{
using var workspace = new TestWorkspace();
var original = EngineOptions.Default;
var configured = original
.WithCompiler(compiler => compiler.SourceResolver = AuroraScript.Core.ScriptSources.FileSystem(workspace.Root))
.WithCompiler(compiler => compiler.Mode = CompilationMode.Dynamic)
.WithOptimization(optimization => optimization.Level = OptimizeOptions.Release)
.WithRuntime(runtime => runtime.HotReload = false)
.WithOptimization(optimization => optimization.AutoModuleDirectCall = true)
.WithOptimization(optimization => optimization.ModuleConstInlining = true)
.WithOutput(output => output.Confused = true)
.WithRuntime(runtime => runtime.DateTimeFormat = "O")
.WithCompiler(compiler => compiler.ExtName = "aurora")
.WithRuntime(runtime => runtime.StringPooling = StringPoolingStrategy.None)
.WithCompiler(compiler => compiler.MaxDegreeOfParallelism = 3);
Assert.NotSame(original, configured);
Assert.Equal(ScriptPath.NormalizeBaseDirectory(workspace.Root), configured.Compiler.SourceResolver.Root);
Assert.Equal(CompilationMode.Dynamic, configured.Compiler.Mode);
Assert.Equal(OptimizeOptions.Release, configured.Optimization.Level);
Assert.False(configured.Runtime.EnableHotReload);
Assert.True(configured.Optimization.EnableAutoModuleDirectCall);
Assert.False(original.Optimization.EnableAutoModuleDirectCall);
Assert.True(configured.Optimization.EnableModuleConstInlining);
Assert.False(original.Optimization.EnableModuleConstInlining);
Assert.True(configured.Output.EnableConfused);
Assert.Equal("O", configured.Runtime.DateTimeFormat);
Assert.Equal(".aurora", configured.Compiler.ExtName);
Assert.Equal(StringPoolingStrategy.None, configured.Runtime.StringPooling);
Assert.Equal(3, configured.Compiler.MaxDegreeOfParallelism);
}
[Theory]
[InlineData(".a.b")]
[InlineData("a.b.c")]
public void RejectsInvalidExtensions(string extension)
{
Assert.Throws<ArgumentException>(() => EngineOptions.Default.WithCompiler(compiler => compiler.ExtName = extension));
}
[Fact]
public void RejectsNegativeParallelismAndNullSerializer()
{
Assert.Throws<ArgumentOutOfRangeException>(() => EngineOptions.Default.WithCompiler(compiler => compiler.MaxDegreeOfParallelism = -1));
Assert.Throws<AuroraException>(() => EngineOptions.Default.WithRuntime(runtime => runtime.JsonSerializer = null!));
}
[Fact]
public void ScriptSourcesResolveRelativeToTheirOwnRoot()
{
using var workspace = new TestWorkspace();
ScriptSource memory = workspace.MemorySource("nested/memory.as", "@module(MEMORY);");
var file = new MemorySource(workspace.Root, "nested/file.as", "@module(FILE);");
Assert.Equal(ScriptPath.GetFullPath(workspace.Root, "nested/memory.as"), memory.FullPath);
Assert.Equal(ScriptPath.GetFullPath(workspace.Root, "nested/file.as"), file.FullPath);
}
[Fact]
public async Task BuildAllowsMemorySourceAndRejectsMissingRootEntry()
{
using var workspace = new TestWorkspace();
var missing = Path.Combine(Path.GetTempPath(), "aurora-missing-" + Guid.NewGuid().ToString("N"));
var engine = new AuroraEngine(EngineOptions.Default.WithCompiler(compiler =>
compiler.SourceResolver = ScriptSources.FileSystem(missing, Encoding.UTF8)));
await engine.BuildAsync(workspace.MemorySource("main.as", "@module(TEST); export func run() { return 42; }"));
ScriptAssert.Equal(42, TestWorkspace.Execute(engine.CreateDomain(), "run"));
var error = await Assert.ThrowsAsync<FileNotFoundException>(() => engine.BuildAsync("missing.as"));
Assert.Contains("missing.as", error.FileName, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task FileSystemResolverEnumeratesAllDirectoriesUnderRoot()
{
using var workspace = new TestWorkspace();
workspace.WriteSource("bin/generated.as", "@module(GENERATED); export const value = 42;");
var resolver = ScriptSources.FileSystem(workspace.Root, Encoding.UTF8);
var sources = new List<ScriptSource>();
await foreach (var source in resolver.GetAllSourcesAsync(new ScriptSourceQuery(".as", Encoding.UTF8)))
{
sources.Add(source);
}
Assert.Contains(
sources,
source => source.FullPath.EndsWith("/bin/generated.as", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public async Task CompositeSourceResolverDoesNotOverrideFileSystemWhenRootsDiffer()
{
using var workspace = new TestWorkspace();
workspace.WriteSource("main.as", """
@module(TEST);
import config from './config';
export func run() { return config.value; }
""");
workspace.WriteSource("config.as", "@module(CONFIG); export const value = 1;");
var memory = ScriptSources.Memory("mem://override/")
.Add("config.as", "@module(CONFIG); export const value = 42;");
var resolver = ScriptSources.Composite(
memory,
ScriptSources.FileSystem(workspace.Root, Encoding.UTF8));
var engine = new AuroraEngine(EngineOptions.Default
.WithCompiler(compiler => compiler.SourceResolver = AuroraScript.Core.ScriptSources.FileSystem(workspace.Root))
.WithCompiler(compiler => compiler.Mode = CompilationMode.Dynamic)
.WithCompiler(compiler => compiler.SourceResolver = resolver));
await engine.BuildAsync("main.as");
ScriptAssert.Equal(1, TestWorkspace.Execute(engine.CreateDomain(), "run"));
}
[Fact]
public async Task CompositeSourceResolverAllowsMemorySourcesToOverrideFileSystemWhenRootsMatch()
{
using var workspace = new TestWorkspace();
workspace.WriteSource("main.as", """
@module(TEST);
import config from './config';
export func run() { return config.value; }
""");
workspace.WriteSource("config.as", "@module(CONFIG); export const value = 1;");
var memory = ScriptSources.Memory(workspace.Root)
.Add("config.as", "@module(CONFIG); export const value = 42;");
var resolver = ScriptSources.Composite(
memory,
ScriptSources.FileSystem(workspace.Root, Encoding.UTF8));
var engine = new AuroraEngine(EngineOptions.Default
.WithCompiler(compiler => compiler.SourceResolver = AuroraScript.Core.ScriptSources.FileSystem(workspace.Root))
.WithCompiler(compiler => compiler.Mode = CompilationMode.Dynamic)
.WithCompiler(compiler => compiler.SourceResolver = resolver));
await engine.BuildAsync("main.as");
ScriptAssert.Equal(42, TestWorkspace.Execute(engine.CreateDomain(), "run"));
}
[Fact]
public async Task CompositeSourceResolverAllowsMemoryParentRootToOverrideRelativeFileSystemImport()
{
using var workspace = new TestWorkspace();
var fileSystemRoot = Path.Combine(workspace.Root, "d");
Directory.CreateDirectory(fileSystemRoot);
workspace.WriteSource("d/main.as", """
@module(TEST);
import value from '../test';
export func run() { return value.number; }
""");
workspace.WriteSource("test.as", "@module(VALUE); export const number = 1;");
var memory = ScriptSources.Memory(workspace.Root)
.Add("test.as", "@module(VALUE); export const number = 42;");
var resolver = ScriptSources.Composite(
memory,
ScriptSources.FileSystem(fileSystemRoot, Encoding.UTF8));
var engine = new AuroraEngine(EngineOptions.Default
.WithCompiler(compiler => compiler.Mode = CompilationMode.Dynamic)
.WithCompiler(compiler => compiler.SourceResolver = resolver));
await engine.BuildAsync("main.as");
ScriptAssert.Equal(42, TestWorkspace.Execute(engine.CreateDomain(), "run"));
}
[Fact]
public async Task CompositeSourceResolverBuildAllSourcesDeduplicatesOnlyWithinSameRoot()
{
using var workspace = new TestWorkspace();
workspace.WriteSource("main.as", "@module(FILE_MAIN); export func run() { return 1; }");
workspace.WriteSource("config.as", "@module(FILE_CONFIG); export const value = 1;");
var memory = ScriptSources.Memory("mem://override/")
.Add("config.as", "@module(MEM_CONFIG); export const value = 42;");
var resolver = ScriptSources.Composite(
memory,
ScriptSources.FileSystem(workspace.Root, Encoding.UTF8));
var engine = new AuroraEngine(EngineOptions.Default
.WithCompiler(compiler => compiler.SourceResolver = AuroraScript.Core.ScriptSources.FileSystem(workspace.Root))
.WithCompiler(compiler => compiler.Mode = CompilationMode.Dynamic)
.WithCompiler(compiler => compiler.SourceResolver = resolver));
await engine.BuildAsync();
var domain = engine.CreateDomain();
Assert.NotSame(AuroraScript.Runtime.Types.ScriptObject.Null, domain.GetModule("FILE_MAIN"));
Assert.NotSame(AuroraScript.Runtime.Types.ScriptObject.Null, domain.GetModule("FILE_CONFIG"));
Assert.NotSame(AuroraScript.Runtime.Types.ScriptObject.Null, domain.GetModule("MEM_CONFIG"));
}
[Fact]
public async Task CompositeSourceResolverBuildAllSourcesDeduplicatesMatchingRootsByModulePath()
{
using var workspace = new TestWorkspace();
workspace.WriteSource("main.as", """
@module(TEST);
import config from './config';
export func run() { return config.value; }
""");
workspace.WriteSource("config.as", "@module(CONFIG); export const value = 1;");
var memory = ScriptSources.Memory(workspace.Root)
.Add("config.as", "@module(CONFIG); export const value = 42;");
var resolver = ScriptSources.Composite(
memory,
ScriptSources.FileSystem(workspace.Root, Encoding.UTF8));
var engine = new AuroraEngine(EngineOptions.Default
.WithCompiler(compiler => compiler.SourceResolver = AuroraScript.Core.ScriptSources.FileSystem(workspace.Root))
.WithCompiler(compiler => compiler.Mode = CompilationMode.Dynamic)
.WithCompiler(compiler => compiler.SourceResolver = resolver));
await engine.BuildAsync();
ScriptAssert.Equal(42, TestWorkspace.Execute(engine.CreateDomain(), "run"));
}
[Fact]
public async Task BuildRejectsNullSourceArrayAndNullElements()
{
using var workspace = new TestWorkspace();
var engine = workspace.CreateEngine();
await Assert.ThrowsAsync<ArgumentNullException>(() => engine.BuildAsync(default(ScriptSource[])!));
await Assert.ThrowsAsync<ArgumentNullException>(() => engine.BuildAsync([null!]));
}
}