Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // 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 | 'use strict'; |
| 6 | |
| 7 | (function TestDefaultConstructor() { |
| 8 | class Stack extends Array { } |
| 9 | { |
| 10 | let s1 = new Stack(); |
| 11 | assertSame(Stack.prototype, s1.__proto__); |
| 12 | assertTrue(Array.isArray(s1)); |
| 13 | assertSame(0, s1.length); |
| 14 | s1[0] = 'xyz'; |
| 15 | assertSame(1, s1.length); |
| 16 | assertSame('xyz', s1[0]); |
| 17 | s1.push(42); |
| 18 | assertSame(2, s1.length); |
| 19 | assertSame('xyz', s1[0]); |
| 20 | assertSame(42, s1[1]); |
| 21 | } |
| 22 | |
| 23 | { |
| 24 | let s2 = new Stack(10); |
| 25 | assertSame(Stack.prototype, s2.__proto__); |
| 26 | assertTrue(Array.isArray(s2)); |
| 27 | assertSame(10, s2.length); |
| 28 | assertSame(undefined, s2[0]); |
| 29 | } |
| 30 | |
| 31 | { |
| 32 | let a = [1,2,3]; |
| 33 | let s3 = new Stack(a); |
| 34 | assertSame(Stack.prototype, s3.__proto__); |
| 35 | assertTrue(Array.isArray(s3)); |
| 36 | assertSame(1, s3.length); |
| 37 | assertSame(a, s3[0]); |
| 38 | } |
| 39 | |
| 40 | { |
| 41 | let s4 = new Stack(1, 2, 3); |
| 42 | assertSame(Stack.prototype, s4.__proto__); |
| 43 | assertTrue(Array.isArray(s4)); |
| 44 | assertSame(3, s4.length); |
| 45 | assertSame(1, s4[0]); |
| 46 | assertSame(2, s4[1]); |
| 47 | assertSame(3, s4[2]); |
| 48 | } |
| 49 | |
| 50 | { |
| 51 | let s5 = new Stack(undefined, undefined, undefined); |
| 52 | assertSame(Stack.prototype, s5.__proto__); |
| 53 | assertTrue(Array.isArray(s5)); |
| 54 | assertSame(3, s5.length); |
| 55 | assertSame(undefined, s5[0]); |
| 56 | assertSame(undefined, s5[1]); |
| 57 | assertSame(undefined, s5[2]); |
| 58 | } |
| 59 | }()); |
| 60 | |
| 61 | |
| 62 | (function TestEmptyArgsSuper() { |
| 63 | class Stack extends Array { |
| 64 | constructor() { super(); } |
| 65 | } |
| 66 | let s1 = new Stack(); |
| 67 | assertSame(Stack.prototype, s1.__proto__); |
| 68 | assertTrue(Array.isArray(s1)); |
| 69 | assertSame(0, s1.length); |
| 70 | s1[0] = 'xyz'; |
| 71 | assertSame(1, s1.length); |
| 72 | assertSame('xyz', s1[0]); |
| 73 | s1.push(42); |
| 74 | assertSame(2, s1.length); |
| 75 | assertSame('xyz', s1[0]); |
| 76 | assertSame(42, s1[1]); |
| 77 | }()); |
| 78 | |
| 79 | |
| 80 | (function TestOneArgSuper() { |
| 81 | class Stack extends Array { |
| 82 | constructor(x) { |
| 83 | super(x); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | { |
| 88 | let s2 = new Stack(10, 'ignored arg'); |
| 89 | assertSame(Stack.prototype, s2.__proto__); |
| 90 | assertTrue(Array.isArray(s2)); |
| 91 | assertSame(10, s2.length); |
| 92 | assertSame(undefined, s2[0]); |
| 93 | } |
| 94 | |
| 95 | { |
| 96 | let a = [1,2,3]; |
| 97 | let s3 = new Stack(a, 'ignored arg'); |
| 98 | assertSame(Stack.prototype, s3.__proto__); |
| 99 | assertTrue(Array.isArray(s3)); |
| 100 | assertSame(1, s3.length); |
| 101 | assertSame(a, s3[0]); |
| 102 | } |
| 103 | }()); |
| 104 | |
| 105 | |
| 106 | (function TestMultipleArgsSuper() { |
| 107 | class Stack extends Array { |
| 108 | constructor(x, y, z) { |
| 109 | super(x, y, z); |
| 110 | } |
| 111 | } |
| 112 | { |
| 113 | let s4 = new Stack(1, 2, 3, 4, 5); |
| 114 | assertSame(Stack.prototype, s4.__proto__); |
| 115 | assertTrue(Array.isArray(s4)); |
| 116 | assertSame(3, s4.length); |
| 117 | assertSame(1, s4[0]); |
| 118 | assertSame(2, s4[1]); |
| 119 | assertSame(3, s4[2]); |
| 120 | } |
| 121 | |
| 122 | { |
| 123 | let s5 = new Stack(undefined); |
| 124 | assertSame(Stack.prototype, s5.__proto__); |
| 125 | assertTrue(Array.isArray(s5)); |
| 126 | assertTrue(s5.__proto__ == Stack.prototype); |
| 127 | assertSame(3, s5.length); |
| 128 | assertSame(undefined, s5[0]); |
| 129 | assertSame(undefined, s5[1]); |
| 130 | assertSame(undefined, s5[2]); |
| 131 | } |
| 132 | }()); |
| 133 | |
| 134 | |
| 135 | (function TestArrayConcat() { |
| 136 | class Stack extends Array { } |
| 137 | let s1 = new Stack(1,2,3); |
| 138 | |
| 139 | assertArrayEquals([1,2,3,4,5,6], s1.concat([4,5,6])); |
| 140 | assertArrayEquals([4,5,6,1,2,3], [4,5,6].concat(s1)); |
| 141 | }()); |
| 142 | |
| 143 | |
| 144 | (function TestJSONStringify() { |
| 145 | class Stack extends Array { } |
| 146 | |
| 147 | let s1 = new Stack(1,2,3); |
| 148 | assertSame("[1,2,3]", JSON.stringify(s1)); |
| 149 | }()); |