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
130 lines (126 loc) · 6.52 KB
/
ScriptFunctionToStrings.js
File metadata and controls
130 lines (126 loc) · 6.52 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
//-------------------------------------------------------------------------------------------------------
// 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 ()
{
eval("function \n\t\r foo() {var a = 5;}");
assert.areEqual("function foo() {var a = 5;}", foo.toString(), "toString should remove all extra whitespace, new lines, tabs and carriage return before the open (");
}
},
{
name: "function assignment test",
body: function ()
{
eval("var a = function \t\n\r\t foo() {var a = 5;}");
assert.areEqual("function foo() {var a = 5;}", a.toString(), "toString should remove all extra whitespace, new lines, tabs and carriage return before the open (");
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 ()
{
eval("function* \t\r\n foo() {var a = 5;}");
assert.areEqual("function* foo() {var a = 5;}", foo.toString(), "toString should remove all extra whitespace, new lines, tabs and carriage return before the open (");
eval("function \t\r\n*\n\r\n \t foo() {var a = 5;}");
assert.areEqual("function* foo() {var a = 5;}", foo.toString(), "toString should remove all extra whitespace, new lines, tabs and carriage return before the open (");
}
},
{
name: "generator function assignment test",
body: function ()
{
eval("var a = function* \t\n\r \t foo() {var a = 5;}");
assert.areEqual("function* foo() {var a = 5;}", a.toString(), "toString should remove all extra whitespace, new lines, tabs and carriage return before the open (");
eval("var a = function \t\n\r * \t\n foo() {var a = 5;}");
assert.areEqual("function* foo() {var a = 5;}", a.toString(), "toString should remove all extra whitespace, new lines, tabs and carriage return before the open (");
}
},
{
name: "Named function expression tests",
body: function ()
{
eval("var o = { foo : function \n\t bar \t () {}}");
eval("o.e = function \t qui \t () {}");
assert.areEqual("function bar() {}", o.foo.toString(), "confirm that the foo identifier does not override the name bar ");
assert.areEqual("function qui() {}", o.e.toString(), "confirm that the foo identifier does not override the name qui");
}
},
{
name: "function expression tests without names",
body: function ()
{
eval("var o = { foo : function \n\t \t () {}}");
eval("o.e = function \t \t () {}");
assert.areEqual("function () {}", o.foo.toString(), "confirm that the foo identifier does not override the name bar ");
assert.areEqual("function () {}", o.e.toString(), "confirm that the foo identifier does not override the name qui");
}
},
{
name: "internal function test",
body: function ()
{
eval("function foo() { return foo.toString(); }");
var a = foo;
assert.areEqual("function foo() { return foo.toString(); }", 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");
}
},
/* TODO fix and re-enable this test case (Microsoft/ChakraCore#3008)
{
name: "class constructor test",
body: function ()
{
eval("var qux = class { constructor(){} static func(){} method(){} get getter(){} set setter(v){}}");
var quxObj = new qux();
// FIXME Test threw exception: assert.areEqual failed: expected: constructor(){} actual: class { constructor(){} static func(){} method(){} get getter(){} set setter(v){}}
assert.areEqual("constructor(){}", quxObj.constructor.toString(), "The constructor should have the toString with name constructor");
var qux = class { };
var quxObj = new qux();
// I left a space between closing ellipse ) and the opening bracket { because that's how all browsers do toString on runtime functions
// Should this not be how other browsers do default constructor go to RuntimeCommon.h and change JS_DEFAULT_CTOR_DISPLAY_STRING
// FIXME Test threw exception: assert.areEqual failed: expected: constructor() {} actual: class { }
assert.areEqual("constructor() {}", quxObj.constructor.toString(), "The constructor should have the toString with name constructor")
}
},
*/
{
name: "shorthand method function test",
body: function ()
{
// TODO update this test after fixing output (see Microsoft/ChakraCore#2914: Incorrect Function toString for methods declared with string in brackets)
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" });