mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-13 12:27:49 +00:00
Cleanup tests and add a _.callback test for argCount.
This commit is contained in:
79
test/test.js
79
test/test.js
@@ -1927,23 +1927,27 @@
|
|||||||
QUnit.module('lodash.callback');
|
QUnit.module('lodash.callback');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
test('should create a callback with a falsey `thisArg`', 1, function() {
|
test('should provide `argCount` number of arguments to `func`', 1, function() {
|
||||||
var fn = function() { return this; },
|
var args;
|
||||||
object = {};
|
|
||||||
|
|
||||||
var expected = _.map(falsey, function(value) {
|
var callback = _.callback(function() {
|
||||||
var result = fn.call(value);
|
args = slice.call(arguments);
|
||||||
return (result && result.Array) ? object : result;
|
}, 1);
|
||||||
|
|
||||||
|
callback('a', 'b', 'c');
|
||||||
|
deepEqual(args, ['a']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should work without an `argCount`', 1, function() {
|
||||||
|
var args,
|
||||||
|
expected = ['a', 'b', 'c', 'd', 'e'];
|
||||||
|
|
||||||
|
var callback = _.callback(function() {
|
||||||
|
args = slice.call(arguments);
|
||||||
});
|
});
|
||||||
|
|
||||||
var actual = _.map(falsey, function(value) {
|
callback.apply(null, expected);
|
||||||
var callback = _.callback(fn, value),
|
deepEqual(args, expected);
|
||||||
result = callback();
|
|
||||||
|
|
||||||
return (result && result.Array) ? object : result;
|
|
||||||
});
|
|
||||||
|
|
||||||
ok(_.isEqual(actual, expected));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return `_.identity` when `func` is nullish', 2, function() {
|
test('should return `_.identity` when `func` is nullish', 2, function() {
|
||||||
@@ -1966,6 +1970,25 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should create a callback with a falsey `thisArg`', 1, function() {
|
||||||
|
var fn = function() { return this; },
|
||||||
|
object = {};
|
||||||
|
|
||||||
|
var expected = _.map(falsey, function(value) {
|
||||||
|
var result = fn.call(value);
|
||||||
|
return (result && result.Array) ? object : result;
|
||||||
|
});
|
||||||
|
|
||||||
|
var actual = _.map(falsey, function(value) {
|
||||||
|
var callback = _.callback(fn, value),
|
||||||
|
result = callback();
|
||||||
|
|
||||||
|
return (result && result.Array) ? object : result;
|
||||||
|
});
|
||||||
|
|
||||||
|
ok(_.isEqual(actual, expected));
|
||||||
|
});
|
||||||
|
|
||||||
test('should return a callback created by `_.matches` when `func` is an object', 2, function() {
|
test('should return a callback created by `_.matches` when `func` is an object', 2, function() {
|
||||||
var callback = _.callback({ 'a': 1 });
|
var callback = _.callback({ 'a': 1 });
|
||||||
strictEqual(callback({ 'a': 1, 'b': 2 }), true);
|
strictEqual(callback({ 'a': 1, 'b': 2 }), true);
|
||||||
@@ -1982,18 +2005,6 @@
|
|||||||
strictEqual(callback(array), 'a');
|
strictEqual(callback(array), 'a');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should work without an `argCount`', 1, function() {
|
|
||||||
var args,
|
|
||||||
expected = ['a', 'b', 'c', 'd', 'e'];
|
|
||||||
|
|
||||||
var callback = _.callback(function() {
|
|
||||||
args = slice.call(arguments);
|
|
||||||
});
|
|
||||||
|
|
||||||
callback.apply(null, expected);
|
|
||||||
deepEqual(args, expected);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should work with functions created by `_.partial` and `_.partialRight`', 2, function() {
|
test('should work with functions created by `_.partial` and `_.partialRight`', 2, function() {
|
||||||
function fn() {
|
function fn() {
|
||||||
var result = [this.a];
|
var result = [this.a];
|
||||||
@@ -2105,13 +2116,13 @@
|
|||||||
deepEqual(curried(1, 2, 3, 4), expected);
|
deepEqual(curried(1, 2, 3, 4), expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should allow explicitly setting arity', 3, function(){
|
test('should allow specifying `arity`', 3, function(){
|
||||||
var curried = _.curry(fn, 3),
|
var curried = _.curry(fn, 3),
|
||||||
expected = [1, 2, 3];
|
expected = [1, 2, 3];
|
||||||
|
|
||||||
deepEqual(curried(1, 2, 3), expected);
|
|
||||||
deepEqual(curried(1)(2)(3), expected);
|
|
||||||
deepEqual(curried(1)(2, 3), expected);
|
deepEqual(curried(1)(2, 3), expected);
|
||||||
|
deepEqual(curried(1, 2)(3), expected);
|
||||||
|
deepEqual(curried(1, 2, 3), expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should work with partialed methods', 2, function() {
|
test('should work with partialed methods', 2, function() {
|
||||||
@@ -2140,7 +2151,7 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should provide additional arguments after reaching arity target', 3, function() {
|
test('should provide additional arguments after reaching the target arity', 3, function() {
|
||||||
var curried = _.curry(fn, 3);
|
var curried = _.curry(fn, 3);
|
||||||
deepEqual(curried(1)(2, 3, 4), [1, 2, 3, 4]);
|
deepEqual(curried(1)(2, 3, 4), [1, 2, 3, 4]);
|
||||||
deepEqual(curried(1, 2)(3, 4, 5), [1, 2, 3, 4, 5]);
|
deepEqual(curried(1, 2)(3, 4, 5), [1, 2, 3, 4, 5]);
|
||||||
@@ -2210,13 +2221,13 @@
|
|||||||
deepEqual(curried(1, 2, 3, 4), expected);
|
deepEqual(curried(1, 2, 3, 4), expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should allow explicitly setting arity', 3, function(){
|
test('should allow specifying `arity`', 3, function(){
|
||||||
var curried = _.curryRight(fn, 3),
|
var curried = _.curryRight(fn, 3),
|
||||||
expected = [1, 2, 3];
|
expected = [1, 2, 3];
|
||||||
|
|
||||||
deepEqual(curried(1, 2, 3), expected);
|
|
||||||
deepEqual(curried(3)(2)(1), expected);
|
|
||||||
deepEqual(curried(3)(1, 2), expected);
|
deepEqual(curried(3)(1, 2), expected);
|
||||||
|
deepEqual(curried(2, 3)(1), expected);
|
||||||
|
deepEqual(curried(1, 2, 3), expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should work with partialed methods', 2, function() {
|
test('should work with partialed methods', 2, function() {
|
||||||
@@ -2245,7 +2256,7 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should provide additional arguments after reaching arity target', 3, function() {
|
test('should provide additional arguments after reaching the target arity', 3, function() {
|
||||||
var curried = _.curryRight(fn, 3);
|
var curried = _.curryRight(fn, 3);
|
||||||
deepEqual(curried(4)(1, 2, 3), [1, 2, 3, 4]);
|
deepEqual(curried(4)(1, 2, 3), [1, 2, 3, 4]);
|
||||||
deepEqual(curried(4, 5)(1, 2, 3), [1, 2, 3, 4, 5]);
|
deepEqual(curried(4, 5)(1, 2, 3), [1, 2, 3, 4, 5]);
|
||||||
|
|||||||
Reference in New Issue
Block a user