diff --git a/test/language/rest-parameters/arrow-function-invalid.js b/test/language/rest-parameters/arrow-function-invalid.js new file mode 100644 index 0000000000..4d226f24b5 --- /dev/null +++ b/test/language/rest-parameters/arrow-function-invalid.js @@ -0,0 +1,9 @@ +// Copyright (C) 2014 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 14.1 +description: > + Invalid rest parameter in arrow function parameters +negative: SyntaxError +---*/ +var x = ...y => y; diff --git a/test/language/rest-parameters/arrow-function.js b/test/language/rest-parameters/arrow-function.js new file mode 100644 index 0000000000..f1b04e9a2d --- /dev/null +++ b/test/language/rest-parameters/arrow-function.js @@ -0,0 +1,16 @@ +// Copyright (C) 2014 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 14.1 +description: >arrow functions +includes: [compareArray.js] +---*/ +var fn = (a, b, ...c) => c; + +assert(compareArray(fn(), []), "`compareArray(fn(), [])` returns `true`"); +assert(compareArray(fn(1, 2), []), "`compareArray(fn(1, 2), [])` returns `true`"); +assert(compareArray(fn(1, 2, 3), [3]),"`compareArray(fn(1, 2, 3), [3])` returns `true`"); +assert(compareArray(fn(1, 2, 3, 4), [3, 4]),"`compareArray(fn(1, 2, 3, 4), [3, 4])` returns `true`"); +assert(compareArray(fn(1, 2, 3, 4, 5), [3, 4, 5]),"`compareArray(fn(1, 2, 3, 4, 5), [3, 4, 5])` returns `true`"); +assert(compareArray(((...args) => args)(), []),"`compareArray(((...args) => args)(), [])` returns `true`"); +assert(compareArray(((...args) => args)(1,2,3), [1,2,3]),"`compareArray(((...args) => args)(1,2,3), [1,2,3])` returns `true`"); diff --git a/test/language/rest-parameters/expected-argument-count.js b/test/language/rest-parameters/expected-argument-count.js new file mode 100644 index 0000000000..39b01c3174 --- /dev/null +++ b/test/language/rest-parameters/expected-argument-count.js @@ -0,0 +1,14 @@ +// Copyright (C) 2014 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 14.1.6 +description: > + The ExpectedArgumentCount of a FormalParameterList is the number of + FormalParameters to the left of either the rest parameter or the first + FormalParameter with an Initializer. +---*/ +function af(...a) {} +function bf(a, ...b) {} + +assert.sameValue(af.length, 0, "The value of `af.length` is `0`"); +assert.sameValue(bf.length, 1, "The value of `bf.length` is `1`"); diff --git a/test/language/rest-parameters/no-alias-arguments.js b/test/language/rest-parameters/no-alias-arguments.js new file mode 100644 index 0000000000..e35165f0ad --- /dev/null +++ b/test/language/rest-parameters/no-alias-arguments.js @@ -0,0 +1,15 @@ +// Copyright (C) 2014 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 14.1 +description: > + no alias arguments +includes: [compareArray.js] +---*/ +function f(a, ...rest) { + arguments[0] = 1; + assert.sameValue(a, 3, "The value of `a` is `3`"); + arguments[1] = 2; + assert(compareArray(rest, [4, 5]), "`compareArray(rest, [4, 5])` returns `true`"); +} +f(3, 4, 5); diff --git a/test/language/rest-parameters/position-invalid.js b/test/language/rest-parameters/position-invalid.js new file mode 100644 index 0000000000..27b6757725 --- /dev/null +++ b/test/language/rest-parameters/position-invalid.js @@ -0,0 +1,9 @@ +// Copyright (C) 2014 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 14.1 +description: > + Rest parameter cannot be followed by another named parameter +negative: SyntaxError +---*/ +function f(a, ...b, c) {} diff --git a/test/language/rest-parameters/rest-index.js b/test/language/rest-parameters/rest-index.js new file mode 100644 index 0000000000..99bfdb849f --- /dev/null +++ b/test/language/rest-parameters/rest-index.js @@ -0,0 +1,37 @@ +// Copyright (C) 2014 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 14.1 +description: > + rest index +---*/ +assert.sameValue( + (function(...args) { return args.length; })(1,2,3,4,5), + 5, + "`(function(...args) { return args.length; })(1,2,3,4,5)` returns `5`" +); +assert.sameValue( + (function(a, ...args) { return args.length; })(1,2,3,4,5), + 4, + "`(function(a, ...args) { return args.length; })(1,2,3,4,5)` returns `4`" +); +assert.sameValue( + (function(a, b, ...args) { return args.length; })(1,2,3,4,5), + 3, + "`(function(a, b, ...args) { return args.length; })(1,2,3,4,5)` returns `3`" +); +assert.sameValue( + (function(a, b, c, ...args) { return args.length; })(1,2,3,4,5), + 2, + "`(function(a, b, c, ...args) { return args.length; })(1,2,3,4,5)` returns `2`" +); +assert.sameValue( + (function(a, b, c, d, ...args) { return args.length; })(1,2,3,4,5), + 1, + "`(function(a, b, c, d, ...args) { return args.length; })(1,2,3,4,5)` returns `1`" +); +assert.sameValue( + (function(a, b, c, d, e, ...args) { return args.length; })(1,2,3,4,5), + 0, + "`(function(a, b, c, d, e, ...args) { return args.length; })(1,2,3,4,5)` returns `0`" +); diff --git a/test/language/rest-parameters/rest-parameters-apply.js b/test/language/rest-parameters/rest-parameters-apply.js new file mode 100644 index 0000000000..5bc14690a9 --- /dev/null +++ b/test/language/rest-parameters/rest-parameters-apply.js @@ -0,0 +1,15 @@ +// Copyright (C) 2014 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 14.1 +description: > + Rest parameter and Function.prototype.apply +---*/ +function af(...a) { + return a.length; +} + +assert.sameValue(af.apply(null, []), 0, "`af.apply(null, [])` returns `0`"); +assert.sameValue(af.apply(null, [1]), 1, "`af.apply(null, [1])` returns `1`"); +assert.sameValue(af.apply(null, [1, 2]), 2, "`af.apply(null, [1, 2])` returns `2`"); +assert.sameValue(af.apply(null, [1, ,2]), 3, "`af.apply(null, [1, ,2])` returns `3`"); diff --git a/test/language/rest-parameters/rest-parameters-call.js b/test/language/rest-parameters/rest-parameters-call.js new file mode 100644 index 0000000000..b59892d1f2 --- /dev/null +++ b/test/language/rest-parameters/rest-parameters-call.js @@ -0,0 +1,14 @@ +// Copyright (C) 2014 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 14.1 +description: > + Rest parameter and Function.prototype.call +---*/ +function af(...a) { + return a.length; +} + +assert.sameValue(af.call(null), 0, "`af.call(null)` returns `0`"); +assert.sameValue(af.call(null, 1), 1, "`af.call(null, 1)` returns `1`"); +assert.sameValue(af.call(null, 1, 2), 2, "`af.call(null, 1, 2)` returns `2`"); diff --git a/test/language/rest-parameters/rest-parameters-produce-an-array.js b/test/language/rest-parameters/rest-parameters-produce-an-array.js new file mode 100644 index 0000000000..8d087bb600 --- /dev/null +++ b/test/language/rest-parameters/rest-parameters-produce-an-array.js @@ -0,0 +1,12 @@ +// Copyright (C) 2014 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 14.1 +description: > + Rest parameter produces an instance of Array +---*/ +function af(...a) { + assert.sameValue(a.constructor, Array, "The value of `a.constructor` is `Array`"); + assert(Array.isArray(a), "`Array.isArray(a)` returns `true`"); +} +af(1); diff --git a/test/language/rest-parameters/with-new-target.js b/test/language/rest-parameters/with-new-target.js new file mode 100644 index 0000000000..209f904f06 --- /dev/null +++ b/test/language/rest-parameters/with-new-target.js @@ -0,0 +1,53 @@ +// Copyright (C) 2014 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 14.1 +description: > + with new target +includes: [compareArray.js] +---*/ +class Base { + constructor(...a) { + assert.sameValue( + arguments.length, + a.length, + "The value of `arguments.length` is `a.length`" + ); + this.base = a; + var args = []; + for (var i = 0; i < arguments.length; ++i) { + args.push(arguments[i]); + } + assert(compareArray(args, a), "`compareArray(args, a)` returns `true`"); + } +} +class Child extends Base { + constructor(...b) { + super(1, 2, 3); + assert.sameValue( + arguments.length, + b.length, + "The value of `arguments.length` is `b.length`" + ); + this.child = b; + var args = []; + for (var i = 0; i < arguments.length; ++i) { + args.push(arguments[i]); + } + assert(compareArray(args, b), "`compareArray(args, b)` returns `true`"); + } +} + +var c = new Child(1, 2, 3); + +assert.sameValue(c.child.length, 3, "The value of `c.child.length` is `3`"); +assert.sameValue(c.base.length, 3, "The value of `c.base.length` is `3`"); + +assert( + compareArray(c.child, [1, 2, 3]), + "`compareArray(c.child, [1, 2, 3])` returns `true`" +); +assert( + compareArray(c.base, [1, 2, 3]), + "`compareArray(c.base, [1, 2, 3])` returns `true`" +);