deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / test / mjsunit / harmony / rest-params.js
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Flags: --harmony-rest-parameters --harmony-classes
6
7 (function testRestIndex() {
8   assertEquals(5, (function(...args) { return args.length; })(1,2,3,4,5));
9   assertEquals(4, (function(a, ...args) { return args.length; })(1,2,3,4,5));
10   assertEquals(3, (function(a, b, ...args) { return args.length; })(1,2,3,4,5));
11   assertEquals(2, (function(a, b, c, ...args) {
12                       return args.length; })(1,2,3,4,5));
13   assertEquals(1, (function(a, b, c, d, ...args) {
14                       return args.length; })(1,2,3,4,5));
15   assertEquals(0, (function(a, b, c, d, e, ...args) {
16                     return args.length; })(1,2,3,4,5));
17 })();
18
19 function strictTest(a, b, ...c) {
20   "use strict";
21   assertEquals(Array, c.constructor);
22   assertTrue(Array.isArray(c));
23
24   var expectedLength = arguments.length >= 3 ? arguments.length - 2 : 0;
25   assertEquals(expectedLength, c.length);
26
27   for (var i = 2, j = 0; i < arguments.length; ++i) {
28     assertEquals(c[j++], arguments[i]);
29   }
30 }
31
32 function sloppyTest(a, b, ...c) {
33   assertEquals(Array, c.constructor);
34   assertTrue(Array.isArray(c));
35
36   var expectedLength = arguments.length >= 3 ? arguments.length - 2 : 0;
37   assertEquals(expectedLength, c.length);
38
39   for (var i = 2, j = 0; i < arguments.length; ++i) {
40     assertEquals(c[j++], arguments[i]);
41   }
42 }
43
44
45 var O = {
46   strict: strictTest,
47   sloppy: sloppyTest
48 };
49
50 (function testStrictRestParamArity() {
51   assertEquals(2, strictTest.length);
52   assertEquals(2, O.strict.length);
53 })();
54
55
56 (function testRestParamsStrictMode() {
57   strictTest();
58   strictTest(1, 2);
59   strictTest(1, 2, 3, 4, 5, 6);
60   strictTest(1, 2, 3);
61   O.strict();
62   O.strict(1, 2);
63   O.strict(1, 2, 3, 4, 5, 6);
64   O.strict(1, 2, 3);
65 })();
66
67
68 (function testRestParamsStrictModeApply() {
69   strictTest.apply(null, []);
70   strictTest.apply(null, [1, 2]);
71   strictTest.apply(null, [1, 2, 3, 4, 5, 6]);
72   strictTest.apply(null, [1, 2, 3]);
73   O.strict.apply(O, []);
74   O.strict.apply(O, [1, 2]);
75   O.strict.apply(O, [1, 2, 3, 4, 5, 6]);
76   O.strict.apply(O, [1, 2, 3]);
77 })();
78
79
80 (function testRestParamsStrictModeCall() {
81   strictTest.call(null);
82   strictTest.call(null, 1, 2);
83   strictTest.call(null, 1, 2, 3, 4, 5, 6);
84   strictTest.call(null, 1, 2, 3);
85   O.strict.call(O);
86   O.strict.call(O, 1, 2);
87   O.strict.call(O, 1, 2, 3, 4, 5, 6);
88   O.strict.call(O, 1, 2, 3);
89 })();
90
91
92 (function testsloppyRestParamArity() {
93   assertEquals(2, sloppyTest.length);
94   assertEquals(2, O.sloppy.length);
95 })();
96
97
98 (function testRestParamssloppyMode() {
99   sloppyTest();
100   sloppyTest(1, 2);
101   sloppyTest(1, 2, 3, 4, 5, 6);
102   sloppyTest(1, 2, 3);
103   O.sloppy();
104   O.sloppy(1, 2);
105   O.sloppy(1, 2, 3, 4, 5, 6);
106   O.sloppy(1, 2, 3);
107 })();
108
109
110 (function testRestParamssloppyModeApply() {
111   sloppyTest.apply(null, []);
112   sloppyTest.apply(null, [1, 2]);
113   sloppyTest.apply(null, [1, 2, 3, 4, 5, 6]);
114   sloppyTest.apply(null, [1, 2, 3]);
115   O.sloppy.apply(O, []);
116   O.sloppy.apply(O, [1, 2]);
117   O.sloppy.apply(O, [1, 2, 3, 4, 5, 6]);
118   O.sloppy.apply(O, [1, 2, 3]);
119 })();
120
121
122 (function testRestParamssloppyModeCall() {
123   sloppyTest.call(null);
124   sloppyTest.call(null, 1, 2);
125   sloppyTest.call(null, 1, 2, 3, 4, 5, 6);
126   sloppyTest.call(null, 1, 2, 3);
127   O.sloppy.call(O);
128   O.sloppy.call(O, 1, 2);
129   O.sloppy.call(O, 1, 2, 3, 4, 5, 6);
130   O.sloppy.call(O, 1, 2, 3);
131 })();
132
133
134 (function testUnmappedArguments() {
135   // Strict/Unmapped arguments should always be used for functions with rest
136   // parameters
137   assertThrows(function(...rest) { return arguments.caller; }, TypeError);
138   assertThrows(function(...rest) { return arguments.callee; }, TypeError);
139   // TODO(caitp): figure out why this doesn't throw sometimes, even though the
140   //              getter always does =)
141   // assertThrows(function(...rest) { arguments.caller = 1; }, TypeError);
142   // assertThrows(function(...rest) { arguments.callee = 1; }, TypeError);
143 })();
144
145
146 (function testNoAliasArgumentsStrict() {
147   function strictF(a, ...rest) {
148     "use strict";
149     arguments[0] = 1;
150     assertEquals(3, a);
151     arguments[1] = 2;
152     assertArrayEquals([4, 5], rest);
153   }
154   strictF(3, 4, 5);
155 })();
156
157
158 (function testNoAliasArgumentsSloppy() {
159   function sloppyF(a, ...rest) {
160     arguments[0] = 1;
161     assertEquals(3, a);
162     arguments[1] = 2;
163     assertArrayEquals([4, 5], rest);
164   }
165   sloppyF(3, 4, 5);
166 })();
167
168
169 /* TODO(caitp): support arrow functions (blocked on spread operator support)
170 (function testRestParamsArrowFunctions() {
171   "use strict";
172
173   var fn = (a, b, ...c) => c;
174   assertEquals([], fn());
175   assertEquals([], fn(1, 2));
176   assertEquals([3], fn(1, 2, 3));
177   assertEquals([3, 4], fn(1, 2, 3, 4));
178   assertEquals([3, 4, 5], fn(1, 2, 3, 4, 5));
179   assertThrows("var x = ...y => y;", SyntaxError);
180   assertEquals([], ((...args) => args)());
181   assertEquals([1,2,3], ((...args) => args)(1,2,3));
182 })();*/
183
184
185 (function testRestParamsWithNewTarget() {
186   "use strict";
187   class Base {
188     constructor(...a) {
189       this.base = a;
190       assertEquals(arguments.length, a.length);
191       var args = [];
192       for (var i = 0; i < arguments.length; ++i) {
193         args.push(arguments[i]);
194       }
195       assertEquals(args, a);
196     }
197   }
198   class Child extends Base {
199     constructor(...b) {
200       super(1, 2, 3);
201       this.child = b;
202       assertEquals(arguments.length, b.length);
203       var args = [];
204       for (var i = 0; i < arguments.length; ++i) {
205         args.push(arguments[i]);
206       }
207       assertEquals(args, b);
208     }
209   }
210
211   var c = new Child(1, 2, 3);
212   assertEquals([1, 2, 3], c.child);
213   assertEquals([1, 2, 3], c.base);
214 })();