forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharpScriptEngineTests.cs
More file actions
369 lines (296 loc) · 17 KB
/
CSharpScriptEngineTests.cs
File metadata and controls
369 lines (296 loc) · 17 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using Moq;
using ScriptCs.Contracts;
using ScriptCs.Engine.Roslyn;
using Should;
using Xunit;
using AutoFixture.Xunit2;
namespace ScriptCs.Tests
{
public class CSharpScriptEngineTests
{
public class TheExecuteMethod
{
private IConsole _console = new Mock<IConsole>().Object;
private IObjectSerializer _serializer = new Mock<IObjectSerializer>().Object;
private Printers _printers;
public TheExecuteMethod()
{
_printers = new Printers(_serializer);
}
[Theory, ScriptCsAutoData]
public void ShouldCreateScriptHostWithContexts(
[Frozen] Mock<IScriptHostFactory> scriptHostFactory,
[Frozen] Mock<IScriptPack> scriptPack,
ScriptPackSession scriptPackSession,
[NoAutoProperties] CSharpScriptEngine engine)
{
// Arrange
const string Code = "var a = 0;";
scriptHostFactory.Setup(f => f.CreateScriptHost(It.IsAny<IScriptPackManager>(), It.IsAny<string[]>()))
.Returns<IScriptPackManager, string[]>((p, q) => new ScriptHost(p, new ScriptEnvironment(q, _console, _printers)));
scriptPack.Setup(p => p.Initialize(It.IsAny<IScriptPackSession>()));
scriptPack.Setup(p => p.GetContext()).Returns((IScriptPackContext)null);
// Act
engine.Execute(Code, new string[0], new AssemblyReferences(), Enumerable.Empty<string>(), scriptPackSession);
// Assert
scriptHostFactory.Verify(f => f.CreateScriptHost(It.IsAny<IScriptPackManager>(), It.IsAny<string[]>()));
}
[Theory, ScriptCsAutoData]
public void ShouldReuseExistingSessionIfProvided(
[Frozen] Mock<IScriptHostFactory> scriptHostFactory,
[NoAutoProperties] CSharpTestScriptEngine engine,
ScriptPackSession scriptPackSession)
{
// Arrange
const string Code = "var a = 0;";
scriptHostFactory.Setup(f => f.CreateScriptHost(It.IsAny<IScriptPackManager>(), It.IsAny<string[]>()))
.Returns<IScriptPackManager, string[]>((p, q) => new ScriptHost(p, new ScriptEnvironment(q, _console, _printers)));
var session = new SessionState<ScriptState> { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() };
scriptPackSession.State[CommonScriptEngine.SessionKey] = session;
// Act
engine.Execute(Code, new string[0], new AssemblyReferences(), Enumerable.Empty<string>(), scriptPackSession);
// Assert
engine.Session.ShouldEqual(session.Session);
}
[Fact]
public void ShouldCreateNewSessionIfNotProvided()
{
var scriptHostFactory = new Mock<IScriptHostFactory>();
scriptHostFactory.Setup(f => f.CreateScriptHost(It.IsAny<IScriptPackManager>(), It.IsAny<string[]>()))
.Returns<IScriptPackManager, string[]>((p, q) => new ScriptHost(p, new ScriptEnvironment(q, _console, _printers)));
// Arrange
var engine = new CSharpTestScriptEngine(scriptHostFactory.Object, new TestLogProvider());
const string Code = "var a = 0;";
// Act
engine.Execute(Code, new string[0], new AssemblyReferences(), Enumerable.Empty<string>(), new ScriptPackSession(new IScriptPack[0], new string[0]));
// Assert
engine.Session.ShouldNotBeNull();
}
[Theory, ScriptCsAutoData]
public void ShouldAddNewReferencesIfTheyAreProvided(
[NoAutoProperties] CSharpTestScriptEngine engine,
ScriptPackSession scriptPackSession)
{
// Arrange
const string Code = "var a = 0;";
var session = new SessionState<ScriptState> { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() };
scriptPackSession.State[CommonScriptEngine.SessionKey] = session;
var refs = new AssemblyReferences(new[] { "System", "System.Runtime" });
// Act
engine.Execute(Code, new string[0], refs, Enumerable.Empty<string>(), scriptPackSession);
// Assert
((SessionState<ScriptState>)scriptPackSession.State[CommonScriptEngine.SessionKey]).References.Paths.Count().ShouldEqual(2);
}
[Theory, ScriptCsAutoData]
public void ShouldReturnAScriptResult(
[NoAutoProperties] CSharpTestScriptEngine engine,
ScriptPackSession scriptPackSession)
{
// Arrange
var code = string.Empty;
var session = new SessionState<ScriptState> { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() };
scriptPackSession.State[CommonScriptEngine.SessionKey] = session;
var refs = new AssemblyReferences(new[] { "System", "System.Runtime" });
// Act
var result = engine.Execute(code, new string[0], refs, Enumerable.Empty<string>(), scriptPackSession);
// Assert
result.ShouldBeType<ScriptResult>();
}
[Theory, ScriptCsAutoData]
public void ShouldReturnCompileExceptionIfCodeDoesNotCompile(
[NoAutoProperties] CSharpScriptEngine engine,
ScriptPackSession scriptPackSession)
{
// Arrange
const string Code = "this shold not compile";
var session = new SessionState<ScriptState> { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() };
scriptPackSession.State[CommonScriptEngine.SessionKey] = session;
var refs = new AssemblyReferences(new[] { "System", "System.Runtime" });
// Act
var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty<string>(), scriptPackSession);
// Assert
result.CompileExceptionInfo.ShouldNotBeNull();
}
[Theory(Skip = "this feature is not supported in Roslyn 1.0.0-rc2: see https://github.com/dotnet/roslyn/issues/1012"), ScriptCsAutoData]
public void ShouldReturnInvalidNamespacesIfCS0241Encountered(
[NoAutoProperties] CSharpScriptEngine engine,
ScriptPackSession scriptPackSession)
{
var session = new SessionState<ScriptState> { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() };
scriptPackSession.State[CommonScriptEngine.SessionKey] = session;
// Act
var result = engine.Execute(string.Empty, new string[0], new AssemblyReferences(), new[] { "foo" }, scriptPackSession);
// Assert
result.CompileExceptionInfo.ShouldNotBeNull();
result.InvalidNamespaces.ShouldContain("foo");
}
//todo: filip: this test will not even compile as it used to do reflection old roslyn assemblies!
//[Theory, ScriptCsAutoData]
//public void ShouldRemoveInvalidNamespacesFromSessionStateAndEngine(
// [Frozen] Mock<IScriptHostFactory> scriptHostFactory,
// [NoAutoProperties] CSharpScriptEngine engine,
// ScriptPackSession scriptPackSession)
//{
// var session = new SessionState<ScriptState> { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() };
// scriptPackSession.State[CommonScriptEngine.SessionKey] = session;
// // Act
// engine.Execute(string.Empty, new string[0], new AssemblyReferences(), new[] { "System", "foo" }, scriptPackSession);
// // Assert
// session.Namespaces.ShouldNotBeEmpty();
// session.Namespaces.ShouldNotContain("foo");
// var pendingNamespacesField = session.Session.GetType().GetField("pendingNamespaces", BindingFlags.Instance | BindingFlags.NonPublic);
// var pendingNamespacesValue = (ReadOnlyArray<string>)pendingNamespacesField.GetValue(session.Session);
// pendingNamespacesValue.AsEnumerable().ShouldNotBeEmpty();
// pendingNamespacesValue.AsEnumerable().ShouldNotContain("foo");
//}
[Theory, ScriptCsAutoData]
public void ShouldNotReturnCompileExceptionIfCodeDoesCompile(
[NoAutoProperties] CSharpScriptEngine engine,
ScriptPackSession scriptPackSession)
{
// Arrange
const string Code = "var theNumber = 42; //this should compile";
var session = new SessionState<ScriptState> { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() };
scriptPackSession.State[CommonScriptEngine.SessionKey] = session;
var refs = new AssemblyReferences(new[] { "System", "System.Runtime" });
// Act
var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty<string>(), scriptPackSession);
// Assert
result.CompileExceptionInfo.ShouldBeNull();
}
[Theory, ScriptCsAutoData]
public void ShouldReturnExecuteExceptionIfCodeExecutionThrowsException(
[NoAutoProperties] CSharpScriptEngine engine,
ScriptPackSession scriptPackSession)
{
// Arrange
const string Code = "throw new System.Exception(); //this should throw an Exception";
var session = new SessionState<ScriptState> { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() };
scriptPackSession.State[CommonScriptEngine.SessionKey] = session;
var refs = new AssemblyReferences(new[] { "System", "System.Runtime" });
// Act
var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty<string>(), scriptPackSession);
// Assert
result.ExecuteExceptionInfo.ShouldNotBeNull();
}
[Theory, ScriptCsAutoData]
public void ShouldNotReturnExecuteExceptionIfCodeExecutionDoesNotThrowAnException(
[NoAutoProperties] CSharpScriptEngine engine,
ScriptPackSession scriptPackSession)
{
// Arrange
const string Code = "var theNumber = 42; //this should not throw an Exception";
var session = new SessionState<ScriptState> { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() };
scriptPackSession.State[CommonScriptEngine.SessionKey] = session;
var refs = new AssemblyReferences(new[] { "System", "System.Runtime" });
// Act
var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty<string>(), scriptPackSession);
// Assert
result.ExecuteExceptionInfo.ShouldBeNull();
}
[Theory, ScriptCsAutoData]
public void ShouldReturnReturnValueIfCodeExecutionReturnsValue(
[NoAutoProperties] CSharpScriptEngine engine,
ScriptPackSession scriptPackSession)
{
const string Code = "\"Hello\" //this should return \"Hello\"";
var session = new SessionState<ScriptState> { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() };
scriptPackSession.State[CommonScriptEngine.SessionKey] = session;
var refs = new AssemblyReferences(new[] { "System", "System.Runtime" });
// Act
var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty<string>(), scriptPackSession);
// Assert
result.ReturnValue.ShouldEqual("Hello");
}
[Theory, ScriptCsAutoData]
public void ShouldNotReturnReturnValueIfCodeExecutionDoesNotReturnValue(
[NoAutoProperties] CSharpScriptEngine engine,
ScriptPackSession scriptPackSession)
{
// Arrange
const string Code = "var theNumber = 42; //this should not return a value";
var session = new SessionState<ScriptState> { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() };
scriptPackSession.State[CommonScriptEngine.SessionKey] = session;
var refs = new AssemblyReferences(new[] { "System", "System.Runtime" });
// Act
var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty<string>(), scriptPackSession);
// Assert
result.ReturnValue.ShouldBeNull();
}
[Theory, ScriptCsAutoData]
public void ShouldNotMarkSubmissionsAsIncompleteWhenRunningScript(
[NoAutoProperties] CSharpScriptEngine engine,
ScriptPackSession scriptPackSession)
{
// Arrange
const string Code = "class test {";
var session = new SessionState<ScriptState> { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() };
scriptPackSession.State[CommonScriptEngine.SessionKey] = session;
var refs = new AssemblyReferences(new[] { "System", "System.Runtime" });
engine.FileName = "test.csx";
// Act
var result = engine.Execute(
Code, new string[0], refs, Enumerable.Empty<string>(), scriptPackSession);
// Assert
result.IsCompleteSubmission.ShouldBeTrue();
result.CompileExceptionInfo.ShouldNotBeNull();
}
[Theory, ScriptCsAutoData]
public void ShouldCompileWhenUsingClassesFromAPassedAssemblyInstance(
[Frozen] Mock<IScriptHostFactory> scriptHostFactory,
[Frozen] ScriptPackSession scriptPackSession)
{
// Arrange
const string Code = "var x = new ScriptCs.Tests.TestMarkerClass();";
scriptHostFactory.Setup(f => f.CreateScriptHost(It.IsAny<IScriptPackManager>(), It.IsAny<string[]>()))
.Returns<IScriptPackManager, string[]>((p, q) => new ScriptHost(p, new ScriptEnvironment(q, _console, _printers)));
var engine = new CSharpScriptEngine(scriptHostFactory.Object, new TestLogProvider());
var session = new SessionState<ScriptState> { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() };
scriptPackSession.State[CommonScriptEngine.SessionKey] = session;
var refs = new AssemblyReferences(new[] { Assembly.GetExecutingAssembly() }, new[] { "System", "System.Runtime" });
// Act
var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty<string>(), scriptPackSession);
// Assert
result.CompileExceptionInfo.ShouldBeNull();
result.ExecuteExceptionInfo.ShouldBeNull();
}
[Theory, ScriptCsAutoData]
public void ShouldInitializeScriptLibraryWrapperHost(
[Frozen] Mock<IScriptHostFactory> scriptHostFactory,
Mock<IScriptPackManager> manager,
[NoAutoProperties] CSharpScriptEngine engine,
ScriptPackSession scriptPackSession
)
{
// Arrange
const string Code = "var theNumber = 42; //this should compile";
var refs = new AssemblyReferences(new[] { "System", "System.Runtime" });
scriptHostFactory.Setup(s => s.CreateScriptHost(It.IsAny<IScriptPackManager>(), It.IsAny<string[]>()))
.Returns(new ScriptHost(manager.Object, null));
// Act
engine.Execute(Code, new string[0], refs, Enumerable.Empty<string>(), scriptPackSession);
// Assert
ScriptLibraryWrapper.ScriptHost.ShouldNotEqual(null);
}
}
public class CSharpTestScriptEngine : CSharpScriptEngine
{
public CSharpTestScriptEngine(IScriptHostFactory scriptHostFactory, TestLogProvider logProvider)
: base(scriptHostFactory, logProvider)
{
}
public ScriptState Session { get; private set; }
protected override ScriptResult Execute(string code, object globals, SessionState<ScriptState> sessionState)
{
base.Execute(code, globals, sessionState);
Session = sessionState.Session;
return ScriptResult.Empty;
}
}
}
public class TestMarkerClass { }
}