| 1 |
function test() { |
| 2 |
var from, to; |
| 3 |
|
| 4 |
// Property changes value. |
| 5 |
from = {x: 1, y: 2}; |
| 6 |
to = {set x(v) { from.y = 5; }}; |
| 7 |
Object.assign(to, from); |
| 8 |
assertEq(to.y, 5); |
| 9 |
|
| 10 |
// Property becomes a getter. |
| 11 |
from = {x: 1, y: 2}; |
| 12 |
to = {set x(v) { Object.defineProperty(from, "y", {get: () => 4}); }}; |
| 13 |
Object.assign(to, from); |
| 14 |
assertEq(to.y, 4); |
| 15 |
|
| 16 |
// Property becomes non-enumerable. |
| 17 |
from = {x: 1, y: 2}; |
| 18 |
to = {set x(v) { Object.defineProperty(from, "y", {value: 2, |
| 19 |
enumerable: false, |
| 20 |
configurable: true, |
| 21 |
writable: true}); }}; |
| 22 |
Object.assign(to, from); |
| 23 |
assertEq("y" in to, false); |
| 24 |
to = {}; |
| 25 |
Object.assign(to, from); |
| 26 |
assertEq("y" in to, false); |
| 27 |
|
| 28 |
// Property is deleted. Should NOT get Object.prototype.toString. |
| 29 |
from = {x: 1, toString: 2}; |
| 30 |
to = {set x(v) { delete from.toString; }}; |
| 31 |
Object.assign(to, from); |
| 32 |
assertEq(to.hasOwnProperty("toString"), false); |
| 33 |
|
| 34 |
from = {toString: 2, x: 1}; |
| 35 |
to = {set x(v) { delete from.toString; }}; |
| 36 |
Object.assign(to, from); |
| 37 |
assertEq(to.toString, 2); |
| 38 |
|
| 39 |
from = {x: 1, toString: 2, y: 3}; |
| 40 |
to = {set x(v) { delete from.toString; }}; |
| 41 |
Object.assign(to, from); |
| 42 |
assertEq(to.hasOwnProperty("toString"), false); |
| 43 |
assertEq(to.y, 3); |
| 44 |
|
| 45 |
// New property is added. |
| 46 |
from = {x: 1, y: 2}; |
| 47 |
to = {set x(v) { from.z = 3; }}; |
| 48 |
Object.assign(to, from); |
| 49 |
assertEq("z" in to, false); |
| 50 |
|
| 51 |
// From getter. |
| 52 |
var c = 7; |
| 53 |
from = {x: 1, get y() { return ++c; }}; |
| 54 |
to = {}; |
| 55 |
Object.assign(to, from); |
| 56 |
Object.assign(to, from, from); |
| 57 |
assertEq(to.y, 10); |
| 58 |
|
| 59 |
// Frozen object. |
| 60 |
from = {x: 1, y: 2}; |
| 61 |
to = {x: 4}; |
| 62 |
Object.freeze(to); |
| 63 |
var ex; |
| 64 |
try { |
| 65 |
Object.assign(to, from); |
| 66 |
} catch (e) { |
| 67 |
ex = e; |
| 68 |
} |
| 69 |
assertEq(ex instanceof TypeError, true); |
| 70 |
assertEq(to.x, 4); |
| 71 |
|
| 72 |
// Non-writable property. |
| 73 |
from = {x: 1, y: 2, z: 3}; |
| 74 |
to = {}; |
| 75 |
Object.defineProperty(to, "y", {value: 9, writable: false}); |
| 76 |
ex = null; |
| 77 |
try { |
| 78 |
Object.assign(to, from); |
| 79 |
} catch(e) { |
| 80 |
ex = e; |
| 81 |
} |
| 82 |
assertEq(ex instanceof TypeError, true); |
| 83 |
assertEq(to.x, 1); |
| 84 |
assertEq(to.y, 9); |
| 85 |
assertEq(to.z, undefined); |
| 86 |
|
| 87 |
// Array with dense elements. |
| 88 |
from = [1, 2, 3]; |
| 89 |
to = {}; |
| 90 |
Object.assign(to, from); |
| 91 |
assertEq(to[2], 3); |
| 92 |
assertEq("length" in to, false); |
| 93 |
|
| 94 |
// Object with sparse elements and symbols. |
| 95 |
from = {x: 1, 1234567: 2, 1234560: 3,[Symbol.iterator]: 5, z: 3}; |
| 96 |
to = {}; |
| 97 |
Object.assign(to, from); |
| 98 |
assertEq(to[1234567], 2); |
| 99 |
assertEq(Object.keys(to).toString(), "1234560,1234567,x,z"); |
| 100 |
assertEq(to[Symbol.iterator], 5); |
| 101 |
|
| 102 |
// Symbol properties need to be assigned last. |
| 103 |
from = {x: 1, [Symbol.iterator]: 2, y: 3}; |
| 104 |
to = {set y(v) { throw 9; }}; |
| 105 |
ex = null; |
| 106 |
try { |
| 107 |
Object.assign(to, from); |
| 108 |
} catch (e) { |
| 109 |
ex = e; |
| 110 |
} |
| 111 |
assertEq(ex, 9); |
| 112 |
assertEq(to.x, 1); |
| 113 |
assertEq(to.hasOwnProperty(Symbol.iterator), false); |
| 114 |
|
| 115 |
// Typed array. |
| 116 |
from = new Int32Array([1, 2, 3]); |
| 117 |
to = {}; |
| 118 |
Object.assign(to, from); |
| 119 |
assertEq(to[1], 2); |
| 120 |
|
| 121 |
// Primitive string. |
| 122 |
from = "foo"; |
| 123 |
to = {}; |
| 124 |
Object.assign(to, from); |
| 125 |
assertEq(to[0], "f"); |
| 126 |
|
| 127 |
// String object. |
| 128 |
from = new String("bar"); |
| 129 |
to = {}; |
| 130 |
Object.assign(to, from); |
| 131 |
assertEq(to[2], "r"); |
| 132 |
} |
| 133 |
test(); |
| 134 |
test(); |
| 135 |
test(); |