forked from microsoft/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrossEngineTest.cs
222 lines (184 loc) · 7.79 KB
/
CrossEngineTest.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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.ClearScript.Util;
using Microsoft.ClearScript.V8;
using Microsoft.ClearScript.Windows;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.ClearScript.Test
{
[TestClass]
[SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable", Justification = "Test classes use TestCleanupAttribute for deterministic teardown.")]
public class CrossEngineTest : ClearScriptTest
{
#region setup / teardown
private static readonly Type[] types =
{
typeof(V8ScriptEngine),
typeof(JScriptEngine),
typeof(VBScriptEngine)
};
private ScriptEngine[] engines;
[TestInitialize]
public void TestInitialize()
{
engines = types.Select(type => (ScriptEngine)type.CreateInstance()).ToArray();
Iterate((engine, type) => engine.AddHostObject(type.Name, type.CreateInstance()));
}
[TestCleanup]
public void TestCleanup()
{
Iterate((engine, type) => engine.Execute(type.Name + ".Dispose()"));
engines.ForEach(engine => engine.Dispose());
BaseTestCleanup();
}
#endregion
#region test methods
// ReSharper disable InconsistentNaming
[TestMethod, TestCategory("CrossEngine")]
public void CrossEngine_Property()
{
var value = new Random();
Iterate(JSOnly, All, (engine, type) =>
{
engine.Script.value = value;
Assert.AreSame(value, engine.Evaluate(type.Name + ".Script.value = value"));
Assert.AreSame(value, engine.Evaluate(type.Name + ".Script.value"));
});
Iterate(VBSOnly, All, (engine, type) =>
{
var innerEngine = (ScriptEngine)engine.Evaluate(type.Name);
innerEngine.Script.value = value;
Assert.AreSame(value, engine.Evaluate(type.Name + ".Script.value"));
});
}
[TestMethod, TestCategory("CrossEngine")]
public void CrossEngine_Property_Scalar()
{
const int value = 123;
Iterate(JSOnly, All, (engine, type) =>
{
engine.Script.value = value;
Assert.AreEqual(value, engine.Evaluate(type.Name + ".Script.value = value"));
Assert.AreEqual(value, engine.Evaluate(type.Name + ".Script.value"));
});
Iterate(VBSOnly, All, (engine, type) =>
{
var innerEngine = (ScriptEngine)engine.Evaluate(type.Name);
innerEngine.Script.value = value;
Assert.AreEqual(value, engine.Evaluate(type.Name + ".Script.value"));
});
}
[TestMethod, TestCategory("CrossEngine")]
public void CrossEngine_Property_Enum()
{
const DayOfWeek value = DayOfWeek.Thursday;
Iterate(JSOnly, All, (engine, type) =>
{
engine.Script.value = value;
Assert.AreEqual(value, engine.Evaluate(type.Name + ".Script.value = value"));
Assert.AreEqual(value, engine.Evaluate(type.Name + ".Script.value"));
});
Iterate(VBSOnly, All, (engine, type) =>
{
var innerEngine = (ScriptEngine)engine.Evaluate(type.Name);
innerEngine.Script.value = value;
Assert.AreEqual(value, engine.Evaluate(type.Name + ".Script.value"));
});
}
[TestMethod, TestCategory("CrossEngine")]
public void CrossEngine_Property_Struct()
{
var value = new DateTime(2007, 5, 22, 6, 15, 43);
Iterate(JSOnly, All, (engine, type) =>
{
engine.Script.value = value;
Assert.AreEqual(value, engine.Evaluate(type.Name + ".Script.value = value"));
Assert.AreEqual(value, engine.Evaluate(type.Name + ".Script.value"));
});
Iterate(VBSOnly, All, (engine, type) =>
{
var innerEngine = (ScriptEngine)engine.Evaluate(type.Name);
innerEngine.Script.value = value;
Assert.AreEqual(value, engine.Evaluate(type.Name + ".Script.value"));
});
}
[TestMethod, TestCategory("CrossEngine")]
public void CrossEngine_Property_Index_ArrayItem()
{
Iterate(JSOnly, JSOnly, (engine, type) =>
{
engine.Execute(type.Name + ".Execute('foo = []')");
Assert.AreEqual(Math.PI, engine.Evaluate(type.Name + ".Script.foo[4] = Math.PI"));
Assert.AreEqual(Math.PI, engine.Evaluate(type.Name + ".Script.foo[4]"));
Assert.AreEqual(5, engine.Evaluate(type.Name + ".Evaluate('foo.length')"));
});
}
[TestMethod, TestCategory("CrossEngine")]
public void CrossEngine_Property_Index_Property()
{
Iterate(JSOnly, JSOnly, (engine, type) =>
{
engine.Execute(type.Name + ".Execute('foo = {}')");
Assert.AreEqual(Math.E, engine.Evaluate(type.Name + ".Script.foo['bar'] = Math.E"));
Assert.AreEqual(Math.E, engine.Evaluate(type.Name + ".Script.foo['bar']"));
Assert.AreEqual(Math.E, engine.Evaluate(type.Name + ".Script.foo.bar"));
});
}
[TestMethod, TestCategory("CrossEngine")]
public void CrossEngine_Property_Method()
{
// WORKAROUND: Windows Script engines apparently refuse to evaluate or execute
// non-enumerable external functions. This affects external script built-ins.
// The workaround is to invoke the built-in from a custom function.
Iterate(All, JSOnly, (engine, type) =>
{
engine.Execute(type.Name + ".Execute(\"function doEval(expr) { return eval(expr); }\")");
Assert.AreEqual(Math.E * Math.PI, engine.Evaluate(type.Name + ".Script.doEval(\"Math.E * Math.PI\")"));
});
Iterate(All, VBSOnly, (engine, type) =>
{
engine.Execute(type.Name + ".Execute(\"function doEval(expr) : doEval = eval(expr): end function\")");
Assert.AreEqual(Math.E * Math.PI, engine.Evaluate(type.Name + ".Script.doEval(\"4 * atn(1) * exp(1)\")"));
});
}
// ReSharper restore InconsistentNaming
#endregion
#region miscellaneous
private void Iterate(Action<ScriptEngine, Type> action)
{
Iterate(All, All, action);
}
private void Iterate(Func<ScriptEngine, bool> engineFilter, Func<Type, bool> typeFilter, Action<ScriptEngine, Type> action)
{
engines.Where(engineFilter).ForEach(engine => types.Where(typeFilter).ForEach(type => action(engine, type)));
}
private static bool All(ScriptEngine engine)
{
return true;
}
private static bool All(Type type)
{
return true;
}
private static bool JSOnly(ScriptEngine engine)
{
return (engine is V8ScriptEngine) || (engine is JScriptEngine);
}
private static bool JSOnly(Type type)
{
return (type == typeof(V8ScriptEngine)) || typeof(JScriptEngine).IsAssignableFrom(type);
}
private static bool VBSOnly(ScriptEngine engine)
{
return engine is VBScriptEngine;
}
private static bool VBSOnly(Type type)
{
return type == typeof(VBScriptEngine);
}
#endregion
}
}