mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-03 16:47:49 +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
76
test/sampleSize.js
Normal file
76
test/sampleSize.js
Normal file
@@ -0,0 +1,76 @@
|
||||
import assert from 'assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey, empties, stubArray } from './utils.js';
|
||||
import sampleSize from '../sampleSize.js';
|
||||
|
||||
describe('sampleSize', function() {
|
||||
var array = [1, 2, 3];
|
||||
|
||||
it('should return an array of random elements', function() {
|
||||
var actual = sampleSize(array, 2);
|
||||
|
||||
assert.strictEqual(actual.length, 2);
|
||||
assert.deepStrictEqual(lodashStable.difference(actual, array), []);
|
||||
});
|
||||
|
||||
it('should contain elements of the collection', function() {
|
||||
var actual = sampleSize(array, array.length).sort();
|
||||
|
||||
assert.deepStrictEqual(actual, array);
|
||||
});
|
||||
|
||||
it('should treat falsey `size` values, except `undefined`, as `0`', function() {
|
||||
var expected = lodashStable.map(falsey, function(value) {
|
||||
return value === undefined ? ['a'] : [];
|
||||
});
|
||||
|
||||
var actual = lodashStable.map(falsey, function(size, index) {
|
||||
return index ? sampleSize(['a'], size) : sampleSize(['a']);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('should return an empty array when `n` < `1` or `NaN`', function() {
|
||||
lodashStable.each([0, -1, -Infinity], function(n) {
|
||||
assert.deepStrictEqual(sampleSize(array, n), []);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return all elements when `n` >= `length`', function() {
|
||||
lodashStable.each([3, 4, Math.pow(2, 32), Infinity], function(n) {
|
||||
var actual = sampleSize(array, n).sort();
|
||||
assert.deepStrictEqual(actual, array);
|
||||
});
|
||||
});
|
||||
|
||||
it('should coerce `n` to an integer', function() {
|
||||
var actual = sampleSize(array, 1.6);
|
||||
assert.strictEqual(actual.length, 1);
|
||||
});
|
||||
|
||||
it('should return an empty array for empty collections', function() {
|
||||
var expected = lodashStable.map(empties, stubArray);
|
||||
|
||||
var actual = lodashStable.transform(empties, function(result, value) {
|
||||
try {
|
||||
result.push(sampleSize(value, 1));
|
||||
} catch (e) {}
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('should sample an object', function() {
|
||||
var object = { 'a': 1, 'b': 2, 'c': 3 },
|
||||
actual = sampleSize(object, 2);
|
||||
|
||||
assert.strictEqual(actual.length, 2);
|
||||
assert.deepStrictEqual(lodashStable.difference(actual, lodashStable.values(object)), []);
|
||||
});
|
||||
|
||||
it('should work as an iteratee for methods like `_.map`', function() {
|
||||
var actual = lodashStable.map([['a']], sampleSize);
|
||||
assert.deepStrictEqual(actual, [['a']]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user