Commit 2c8ee5d6 2c8ee5d60d1c0ade6896333aad19e419401547ea by Nicolas Perriault

Merge pull request #370 from ThomasR/master

Fix for util.mergeObjects({}, foo);
2 parents 01395ab9 05931919
......@@ -545,14 +545,14 @@ exports.isWebPage = isWebPage;
function mergeObjects(origin, add) {
"use strict";
for (var p in add) {
try {
if (add[p].constructor === Object) {
if (add[p] && add[p].constructor === Object) {
if (origin[p] && origin[p].constructor === Object) {
origin[p] = mergeObjects(origin[p], add[p]);
} else {
origin[p] = add[p];
origin[p] = clone(add[p]);
}
} catch(e) {
origin[p] = add[p];
} else {
origin[p] = add[p];
}
}
return origin;
......
......@@ -264,7 +264,7 @@ casper.test.begin('isJsFile() tests', 5, function(test) {
test.done();
});
casper.test.begin('mergeObjects() tests', 5, function(test) {
casper.test.begin('mergeObjects() tests', 7, function(test) {
var testCases = [
{
obj1: {a: 1}, obj2: {b: 2}, merged: {a: 1, b: 2}
......@@ -273,6 +273,9 @@ casper.test.begin('mergeObjects() tests', 5, function(test) {
obj1: {}, obj2: {a: 1}, merged: {a: 1}
},
{
obj1: {}, obj2: {a: {b: 2}}, merged: {a: {b: 2}}
},
{
obj1: {a: 1}, obj2: {}, merged: {a: 1}
},
{
......@@ -295,6 +298,12 @@ casper.test.begin('mergeObjects() tests', 5, function(test) {
'mergeObjects() can merge objects'
);
});
var obj = {x: 1};
var merged1 = utils.mergeObjects({}, {a: obj});
var merged2 = utils.mergeObjects({a: {}}, {a: obj});
merged1.a.x = 2;
merged2.a.x = 2;
test.assertEquals(obj.x, 1, 'mergeObjects() creates deep clones');
test.done();
});
......