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

83 lines
2.9 KiB
JavaScript

import assert from 'assert';
import lodashStable from 'lodash';
import { _, falsey } from './utils.js';
describe('range methods', function() {
lodashStable.each(['range', 'rangeRight'], function(methodName) {
var func = _[methodName],
isRange = methodName == 'range';
function resolve(range) {
return isRange ? range : range.reverse();
}
it('`_.' + methodName + '` should infer the sign of `step` when only `end` is given', function() {
assert.deepStrictEqual(func(4), resolve([0, 1, 2, 3]));
assert.deepStrictEqual(func(-4), resolve([0, -1, -2, -3]));
});
it('`_.' + methodName + '` should infer the sign of `step` when only `start` and `end` are given', function() {
assert.deepStrictEqual(func(1, 5), resolve([1, 2, 3, 4]));
assert.deepStrictEqual(func(5, 1), resolve([5, 4, 3, 2]));
});
it('`_.' + methodName + '` should work with a `start`, `end`, and `step`', function() {
assert.deepStrictEqual(func(0, -4, -1), resolve([0, -1, -2, -3]));
assert.deepStrictEqual(func(5, 1, -1), resolve([5, 4, 3, 2]));
assert.deepStrictEqual(func(0, 20, 5), resolve([0, 5, 10, 15]));
});
it('`_.' + methodName + '` should support a `step` of `0`', function() {
assert.deepStrictEqual(func(1, 4, 0), [1, 1, 1]);
});
it('`_.' + methodName + '` should work with a `step` larger than `end`', function() {
assert.deepStrictEqual(func(1, 5, 20), [1]);
});
it('`_.' + methodName + '` should work with a negative `step`', function() {
assert.deepStrictEqual(func(0, -4, -1), resolve([0, -1, -2, -3]));
assert.deepStrictEqual(func(21, 10, -3), resolve([21, 18, 15, 12]));
});
it('`_.' + methodName + '` should support `start` of `-0`', function() {
var actual = func(-0, 1);
assert.strictEqual(1 / actual[0], -Infinity);
});
it('`_.' + methodName + '` should treat falsey `start` as `0`', function() {
lodashStable.each(falsey, function(value, index) {
if (index) {
assert.deepStrictEqual(func(value), []);
assert.deepStrictEqual(func(value, 1), [0]);
} else {
assert.deepStrictEqual(func(), []);
}
});
});
it('`_.' + methodName + '` should coerce arguments to finite numbers', function() {
var actual = [
func('1'),
func('0', 1),
func(0, 1, '1'),
func(NaN),
func(NaN, NaN)
];
assert.deepStrictEqual(actual, [[0], [0], [0], [], []]);
});
it('`_.' + methodName + '` should work as an iteratee for methods like `_.map`', function() {
var array = [1, 2, 3],
object = { 'a': 1, 'b': 2, 'c': 3 },
expected = lodashStable.map([[0], [0, 1], [0, 1, 2]], resolve);
lodashStable.each([array, object], function(collection) {
var actual = lodashStable.map(collection, func);
assert.deepStrictEqual(actual, expected);
});
});
});
});