Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1835)

Side by Side Diff: test/mjsunit/harmony/array-concat.js

Issue 771483002: Implement ES6 @@isConcatSpreadable (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebase Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/runtime/runtime-array.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2008 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Use of this source code is governed by a BSD-style license that can be
3 // modification, are permitted provided that the following conditions are 3 // found in the LICENSE file.
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 4
28 /** 5 // Flags: --harmony-arrays --harmony-classes
29 * @fileoverview Test concat on small and large arrays 6
30 */ 7 "use strict";
8
9 (function testArrayConcatArity() {
Dmitry Lomov (no reviews) 2014/12/10 14:41:59 Please add a test that sets isConcatSpreadable on
caitp (gmail) 2014/12/10 15:29:20 Acknowledged --- but reading the draft, I am not s
10 assertEquals(1, Array.prototype.concat.length);
11 })();
12
13
14 (function testArrayConcatNoPrototype() {
15 assertEquals(void 0, Array.prototype.concat.prototype);
16 })();
17
18
19 (function testArrayConcatDescriptor() {
20 var desc = Object.getOwnPropertyDescriptor(Array.prototype, 'concat');
21 assertEquals(false, desc.enumerable);
22 })();
23
24
25 (function testConcatArrayLike() {
26 var obj = {
27 "length": 6,
28 "1": "A",
29 "3": "B",
30 "5": "C"
31 };
32 obj[Symbol.isConcatSpreadable] = true;
33 var obj2 = { length: 3, "0": "0", "1": "1", "2": "2" };
34 var arr = ["X", "Y", "Z"];
35 assertEquals([void 0, "A", void 0, "B", void 0, "C",
36 { "length": 3, "0": "0", "1": "1", "2": "2" },
37 "X", "Y", "Z"], Array.prototype.concat.call(obj, obj2, arr));
38 })();
39
40
41 (function testConcatHoleyArray() {
42 var arr = [];
43 arr[4] = "Item 4";
44 arr[8] = "Item 8";
45 var arr2 = [".", "!", "?"];
46 assertEquals([void 0, void 0, void 0, void 0, "Item 4", void 0, void 0,
47 void 0, "Item 8", ".", "!", "?"], arr.concat(arr2));
48 })();
49
50
51 (function testIsConcatSpreadableGetterThrows() {
52 function MyError() {}
53 var obj = {};
54 Object.defineProperty(obj, Symbol.isConcatSpreadable, {
55 get: function() { throw new MyError(); }
56 });
57
58 assertThrows(function() {
59 [].concat(obj);
60 }, MyError);
61
62 assertThrows(function() {
63 Array.prototype.concat.call(obj, 1, 2, 3);
64 }, MyError);
65 })();
66
67
68 (function testConcatLengthThrows() {
69 function MyError() {}
70 var obj = {};
71 obj[Symbol.isConcatSpreadable] = true;
72 Object.defineProperty(obj, "length", {
73 get: function() { throw new MyError(); }
74 });
75
76 assertThrows(function() {
77 [].concat(obj);
78 }, MyError);
79
80 assertThrows(function() {
81 Array.prototype.concat.call(obj, 1, 2, 3);
82 }, MyError);
83 })();
84
85
86 (function testConcatArraySubclass() {
87 // TODO(caitp): when concat is called on instances of classes which extend
88 // Array, they should:
89 //
90 // - return an instance of the class, rather than an Array instance (if from
91 // same Realm)
92 // - always treat such classes as concat-spreadable
93 })();
94
95
96 (function testConcatNonArray() {
97 class NonArray {
98 constructor() { Array.apply(this, arguments); }
99 };
100
101 var obj = new NonArray(1,2,3);
102 var result = Array.prototype.concat.call(obj, 4, 5, 6);
103 assertEquals(Array, result.constructor);
104 assertEquals([obj,4,5,6], result);
105 assertFalse(result instanceof NonArray);
106 })();
107
108
109 // ES5 tests
31 110
32 var poses; 111 var poses;
112 var pos;
33 113
34 poses = [140, 4000000000]; 114 poses = [140, 4000000000];
35 while (pos = poses.shift()) { 115 while (pos = poses.shift()) {
36 var a = new Array(pos); 116 var a = new Array(pos);
37 var array_proto = []; 117 var array_proto = [];
38 a.__proto__ = array_proto; 118 a.__proto__ = array_proto;
39 assertEquals(pos, a.length); 119 assertEquals(pos, a.length);
40 a.push('foo'); 120 a.push('foo');
41 assertEquals(pos + 1, a.length); 121 assertEquals(pos + 1, a.length);
42 var b = ['bar']; 122 var b = ['bar'];
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 function mkGetter(i) { return function() { trace.push(i); }; } 308 function mkGetter(i) { return function() { trace.push(i); }; }
229 arr3.length = 10000; 309 arr3.length = 10000;
230 for (var i = 0; i < 100; i++) { 310 for (var i = 0; i < 100; i++) {
231 Object.defineProperty(arr3, i * i, {get: mkGetter(i)}); 311 Object.defineProperty(arr3, i * i, {get: mkGetter(i)});
232 expectedTrace[i] = i; 312 expectedTrace[i] = i;
233 expectedTrace[100 + i] = i; 313 expectedTrace[100 + i] = i;
234 } 314 }
235 var r4 = [0].concat(arr3, arr3); 315 var r4 = [0].concat(arr3, arr3);
236 assertEquals(1 + arr3.length * 2, r4.length); 316 assertEquals(1 + arr3.length * 2, r4.length);
237 assertEquals(expectedTrace, trace); 317 assertEquals(expectedTrace, trace);
OLDNEW
« no previous file with comments | « src/runtime/runtime-array.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698