Files
lodash/test/filter-methods.js
Benjamin Tan d5ef31929a Add initial test files from lodash v4. (#4172)
* 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.
2019-02-12 09:11:32 -08:00

101 lines
3.4 KiB
JavaScript

import assert from 'assert';
import lodashStable from 'lodash';
import { _, LARGE_ARRAY_SIZE, isEven, square } from './utils.js';
describe('filter methods', function() {
lodashStable.each(['filter', 'reject'], function(methodName) {
var array = [1, 2, 3, 4],
func = _[methodName],
isFilter = methodName == 'filter',
objects = [{ 'a': 0 }, { 'a': 1 }];
it('`_.' + methodName + '` should not modify the resulting value from within `predicate`', function() {
var actual = func([0], function(value, index, array) {
array[index] = 1;
return isFilter;
});
assert.deepStrictEqual(actual, [0]);
});
it('`_.' + methodName + '` should work with `_.property` shorthands', function() {
assert.deepStrictEqual(func(objects, 'a'), [objects[isFilter ? 1 : 0]]);
});
it('`_.' + methodName + '` should work with `_.matches` shorthands', function() {
assert.deepStrictEqual(func(objects, objects[1]), [objects[isFilter ? 1 : 0]]);
});
it('`_.' + methodName + '` should not modify wrapped values', function() {
var wrapped = _(array);
var actual = wrapped[methodName](function(n) {
return n < 3;
});
assert.deepEqual(actual.value(), isFilter ? [1, 2] : [3, 4]);
actual = wrapped[methodName](function(n) {
return n > 2;
});
assert.deepEqual(actual.value(), isFilter ? [3, 4] : [1, 2]);
});
it('`_.' + methodName + '` should work in a lazy sequence', function() {
var array = lodashStable.range(LARGE_ARRAY_SIZE + 1),
predicate = function(value) { return isFilter ? isEven(value) : !isEven(value); };
var object = lodashStable.zipObject(lodashStable.times(LARGE_ARRAY_SIZE, function(index) {
return ['key' + index, index];
}));
var actual = _(array).slice(1).map(square)[methodName](predicate).value();
assert.deepEqual(actual, _[methodName](lodashStable.map(array.slice(1), square), predicate));
actual = _(object).mapValues(square)[methodName](predicate).value();
assert.deepEqual(actual, _[methodName](lodashStable.mapValues(object, square), predicate));
});
it('`_.' + methodName + '` should provide correct `predicate` arguments in a lazy sequence', function() {
var args,
array = lodashStable.range(LARGE_ARRAY_SIZE + 1),
expected = [1, 0, lodashStable.map(array.slice(1), square)];
_(array).slice(1)[methodName](function(value, index, array) {
args || (args = slice.call(arguments));
}).value();
assert.deepEqual(args, [1, 0, array.slice(1)]);
args = undefined;
_(array).slice(1).map(square)[methodName](function(value, index, array) {
args || (args = slice.call(arguments));
}).value();
assert.deepEqual(args, expected);
args = undefined;
_(array).slice(1).map(square)[methodName](function(value, index) {
args || (args = slice.call(arguments));
}).value();
assert.deepEqual(args, expected);
args = undefined;
_(array).slice(1).map(square)[methodName](function(value) {
args || (args = slice.call(arguments));
}).value();
assert.deepEqual(args, [1]);
args = undefined;
_(array).slice(1).map(square)[methodName](function() {
args || (args = slice.call(arguments));
}).value();
assert.deepEqual(args, expected);
});
});
});