classes-subclass-arrays.js 3.36 KB
Newer Older
1 2 3
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
'use strict';

(function TestDefaultConstructor() {
  class Stack extends Array { }
  {
    let s1 = new Stack();
    assertSame(Stack.prototype, s1.__proto__);
    assertTrue(Array.isArray(s1));
    assertSame(0, s1.length);
    s1[0] = 'xyz';
    assertSame(1, s1.length);
    assertSame('xyz', s1[0]);
    s1.push(42);
    assertSame(2, s1.length);
    assertSame('xyz', s1[0]);
    assertSame(42, s1[1]);
  }

  {
    let s2 = new Stack(10);
    assertSame(Stack.prototype, s2.__proto__);
    assertTrue(Array.isArray(s2));
    assertSame(10, s2.length);
    assertSame(undefined, s2[0]);
  }

  {
    let a = [1,2,3];
    let s3 = new Stack(a);
    assertSame(Stack.prototype, s3.__proto__);
    assertTrue(Array.isArray(s3));
    assertSame(1, s3.length);
    assertSame(a, s3[0]);
  }

  {
    let s4 = new Stack(1, 2, 3);
    assertSame(Stack.prototype, s4.__proto__);
    assertTrue(Array.isArray(s4));
    assertSame(3, s4.length);
    assertSame(1, s4[0]);
    assertSame(2, s4[1]);
    assertSame(3, s4[2]);
  }

  {
    let s5 = new Stack(undefined, undefined, undefined);
    assertSame(Stack.prototype, s5.__proto__);
    assertTrue(Array.isArray(s5));
    assertSame(3, s5.length);
    assertSame(undefined, s5[0]);
    assertSame(undefined, s5[1]);
    assertSame(undefined, s5[2]);
  }
}());


(function TestEmptyArgsSuper() {
  class Stack extends Array {
    constructor() { super(); }
  }
  let s1 = new Stack();
  assertSame(Stack.prototype, s1.__proto__);
  assertTrue(Array.isArray(s1));
  assertSame(0, s1.length);
  s1[0] = 'xyz';
  assertSame(1, s1.length);
  assertSame('xyz', s1[0]);
  s1.push(42);
  assertSame(2, s1.length);
  assertSame('xyz', s1[0]);
  assertSame(42, s1[1]);
}());


(function TestOneArgSuper() {
  class Stack extends Array {
    constructor(x) {
      super(x);
    }
  }

  {
    let s2 = new Stack(10, 'ignored arg');
    assertSame(Stack.prototype, s2.__proto__);
    assertTrue(Array.isArray(s2));
    assertSame(10, s2.length);
    assertSame(undefined, s2[0]);
  }

  {
    let a = [1,2,3];
    let s3 = new Stack(a, 'ignored arg');
    assertSame(Stack.prototype, s3.__proto__);
    assertTrue(Array.isArray(s3));
    assertSame(1, s3.length);
    assertSame(a, s3[0]);
  }
}());


(function TestMultipleArgsSuper() {
  class Stack extends Array {
    constructor(x, y, z) {
      super(x, y, z);
    }
  }
  {
    let s4 = new Stack(1, 2, 3, 4, 5);
    assertSame(Stack.prototype, s4.__proto__);
    assertTrue(Array.isArray(s4));
    assertSame(3, s4.length);
    assertSame(1, s4[0]);
    assertSame(2, s4[1]);
    assertSame(3, s4[2]);
  }

  {
    let s5 = new Stack(undefined);
    assertSame(Stack.prototype, s5.__proto__);
    assertTrue(Array.isArray(s5));
    assertTrue(s5.__proto__ == Stack.prototype);
    assertSame(3, s5.length);
    assertSame(undefined, s5[0]);
    assertSame(undefined, s5[1]);
    assertSame(undefined, s5[2]);
  }
}());


(function TestArrayConcat() {
  class Stack extends Array { }
  let s1 = new Stack(1,2,3);

  assertArrayEquals([1,2,3,4,5,6], s1.concat([4,5,6]));
  assertArrayEquals([4,5,6,1,2,3], [4,5,6].concat(s1));
}());


(function TestJSONStringify() {
  class Stack extends Array { }

  let s1 = new Stack(1,2,3);
  assertSame("[1,2,3]", JSON.stringify(s1));
}());