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

82 lines
2.6 KiB
JavaScript

import assert from 'assert';
import lodashStable from 'lodash';
import { lodashBizarro, whitespace, stubZero } from './utils.js';
import parseInt from '../parseInt.js';
describe('parseInt', function() {
it('should accept a `radix`', function() {
var expected = lodashStable.range(2, 37);
var actual = lodashStable.map(expected, function(radix) {
return parseInt('10', radix);
});
assert.deepStrictEqual(actual, expected);
});
it('should use a radix of `10`, for non-hexadecimals, if `radix` is `undefined` or `0`', function() {
assert.strictEqual(parseInt('10'), 10);
assert.strictEqual(parseInt('10', 0), 10);
assert.strictEqual(parseInt('10', 10), 10);
assert.strictEqual(parseInt('10', undefined), 10);
});
it('should use a radix of `16`, for hexadecimals, if `radix` is `undefined` or `0`', function() {
lodashStable.each(['0x20', '0X20'], function(string) {
assert.strictEqual(parseInt(string), 32);
assert.strictEqual(parseInt(string, 0), 32);
assert.strictEqual(parseInt(string, 16), 32);
assert.strictEqual(parseInt(string, undefined), 32);
});
});
it('should use a radix of `10` for string with leading zeros', function() {
assert.strictEqual(parseInt('08'), 8);
assert.strictEqual(parseInt('08', 10), 8);
});
it('should parse strings with leading whitespace', function() {
var expected = [8, 8, 10, 10, 32, 32, 32, 32];
lodashStable.times(2, function(index) {
var actual = [],
func = (index ? (lodashBizarro || {}) : _).parseInt;
if (func) {
lodashStable.times(2, function(otherIndex) {
var string = otherIndex ? '10' : '08';
actual.push(
func(whitespace + string, 10),
func(whitespace + string)
);
});
lodashStable.each(['0x20', '0X20'], function(string) {
actual.push(
func(whitespace + string),
func(whitespace + string, 16)
);
});
assert.deepStrictEqual(actual, expected);
}
});
});
it('should coerce `radix` to a number', function() {
var object = { 'valueOf': stubZero };
assert.strictEqual(parseInt('08', object), 8);
assert.strictEqual(parseInt('0x20', object), 32);
});
it('should work as an iteratee for methods like `_.map`', function() {
var strings = lodashStable.map(['6', '08', '10'], Object),
actual = lodashStable.map(strings, parseInt);
assert.deepStrictEqual(actual, [6, 8, 10]);
actual = lodashStable.map('123', parseInt);
assert.deepStrictEqual(actual, [1, 2, 3]);
});
});