mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-04 08:57: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
82
test/round-methods.js
Normal file
82
test/round-methods.js
Normal file
@@ -0,0 +1,82 @@
|
||||
import assert from 'assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _, MAX_SAFE_INTEGER, stubFalse } from './utils.js';
|
||||
import round from '../round.js';
|
||||
|
||||
describe('round methods', function() {
|
||||
lodashStable.each(['ceil', 'floor', 'round'], function(methodName) {
|
||||
var func = _[methodName],
|
||||
isCeil = methodName == 'ceil',
|
||||
isFloor = methodName == 'floor';
|
||||
|
||||
it('`_.' + methodName + '` should return a rounded number without a precision', function() {
|
||||
var actual = func(4.006);
|
||||
assert.strictEqual(actual, isCeil ? 5 : 4);
|
||||
});
|
||||
|
||||
it('`_.' + methodName + '` should work with a precision of `0`', function() {
|
||||
var actual = func(4.006, 0);
|
||||
assert.strictEqual(actual, isCeil ? 5 : 4);
|
||||
});
|
||||
|
||||
it('`_.' + methodName + '` should work with a positive precision', function() {
|
||||
var actual = func(4.016, 2);
|
||||
assert.strictEqual(actual, isFloor ? 4.01 : 4.02);
|
||||
|
||||
actual = func(4.1, 2);
|
||||
assert.strictEqual(actual, 4.1);
|
||||
});
|
||||
|
||||
it('`_.' + methodName + '` should work with a negative precision', function() {
|
||||
var actual = func(4160, -2);
|
||||
assert.strictEqual(actual, isFloor ? 4100 : 4200);
|
||||
});
|
||||
|
||||
it('`_.' + methodName + '` should coerce `precision` to an integer', function() {
|
||||
var actual = func(4.006, NaN);
|
||||
assert.strictEqual(actual, isCeil ? 5 : 4);
|
||||
|
||||
var expected = isFloor ? 4.01 : 4.02;
|
||||
|
||||
actual = func(4.016, 2.6);
|
||||
assert.strictEqual(actual, expected);
|
||||
|
||||
actual = func(4.016, '+2');
|
||||
assert.strictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('`_.' + methodName + '` should work with exponential notation and `precision`', function() {
|
||||
var actual = func(5e1, 2);
|
||||
assert.deepStrictEqual(actual, 50);
|
||||
|
||||
actual = func('5e', 1);
|
||||
assert.deepStrictEqual(actual, NaN);
|
||||
|
||||
actual = func('5e1e1', 1);
|
||||
assert.deepStrictEqual(actual, NaN);
|
||||
});
|
||||
|
||||
it('`_.' + methodName + '` should preserve the sign of `0`', function() {
|
||||
var values = [[0], [-0], ['0'], ['-0'], [0, 1], [-0, 1], ['0', 1], ['-0', 1]],
|
||||
expected = [Infinity, -Infinity, Infinity, -Infinity, Infinity, -Infinity, Infinity, -Infinity];
|
||||
|
||||
var actual = lodashStable.map(values, function(args) {
|
||||
return 1 / func.apply(undefined, args);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('`_.' + methodName + '` should not return `NaN` for large `precision` values', function() {
|
||||
var results = [
|
||||
round(10.0000001, 1000),
|
||||
round(MAX_SAFE_INTEGER, 293)
|
||||
];
|
||||
|
||||
var expected = lodashStable.map(results, stubFalse),
|
||||
actual = lodashStable.map(results, lodashStable.isNaN);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user