mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
* Install test dependencies. * Add initial test files. These files were created using a simplistic AST manipulator using `recast` to preserve formatting. There's bound to be a huge chunk of errors, but this serves as a good start. QUnit was replaced with Mocha, with ES2015 imports running via `esm`. As far as possible, QUnit-specific syntax has been replaced with Mocha's `describe` and `it`, while the native Node.js `assert` module is used for assertions. Files in the `test` directory ending in `.test.js` will be treated as test files. * Add initial passing files to test run.
109 lines
3.7 KiB
JavaScript
109 lines
3.7 KiB
JavaScript
import assert from 'assert';
|
|
import lodashStable from 'lodash';
|
|
import { _, add, square, noop, identity, LARGE_ARRAY_SIZE, isEven, isNpm } from './utils.js';
|
|
import times from '../times.js';
|
|
import curry from '../curry.js';
|
|
import head from '../head.js';
|
|
import filter from '../filter.js';
|
|
import rearg from '../rearg.js';
|
|
import ary from '../ary.js';
|
|
import map from '../map.js';
|
|
import take from '../take.js';
|
|
import compact from '../compact.js';
|
|
import uniq from '../uniq.js';
|
|
|
|
describe('flow methods', function() {
|
|
lodashStable.each(['flow', 'flowRight'], function(methodName) {
|
|
var func = _[methodName],
|
|
isFlow = methodName == 'flow';
|
|
|
|
it('`_.' + methodName + '` should supply each function with the return value of the previous', function() {
|
|
var fixed = function(n) { return n.toFixed(1); },
|
|
combined = isFlow ? func(add, square, fixed) : func(fixed, square, add);
|
|
|
|
assert.strictEqual(combined(1, 2), '9.0');
|
|
});
|
|
|
|
it('`_.' + methodName + '` should return a new function', function() {
|
|
assert.notStrictEqual(func(noop), noop);
|
|
});
|
|
|
|
it('`_.' + methodName + '` should return an identity function when no arguments are given', function() {
|
|
times(2, function(index) {
|
|
try {
|
|
var combined = index ? func([]) : func();
|
|
assert.strictEqual(combined('a'), 'a');
|
|
} catch (e) {
|
|
assert.ok(false, e.message);
|
|
}
|
|
assert.strictEqual(combined.length, 0);
|
|
assert.notStrictEqual(combined, identity);
|
|
});
|
|
});
|
|
|
|
it('`_.' + methodName + '` should work with a curried function and `_.head`', function() {
|
|
var curried = curry(identity);
|
|
|
|
var combined = isFlow
|
|
? func(head, curried)
|
|
: func(curried, head);
|
|
|
|
assert.strictEqual(combined([1]), 1);
|
|
});
|
|
|
|
it('`_.' + methodName + '` should support shortcut fusion', function() {
|
|
var filterCount,
|
|
mapCount,
|
|
array = lodashStable.range(LARGE_ARRAY_SIZE),
|
|
iteratee = function(value) { mapCount++; return square(value); },
|
|
predicate = function(value) { filterCount++; return isEven(value); };
|
|
|
|
lodashStable.times(2, function(index) {
|
|
var filter1 = filter,
|
|
filter2 = curry(rearg(ary(filter, 2), 1, 0), 2),
|
|
filter3 = (filter = index ? filter2 : filter1, filter2(predicate));
|
|
|
|
var map1 = map,
|
|
map2 = curry(rearg(ary(map, 2), 1, 0), 2),
|
|
map3 = (map = index ? map2 : map1, map2(iteratee));
|
|
|
|
var take1 = take,
|
|
take2 = curry(rearg(ary(take, 2), 1, 0), 2),
|
|
take3 = (take = index ? take2 : take1, take2(2));
|
|
|
|
var combined = isFlow
|
|
? func(map3, filter3, compact, take3)
|
|
: func(take3, compact, filter3, map3);
|
|
|
|
filterCount = mapCount = 0;
|
|
assert.deepStrictEqual(combined(array), [4, 16]);
|
|
|
|
if (!isNpm && WeakMap && WeakMap.name) {
|
|
assert.strictEqual(filterCount, 5, 'filterCount');
|
|
assert.strictEqual(mapCount, 5, 'mapCount');
|
|
}
|
|
filter = filter1;
|
|
map = map1;
|
|
take = take1;
|
|
});
|
|
});
|
|
|
|
it('`_.' + methodName + '` should work with curried functions with placeholders', function() {
|
|
var curried = curry(ary(map, 2), 2),
|
|
getProp = curried(curried.placeholder, 'a'),
|
|
objects = [{ 'a': 1 }, { 'a': 2 }, { 'a': 1 }];
|
|
|
|
var combined = isFlow
|
|
? func(getProp, uniq)
|
|
: func(uniq, getProp);
|
|
|
|
assert.deepStrictEqual(combined(objects), [1, 2]);
|
|
});
|
|
|
|
it('`_.' + methodName + '` should return a wrapped value when chaining', function() {
|
|
var wrapped = _(noop)[methodName]();
|
|
assert.ok(wrapped instanceof _);
|
|
});
|
|
});
|
|
});
|