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

64 lines
2.0 KiB
JavaScript

import assert from 'assert';
import lodashStable from 'lodash';
import { _, falsey } from './utils.js';
describe('indexOf methods', function() {
lodashStable.each(['indexOf', 'lastIndexOf', 'sortedIndexOf', 'sortedLastIndexOf'], function(methodName) {
var func = _[methodName],
isIndexOf = !/last/i.test(methodName),
isSorted = /^sorted/.test(methodName);
it('`_.' + methodName + '` should accept a falsey `array`', function() {
var expected = lodashStable.map(falsey, lodashStable.constant(-1));
var actual = lodashStable.map(falsey, function(array, index) {
try {
return index ? func(array) : func();
} catch (e) {}
});
assert.deepStrictEqual(actual, expected);
});
it('`_.' + methodName + '` should return `-1` for an unmatched value', function() {
var array = [1, 2, 3],
empty = [];
assert.strictEqual(func(array, 4), -1);
assert.strictEqual(func(array, 4, true), -1);
assert.strictEqual(func(array, undefined, true), -1);
assert.strictEqual(func(empty, undefined), -1);
assert.strictEqual(func(empty, undefined, true), -1);
});
it('`_.' + methodName + '` should not match values on empty arrays', function() {
var array = [];
array[-1] = 0;
assert.strictEqual(func(array, undefined), -1);
assert.strictEqual(func(array, 0, true), -1);
});
it('`_.' + methodName + '` should match `NaN`', function() {
var array = isSorted
? [1, 2, NaN, NaN]
: [1, NaN, 3, NaN, 5, NaN];
if (isSorted) {
assert.strictEqual(func(array, NaN, true), isIndexOf ? 2 : 3);
}
else {
assert.strictEqual(func(array, NaN), isIndexOf ? 1 : 5);
assert.strictEqual(func(array, NaN, 2), isIndexOf ? 3 : 1);
assert.strictEqual(func(array, NaN, -2), isIndexOf ? 5 : 3);
}
});
it('`_.' + methodName + '` should match `-0` as `0`', function() {
assert.strictEqual(func([-0], 0), 0);
assert.strictEqual(func([0], -0), 0);
});
});
});