forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptFunctionToStrings.js
More file actions
140 lines (135 loc) · 5.99 KB
/
ScriptFunctionToStrings.js
File metadata and controls
140 lines (135 loc) · 5.99 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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
if (this.WScript && this.WScript.LoadScriptFile) { // Check for running in ch
this.WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
}
var tests = [
{
name: "function declaration test",
body: function ()
{
var s = "function \n\t\r foo() {var a = 5;}";
eval(s);
assert.areEqual(s, foo.toString(), "Function.toString should preserve original formatting");
}
},
{
name: "function assignment test",
body: function ()
{
var s = "function \t\n\r\t foo() {var a = 5;}";
eval("var a = " + s);
assert.areEqual(s, a.toString(), "Function.toString should preserve original formatting");
a = function (i) { i++; }
assert.areEqual("function (i) { i++; }", a.toString(), "toString should add a space if one does not exist");
}
},
{
name: "generator function declaration test",
body: function ()
{
var s = "function* \t\r\n foo() {var a = 5;}";
eval(s);
assert.areEqual(s, foo.toString(), "Function.toString should preserve original formatting");
s = "function \t\r\n*\n\r\n \t foo() {var a = 5;}";
eval(s);
assert.areEqual(s, foo.toString(), "Function.toString should preserve original formatting");
}
},
{
name: "generator function assignment test",
body: function ()
{
var s = "function* \t\n\r \t foo() {var a = 5;}";
eval("var a = " + s);
assert.areEqual(s, a.toString(), "Function.toString should preserve original formatting");
s = "function \t\n\r * \t\n foo() {var a = 5;}";
eval("var a = " + s);
assert.areEqual(s, a.toString(), "Function.toString should preserve original formatting");
}
},
{
name: "Named function expression tests",
body: function ()
{
var s1 = "function \n\t bar \t () {}";
var s2 = "function \t qui \t () {}";
eval("var o = { foo : " + s1 + "}");
eval("o.e = " + s2);
assert.areEqual(s1, o.foo.toString(), "confirm that the foo identifier does not override the name bar ");
assert.areEqual(s2, o.e.toString(), "confirm that the foo identifier does not override the name qui");
}
},
{
name: "function expression tests without names",
body: function ()
{
var s1 = "function \n\t \t () {}";
var s2 = "function \t \t () {}";
eval("var o = { foo : " + s1 + "}");
eval("o.e = " + s2);
assert.areEqual(s1, o.foo.toString(), "confirm that the foo identifier does not override the name bar ");
assert.areEqual(s2, o.e.toString(), "confirm that the foo identifier does not override the name qui");
}
},
{
name: "internal function test",
body: function ()
{
var s = "function foo() { return foo.toString(); }";
eval(s);
var a = foo;
assert.areEqual(s, a(), "confirm that even if we call toString internally it has no effect on the name")
}
},
{
name: "class method test",
body: function ()
{
eval("var qux = class { constructor(){} static func(){} method(){} get getter(){} set setter(v){}}");
var quxObj = new qux();
assert.areEqual("func(){}", qux.func.toString(), "the name should be func")
assert.areEqual("method(){}", quxObj.method.toString(), "the name should be method")
var oGet = Object.getOwnPropertyDescriptor(qux.prototype, "getter");
var oSet = Object.getOwnPropertyDescriptor(qux.prototype, "setter");
assert.areEqual("getter(){}", oGet.get.toString(), "the name should be getter");
assert.areEqual("setter(v){}", oSet.set.toString(), "the name should be setter");
}
},
{
name: "class constructor test",
body: function ()
{
// [19.2.3.5] Function.prototype.toString()
// The string representation must have the syntax of a FunctionDeclaration, FunctionExpression, GeneratorDeclaration,
// GeneratorExpression, AsyncFunctionDeclaration, AsyncFunctionExpression, ClassDeclaration, ClassExpression, ArrowFunction,
// AsyncArrowFunction, or MethodDefinition depending upon the actual characteristics of the object.
eval("var qux = class { constructor(){} static func(){} method(){} get getter(){} set setter(v){}}");
var quxObj = new qux();
assert.areEqual("class { constructor(){} static func(){} method(){} get getter(){} set setter(v){}}",
quxObj.constructor.toString(), "The string representation must have the syntax of a ClassExpression");
var qux = class { };
var quxObj = new qux();
assert.areEqual("class { }", quxObj.constructor.toString(), "The string representation must have the syntax of a ClassDeclaration")
}
},
{
name: "shorthand method function test",
body: function ()
{
var o = { ['f']() { }, g() { } };
assert.areEqual("['f']() { }", o.f.toString());
}
},
{
name: "arrow function Test",
body: function ()
{
var arrowDecl = () => { };
assert.areEqual("() => { }", arrowDecl.toString(), "Make sure arrow functions remain unaffected by ie12 formatting");
}
}
];
testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });