mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 01:47:48 +00:00
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.
This commit is contained in:
committed by
John-David Dalton
parent
7606ea3e25
commit
d5ef31929a
63
test/indexOf-methods.js
Normal file
63
test/indexOf-methods.js
Normal file
@@ -0,0 +1,63 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user