Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / js / kde / script-tests / function.js
blobf3f26f8097fda1552c8189791cdb8dbce8c45264
1 ///////////////////////////////
2 // empty bodies
4 function empty1() { ; }
5 shouldBe("empty1()", "undefined");
7 function empty2() { }
8 shouldBe("empty2()", "undefined");
10 var g = 2;
11 var obj = new Object();
12 with (obj) {
13 obj.func = function(a) { return g*l*a; };
15 obj.l = 3;
17 shouldBe("obj.func(11)", "66");
19 ///////////////////////////////
21 function ctor(xx) {
22 this.x = xx;
23 this.dummy = new Array(); // once broke the returned completion
26 c = new ctor(11);
27 shouldBe("c.x", "11");
29 ///////////////////////////////
30 // anonymous functions
31 ///////////////////////////////
33 f = function (arg) {
34 return arg;
38 shouldBe("f('bbbbb')", "'bbbbb'");
41 function f() {return 1;};
43 // just verify that this can be parsed
44 Math.round(function anon() { });
46 ///////////////////////////////
47 // arguments object
48 ///////////////////////////////
50 function foo() {
51 var result = "|";
52 for (i = 0; i < arguments.length; i++)
53 result += arguments[i] + "|";
54 return result;
57 shouldBe("foo()", "'|'");
58 shouldBe("foo('bar')", "'|bar|'");
59 shouldBe("foo('bar', 'x')", "'|bar|x|'");
61 function foo2(a) {
62 var i = 0;
63 for(a in arguments) // should be enumerable
64 i++;
65 return i;
68 shouldBe("foo2(7)", "1");
70 // I have my doubts about the standard conformance of this
71 function foo3(i, j) {
72 switch(i) {
73 case 0:
74 return foo3.arguments.length;
75 case 1:
76 return foo3.arguments;
77 case 2:
78 return foo3.j;
82 shouldBe("foo3(0, 99)", "2");
83 shouldBe("foo3(1, 99).length", "2");
84 //shouldBe("foo3(2, 99)", "99"); // IE doesn't support this either
86 ///////////////////////////////
87 // nested function declarations
88 ///////////////////////////////
90 function nest0(a, b)
92 function nest1(c) { return c*c; }
93 return nest1(a*b);
95 shouldBe("nest0(2,3)", "36");
97 ///////////////////////////////
98 // Function object
99 ///////////////////////////////
101 shouldBe("(new Function('return 11;'))()", "11");
102 shouldBe("(new Function('', 'return 22;'))()", "22");
103 shouldBe("(new Function('a', 'b', 'return a*b;'))(11, 3)", "33");
104 shouldBe("(new Function(' a ', ' b ', 'return a*b;'))(11, 4)", "44");
105 shouldBe("(new Function(' a,b ', ' c ', 'return a*b;'))(11, 5)", "55");
106 shouldBe("(new Function(' a,b ', ' c3 ', 'return a*b;'))(11, 6)", "66");
109 ///////////////////////////////
110 // length property
111 function funcp3(a, b, c) { }
112 shouldBe("funcp3.length", "3");
113 shouldBe("(function(a, b, c, d) { }).length", "4");
114 shouldBe("(new Function('a', 'b', '')).length", "2");
116 // recursive call
117 function f4(i)
119 return i == 1 ? 1 : i*f4(i-1);
122 shouldBe("f4(4)", "24");
124 function f5(a) {
125 return f6();
128 function f6() {
129 return f5.arguments[0];
132 shouldBe("f5(11)", "11");
133 shouldBe("f5.arguments", "null");
135 ///////////////////////////////
136 // calling conventions
137 ///////////////////////////////
139 function manipulate(x) {
140 x[0] = 99; // should operate on reference
141 x = "nothing"; // should detach
143 var arr = new Array(1, 2, 3);
144 manipulate(arr);
145 shouldBe("arr[0]", "99");
146 arr = [1, 2, 3];
147 manipulate(arr);
148 shouldBe("arr[0]", "99");
150 ///////////////////////////////
151 // toString() on functions
152 ///////////////////////////////
154 function orgFunc(s) { return 'Hello ' + s; }
155 eval("var orgFuncClone = " + orgFunc);
156 shouldBe("typeof orgFuncClone", "'function'");
157 shouldBe("orgFuncClone('world')", "'Hello world'");
159 function groupFunc(a, b) { return (a+b)*3; } // check for () being preserved
160 eval("var groupClone = " + groupFunc);
161 shouldBe("groupClone(1, 2)", "9");
163 var sinStr = 'function sin() {\n [native code]\n}'
164 shouldBe("String(Math.sin)", "sinStr");
166 ///////////////////////////////
167 // shadowed functions
168 ///////////////////////////////
170 function shadow() { return 1; }
171 function shadow() { return 2; }
172 shouldBe("shadow()", "2");
174 ///////////////////////////////
175 // nested functions
177 var nest_property = "global nest";
179 function nested_outer() {
181 var nest_property = "inner nest";
183 function nested_inner() {
184 return nest_property;
187 return nested_inner;
190 function not_nested() {
191 return nest_property;
194 nested_outer.nest_property = "outer nest";
196 var nested = nested_outer();
197 var nestedret = nested();
198 shouldBe("nestedret","'inner nest'");
200 var not_nestedret = not_nested();
201 shouldBe("not_nestedret","'global nest'");
203 ///////////////////////////////
204 // other properties
205 ///////////////////////////////
206 function sample() { }
207 shouldBeUndefined("sample.prototype.prototype");
208 shouldBeTrue("sample.prototype.constructor == sample");
210 var caught = false;
211 try {
212 sample.apply(1, 1); // invalid argument
213 } catch(dummy) {
214 caught = true;
216 shouldBeTrue("caught");
219 function functionWithVarOverride(a) {
220 var a = 2;
221 return a;
224 shouldBe("functionWithVarOverride(1)", "2");