tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

object-assign-plain.js (4033B)


      1 // Tests for Object.assign's fast path for plain objects.
      2 
      3 load(libdir + "asserts.js");
      4 
      5 function testProtoSetter() {
      6    var from = Object.create(null);
      7    from.__proto__ = {};
      8    assertEq(Object.getPrototypeOf(from), null);
      9 
     10    var to = Object.assign({}, from);
     11    assertEq(Object.getPrototypeOf(to), from.__proto__);
     12    assertEq(Object.getOwnPropertyNames(to).length, 0);
     13 }
     14 testProtoSetter();
     15 testProtoSetter();
     16 testProtoSetter();
     17 
     18 function testProtoDataProp() {
     19    var to = Object.create(null);
     20    to.__proto__ = 1;
     21    var from = Object.create(null);
     22    from.__proto__ = 2;
     23    Object.assign(to, from);
     24    assertEq(Object.getPrototypeOf(to), null);
     25    assertEq(to.__proto__, 2);
     26 }
     27 testProtoDataProp();
     28 testProtoDataProp();
     29 testProtoDataProp();
     30 
     31 function testNonExtensible() {
     32    var to = Object.preventExtensions({x: 1});
     33    Object.assign(to, {x: 2});
     34    assertEq(to.x, 2);
     35    assertThrowsInstanceOf(() => Object.assign(to, {x: 3, y: 4}), TypeError);
     36    assertEq(to.x, 3);
     37    assertEq("y" in to, false);
     38 }
     39 testNonExtensible();
     40 testNonExtensible();
     41 testNonExtensible();
     42 
     43 function testNonExtensibleNoProps() {
     44    var to = Object.preventExtensions({});
     45    Object.assign(to, {}); // No exception.
     46 }
     47 testNonExtensibleNoProps();
     48 testNonExtensibleNoProps();
     49 testNonExtensibleNoProps();
     50 
     51 function testDenseElements() {
     52    var to = Object.assign({}, {0: 1, 1: 2});
     53    assertEq(to[0], 1);
     54    assertEq(to[1], 2);
     55 }
     56 testDenseElements();
     57 testDenseElements();
     58 testDenseElements();
     59 
     60 function testNonWritableOnProto() {
     61    var proto = {};
     62    Object.defineProperty(proto, "x", {value: 1, enumerable: true, configurable: true});
     63    var to = Object.create(proto);
     64    assertThrowsInstanceOf(() => Object.assign(to, {x: 2}), TypeError);
     65    assertEq(to.x, 1);
     66    assertEq(Object.getOwnPropertyNames(to).length, 0);
     67 }
     68 testNonWritableOnProto();
     69 testNonWritableOnProto();
     70 testNonWritableOnProto();
     71 
     72 function testAccessorOnProto() {
     73    var setterVal;
     74    var proto = {set a(v) { setterVal = v; }};
     75    var to = Object.assign(Object.create(proto), {a: 9});
     76    assertEq(setterVal, 9);
     77    assertEq(Object.getOwnPropertyNames(to).length, 0);
     78 }
     79 testAccessorOnProto();
     80 testAccessorOnProto();
     81 testAccessorOnProto();
     82 
     83 function testSetAndAdd() {
     84    var to = Object.assign({x: 1, y: 2}, {x: 3, y: 4, z: 5});
     85    assertEq(to.x, 3);
     86    assertEq(to.y, 4);
     87    assertEq(to.z, 5);
     88 }
     89 testSetAndAdd();
     90 testSetAndAdd();
     91 testSetAndAdd();
     92 
     93 function testNonConfigurableFrom() {
     94    var from = {};
     95    Object.defineProperty(from, "x", {value: 1, enumerable: true, writable: true});
     96    var to = Object.assign({}, from);
     97    assertEq(to.x, 1);
     98    assertEq(Object.getOwnPropertyDescriptor(to, "x").configurable, true);
     99 }
    100 testNonConfigurableFrom();
    101 testNonConfigurableFrom();
    102 testNonConfigurableFrom();
    103 
    104 function testNonEnumerableFrom() {
    105    var from = {};
    106    Object.defineProperty(from, "x", {value: 1, configurable: true, writable: true});
    107    var to = Object.assign({}, from);
    108    assertEq(Object.getOwnPropertyNames(to).length, 0);
    109    assertEq(to.x, undefined);
    110 }
    111 testNonEnumerableFrom();
    112 testNonEnumerableFrom();
    113 testNonEnumerableFrom();
    114 
    115 function testNonWritableFrom() {
    116    var from = {};
    117    Object.defineProperty(from, "x", {value: 1, configurable: true, enumerable: true});
    118    var to = Object.assign({}, from);
    119    assertEq(to.x, 1);
    120    assertEq(Object.getOwnPropertyDescriptor(to, "x").writable, true);
    121 }
    122 testNonWritableFrom();
    123 testNonWritableFrom();
    124 testNonWritableFrom();
    125 
    126 function testFrozenProto() {
    127    var proto = Object.freeze({x: 1});
    128    var target = Object.create(proto);
    129    Object.assign(target, {foo: 1});
    130    assertEq(target.foo, 1);
    131    assertThrowsInstanceOf(() => Object.assign(target, {x: 2}), TypeError);
    132    assertEq(target.x, 1);
    133 }
    134 testFrozenProto();
    135 testFrozenProto();
    136 testFrozenProto();
    137 
    138 function testReuseShape() {
    139    var from = {};
    140    from.x = 1;
    141    from.y = 2;
    142    var to = Object.assign({}, from);
    143    assertEq(to.x, 1);
    144    assertEq(to.y, 2);
    145 }
    146 testReuseShape();
    147 testReuseShape();
    148 testReuseShape();