• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15function assertArrayEqual (actual, expected) {
16  assert (actual.length === expected.length);
17
18  for (var i = 0; i < actual.length; i++) {
19    assert (actual[i] === expected[i]);
20  }
21}
22
23function checkSyntax (str) {
24  try {
25    eval (str);
26    assert (false);
27  } catch (e) {
28    assert (e instanceof SyntaxError);
29  }
30}
31
32function mustThrow (str) {
33  try {
34    eval (str);
35    assert (false);
36  } catch (e) {
37    assert (e instanceof TypeError);
38  }
39}
40
41checkSyntax ("{...a}");
42checkSyntax ("...a");
43checkSyntax ("[...]");
44checkSyntax ("[...(...)]");
45checkSyntax ("[......]");
46
47mustThrow ("[...5]");
48mustThrow ("[...5, 'foo', 'bar']");
49mustThrow ("[...{}]");
50mustThrow ("[...{ get [Symbol.iterator] () { throw new TypeError } }]");
51mustThrow ("[...{ [Symbol.iterator] () {} }, 'foo']");
52mustThrow ("[...{ [Symbol.iterator] () { return {} } }]");
53mustThrow ("[...{ [Symbol.iterator] () { return { next: 5 } } }]");
54mustThrow ("[...{ [Symbol.iterator] () { return { next: 5 } } }], 'foo'");
55mustThrow ("[...{ [Symbol.iterator] () { return { get next() { throw new TypeError } } } }]");
56mustThrow ("[...{ [Symbol.iterator] () { return { next () { } } } }]");
57mustThrow ("[...{ [Symbol.iterator] () { return { next () { } } } }, 'foo']");
58mustThrow ("[...{ [Symbol.iterator] () { return { next () { return { get value () { throw new TypeError } } } } } } ]");
59mustThrow ("[...{ [Symbol.iterator] () { return { next () { return { get done () { throw new TypeError } } } } } } ]");
60
61var arr1 = [0, 1, 2];
62var arr2 = [3, 4, 5];
63var arr3 = [{}, {}, {}];
64var expected = [0, 1, 2, 3 ,4, 5];
65
66assertArrayEqual ([...arr1, ...arr2], [0, 1, 2, 3 ,4, 5]);
67assertArrayEqual ([...arr2, ...arr1], [3 ,4, 5, 0, 1, 2]);
68assertArrayEqual ([...arr1, 9, 9, 9, ...arr2], [0, 1, 2, 9, 9, 9, 3 ,4, 5]);
69assertArrayEqual ([...arr1, ...[...arr2]], [0, 1, 2, 3 ,4, 5]);
70assertArrayEqual (["0" , "1", ...arr1, ...[...arr2]], ["0", "1", 0, 1, 2, 3 ,4, 5]);
71assertArrayEqual ([...arr3], arr3);
72assertArrayEqual ([..."foobar"], ["f", "o", "o", "b", "a" ,"r"]);
73assertArrayEqual ([...(new Set([1, "foo", arr3]))], [1, "foo", arr3]);
74
75var holyArray = [,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,...arr1];
76assert (holyArray.length === 83);
77assert (holyArray[82] === 2);
78assert (holyArray[81] === 1);
79assert (holyArray[80] === 0);
80