Ensure _.first, _.last, and _.sample return the undefined if called with a falsey argument or an empty array if called with a falsey argument and an n value.

This commit is contained in:
John-David Dalton
2013-09-09 08:21:24 -07:00
parent fb61dfd39d
commit 21a45a940e
2 changed files with 60 additions and 38 deletions

View File

@@ -4380,7 +4380,7 @@
];
_.forEach(funcs, function(methodName) {
test('`_.' + methodName + '` should return an unwrapped value', function() {
test('`_(...).' + methodName + '` should return an unwrapped value', function() {
var result = methodName == 'reduceRight'
? wrapped[methodName](_.identity)
: wrapped[methodName]();
@@ -4400,17 +4400,46 @@
var funcs = [
'first',
'last'
'last',
'sample'
];
_.forEach(funcs, function(methodName) {
test('`_.' + methodName + '` should return an unwrapped value', function() {
test('`_(...).' + methodName + '` called without an `n` argument should return an unwrapped value', function() {
equal(typeof wrapped[methodName](), 'number');
});
test('`_.' + methodName + '` should return a wrapped value', function() {
test('`_(...).' + methodName + '` called with an `n` argument should return a wrapped value', function() {
ok(wrapped[methodName](1) instanceof _);
});
test('`_.' + methodName + '` should return `undefined` when querying falsey arguments without an `n` argument', function() {
var actual = [],
expected = _.map(falsey, function() { return undefined; }),
func = _[methodName];
_.forEach(falsey, function(value, index) {
try {
actual.push(index ? func(value) : func());
} catch(e) { console.log(e)}
});
deepEqual(actual, expected);
});
test('`_.' + methodName + '` should return an empty array when querying falsey arguments with an `n` argument', function() {
var actual = [],
expected = _.map(falsey, function() { return []; }),
func = _[methodName];
_.forEach(falsey, function(value, index) {
try {
actual.push(func(value, 2));
} catch(e) { }
});
deepEqual(actual, expected);
});
});
}());