From 71a89a575f2238a5e619f0eb2fa95531d00a47bc Mon Sep 17 00:00:00 2001 From: jdalton Date: Fri, 3 Apr 2015 11:46:37 -0500 Subject: [PATCH] Add `_.get` and `_.set` tests. --- test/test.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/test/test.js b/test/test.js index 34cd8f993..242658787 100644 --- a/test/test.js +++ b/test/test.js @@ -5995,6 +5995,54 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.get'); + + (function() { + test('should get deep property values', 1, function() { + var object = { 'a': { 'b': { 'c': 3 } } }; + strictEqual(_.get(object, 'a.b.c'), 3); + }); + + test('should return `undefined` when `object` is nullish', 2, function() { + strictEqual(_.get(null, 'a'), undefined); + strictEqual(_.get(undefined, 'a'), undefined); + }); + + test('should return `undefined` if parts of `path` are missing', 1, function() { + var object = {}; + strictEqual(_.get(object, 'a[1].b.c'), undefined); + }); + + test('should follow `path` over non-plain objects', 2, function() { + var actual = _.get(0, 'constructor.prototype.toFixed'); + strictEqual(actual, 0..toFixed); + + var object = { 'a': '' }; + actual = _.get(object, 'a.replace'); + strictEqual(actual, stringProto.replace); + }); + + test('should return the specified default value for `undefined` values', 1, function() { + var object = { 'a': {} }, + values = empties.concat(true, new Date, 1, /x/, 'a'); + + var expected = _.transform(values, function(result, value) { + result.push(value, value); + }); + + var actual = _.transform(values, function(result, value) { + result.push( + _.get(object, 'a.b.c', value), + _.get(null, 'a.b.c', value) + ); + }); + + deepEqual(actual, expected); + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.groupBy'); (function() { @@ -12880,6 +12928,20 @@ ok(!(0 in object.a)); }); + test('should not error when `object` is nullish', 1, function() { + var values = [null, undefined]; + + var actual = _.map(values, function(value) { + try { + return _.set(value, 'a.b', 1); + } catch(e) { + return e; + } + }); + + deepEqual(actual, values); + }); + test('should follow `path` over non-plain objects', 2, function() { _.set(0, 'constructor.prototype.a', 'ok'); strictEqual(1..a, 'ok');