arrow-rest-params.js 3.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright 2014 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 testRestIndex() {
  assertEquals(5, ((...args) => args.length)(1,2,3,4,5));
  assertEquals(4, ((a, ...args) => args.length)(1,2,3,4,5));
  assertEquals(3, ((a, b, ...args) => args.length)(1,2,3,4,5));
  assertEquals(2, ((a, b, c, ...args) => args.length)(1,2,3,4,5));
  assertEquals(1, ((a, b, c, d, ...args) => args.length)(1,2,3,4,5));
  assertEquals(0, ((a, b, c, d, e, ...args) => args.length)(1,2,3,4,5));
})();

// strictTest and sloppyTest should be called with descending natural
// numbers, as in:
//
//   strictTest(6,5,4,3,2,1)
//
19
var strictTest = (() => {
20
  "use strict";
21 22 23
  return (a, b, ...c) => {
    assertEquals(Array, c.constructor);
    assertTrue(Array.isArray(c));
24

25 26
    var expectedLength = (a === undefined) ? 0 : a - 2;
    assertEquals(expectedLength, c.length);
27

28 29 30 31 32
    for (var i = 2; i < a; ++i) {
      assertEquals(c[i - 2], a - i);
    }
  };
})();
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

var sloppyTest = (a, b, ...c) => {
  assertEquals(Array, c.constructor);
  assertTrue(Array.isArray(c));

  var expectedLength = (a === undefined) ? 0 : a - 2;
  assertEquals(expectedLength, c.length);

  for (var i = 2; i < a; ++i) {
    assertEquals(c[i - 2], a - i);
  }
}


var O = {
  strict: strictTest,
  sloppy: sloppyTest
};

(function testStrictRestParamArity() {
  assertEquals(2, strictTest.length);
  assertEquals(2, O.strict.length);
})();


(function testRestParamsStrictMode() {
  strictTest();
  strictTest(2, 1);
  strictTest(6, 5, 4, 3, 2, 1);
  strictTest(3, 2, 1);
  O.strict();
  O.strict(2, 1);
  O.strict(6, 5, 4, 3, 2, 1);
  O.strict(3, 2, 1);
})();


(function testRestParamsStrictModeApply() {
  strictTest.apply(null, []);
  strictTest.apply(null, [2, 1]);
  strictTest.apply(null, [6, 5, 4, 3, 2, 1]);
  strictTest.apply(null, [3, 2, 1]);
  O.strict.apply(O, []);
  O.strict.apply(O, [2, 1]);
  O.strict.apply(O, [6, 5, 4, 3, 2, 1]);
  O.strict.apply(O, [3, 2, 1]);
})();


(function testRestParamsStrictModeCall() {
  strictTest.call(null);
  strictTest.call(null, 2, 1);
  strictTest.call(null, 6, 5, 4, 3, 2, 1);
  strictTest.call(null, 3, 2, 1);
  O.strict.call(O);
  O.strict.call(O, 2, 1);
  O.strict.call(O, 6, 5, 4, 3, 2, 1);
  O.strict.call(O, 3, 2, 1);
})();


(function testsloppyRestParamArity() {
  assertEquals(2, sloppyTest.length);
  assertEquals(2, O.sloppy.length);
})();


(function testRestParamssloppyMode() {
  sloppyTest();
  sloppyTest(2, 1);
  sloppyTest(6, 5, 4, 3, 2, 1);
  sloppyTest(3, 2, 1);
  O.sloppy();
  O.sloppy(2, 1);
  O.sloppy(6, 5, 4, 3, 2, 1);
  O.sloppy(3, 2, 1);
})();


(function testRestParamssloppyModeApply() {
  sloppyTest.apply(null, []);
  sloppyTest.apply(null, [2, 1]);
  sloppyTest.apply(null, [6, 5, 4, 3, 2, 1]);
  sloppyTest.apply(null, [3, 2, 1]);
  O.sloppy.apply(O, []);
  O.sloppy.apply(O, [2, 1]);
  O.sloppy.apply(O, [6, 5, 4, 3, 2, 1]);
  O.sloppy.apply(O, [3, 2, 1]);
})();


(function testRestParamssloppyModeCall() {
  sloppyTest.call(null);
  sloppyTest.call(null, 2, 1);
  sloppyTest.call(null, 6, 5, 4, 3, 2, 1);
  sloppyTest.call(null, 3, 2, 1);
  O.sloppy.call(O);
  O.sloppy.call(O, 2, 1);
  O.sloppy.call(O, 6, 5, 4, 3, 2, 1);
  O.sloppy.call(O, 3, 2, 1);
})();


(function testUnmappedArguments() {
  // Normal functions make their arguments object unmapped, but arrow
  // functions don't have an arguments object anyway.  Check that the
  // right thing happens for arguments in arrow functions with rest
  // parameters.
  assertSame(arguments, ((...rest) => arguments)());
})();