From 48a6c4f2bfa4127d6fec9083d47841b25f55d836 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 26 Jul 2015 10:32:32 -0700 Subject: [PATCH] Add `_.setWith` tests. --- lodash.js | 4 ++-- test/test.js | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lodash.js b/lodash.js index 22bb1b66d..7f0860fac 100644 --- a/lodash.js +++ b/lodash.js @@ -9453,12 +9453,12 @@ * @returns {Object} Returns `object`. * @example * - * _.setWith({ '0': {} }, '[0][1][2]', 3, function(value) { + * _.setWith({ '0': { 'length': 2 } }, '[0][1][2]', 3, function(value) { * if (!_.isObject(value)) { * return {}; * } * }); - * // => { '0': { '1': { '2': 3 } } } + * // => { '0': { '1': { '2': 3 }, 'length': 2 } } */ function setWith(object, path, value, customizer) { customizer = typeof customizer == 'function' ? customizer : undefined; diff --git a/test/test.js b/test/test.js index a4bdd476b..34f2d6f38 100644 --- a/test/test.js +++ b/test/test.js @@ -1016,7 +1016,7 @@ test('`_.' + methodName + '` should work with a `customizer` that returns `undefined`', 1, function() { 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'); _.each(['set', 'setWith'], function(methodName) {