1 | 'use strict';
|
---|
2 |
|
---|
3 | var hasSymbols = require('has-symbols/shams')();
|
---|
4 | var forEach = require('for-each');
|
---|
5 | var hasOwn = require('hasown');
|
---|
6 | var mockProperty = require('mock-property');
|
---|
7 |
|
---|
8 | module.exports = function (assign, t) {
|
---|
9 | t.test('error cases', function (st) {
|
---|
10 | st['throws'](function () { assign(null); }, TypeError, 'target must be an object');
|
---|
11 | st['throws'](function () { assign(undefined); }, TypeError, 'target must be an object');
|
---|
12 | st['throws'](function () { assign(null, {}); }, TypeError, 'target must be an object');
|
---|
13 | st['throws'](function () { assign(undefined, {}); }, TypeError, 'target must be an object');
|
---|
14 | st.end();
|
---|
15 | });
|
---|
16 |
|
---|
17 | t.test('non-object target, no sources', function (st) {
|
---|
18 | var bool = assign(true);
|
---|
19 | st.equal(typeof bool, 'object', 'bool is object');
|
---|
20 | st.equal(Boolean.prototype.valueOf.call(bool), true, 'bool coerces to `true`');
|
---|
21 |
|
---|
22 | var number = assign(1);
|
---|
23 | st.equal(typeof number, 'object', 'number is object');
|
---|
24 | st.equal(Number.prototype.valueOf.call(number), 1, 'number coerces to `1`');
|
---|
25 |
|
---|
26 | var string = assign('1');
|
---|
27 | st.equal(typeof string, 'object', 'number is object');
|
---|
28 | st.equal(String.prototype.valueOf.call(string), '1', 'number coerces to `"1"`');
|
---|
29 |
|
---|
30 | st.end();
|
---|
31 | });
|
---|
32 |
|
---|
33 | t.test('non-object target, with sources', function (st) {
|
---|
34 | var signal = {};
|
---|
35 |
|
---|
36 | st.test('boolean', function (st2) {
|
---|
37 | var bool = assign(true, { a: signal });
|
---|
38 | st2.equal(typeof bool, 'object', 'bool is object');
|
---|
39 | st2.equal(Boolean.prototype.valueOf.call(bool), true, 'bool coerces to `true`');
|
---|
40 | st2.equal(bool.a, signal, 'source properties copied');
|
---|
41 | st2.end();
|
---|
42 | });
|
---|
43 |
|
---|
44 | st.test('number', function (st2) {
|
---|
45 | var number = assign(1, { a: signal });
|
---|
46 | st2.equal(typeof number, 'object', 'number is object');
|
---|
47 | st2.equal(Number.prototype.valueOf.call(number), 1, 'number coerces to `1`');
|
---|
48 | st2.equal(number.a, signal, 'source properties copied');
|
---|
49 | st2.end();
|
---|
50 | });
|
---|
51 |
|
---|
52 | st.test('string', function (st2) {
|
---|
53 | var string = assign('1', { a: signal });
|
---|
54 | st2.equal(typeof string, 'object', 'number is object');
|
---|
55 | st2.equal(String.prototype.valueOf.call(string), '1', 'number coerces to `"1"`');
|
---|
56 | st2.equal(string.a, signal, 'source properties copied');
|
---|
57 | st2.end();
|
---|
58 | });
|
---|
59 |
|
---|
60 | st.end();
|
---|
61 | });
|
---|
62 |
|
---|
63 | t.test('non-object sources', function (st) {
|
---|
64 | st.deepEqual(assign({ a: 1 }, null, { b: 2 }), { a: 1, b: 2 }, 'ignores null source');
|
---|
65 | st.deepEqual(assign({ a: 1 }, { b: 2 }, undefined), { a: 1, b: 2 }, 'ignores undefined source');
|
---|
66 | st.end();
|
---|
67 | });
|
---|
68 |
|
---|
69 | t.test('returns the modified target object', function (st) {
|
---|
70 | var target = {};
|
---|
71 | var returned = assign(target, { a: 1 });
|
---|
72 | st.equal(returned, target, 'returned object is the same reference as the target object');
|
---|
73 | st.end();
|
---|
74 | });
|
---|
75 |
|
---|
76 | t.test('has the right length', function (st) {
|
---|
77 | st.equal(assign.length, 2, 'length is 2 => 2 required arguments');
|
---|
78 | st.end();
|
---|
79 | });
|
---|
80 |
|
---|
81 | t.test('merge two objects', function (st) {
|
---|
82 | var target = { a: 1 };
|
---|
83 | var returned = assign(target, { b: 2 });
|
---|
84 | st.deepEqual(returned, { a: 1, b: 2 }, 'returned object has properties from both');
|
---|
85 | st.end();
|
---|
86 | });
|
---|
87 |
|
---|
88 | t.test('works with functions', function (st) {
|
---|
89 | var target = function () {};
|
---|
90 | target.a = 1;
|
---|
91 | var returned = assign(target, { b: 2 });
|
---|
92 | st.equal(target, returned, 'returned object is target');
|
---|
93 | st.equal(returned.a, 1);
|
---|
94 | st.equal(returned.b, 2);
|
---|
95 | st.end();
|
---|
96 | });
|
---|
97 |
|
---|
98 | t.test('works with primitives', function (st) {
|
---|
99 | var target = 2;
|
---|
100 | var source = { b: 42 };
|
---|
101 | var returned = assign(target, source);
|
---|
102 | st.equal(Object.prototype.toString.call(returned), '[object Number]', 'returned is object form of number primitive');
|
---|
103 | st.equal(Number(returned), target, 'returned and target have same valueOf');
|
---|
104 | st.equal(returned.b, source.b);
|
---|
105 | st.end();
|
---|
106 | });
|
---|
107 |
|
---|
108 | /* globals window */
|
---|
109 | t.test('works with window.location', { skip: typeof window === 'undefined' }, function (st) {
|
---|
110 | var target = {};
|
---|
111 | assign(target, window.location);
|
---|
112 | for (var prop in window.location) {
|
---|
113 | if (hasOwn(window.location, prop)) {
|
---|
114 | st.deepEqual(target[prop], window.location[prop], prop + ' is copied');
|
---|
115 | }
|
---|
116 | }
|
---|
117 | st.end();
|
---|
118 | });
|
---|
119 |
|
---|
120 | t.test('merge N objects', function (st) {
|
---|
121 | var target = { a: 1 };
|
---|
122 | var source1 = { b: 2 };
|
---|
123 | var source2 = { c: 3 };
|
---|
124 | var returned = assign(target, source1, source2);
|
---|
125 | st.deepEqual(returned, { a: 1, b: 2, c: 3 }, 'returned object has properties from all sources');
|
---|
126 | st.end();
|
---|
127 | });
|
---|
128 |
|
---|
129 | t.test('only iterates over own keys', function (st) {
|
---|
130 | var Foo = function () {};
|
---|
131 | Foo.prototype.bar = true;
|
---|
132 | var foo = new Foo();
|
---|
133 | foo.baz = true;
|
---|
134 | var target = { a: 1 };
|
---|
135 | var returned = assign(target, foo);
|
---|
136 | st.equal(returned, target, 'returned object is the same reference as the target object');
|
---|
137 | st.deepEqual(target, { a: 1, baz: true }, 'returned object has only own properties from both');
|
---|
138 | st.end();
|
---|
139 | });
|
---|
140 |
|
---|
141 | t.test('includes enumerable symbols, after keys', { skip: !hasSymbols }, function (st) {
|
---|
142 | var visited = [];
|
---|
143 | var obj = {};
|
---|
144 | Object.defineProperty(obj, 'a', { enumerable: true, get: function () { visited.push('a'); return 42; } });
|
---|
145 | var symbol = Symbol('enumerable');
|
---|
146 | Object.defineProperty(obj, symbol, {
|
---|
147 | enumerable: true,
|
---|
148 | get: function () { visited.push(symbol); return Infinity; }
|
---|
149 | });
|
---|
150 | var nonEnumSymbol = Symbol('non-enumerable');
|
---|
151 | Object.defineProperty(obj, nonEnumSymbol, {
|
---|
152 | enumerable: false,
|
---|
153 | get: function () { visited.push(nonEnumSymbol); return -Infinity; }
|
---|
154 | });
|
---|
155 | var target = assign({}, obj);
|
---|
156 | st.deepEqual(visited, ['a', symbol], 'key is visited first, then symbol');
|
---|
157 | st.equal(target.a, 42, 'target.a is 42');
|
---|
158 | st.equal(target[symbol], Infinity, 'target[symbol] is Infinity');
|
---|
159 | st.notEqual(target[nonEnumSymbol], -Infinity, 'target[nonEnumSymbol] is not -Infinity');
|
---|
160 | st.end();
|
---|
161 | });
|
---|
162 |
|
---|
163 | t.test('does not fail when symbols are not present', { skip: !Object.isFrozen || Object.isFrozen(Object) }, function (st) {
|
---|
164 | st.teardown(mockProperty(Object, 'getOwnPropertySymbols', { 'delete': true }));
|
---|
165 |
|
---|
166 | var visited = [];
|
---|
167 | var obj = {};
|
---|
168 | Object.defineProperty(obj, 'a', { enumerable: true, get: function () { visited.push('a'); return 42; } });
|
---|
169 | var keys = ['a'];
|
---|
170 | if (hasSymbols) {
|
---|
171 | var symbol = Symbol('sym');
|
---|
172 | Object.defineProperty(obj, symbol, {
|
---|
173 | enumerable: true,
|
---|
174 | get: function () { visited.push(symbol); return Infinity; }
|
---|
175 | });
|
---|
176 | keys.push(symbol);
|
---|
177 | }
|
---|
178 | var target = assign({}, obj);
|
---|
179 | st.deepEqual(visited, keys, 'assign visits expected keys');
|
---|
180 | st.equal(target.a, 42, 'target.a is 42');
|
---|
181 |
|
---|
182 | if (hasSymbols) {
|
---|
183 | st.equal(target[symbol], Infinity);
|
---|
184 | }
|
---|
185 | st.end();
|
---|
186 | });
|
---|
187 |
|
---|
188 | t.test('preserves correct property enumeration order', function (st) {
|
---|
189 | var str = 'abcdefghijklmnopqrst';
|
---|
190 | var letters = {};
|
---|
191 | forEach(str.split(''), function (letter) {
|
---|
192 | letters[letter] = letter;
|
---|
193 | });
|
---|
194 |
|
---|
195 | var n = 5;
|
---|
196 | st.comment('run the next test ' + n + ' times');
|
---|
197 | var object = assign({}, letters);
|
---|
198 | var actual = '';
|
---|
199 | for (var k in object) {
|
---|
200 | actual += k;
|
---|
201 | }
|
---|
202 | for (var i = 0; i < n; ++i) {
|
---|
203 | st.equal(actual, str, 'property enumeration order should be followed');
|
---|
204 | }
|
---|
205 | st.end();
|
---|
206 | });
|
---|
207 |
|
---|
208 | t.test('checks enumerability and existence, in case of modification during [[Get]]', { skip: !Object.defineProperty }, function (st) {
|
---|
209 | var targetBvalue = {};
|
---|
210 | var targetCvalue = {};
|
---|
211 | var target = { b: targetBvalue, c: targetCvalue };
|
---|
212 | var source = {};
|
---|
213 | Object.defineProperty(source, 'a', {
|
---|
214 | enumerable: true,
|
---|
215 | get: function () {
|
---|
216 | delete this.b;
|
---|
217 | Object.defineProperty(this, 'c', { enumerable: false });
|
---|
218 | return 'a';
|
---|
219 | }
|
---|
220 | });
|
---|
221 | var sourceBvalue = {};
|
---|
222 | var sourceCvalue = {};
|
---|
223 | source.b = sourceBvalue;
|
---|
224 | source.c = sourceCvalue;
|
---|
225 | var result = assign(target, source);
|
---|
226 | st.equal(result, target, 'sanity check: result is === target');
|
---|
227 | st.equal(result.b, targetBvalue, 'target key not overwritten by deleted source key');
|
---|
228 | st.equal(result.c, targetCvalue, 'target key not overwritten by non-enumerable source key');
|
---|
229 |
|
---|
230 | st.end();
|
---|
231 | });
|
---|
232 | };
|
---|