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 // Based on Mozilla Array.of() tests at http://dxr.mozilla.org/mozilla-central/s ource/js/src/tests/ecma_6/Object/assign.js | |
6 | |
7 function checkDataProperty(object, propertyKey, value, writable, enumerable, con figurable) { | |
8 var desc = Object.getOwnPropertyDescriptor(object, propertyKey); | |
9 assertFalse(desc === undefined); | |
10 assertTrue('value' in desc); | |
11 assertEquals(desc.value, value); | |
12 assertEquals(desc.writable, writable); | |
13 assertEquals(desc.enumerable, enumerable); | |
14 assertEquals(desc.configurable, configurable); | |
15 } | |
16 | |
17 // 19.1.2.1 Object.assign ( target, ...sources ) | |
18 assertEquals(Object.assign.length, 2); | |
19 | |
20 // Basic functionality works with multiple sources | |
21 (function basicMultipleSources() { | |
22 var a = {}; | |
23 var b = { bProp: 1 }; | |
24 var c = { cProp: 2 }; | |
25 Object.assign(a, b, c); | |
26 assertEquals(a, { | |
27 bProp: 1, | |
28 cProp: 2 | |
29 }); | |
30 })(); | |
31 | |
32 // Basic functionality works with symbols | |
33 // TODO: `b.aSymbol is not copied into a | |
jsbell
2014/09/08 17:22:42
Extraneous (or missing) `
caitp (gmail)
2014/09/08 17:48:27
Acknowledged.
| |
34 /*(function basicSymbols() { | |
35 var a = {}; | |
36 var b = { bProp: 1 }; | |
37 var aSymbol = Symbol("aSymbol"); | |
38 b[aSymbol] = 2; | |
39 Object.assign(a, b); | |
40 assertEquals(1, a.bProp); | |
41 assertEquals(2, a[aSymbol]); | |
42 })();*/ | |
43 | |
44 // Dies if target is null or undefined | |
45 assertThrows(function() { return Object.assign(null, null); }, TypeError); | |
46 assertThrows(function() { return Object.assign(null, {}); }, TypeError); | |
47 assertThrows(function() { return Object.assign(undefined); }, TypeError); | |
48 assertThrows(function() { return Object.assign(); }, TypeError); | |
49 | |
50 // Calls ToObject for target | |
51 assertTrue(Object.assign(true, {}) instanceof Boolean); | |
52 assertTrue(Object.assign(1, {}) instanceof Number); | |
53 assertTrue(Object.assign("string", {}) instanceof String); | |
54 var o = {}; | |
55 assertSame(Object.assign(o, {}), o); | |
56 | |
57 // Only [[Enumerable]] properties are assigned to target | |
58 (function onlyEnumerablePropertiesAssigned() { | |
59 var source = Object.defineProperties({}, { | |
60 a: {value: 1, enumerable: true}, | |
61 b: {value: 2, enumerable: false}, | |
62 }); | |
63 var target = Object.assign({}, source); | |
64 assertTrue("a" in target); | |
65 assertFalse("b" in target); | |
66 })(); | |
67 | |
68 // Properties are retrieved through Get() | |
69 // Properties are assigned through Put() | |
70 (function testPropertiesAssignedThroughPut() { | |
71 var setterCalled = false; | |
72 Object.assign({set a(v) { setterCalled = v }}, {a: true}); | |
73 assertTrue(setterCalled); | |
74 })(); | |
75 | |
76 // Properties are retrieved through Get() | |
77 // Properties are assigned through Put(): Existing property attributes are not a ltered | |
78 (function propertiesAssignedExistingNotAltered() { | |
79 var source = {a: 1, b: 2, c: 3}; | |
80 var target = {a: 0, b: 0, c: 0}; | |
81 Object.defineProperty(target, "a", {enumerable: false}); | |
82 Object.defineProperty(target, "b", {configurable: false}); | |
83 Object.defineProperty(target, "c", {enumerable: false, configurable: false}); | |
84 Object.assign(target, source); | |
85 checkDataProperty(target, "a", 1, true, false, true); | |
86 checkDataProperty(target, "b", 2, true, true, false); | |
87 checkDataProperty(target, "c", 3, true, false, false); | |
88 })(); | |
89 | |
90 // Properties are retrieved through Get() | |
91 // Properties are assigned through Put(): Throws TypeError if non-writable | |
92 (function propertiesAssignedTypeErrorNonWritable() { | |
93 var source = {a: 1}; | |
94 var target = {a: 0}; | |
95 Object.defineProperty(target, "a", {writable: false}); | |
96 assertThrows(function() { return Object.assign(target, source); }, TypeError); | |
97 checkDataProperty(target, "a", 0, false, true, true); | |
98 })(); | |
99 | |
100 // Properties are retrieved through Get() | |
101 // Put() creates standard properties; Property attributes from source are | |
102 // ignored | |
103 (function createsStandardProperties() { | |
104 var source = {a: 1, b: 2, c: 3, get d() { return 4 }}; | |
105 Object.defineProperty(source, "b", {writable: false}); | |
106 Object.defineProperty(source, "c", {configurable: false}); | |
107 var target = Object.assign({}, source); | |
108 checkDataProperty(target, "a", 1, true, true, true); | |
109 checkDataProperty(target, "b", 2, true, true, true); | |
110 checkDataProperty(target, "c", 3, true, true, true); | |
111 checkDataProperty(target, "d", 4, true, true, true); | |
112 })(); | |
113 | |
114 // Properties created during traversal are not copied | |
115 (function propertiesCreatedDuringTraversalNotCopied() { | |
116 var source = {get a() { this.b = 2 }}; | |
117 var target = Object.assign({}, source); | |
118 assertTrue("a" in target); | |
119 assertFalse("b" in target); | |
120 })(); | |
121 | |
122 // String and Symbol valued properties are copied | |
jsbell
2014/09/08 17:22:42
Add another TODO here for Symbol property?
caitp (gmail)
2014/09/08 17:48:28
All of the tests here were basically copy/pasted f
| |
123 (function testStringAndSymbolPropertiesCopied() { | |
124 var keyA = "str-prop"; | |
125 var source = {"str-prop": 1}; | |
126 var target = Object.assign({}, source); | |
127 checkDataProperty(target, keyA, 1, true, true, true); | |
128 })(); | |
129 | |
130 // Intermediate exceptions do not stop property traversal, first exception is | |
jsbell
2014/09/08 17:22:42
Add a property that *is* copied successfully, even
caitp (gmail)
2014/09/08 17:48:28
asserting that log equals "ba" is asserting that b
| |
131 // reported (3) | |
jsbell
2014/09/08 17:22:42
What's the '(3)' here?
caitp (gmail)
2014/09/08 17:48:27
These tests are pulled out of the spidermonkey Obj
| |
132 (function testExceptionsDoNotStopFirstReported3() { | |
133 var ErrorA = function ErrorA() {}; | |
134 var ErrorB = function ErrorB() {}; | |
135 var log = ""; | |
136 var source = { b: 1, a: 1 }; | |
137 var target = { | |
138 set a(v) { log += "a"; throw new ErrorA }, | |
139 set b(v) { log += "b"; throw new ErrorB }, | |
140 }; | |
141 assertThrows(function() { return Object.assign(target, source); }, ErrorB); | |
142 assertEquals(log, "ba"); | |
143 })(); | |
144 | |
145 // TODO: Many of these tests depend on a more adequate Proxy implementation, | |
146 // so that [[OwnPropertyKeys]] and other internal methods can be mocked. The | |
147 // old-style Proxy implementation in v8 does not satisfy these needs. | |
OLD | NEW |