Add more _.callback tests.

This commit is contained in:
John-David Dalton
2014-04-08 01:21:21 -07:00
parent 79ec1f5a02
commit 52cca034cf

View File

@@ -1649,28 +1649,46 @@
QUnit.module('lodash.callback'); QUnit.module('lodash.callback');
(function() { (function() {
test('should work with functions created by `_.partial` and `_.partialRight`', 2, function() { test('should create a callback with a falsey `thisArg`', 1, function() {
var fn = function() { var values = _.reject(falsey, function(value) {
var result = [this.a]; return value == null;
push.apply(result, arguments); });
return result;
};
var expected = [1, 2, 3], var actual = _.map(values, function(value) {
object = { 'a': 1 }, var callback = _.callback(function() { return this; }, value);
callback = _.createCallback(_.partial(fn, 2), object); return callback();
});
deepEqual(callback(3), expected); ok(_.isEqual(actual, values));
});
callback = _.createCallback(_.partialRight(fn, 3), object); test('should return `identity` when `func` is `null` or `undefined`', 2, function() {
deepEqual(callback(2), expected); _.each([null, undefined], function(value) {
strictEqual(_.callback(value), _.identity);
});
});
test('should return a callback created by `_.matches` when `func` is an object', 2, function() {
var callback = _.callback({ 'a': 1 });
strictEqual(callback({ 'a': 1, 'b': 2 }), true);
strictEqual(callback({}), false);
});
test('should return a callback created by `_.property` when `func` is a number or string', 2, function() {
var array = ['a'],
callback = _.callback(0);
strictEqual(callback(array), 'a');
callback = _.callback('0');
strictEqual(callback(array), 'a');
}); });
test('should work without an `argCount`', 1, function() { test('should work without an `argCount`', 1, function() {
var args, var args,
expected = ['a', 'b', 'c', 'd', 'e']; expected = ['a', 'b', 'c', 'd', 'e'];
var callback = _.createCallback(function() { var callback = _.callback(function() {
args = slice.call(arguments); args = slice.call(arguments);
}); });
@@ -1678,6 +1696,22 @@
deepEqual(args, expected); deepEqual(args, expected);
}); });
test('should work with functions created by `_.partial` and `_.partialRight`', 2, function() {
function fn() {
var result = [this.a];
push.apply(result, arguments);
return result;
}
var expected = [1, 2, 3],
object = { 'a': 1 },
callback = _.callback(_.partial(fn, 2), object);
deepEqual(callback(3), expected);
callback = _.callback(_.partialRight(fn, 3), object);
deepEqual(callback(2), expected);
});
test('should return the function provided if already bound with `Function#bind`', 1, function() { test('should return the function provided if already bound with `Function#bind`', 1, function() {
function a() {} function a() {}
@@ -1686,7 +1720,7 @@
if (bound && !('prototype' in bound)) { if (bound && !('prototype' in bound)) {
var bound = a.bind(object); var bound = a.bind(object);
strictEqual(_.createCallback(bound, object), bound); strictEqual(_.callback(bound, object), bound);
} }
else { else {
skipTest(); skipTest();
@@ -1700,8 +1734,8 @@
var object = {}; var object = {};
if (_.support.funcDecomp) { if (_.support.funcDecomp) {
strictEqual(_.createCallback(a, object), a); strictEqual(_.callback(a, object), a);
notStrictEqual(_.createCallback(b, object), b); notStrictEqual(_.callback(b, object), b);
} }
else { else {
skipTest(2); skipTest(2);
@@ -1710,21 +1744,21 @@
test('should only write metadata to named functions', 3, function() { test('should only write metadata to named functions', 3, function() {
function a() {}; function a() {};
var b = function() {};
function c() {}; function c() {};
var b = function() {}, var object = {};
object = {};
if (defineProperty && _.support.funcDecomp) { if (defineProperty && _.support.funcDecomp) {
_.createCallback(a, object); _.callback(a, object);
ok(expando in a); ok(expando in a);
_.createCallback(b, object); _.callback(b, object);
ok(!(expando in b)); ok(!(expando in b));
if (_.support.funcNames) { if (_.support.funcNames) {
_.support.funcNames = false; _.support.funcNames = false;
_.createCallback(c, object); _.callback(c, object);
ok(expando in c); ok(expando in c);
_.support.funcNames = true; _.support.funcNames = true;
@@ -1742,7 +1776,7 @@
function a() {}; function a() {};
if (defineProperty && lodashBizarro) { if (defineProperty && lodashBizarro) {
lodashBizarro.createCallback(a, {}); lodashBizarro.callback(a, {});
ok(!(expando in a)); ok(!(expando in a));
} }
else { else {