blob: 05e92b6ed65a981db426c73d5e040296562b0cc1 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 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: --min-preparse-length=0
6
7(function testRestIndex() {
8 assertEquals(5, ((...args) => args.length)(1,2,3,4,5));
9 assertEquals(4, ((a, ...args) => args.length)(1,2,3,4,5));
10 assertEquals(3, ((a, b, ...args) => args.length)(1,2,3,4,5));
11 assertEquals(2, ((a, b, c, ...args) => args.length)(1,2,3,4,5));
12 assertEquals(1, ((a, b, c, d, ...args) => args.length)(1,2,3,4,5));
13 assertEquals(0, ((a, b, c, d, e, ...args) => args.length)(1,2,3,4,5));
14})();
15
16// strictTest and sloppyTest should be called with descending natural
17// numbers, as in:
18//
19// strictTest(6,5,4,3,2,1)
20//
21var strictTest = (function() {
22 "use strict";
23 return (a, b, ...c) => {
24 assertEquals(Array, c.constructor);
25 assertTrue(Array.isArray(c));
26
27 var expectedLength = (a === undefined) ? 0 : a - 2;
28 assertEquals(expectedLength, c.length);
29
30 for (var i = 2; i < a; ++i) {
31 assertEquals(c[i - 2], a - i);
32 }
33 };
34})();
35
36var sloppyTest = (a, b, ...c) => {
37 assertEquals(Array, c.constructor);
38 assertTrue(Array.isArray(c));
39
40 var expectedLength = (a === undefined) ? 0 : a - 2;
41 assertEquals(expectedLength, c.length);
42
43 for (var i = 2; i < a; ++i) {
44 assertEquals(c[i - 2], a - i);
45 }
46};
47
48
49var O = {
50 strict: strictTest,
51 sloppy: sloppyTest
52};
53
54(function testStrictRestParamArity() {
55 assertEquals(2, strictTest.length);
56 assertEquals(2, O.strict.length);
57})();
58
59
60(function testRestParamsStrictMode() {
61 strictTest();
62 strictTest(2, 1);
63 strictTest(6, 5, 4, 3, 2, 1);
64 strictTest(3, 2, 1);
65 O.strict();
66 O.strict(2, 1);
67 O.strict(6, 5, 4, 3, 2, 1);
68 O.strict(3, 2, 1);
69})();
70
71
72(function testRestParamsStrictModeApply() {
73 strictTest.apply(null, []);
74 strictTest.apply(null, [2, 1]);
75 strictTest.apply(null, [6, 5, 4, 3, 2, 1]);
76 strictTest.apply(null, [3, 2, 1]);
77 O.strict.apply(O, []);
78 O.strict.apply(O, [2, 1]);
79 O.strict.apply(O, [6, 5, 4, 3, 2, 1]);
80 O.strict.apply(O, [3, 2, 1]);
81})();
82
83
84(function testRestParamsStrictModeCall() {
85 strictTest.call(null);
86 strictTest.call(null, 2, 1);
87 strictTest.call(null, 6, 5, 4, 3, 2, 1);
88 strictTest.call(null, 3, 2, 1);
89 O.strict.call(O);
90 O.strict.call(O, 2, 1);
91 O.strict.call(O, 6, 5, 4, 3, 2, 1);
92 O.strict.call(O, 3, 2, 1);
93})();
94
95
96(function testsloppyRestParamArity() {
97 assertEquals(2, sloppyTest.length);
98 assertEquals(2, O.sloppy.length);
99})();
100
101
102(function testRestParamsSloppyMode() {
103 sloppyTest();
104 sloppyTest(2, 1);
105 sloppyTest(6, 5, 4, 3, 2, 1);
106 sloppyTest(3, 2, 1);
107 O.sloppy();
108 O.sloppy(2, 1);
109 O.sloppy(6, 5, 4, 3, 2, 1);
110 O.sloppy(3, 2, 1);
111})();
112
113
114(function testRestParamssloppyModeApply() {
115 sloppyTest.apply(null, []);
116 sloppyTest.apply(null, [2, 1]);
117 sloppyTest.apply(null, [6, 5, 4, 3, 2, 1]);
118 sloppyTest.apply(null, [3, 2, 1]);
119 O.sloppy.apply(O, []);
120 O.sloppy.apply(O, [2, 1]);
121 O.sloppy.apply(O, [6, 5, 4, 3, 2, 1]);
122 O.sloppy.apply(O, [3, 2, 1]);
123})();
124
125
126(function testRestParamssloppyModeCall() {
127 sloppyTest.call(null);
128 sloppyTest.call(null, 2, 1);
129 sloppyTest.call(null, 6, 5, 4, 3, 2, 1);
130 sloppyTest.call(null, 3, 2, 1);
131 O.sloppy.call(O);
132 O.sloppy.call(O, 2, 1);
133 O.sloppy.call(O, 6, 5, 4, 3, 2, 1);
134 O.sloppy.call(O, 3, 2, 1);
135})();
136
137
138(function testUnmappedArguments() {
139 // Normal functions make their arguments object unmapped, but arrow
140 // functions don't have an arguments object anyway. Check that the
141 // right thing happens for arguments in arrow functions with rest
142 // parameters.
143 assertSame(arguments, ((...rest) => arguments)());
144})();