forked from microsoft/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensionsTest.cs
256 lines (205 loc) · 11.6 KB
/
ExtensionsTest.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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.V8;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.ClearScript.Test
{
// ReSharper disable once PartialTypeWithSinglePart
[TestClass]
[SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable", Justification = "Test classes use TestCleanupAttribute for deterministic teardown.")]
public partial class ExtensionsTest : ClearScriptTest
{
#region setup / teardown
private ScriptEngine engine;
[TestInitialize]
public void TestInitialize()
{
engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);
engine.AddHostType(typeof(Extensions));
engine.AddHostType(typeof(JavaScriptExtensions));
}
[TestCleanup]
public void TestCleanup()
{
engine.Dispose();
BaseTestCleanup();
}
#endregion
#region test methods
// ReSharper disable InconsistentNaming
[TestMethod, TestCategory("Extensions")]
public void Extensions_ToHostType()
{
engine.Script.ClrMath = typeof(Math).ToHostType(engine);
Assert.AreEqual(Math.PI, engine.Evaluate("ClrMath.PI"));
engine.Script.randomType = typeof(Random);
Assert.IsInstanceOfType(engine.Evaluate("new (randomType.ToHostType())"), typeof(Random));
}
[TestMethod, TestCategory("Extensions")]
public void Extensions_ToRestrictedHostObject()
{
IConvertible convertible = 123;
engine.Script.convertible = convertible.ToRestrictedHostObject(engine);
Assert.AreEqual(TypeCode.Int32, engine.Evaluate("convertible.GetTypeCode()"));
engine.AddHostType(typeof(IConvertible));
Assert.AreEqual(TypeCode.Int32, engine.Evaluate("Extensions.ToRestrictedHostObject(IConvertible, 456).GetTypeCode()"));
}
[TestMethod, TestCategory("Extensions")]
public void Extensions_JavaScript_ToPromise()
{
engine.Script.promise = Task.FromResult(Math.PI).ToPromise(engine);
engine.Execute("(async function () { result = await promise; })()");
Assert.AreEqual(Math.PI, engine.Script.result);
engine.Script.task = Task.FromResult(Math.E);
engine.Execute("(async function () { result = await task.ToPromise(); })()");
Assert.AreEqual(Math.E, engine.Script.result);
}
[TestMethod, TestCategory("Extensions")]
public void Extensions_JavaScript_ToPromise_Faulted()
{
const string message = "No task for you!";
var task = Task.FromException<double>(new UnauthorizedAccessException(message));
engine.Script.promise = task.ToPromise(engine);
engine.Execute("(async function () { try { result = await promise; } catch (exception) { result = exception.hostException.InnerException.InnerException.Message; } })()");
Assert.AreEqual(message, engine.Script.result);
engine.Script.task = task;
engine.Execute("(async function () { try { result = await task.ToPromise(); } catch (exception) { result = exception.hostException.InnerException.InnerException.Message; } })()");
Assert.AreEqual(message, engine.Script.result);
}
[TestMethod, TestCategory("Extensions")]
public void Extensions_JavaScript_ToPromise_Canceled()
{
var message = new TaskCanceledException().Message;
var task = Task.FromCanceled<double>(new CancellationToken(true));
engine.Script.promise = task.ToPromise(engine);
engine.Execute("(async function () { try { result = await promise; } catch (exception) { result = exception.hostException.InnerException.InnerException.Message; } })()");
Assert.AreEqual(message, engine.Script.result);
engine.Script.task = task;
engine.Execute("(async function () { try { result = await task.ToPromise(); } catch (exception) { result = exception.hostException.InnerException.InnerException.Message; } })()");
Assert.AreEqual(message, engine.Script.result);
}
[TestMethod, TestCategory("Extensions")]
public void Extensions_JavaScript_ToPromise_NoResult()
{
engine.Script.promise = RunAsTask(() => engine.Script.result = Math.PI).ToPromise(engine);
engine.Execute("(async function () { await promise; })()");
Assert.AreEqual(Math.PI, engine.Script.result);
engine.Script.task = RunAsTask(() => engine.Script.result = Math.E);
engine.Execute("(async function () { await task.ToPromise(); })()");
Assert.AreEqual(Math.E, engine.Script.result);
}
[TestMethod, TestCategory("Extensions")]
public void Extensions_JavaScript_ToPromise_NoResult_Faulted()
{
const string message = "No task for you!";
var task = Task.FromException(new UnauthorizedAccessException(message));
engine.Script.promise = task.ToPromise(engine);
engine.Execute("(async function () { try { result = await promise; } catch (exception) { result = exception.hostException.InnerException.InnerException.Message; } })()");
Assert.AreEqual(message, engine.Script.result);
engine.Script.task = task;
engine.Execute("(async function () { try { result = await task.ToPromise(); } catch (exception) { result = exception.hostException.InnerException.InnerException.Message; } })()");
Assert.AreEqual(message, engine.Script.result);
}
[TestMethod, TestCategory("Extensions")]
public void Extensions_JavaScript_ToPromise_NoResult_Canceled()
{
var message = new TaskCanceledException().Message;
var task = Task.FromCanceled(new CancellationToken(true));
engine.Script.promise = task.ToPromise(engine);
engine.Execute("(async function () { try { result = await promise; } catch (exception) { result = exception.hostException.InnerException.InnerException.Message; } })()");
Assert.AreEqual(message, engine.Script.result);
engine.Script.task = task;
engine.Execute("(async function () { try { result = await task.ToPromise(); } catch (exception) { result = exception.hostException.InnerException.InnerException.Message; } })()");
Assert.AreEqual(message, engine.Script.result);
}
[TestMethod, TestCategory("Extensions")]
public void Extensions_JavaScript_ToTask()
{
engine.AddHostType(typeof(Task));
Assert.AreEqual(Math.PI, EvaluateAsync("(async function () { await Task.Delay(100).ToPromise(); return Math.PI; })()").Result);
}
[TestMethod, TestCategory("Extensions")]
public void Extensions_JavaScript_ToTask_Fail()
{
engine.AddHostType(typeof(Task));
TestUtil.AssertException<ScriptEngineException>(() => EvaluateAsync("(async function () { await Task.Delay(100).ToPromise(); throw new Error('Unauthorized'); })()").Wait());
}
[TestMethod, TestCategory("Extensions")]
public void Extension_JavaScript_ToEnumerable_Generator()
{
engine.Execute("foo = (function* () { yield 'This'; yield 'is'; yield 'not'; yield 'a'; yield 'drill!'; })()");
Assert.AreEqual("This is not a drill!", string.Join(" ", engine.Global["foo"].ToEnumerable()));
}
[TestMethod, TestCategory("Extensions")]
public void Extension_JavaScript_ToEnumerable_GenericObject()
{
engine.Execute("foo = { 'This': 1, 'is': 2, 'not': 3, 'a': 4, 'drill!': 5 }");
TestUtil.AssertException<ArgumentException>(() => string.Join(" ", engine.Global["foo"].ToEnumerable()));
engine.Execute("foo[Symbol.iterator] = function* () { for (const item of Object.keys(foo)) yield item; }");
Assert.AreEqual("This is not a drill!", string.Join(" ", engine.Global["foo"].ToEnumerable()));
}
[TestMethod, TestCategory("Extensions")]
public void Extension_JavaScript_ToEnumerable_Array()
{
engine.Execute("foo = [ 'This', 'is', 'not', 'a', 'drill!' ]");
Assert.AreEqual("This is not a drill!", string.Join(" ", engine.Global["foo"].ToEnumerable()));
}
[TestMethod, TestCategory("Extensions")]
public void Extension_JavaScript_ToEnumerable_Managed_Object()
{
engine.Global["bar"] = new PropertyBag { { "This", 1 }, { "is", 2 }, { "not", 3 }, { "a", 4 }, { "drill!", 5 } };
TestUtil.AssertException<ArgumentException>(() => string.Join(" ", engine.Global["bar"].ToEnumerable()));
engine.Execute("foo = (function* () { for (const item of Object.keys(bar)) yield item; })()");
Assert.AreEqual("This is not a drill!", string.Join(" ", engine.Global["foo"].ToEnumerable()));
}
[TestMethod, TestCategory("Extensions")]
public void Extension_JavaScript_ToEnumerable_Managed_Array()
{
engine.Global["bar"] = new[] { "This", "is", "not", "a", "drill!" };
Assert.AreEqual("This is not a drill!", string.Join(" ", engine.Global["bar"].ToEnumerable()));
engine.Global["bar"] = new object[] { "This", "is", "not", "a", "drill!" };
Assert.AreEqual("This is not a drill!", string.Join(" ", engine.Global["bar"].ToEnumerable()));
engine.Execute("foo = (function* () { for (const item of bar) yield item; })()");
Assert.AreEqual("This is not a drill!", string.Join(" ", engine.Global["foo"].ToEnumerable()));
}
[TestMethod, TestCategory("Extensions")]
public void Extension_JavaScript_ToEnumerable_Managed_List()
{
engine.Global["bar"] = new List<string> { "This", "is", "not", "a", "drill!" };
Assert.AreEqual("This is not a drill!", string.Join(" ", engine.Global["bar"].ToEnumerable()));
engine.Global["bar"] = new List<object> { "This", "is", "not", "a", "drill!" };
Assert.AreEqual("This is not a drill!", string.Join(" ", engine.Global["bar"].ToEnumerable()));
engine.Execute("foo = (function* () { for (const item of bar) yield item; })()");
Assert.AreEqual("This is not a drill!", string.Join(" ", engine.Global["foo"].ToEnumerable()));
}
[TestMethod, TestCategory("Extensions")]
public void Extension_JavaScript_ToEnumerable_Managed_ArrayList()
{
engine.Global["bar"] = new ArrayList { "This", "is", "not", "a", "drill!" };
Assert.AreEqual("This is not a drill!", string.Join(" ", engine.Global["bar"].ToEnumerable()));
engine.Execute("foo = (function* () { for (const item of bar) yield item; })()");
Assert.AreEqual("This is not a drill!", string.Join(" ", engine.Global["foo"].ToEnumerable()));
}
// ReSharper restore InconsistentNaming
#endregion
#region miscellaneous
private static Task RunAsTask(Action action)
{
var task = Task.Run(action);
task.Wait();
return task;
}
private async Task<object> EvaluateAsync(string code)
{
return await engine.Evaluate(code).ToTask();
}
#endregion
}
}