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.
97 lines
2.8 KiB
JavaScript
97 lines
2.8 KiB
JavaScript
import assert from 'assert';
|
|
import lodashStable from 'lodash';
|
|
import { slice, LARGE_ARRAY_SIZE } from './utils.js';
|
|
import takeRightWhile from '../takeRightWhile.js';
|
|
|
|
describe('takeRightWhile', function() {
|
|
var array = [1, 2, 3, 4];
|
|
|
|
var objects = [
|
|
{ 'a': 0, 'b': 0 },
|
|
{ 'a': 1, 'b': 1 },
|
|
{ 'a': 2, 'b': 2 }
|
|
];
|
|
|
|
it('should take elements while `predicate` returns truthy', function() {
|
|
var actual = takeRightWhile(array, function(n) {
|
|
return n > 2;
|
|
});
|
|
|
|
assert.deepStrictEqual(actual, [3, 4]);
|
|
});
|
|
|
|
it('should provide correct `predicate` arguments', function() {
|
|
var args;
|
|
|
|
takeRightWhile(array, function() {
|
|
args = slice.call(arguments);
|
|
});
|
|
|
|
assert.deepStrictEqual(args, [4, 3, array]);
|
|
});
|
|
|
|
it('should work with `_.matches` shorthands', function() {
|
|
assert.deepStrictEqual(takeRightWhile(objects, { 'b': 2 }), objects.slice(2));
|
|
});
|
|
|
|
it('should work with `_.matchesProperty` shorthands', function() {
|
|
assert.deepStrictEqual(takeRightWhile(objects, ['b', 2]), objects.slice(2));
|
|
});
|
|
|
|
it('should work with `_.property` shorthands', function() {
|
|
assert.deepStrictEqual(takeRightWhile(objects, 'b'), objects.slice(1));
|
|
});
|
|
|
|
it('should work in a lazy sequence', function() {
|
|
var array = lodashStable.range(LARGE_ARRAY_SIZE),
|
|
predicate = function(n) { return n > 2; },
|
|
expected = takeRightWhile(array, predicate),
|
|
wrapped = _(array).takeRightWhile(predicate);
|
|
|
|
assert.deepEqual(wrapped.value(), expected);
|
|
assert.deepEqual(wrapped.reverse().value(), expected.slice().reverse());
|
|
assert.strictEqual(wrapped.last(), _.last(expected));
|
|
});
|
|
|
|
it('should provide correct `predicate` arguments in a lazy sequence', function() {
|
|
var args,
|
|
array = lodashStable.range(LARGE_ARRAY_SIZE + 1);
|
|
|
|
var expected = [
|
|
square(LARGE_ARRAY_SIZE),
|
|
LARGE_ARRAY_SIZE - 1,
|
|
lodashStable.map(array.slice(1), square)
|
|
];
|
|
|
|
_(array).slice(1).takeRightWhile(function(value, index, array) {
|
|
args = slice.call(arguments);
|
|
}).value();
|
|
|
|
assert.deepEqual(args, [LARGE_ARRAY_SIZE, LARGE_ARRAY_SIZE - 1, array.slice(1)]);
|
|
|
|
_(array).slice(1).map(square).takeRightWhile(function(value, index, array) {
|
|
args = slice.call(arguments);
|
|
}).value();
|
|
|
|
assert.deepEqual(args, expected);
|
|
|
|
_(array).slice(1).map(square).takeRightWhile(function(value, index) {
|
|
args = slice.call(arguments);
|
|
}).value();
|
|
|
|
assert.deepEqual(args, expected);
|
|
|
|
_(array).slice(1).map(square).takeRightWhile(function(index) {
|
|
args = slice.call(arguments);
|
|
}).value();
|
|
|
|
assert.deepEqual(args, [square(LARGE_ARRAY_SIZE)]);
|
|
|
|
_(array).slice(1).map(square).takeRightWhile(function() {
|
|
args = slice.call(arguments);
|
|
}).value();
|
|
|
|
assert.deepEqual(args, expected);
|
|
});
|
|
});
|