Files
lodash/test/slice.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

134 lines
5.1 KiB
JavaScript

import assert from 'assert';
import lodashStable from 'lodash';
import { falsey, LARGE_ARRAY_SIZE } from './utils.js';
import slice from '../slice.js';
describe('slice', function() {
var array = [1, 2, 3];
it('should use a default `start` of `0` and a default `end` of `length`', function() {
var actual = slice(array);
assert.deepStrictEqual(actual, array);
assert.notStrictEqual(actual, array);
});
it('should work with a positive `start`', function() {
assert.deepStrictEqual(slice(array, 1), [2, 3]);
assert.deepStrictEqual(slice(array, 1, 3), [2, 3]);
});
it('should work with a `start` >= `length`', function() {
lodashStable.each([3, 4, Math.pow(2, 32), Infinity], function(start) {
assert.deepStrictEqual(slice(array, start), []);
});
});
it('should treat falsey `start` values as `0`', function() {
var expected = lodashStable.map(falsey, lodashStable.constant(array));
var actual = lodashStable.map(falsey, function(start) {
return slice(array, start);
});
assert.deepStrictEqual(actual, expected);
});
it('should work with a negative `start`', function() {
assert.deepStrictEqual(slice(array, -1), [3]);
});
it('should work with a negative `start` <= negative `length`', function() {
lodashStable.each([-3, -4, -Infinity], function(start) {
assert.deepStrictEqual(slice(array, start), array);
});
});
it('should work with `start` >= `end`', function() {
lodashStable.each([2, 3], function(start) {
assert.deepStrictEqual(slice(array, start, 2), []);
});
});
it('should work with a positive `end`', function() {
assert.deepStrictEqual(slice(array, 0, 1), [1]);
});
it('should work with a `end` >= `length`', function() {
lodashStable.each([3, 4, Math.pow(2, 32), Infinity], function(end) {
assert.deepStrictEqual(slice(array, 0, end), array);
});
});
it('should treat falsey `end` values, except `undefined`, as `0`', function() {
var expected = lodashStable.map(falsey, function(value) {
return value === undefined ? array : [];
});
var actual = lodashStable.map(falsey, function(end, index) {
return index ? slice(array, 0, end) : slice(array, 0);
});
assert.deepStrictEqual(actual, expected);
});
it('should work with a negative `end`', function() {
assert.deepStrictEqual(slice(array, 0, -1), [1, 2]);
});
it('should work with a negative `end` <= negative `length`', function() {
lodashStable.each([-3, -4, -Infinity], function(end) {
assert.deepStrictEqual(slice(array, 0, end), []);
});
});
it('should coerce `start` and `end` to integers', function() {
var positions = [[0.1, 1.6], ['0', 1], [0, '1'], ['1'], [NaN, 1], [1, NaN]];
var actual = lodashStable.map(positions, function(pos) {
return slice.apply(_, [array].concat(pos));
});
assert.deepStrictEqual(actual, [[1], [1], [1], [2, 3], [1], []]);
});
it('should work as an iteratee for methods like `_.map`', function() {
var array = [[1], [2, 3]],
actual = lodashStable.map(array, slice);
assert.deepStrictEqual(actual, array);
assert.notStrictEqual(actual, array);
});
it('should work in a lazy sequence', function() {
var array = lodashStable.range(1, LARGE_ARRAY_SIZE + 1),
length = array.length,
wrapped = _(array);
lodashStable.each(['map', 'filter'], function(methodName) {
assert.deepEqual(wrapped[methodName]().slice(0, -1).value(), array.slice(0, -1));
assert.deepEqual(wrapped[methodName]().slice(1).value(), array.slice(1));
assert.deepEqual(wrapped[methodName]().slice(1, 3).value(), array.slice(1, 3));
assert.deepEqual(wrapped[methodName]().slice(-1).value(), array.slice(-1));
assert.deepEqual(wrapped[methodName]().slice(length).value(), array.slice(length));
assert.deepEqual(wrapped[methodName]().slice(3, 2).value(), array.slice(3, 2));
assert.deepEqual(wrapped[methodName]().slice(0, -length).value(), array.slice(0, -length));
assert.deepEqual(wrapped[methodName]().slice(0, null).value(), array.slice(0, null));
assert.deepEqual(wrapped[methodName]().slice(0, length).value(), array.slice(0, length));
assert.deepEqual(wrapped[methodName]().slice(-length).value(), array.slice(-length));
assert.deepEqual(wrapped[methodName]().slice(null).value(), array.slice(null));
assert.deepEqual(wrapped[methodName]().slice(0, 1).value(), array.slice(0, 1));
assert.deepEqual(wrapped[methodName]().slice(NaN, '1').value(), array.slice(NaN, '1'));
assert.deepEqual(wrapped[methodName]().slice(0.1, 1.1).value(), array.slice(0.1, 1.1));
assert.deepEqual(wrapped[methodName]().slice('0', 1).value(), array.slice('0', 1));
assert.deepEqual(wrapped[methodName]().slice(0, '1').value(), array.slice(0, '1'));
assert.deepEqual(wrapped[methodName]().slice('1').value(), array.slice('1'));
assert.deepEqual(wrapped[methodName]().slice(NaN, 1).value(), array.slice(NaN, 1));
assert.deepEqual(wrapped[methodName]().slice(1, NaN).value(), array.slice(1, NaN));
});
});
});