Repository navigation
Expand file tree
/
Copy pathScriptTests.cs
More file actions
179 lines (156 loc) · 5.77 KB
/
Copy pathScriptTests.cs
File metadata and controls
179 lines (156 loc) · 5.77 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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CodeAnalysis.Scripting.CSharp;
using Roslyn.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.Scripting.CSharp.Test
{
public class ScriptTests : CSharpTestBase
{
[Fact]
public void TestCreateScript()
{
var script = CSharpScript.Create("1 + 2");
Assert.Equal("1 + 2", script.Code);
}
[Fact]
public void TestGetCompilation()
{
var script = CSharpScript.Create("1 + 2");
var compilation = script.GetCompilation();
Assert.Equal(script.Code, compilation.SyntaxTrees.First().GetText().ToString());
}
[Fact]
public void TestCreateScriptDelegate()
{
// create a delegate for the entire script
var script = CSharpScript.Create("1 + 2");
var fn = script.CreateDelegate();
var value = fn();
Assert.Equal(3, value);
}
[Fact]
public void TestRunScript()
{
var result = CSharpScript.Run("1 + 2");
Assert.Equal(3, result.ReturnValue);
}
[Fact]
public void TestCreateAndRunScript()
{
var script = CSharpScript.Create("1 + 2");
var result = script.Run();
Assert.Same(script, result.Script);
Assert.Equal(3, result.ReturnValue);
}
[Fact]
public void TestEvalScript()
{
var value = CSharpScript.Eval("1 + 2");
Assert.Equal(3, value);
}
[Fact]
public void TestRunScriptWithSpecifiedReturnType()
{
var result = CSharpScript.Create("1 + 2").WithReturnType(typeof(int)).Run();
Assert.Equal(3, result.ReturnValue);
}
[Fact]
public void TestRunVoidScript()
{
var result = CSharpScript.Run("Console.WriteLine(0);");
}
public class Globals
{
public int X;
public int Y;
}
[Fact]
public void TestRunScriptWithGlobals()
{
var result = CSharpScript.Run("X + Y", new Globals { X = 1, Y = 2 });
Assert.Equal(3, result.ReturnValue);
}
[Fact]
public void TestRunCreatedScriptWithExpectedGlobals()
{
var script = CSharpScript.Create("X + Y").WithGlobalsType(typeof(Globals));
var result = script.Run(new Globals { X = 1, Y = 2 });
Assert.Equal(3, result.ReturnValue);
Assert.Same(script, result.Script);
}
[Fact]
public void TestRunCreatedScriptWithUnexpectedGlobals()
{
var script = CSharpScript.Create("X + Y");
var result = script.Run(new Globals { X = 1, Y = 2 });
Assert.Equal(3, result.ReturnValue);
// the end state of running the script should be based on a different script instance because of the globals
// not matching the original script definition.
Assert.NotSame(script, result.Script);
}
[Fact]
public void TestRunScriptWithScriptState()
{
// run a script using another scripts end state as the starting state (globals)
var result = CSharpScript.Run("int X = 100;");
var result2 = CSharpScript.Run("X + X", result);
Assert.Equal(200, result2.ReturnValue);
}
[Fact]
public void TestRepl()
{
string[] submissions = new[]
{
"int x = 100;",
"int y = x * x;",
"x + y"
};
object input = null;
ScriptState result = null;
foreach (var submission in submissions)
{
result = CSharpScript.Run(submission, input);
input = result;
}
Assert.Equal(10100, result.ReturnValue);
}
[Fact]
public void TestCreateMethodDelegate()
{
// create a delegate to a method declared in the script
var state = CSharpScript.Run("int Times(int x) { return x * x; }");
var fn = state.CreateDelegate<Func<int, int>>("Times");
var result = fn(5);
Assert.Equal(25, result);
}
[Fact]
public void TestGetScriptVariableAfterRunningScript()
{
var result = CSharpScript.Run("int x = 100;");
var globals = result.Variables.Names.ToList();
Assert.Equal(1, globals.Count);
Assert.Equal(true, globals.Contains("x"));
Assert.Equal(true, result.Variables.ContainsVariable("x"));
Assert.Equal(100, (int)result.Variables["x"].Value);
}
[Fact]
public void TestBranchingSubscripts()
{
// run script to create declaration of M
var result1 = CSharpScript.Run("int M(int x) { return x + x; }");
// run second script starting from first script's end state
// this script's new declaration should hide the old declaration
var result2 = CSharpScript.Run("int M(int x) { return x * x; } M(5)", result1);
Assert.Equal(25, result2.ReturnValue);
// run third script also starting from first script's end state
// it should not see any declarations made by the second script.
var result3 = CSharpScript.Run("M(5)", result1);
Assert.Equal(10, result3.ReturnValue);
}
}
}