harmony-string-pad-start.js 3.4 KB
Newer Older
1 2 3 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
// Copyright 2016 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.

(function TestMeta() {
  assertEquals(1, String.prototype.padStart.length);
  assertEquals("function", typeof String.prototype.padStart);
  assertEquals(Object.getPrototypeOf(Function),
               Object.getPrototypeOf(String.prototype.padStart));
  assertEquals("padStart", String.prototype.padStart.name);

  var desc = Object.getOwnPropertyDescriptor(String.prototype, "padStart");
  assertFalse(desc.enumerable);
  assertTrue(desc.configurable);
  assertTrue(desc.writable);
  assertEquals(undefined, desc.get);
  assertEquals(undefined, desc.set);

  assertThrows(() => new Function(`${String.prototype.padStart}`), SyntaxError);
})();


(function TestRequireObjectCoercible() {
  var padStart = String.prototype.padStart;
  assertThrows(() => padStart.call(null, 4, "test"), TypeError);
  assertThrows(() => padStart.call(undefined, 4, "test"), TypeError);
  assertEquals("   123", padStart.call({
    __proto__: null,
    valueOf() { return 123; }
  }, 6, " "));

  var proxy = new Proxy({}, {
    get(t, name) {
      if (name === Symbol.toPrimitive || name === "toString") return;
      if (name === "valueOf") return () => 6.7;
      assertUnreachable();
    }
  });
  assertEquals("   6.7", padStart.call(proxy, 6, " "));

  proxy = new Proxy({}, {
    get(t, name) {
      if (name === Symbol.toPrimitive || name === "valueOf") return;
      if (name === "toString") return () => 6.7;
      assertUnreachable();
    }
  });
  assertEquals("   6.7", padStart.call(proxy, 6, " "));
})();


(function TestToLength() {
  assertThrows(() => "123".padStart(Symbol("16")), TypeError);
  assertEquals("123", "123".padStart(-1));
  assertEquals("123", "123".padStart({ toString() { return -1; } }));
  assertEquals("123", "123".padStart(-0));
  assertEquals("123", "123".padStart({ toString() { return -0; } }));
  assertEquals("123", "123".padStart(+0));
  assertEquals("123", "123".padStart({ toString() { return +0; } }));
  assertEquals("123", "123".padStart(NaN));
  assertEquals("123", "123".padStart({ toString() { return NaN; } }));
})();


(function TestFillerToString() {
  assertEquals("         .", ".".padStart(10));
  assertEquals("         .", ".".padStart(10, undefined));
  assertEquals("nullnulln.", ".".padStart(10, null));
69 70 71 72 73 74 75 76 77 78 79 80
  assertEquals("XXXXXXXXX.", ".".padStart(10, { toString() { return "X"; } }));
  assertEquals(
      "111111111.",
      ".".padStart(10, { toString: undefined, valueOf() { return 1; } }));
})();


(function TestFillerEmptyString() {
  assertEquals(".", ".".padStart(10, ""));
  assertEquals(".", ".".padStart(10, { toString() { return ""; } }));
  assertEquals(
      ".", ".".padStart(10, { toString: undefined, valueOf() { return ""; } }));
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
})();


(function TestFillerRepetition() {
  for (var i = 2000; i > 0; --i) {
    var expected = "xoxo".repeat(i / 4).slice(0, i - 3) + "123";
    var actual = "123".padStart(i, "xoxo");
    assertEquals(expected, actual);
    assertEquals(i > "123".length ? i : 3, actual.length);
  }
})();


(function TestTruncation() {
  assertEquals("ba", "a".padStart(2, "bc"));
})();
97 98 99 100 101 102 103 104 105 106 107


(function TestMaxLength() {
  assertThrows(() => "123".padStart(Math.pow(2, 40)), RangeError);
  assertThrows(() => "123".padStart(1 << 30), RangeError);
})();


(function TestNoArguments() {
  assertEquals("abc", "abc".padStart());
})();