Add _.setWith tests.

This commit is contained in:
John-David Dalton
2015-07-26 10:32:32 -07:00
parent f1597386ef
commit 48a6c4f2bf
2 changed files with 24 additions and 3 deletions

View File

@@ -9453,12 +9453,12 @@
* @returns {Object} Returns `object`. * @returns {Object} Returns `object`.
* @example * @example
* *
* _.setWith({ '0': {} }, '[0][1][2]', 3, function(value) { * _.setWith({ '0': { 'length': 2 } }, '[0][1][2]', 3, function(value) {
* if (!_.isObject(value)) { * if (!_.isObject(value)) {
* return {}; * return {};
* } * }
* }); * });
* // => { '0': { '1': { '2': 3 } } } * // => { '0': { '1': { '2': 3 }, 'length': 2 } }
*/ */
function setWith(object, path, value, customizer) { function setWith(object, path, value, customizer) {
customizer = typeof customizer == 'function' ? customizer : undefined; customizer = typeof customizer == 'function' ? customizer : undefined;

View File

@@ -1016,7 +1016,7 @@
test('`_.' + methodName + '` should work with a `customizer` that returns `undefined`', 1, function() { test('`_.' + methodName + '` should work with a `customizer` that returns `undefined`', 1, function() {
var expected = { 'a': undefined }; var expected = { 'a': undefined };
deepEqual(func({}, expected, _.identity), expected); deepEqual(func({}, expected, _.constant(undefined)), expected);
}); });
}); });
@@ -13404,6 +13404,27 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
QUnit.module('lodash.setWith');
(function() {
test('should work with a `customizer` callback', 1, function() {
var actual = _.setWith({ '0': { 'length': 2 } }, '[0][1][2]', 3, function(value) {
if (!_.isObject(value)) {
return {};
}
});
deepEqual(actual, { '0': { '1': { '2': 3 }, 'length': 2 } });
});
test('should work with a `customizer` that returns `undefined`', 1, function() {
var actual = _.setWith({}, 'a[0].b.c', 4, _.constant(undefined));
deepEqual(actual, { 'a': [{ 'b': { 'c': 4 } }] });
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('set methods'); QUnit.module('set methods');
_.each(['set', 'setWith'], function(methodName) { _.each(['set', 'setWith'], function(methodName) {