OLD | NEW |
| (Empty) |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Flags: --harmony-object | |
6 | |
7 // Based on Mozilla Object.assign() tests | |
8 | |
9 function checkDataProperty(object, propertyKey, value, writable, enumerable, con
figurable) { | |
10 var desc = Object.getOwnPropertyDescriptor(object, propertyKey); | |
11 assertFalse(desc === undefined); | |
12 assertTrue('value' in desc); | |
13 assertEquals(desc.value, value); | |
14 assertEquals(desc.writable, writable); | |
15 assertEquals(desc.enumerable, enumerable); | |
16 assertEquals(desc.configurable, configurable); | |
17 } | |
18 | |
19 // 19.1.2.1 Object.assign ( target, ...sources ) | |
20 assertEquals(Object.assign.length, 2); | |
21 | |
22 // Basic functionality works with multiple sources | |
23 (function basicMultipleSources() { | |
24 var a = {}; | |
25 var b = { bProp: 1 }; | |
26 var c = { cProp: 2 }; | |
27 Object.assign(a, b, c); | |
28 assertEquals(a, { | |
29 bProp: 1, | |
30 cProp: 2 | |
31 }); | |
32 })(); | |
33 | |
34 // Basic functionality works with symbols | |
35 (function basicSymbols() { | |
36 var a = {}; | |
37 var b = { bProp: 1 }; | |
38 var aSymbol = Symbol("aSymbol"); | |
39 b[aSymbol] = 2; | |
40 Object.assign(a, b); | |
41 assertEquals(1, a.bProp); | |
42 assertEquals(2, a[aSymbol]); | |
43 })(); | |
44 | |
45 // Dies if target is null or undefined | |
46 assertThrows(function() { return Object.assign(null, null); }, TypeError); | |
47 assertThrows(function() { return Object.assign(null, {}); }, TypeError); | |
48 assertThrows(function() { return Object.assign(undefined); }, TypeError); | |
49 assertThrows(function() { return Object.assign(); }, TypeError); | |
50 | |
51 // Calls ToObject for target | |
52 assertTrue(Object.assign(true, {}) instanceof Boolean); | |
53 assertTrue(Object.assign(1, {}) instanceof Number); | |
54 assertTrue(Object.assign("string", {}) instanceof String); | |
55 var o = {}; | |
56 assertSame(Object.assign(o, {}), o); | |
57 | |
58 // Only [[Enumerable]] properties are assigned to target | |
59 (function onlyEnumerablePropertiesAssigned() { | |
60 var source = Object.defineProperties({}, { | |
61 a: {value: 1, enumerable: true}, | |
62 b: {value: 2, enumerable: false}, | |
63 }); | |
64 var target = Object.assign({}, source); | |
65 assertTrue("a" in target); | |
66 assertFalse("b" in target); | |
67 })(); | |
68 | |
69 // Properties are retrieved through Get() | |
70 // Properties are assigned through Put() | |
71 (function testPropertiesAssignedThroughPut() { | |
72 var setterCalled = false; | |
73 Object.assign({set a(v) { setterCalled = v }}, {a: true}); | |
74 assertTrue(setterCalled); | |
75 })(); | |
76 | |
77 // Properties are retrieved through Get() | |
78 // Properties are assigned through Put(): Existing property attributes are not a
ltered | |
79 (function propertiesAssignedExistingNotAltered() { | |
80 var source = {a: 1, b: 2, c: 3}; | |
81 var target = {a: 0, b: 0, c: 0}; | |
82 Object.defineProperty(target, "a", {enumerable: false}); | |
83 Object.defineProperty(target, "b", {configurable: false}); | |
84 Object.defineProperty(target, "c", {enumerable: false, configurable: false}); | |
85 Object.assign(target, source); | |
86 checkDataProperty(target, "a", 1, true, false, true); | |
87 checkDataProperty(target, "b", 2, true, true, false); | |
88 checkDataProperty(target, "c", 3, true, false, false); | |
89 })(); | |
90 | |
91 // Properties are retrieved through Get() | |
92 // Properties are assigned through Put(): Throws TypeError if non-writable | |
93 (function propertiesAssignedTypeErrorNonWritable() { | |
94 var source = {a: 1}; | |
95 var target = {a: 0}; | |
96 Object.defineProperty(target, "a", {writable: false}); | |
97 assertThrows(function() { return Object.assign(target, source); }, TypeError); | |
98 checkDataProperty(target, "a", 0, false, true, true); | |
99 })(); | |
100 | |
101 // Properties are retrieved through Get() | |
102 // Put() creates standard properties; Property attributes from source are | |
103 // ignored | |
104 (function createsStandardProperties() { | |
105 var source = {a: 1, b: 2, c: 3, get d() { return 4 }}; | |
106 Object.defineProperty(source, "b", {writable: false}); | |
107 Object.defineProperty(source, "c", {configurable: false}); | |
108 var target = Object.assign({}, source); | |
109 checkDataProperty(target, "a", 1, true, true, true); | |
110 checkDataProperty(target, "b", 2, true, true, true); | |
111 checkDataProperty(target, "c", 3, true, true, true); | |
112 checkDataProperty(target, "d", 4, true, true, true); | |
113 })(); | |
114 | |
115 // Properties created during traversal are not copied | |
116 (function propertiesCreatedDuringTraversalNotCopied() { | |
117 var source = {get a() { this.b = 2 }}; | |
118 var target = Object.assign({}, source); | |
119 assertTrue("a" in target); | |
120 assertFalse("b" in target); | |
121 })(); | |
122 | |
123 // String and Symbol valued properties are copied | |
124 (function testStringAndSymbolPropertiesCopied() { | |
125 var keyA = "str-prop"; | |
126 var source = {"str-prop": 1}; | |
127 var target = Object.assign({}, source); | |
128 checkDataProperty(target, keyA, 1, true, true, true); | |
129 })(); | |
130 | |
131 (function testExceptionsStopFirstException() { | |
132 var ErrorA = function ErrorA() {}; | |
133 var ErrorB = function ErrorB() {}; | |
134 var log = ""; | |
135 var source = { b: 1, a: 1 }; | |
136 var target = { | |
137 set a(v) { log += "a"; throw new ErrorA }, | |
138 set b(v) { log += "b"; throw new ErrorB }, | |
139 }; | |
140 assertThrows(function() { return Object.assign(target, source); }, ErrorB); | |
141 assertEquals(log, "b"); | |
142 })(); | |
OLD | NEW |