Ensure merge works with sources containing circular references.

Former-commit-id: cb3301868299c4e4d32a3d3608199e4269d951ec
This commit is contained in:
John-David Dalton
2012-08-04 17:12:25 -07:00
parent 4244b92b08
commit b7374e3f8e
2 changed files with 111 additions and 27 deletions

View File

@@ -231,11 +231,11 @@
'bar': { }
};
object.foo.b.foo.c.foo = object;
object.foo.b.foo.c = object;
object.bar.b = object.foo.b;
var clone = _.clone(object, true);
ok(clone.bar.b === clone.foo.b && clone === clone.foo.b.foo.c.foo && clone !== object);
ok(clone.bar.b === clone.foo.b && clone === clone.foo.b.foo.c && clone !== object);
});
test('should clone using Klass#clone', function() {
@@ -816,6 +816,72 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.merge');
(function() {
test('should merge `source` into the destination object', function() {
var stooges = [
{ 'name': 'moe' },
{ 'name': 'larry' }
];
var ages = [
{ 'age': 40 },
{ 'age': 50 }
];
var heights = [
{ 'height': '5\'4"' },
{ 'height': '5\'5"' },
];
var expected = [
{ 'name': 'moe', 'age': 40, 'height': '5\'4"' },
{ 'name': 'larry', 'age': 50, 'height': '5\'5"' }
];
deepEqual(_.merge(stooges, ages, heights), expected);
});
test('should merge sources containing circular references', function() {
var object = {
'foo': { 'a': 1 },
'bar': { 'a': 2 }
};
var source = {
'foo': { 'b': { 'foo': { 'c': { } } } },
'bar': { }
};
source.foo.b.foo.c = source;
source.bar.b = source.foo.b;
var actual = _.merge(object, source);
ok(actual.bar.b === actual.foo.b && actual.foo.b.foo.c === actual.foo.b.foo.c.foo.b.foo.c);
});
test('should merge problem JScript properties (test in IE < 9)', function() {
var object = [{
'constructor': 1,
'hasOwnProperty': 2,
'isPrototypeOf': 3
}];
var source = [{
'propertyIsEnumerable': 4,
'toLocaleString': 5,
'toString': 6,
'valueOf': 7
}];
deepEqual(_.merge(object, source), [shadowed]);
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.partial');
(function() {