Added tests for mergeObjects;
Fixed exception when add[p] or origin[p] was falsy
Showing
2 changed files
with
12 additions
and
3 deletions
... | @@ -533,8 +533,8 @@ exports.isWebPage = isWebPage; | ... | @@ -533,8 +533,8 @@ exports.isWebPage = isWebPage; |
533 | function mergeObjects(origin, add) { | 533 | function mergeObjects(origin, add) { |
534 | "use strict"; | 534 | "use strict"; |
535 | for (var p in add) { | 535 | for (var p in add) { |
536 | if (add[p].constructor === Object) { | 536 | if (add[p] && add[p].constructor === Object) { |
537 | if (origin.hasOwnProperty(p) && origin[p].constructor === Object) { | 537 | if (origin[p] && origin[p].constructor === Object) { |
538 | origin[p] = mergeObjects(origin[p], add[p]); | 538 | origin[p] = mergeObjects(origin[p], add[p]); |
539 | } else { | 539 | } else { |
540 | origin[p] = clone(add[p]); | 540 | origin[p] = clone(add[p]); | ... | ... |
... | @@ -264,7 +264,7 @@ casper.test.begin('isJsFile() tests', 5, function(test) { | ... | @@ -264,7 +264,7 @@ casper.test.begin('isJsFile() tests', 5, function(test) { |
264 | test.done(); | 264 | test.done(); |
265 | }); | 265 | }); |
266 | 266 | ||
267 | casper.test.begin('mergeObjects() tests', 5, function(test) { | 267 | casper.test.begin('mergeObjects() tests', 7, function(test) { |
268 | var testCases = [ | 268 | var testCases = [ |
269 | { | 269 | { |
270 | obj1: {a: 1}, obj2: {b: 2}, merged: {a: 1, b: 2} | 270 | obj1: {a: 1}, obj2: {b: 2}, merged: {a: 1, b: 2} |
... | @@ -273,6 +273,9 @@ casper.test.begin('mergeObjects() tests', 5, function(test) { | ... | @@ -273,6 +273,9 @@ casper.test.begin('mergeObjects() tests', 5, function(test) { |
273 | obj1: {}, obj2: {a: 1}, merged: {a: 1} | 273 | obj1: {}, obj2: {a: 1}, merged: {a: 1} |
274 | }, | 274 | }, |
275 | { | 275 | { |
276 | obj1: {}, obj2: {a: {b: 2}}, merged: {a: {b: 2}} | ||
277 | }, | ||
278 | { | ||
276 | obj1: {a: 1}, obj2: {}, merged: {a: 1} | 279 | obj1: {a: 1}, obj2: {}, merged: {a: 1} |
277 | }, | 280 | }, |
278 | { | 281 | { |
... | @@ -295,6 +298,12 @@ casper.test.begin('mergeObjects() tests', 5, function(test) { | ... | @@ -295,6 +298,12 @@ casper.test.begin('mergeObjects() tests', 5, function(test) { |
295 | 'mergeObjects() can merge objects' | 298 | 'mergeObjects() can merge objects' |
296 | ); | 299 | ); |
297 | }); | 300 | }); |
301 | var obj = {x: 1}; | ||
302 | var merged1 = utils.mergeObjects({}, {a: obj}); | ||
303 | var merged2 = utils.mergeObjects({a: {}}, {a: obj}); | ||
304 | merged1.a.x = 2; | ||
305 | merged2.a.x = 2; | ||
306 | test.assertEquals(obj.x, 1, 'mergeObjects() creates deep clones'); | ||
298 | test.done(); | 307 | test.done(); |
299 | }); | 308 | }); |
300 | 309 | ... | ... |
-
Please register or sign in to post a comment