forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionsTest.cs
More file actions
110 lines (89 loc) · 3.8 KB
/
ExtensionsTest.cs
File metadata and controls
110 lines (89 loc) · 3.8 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.V8;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.ClearScript.Test
{
[TestClass]
[DeploymentItem("ClearScriptV8-64.dll")]
[DeploymentItem("ClearScriptV8-32.dll")]
[DeploymentItem("v8-x64.dll")]
[DeploymentItem("v8-ia32.dll")]
[DeploymentItem("v8-base-x64.dll")]
[DeploymentItem("v8-base-ia32.dll")]
[DeploymentItem("v8-libcpp-x64.dll")]
[DeploymentItem("v8-libcpp-ia32.dll")]
[SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable", Justification = "Test classes use TestCleanupAttribute for deterministic teardown.")]
public 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_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);
}
// ReSharper restore InconsistentNaming
#endregion
#region miscellaneous
private static Task RunAsTask(Action action)
{
var task = Task.Run(action);
task.Wait();
return task;
}
#endregion
}
}