Cleanup .next tests.

This commit is contained in:
John-David Dalton
2015-08-31 17:52:32 -07:00
parent 9e86efadbc
commit 7206edec2e

View File

@@ -62,6 +62,7 @@
/** Math helpers. */ /** Math helpers. */
var add = function(x, y) { return x + y; }, var add = function(x, y) { return x + y; },
doubled = function(n) { return n * 2; }, doubled = function(n) { return n * 2; },
isEven = function(n) { return n % 2 == 0; },
square = function(n) { return n * n; }; square = function(n) { return n * n; };
/** Used to set property descriptors. */ /** Used to set property descriptors. */
@@ -16583,73 +16584,64 @@
QUnit.module('lodash(...).next'); QUnit.module('lodash(...).next');
_.each([true, false], function(implict) { _.each([true, false], function(implict) {
function arrayFrom(iter) { function chain(value) {
var item; return implict ? _(value) : _.chain(value);
var result = []; }
while (!(item = iter.next()).done) {
result.push(item.value); var chainType = 'in an ' + (implict ? 'implict' : 'explict') + ' chain';
test('should follow the iterator protocol ' + chainType, 3, function() {
if (!isNpm) {
var wrapped = chain([1, 2]);
deepEqual(wrapped.next(), { 'done': false, 'value': 1 });
deepEqual(wrapped.next(), { 'done': false, 'value': 2 });
deepEqual(wrapped.next(), { 'done': true, 'value': undefined });
}
else {
skipTest(3);
} }
return result;
}
function chain(obj) {
return implict ? _(obj) : _.chain(obj);
}
var chainType = ' in an ' + (implict ? 'implict' : 'explict') + ' chain';
test('should produce an ES6 compliant object' + chainType, 7, function() {
var array = [0, 1, 1, 2, 3],
chained = chain(array);
deepEqual(chained.next(), {done: false, value: 0});
deepEqual(chained.next(), {done: false, value: 1});
deepEqual(chained.next(), {done: false, value: 1});
deepEqual(chained.next(), {done: false, value: 2});
deepEqual(chained.next(), {done: false, value: 3});
deepEqual(chained.next(), {done: true, value: undefined});
deepEqual(chained.next(), {done: true, value: undefined});
}); });
test('should make a lodash instance act as an iterable' + chainType, 1, function() { test('should act as an iterable ' + chainType, 1, function() {
var array = [0, 1, 1, 2, 3, 5, 8, 13, 21], if (!isNpm && Symbol && Symbol.iterator) {
chained = chain(array); var wrapped = chain([1, 2]);
ok(wrapped[Symbol.iterator]() === wrapped);
deepEqual(arrayFrom(chained), array); }
else {
skipTest();
}
}); });
test('should reset the iterator upon forking (adding an action to the chain)' + chainType, 5, function() { test('should reset the iterator correctly ' + chainType, 4, function() {
var array = [0, 1, 1, 2, 3, 5, 8, 13, 21], if (!isNpm) {
chained = chain(array); var array = [1, 2],
wrapped = chain(array);
deepEqual(arrayFrom(chained), array); deepEqual(_.toArray(wrapped), array);
// before reset produces empty array as iterator is exhausted deepEqual(_.toArray(wrapped), [], 'produces an empty array for exhausted iterator');
deepEqual(arrayFrom(chained), []);
var newChain = chained.filter(_.constant(true)); var other = wrapped.filter();
deepEqual(arrayFrom(newChain), array); deepEqual(_.toArray(other), array, 'reset for new chain segments');
deepEqual(_.toArray(wrapped), [], 'iterator is still exhausted');
// original chain is still exhausted }
deepEqual(arrayFrom(chained), []); else {
skipTest(4);
newChain = chained.slice(); }
deepEqual(arrayFrom(newChain), array);
}); });
test('should work in a lazy sequence' + chainType, 3, function() { test('should work in a lazy sequence' + chainType, 3, function() {
if (true) { if (!isNpm) {
var array = _.range(1, LARGE_ARRAY_SIZE + 1), var array = _.range(LARGE_ARRAY_SIZE),
predicate = function(value) { values.push(value); return isEven(value); },
values = [], values = [],
predicate = function(value) { values.push(value); return value > 99; }, wrapped = chain(array);
chained = chain(array);
deepEqual(arrayFrom(chained), array); deepEqual(_.toArray(wrapped), array);
// resets index & supports lazy array wrapped = wrapped.filter(predicate);
chained = chained.filter(predicate); deepEqual(_.toArray(wrapped), _.filter(array, isEven), 'reset for new lazy chain segments');
deepEqual(arrayFrom(chained), array.slice(99)); deepEqual(values, array, 'memoizes iterator values');
// Doesn't recompute everything each time
deepEqual(values, array);
} }
else { else {
skipTest(3); skipTest(3);