mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Add lazy chaining tests for _.find and _.findLast.
This commit is contained in:
133
test/test.js
133
test/test.js
@@ -2199,7 +2199,7 @@
|
|||||||
: fn(take3, _.compact, filter3, map3);
|
: fn(take3, _.compact, filter3, map3);
|
||||||
|
|
||||||
filterCount = mapCount = 0;
|
filterCount = mapCount = 0;
|
||||||
deepEqual(combined(_.range(200)), [4, 16]);
|
deepEqual(combined(_.range(LARGE_ARRAY_SIZE)), [4, 16]);
|
||||||
|
|
||||||
if (!isNpm && WeakMap && WeakMap.name) {
|
if (!isNpm && WeakMap && WeakMap.name) {
|
||||||
strictEqual(filterCount, 5, 'filterCount');
|
strictEqual(filterCount, 5, 'filterCount');
|
||||||
@@ -3927,7 +3927,8 @@
|
|||||||
_.each(['find', 'findLast', 'findIndex', 'findLastIndex', 'findKey', 'findLastKey'], function(methodName) {
|
_.each(['find', 'findLast', 'findIndex', 'findLastIndex', 'findKey', 'findLastKey'], function(methodName) {
|
||||||
QUnit.module('lodash.' + methodName);
|
QUnit.module('lodash.' + methodName);
|
||||||
|
|
||||||
var func = _[methodName];
|
var func = _[methodName],
|
||||||
|
isFindKey = /Key$/.test(methodName);
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
var objects = [
|
var objects = [
|
||||||
@@ -3977,44 +3978,65 @@
|
|||||||
|
|
||||||
deepEqual(actual, expecting);
|
deepEqual(actual, expecting);
|
||||||
});
|
});
|
||||||
|
}());
|
||||||
|
|
||||||
if (!/(Index|Key)/.test(methodName)) {
|
(function() {
|
||||||
test('should enable shortcut fusion in chaining', 2, function() {
|
var array = [1, 2, 3, 4];
|
||||||
if (!isNpm) {
|
|
||||||
var callCount = 0;
|
|
||||||
var items = _.range(0, LARGE_ARRAY_SIZE + 1);
|
|
||||||
var expectedCalls = LARGE_ARRAY_SIZE / 2 + 1;
|
|
||||||
var expected = LARGE_ARRAY_SIZE / 2 - 25;
|
|
||||||
var result = _(items).map(function(x) {
|
|
||||||
callCount++;
|
|
||||||
return x - 25;
|
|
||||||
})[methodName](function(x) {
|
|
||||||
return x === expected;
|
|
||||||
});
|
|
||||||
strictEqual(callCount, expectedCalls);
|
|
||||||
strictEqual(result, expected);
|
|
||||||
} else {
|
|
||||||
skipTest(2);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should function when the only action in chaining', 2, function() {
|
var expected = ({
|
||||||
if (!isNpm) {
|
'find': 1,
|
||||||
var callCount = 0;
|
'findLast': 4,
|
||||||
var items = _.range(0, LARGE_ARRAY_SIZE + 1);
|
'findIndex': 0,
|
||||||
var expectedCalls = LARGE_ARRAY_SIZE / 2 + 1;
|
'findLastIndex': 3,
|
||||||
var expected = LARGE_ARRAY_SIZE / 2;
|
'findKey': '0',
|
||||||
var result = _(items)[methodName](function(x) {
|
'findLastKey': '3'
|
||||||
callCount++;
|
})[methodName];
|
||||||
return x === expected;
|
|
||||||
});
|
test('should return an unwrapped value when implicitly chaining', 1, function() {
|
||||||
strictEqual(callCount, expectedCalls);
|
if (!isNpm) {
|
||||||
strictEqual(result, expected);
|
strictEqual(_(array)[methodName](), expected);
|
||||||
} else {
|
}
|
||||||
skipTest(2);
|
else {
|
||||||
}
|
skipTest();
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
test('should return a wrapped value when explicitly chaining', 1, function() {
|
||||||
|
if (!isNpm) {
|
||||||
|
ok(_(array).chain()[methodName]() instanceof _);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipTest();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not execute immediately when explicitly chaining', 1, function() {
|
||||||
|
if (!isNpm) {
|
||||||
|
var wrapped = _(array).chain()[methodName]();
|
||||||
|
strictEqual(wrapped.__wrapped__, array);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipTest();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should work in a lazy chain sequence', 2, function() {
|
||||||
|
if (!isNpm) {
|
||||||
|
var largeArray = _.range(1, LARGE_ARRAY_SIZE + 1),
|
||||||
|
smallArray = array;
|
||||||
|
|
||||||
|
_.times(2, function(index) {
|
||||||
|
var array = index ? largeArray : smallArray,
|
||||||
|
predicate = function(value) { return value % 2; },
|
||||||
|
wrapped = _(array).filter(predicate);
|
||||||
|
|
||||||
|
strictEqual(wrapped[methodName](), func(_.filter(array, predicate)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipTest(2);
|
||||||
|
}
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@@ -4058,6 +4080,41 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('lodash.find and lodash.findLast');
|
||||||
|
|
||||||
|
_.each(['find', 'findLast'], function(methodName) {
|
||||||
|
var isFind = methodName == 'find';
|
||||||
|
|
||||||
|
test('`_.' + methodName + '` should support shortcut fusion', 3, function() {
|
||||||
|
if (!isNpm) {
|
||||||
|
var findCount = 0,
|
||||||
|
mapCount = 0;
|
||||||
|
|
||||||
|
var iteratee = function(value) {
|
||||||
|
mapCount++;
|
||||||
|
return value * value;
|
||||||
|
};
|
||||||
|
|
||||||
|
var predicate = function(value) {
|
||||||
|
findCount++;
|
||||||
|
return value % 2 == 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
var array = _.range(1, LARGE_ARRAY_SIZE + 1),
|
||||||
|
result = _(array).map(iteratee)[methodName](predicate);
|
||||||
|
|
||||||
|
strictEqual(findCount, isFind ? 2 : 1);
|
||||||
|
strictEqual(mapCount, isFind ? 2 : 1);
|
||||||
|
strictEqual(result, isFind ? 4 : square(LARGE_ARRAY_SIZE));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipTest(3);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash.first');
|
QUnit.module('lodash.first');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user