Cleanup _.toNumber test.

This commit is contained in:
John-David Dalton
2015-11-01 13:43:38 -08:00
parent 7d1e24d07f
commit ae76c3ee71

View File

@@ -19376,322 +19376,184 @@
QUnit.module('lodash.toNumber'); QUnit.module('lodash.toNumber');
(function() { (function() {
QUnit.test('null, empty strings and undefined should convert to `0` and `NaN`', function(assert) { function negative(string) {
assert.expect(2);
assert.deepEqual(_.toNumber(), NaN);
var values = [undefined, null, '', whitespace],
expected = [NaN, Infinity, Infinity, Infinity],
actual = lodashStable.map(values, function(value) {
return 1 / _.toNumber(value);
});
assert.deepEqual(actual, expected);
});
QUnit.test('zeros should preserve their sign', function(assert) {
assert.expect(1);
var temp = [0, '0', ' 0 ', '+0', ' +0 '],
values = temp,
length = temp.length * 2,
expected = _.fill(Array(length), Infinity),
actual = lodashStable.reduce(temp, function(acc, value) {
acc.push(1 / _.toNumber(value), 1 / _.toNumber(Object(value)));
return acc;
}, []);
temp = [-0, '-0', ' -0 '];
values = values.concat(temp);
expected.length += temp.length * 2;
_.fill(expected, -Infinity, length);
lodashStable.reduce(temp, function(acc, value) {
acc.push(1 / _.toNumber(value), 1 / _.toNumber(Object(value)));
return acc;
}, actual);
assert.deepEqual(actual, expected);
});
var toNumberNumbers = [10, 1.23456789, MAX_SAFE_INTEGER, MAX_INTEGER, Infinity, NaN];
lodashStable.each(toNumberNumbers, function (number) {
QUnit.test('number literal and objects should return number literals', function(assert) {
assert.expect(1);
var expected = [number, -number, number, -number],
actual = [_.toNumber(number), _.toNumber(-number), _.toNumber(Object(number)), _.toNumber(Object(-number))];
assert.deepEqual(actual, expected);
});
});
function negStr(string) {
return '-' + string; return '-' + string;
} }
function posStr(string) { function pad(string) {
return '+' + string;
}
function wrapWS(string) {
return whitespace + string + whitespace; return whitespace + string + whitespace;
} }
var toNumberBasicStrings = ['10', '1.234567890', '9007199254740991', '1e+308', '1e308', '1E+308', '1E308', '5e-324', '5E-324', 'Infinity', 'NaN']; function positive(string) {
lodashStable.each(toNumberNumbers, function (string) { return '+' + string;
QUnit.test('should convert basic string literals and objects accurately', function(assert) { }
assert.expect(1);
var actual = [], QUnit.test('should convert empty values to `0` or `NaN`', function(assert) {
expected = [];
actual.push(_.toNumber(string));
expected.push(+string);
actual.push(_.toNumber(posStr(string)));
expected.push(+posStr(string));
actual.push(_.toNumber(negStr(string)));
expected.push(+negStr(string));
actual.push(_.toNumber(wrapWS(string)));
expected.push(+string);
actual.push(_.toNumber(wrapWS(posStr(string))));
expected.push(+posStr(string));
actual.push(_.toNumber(wrapWS(negStr(string))));
expected.push(+negStr(string));
actual.push(_.toNumber(Object(string))),
expected.push(+string);
actual.push(_.toNumber(Object(posStr(string))));
expected.push(+posStr(string));
actual.push(_.toNumber(Object(negStr(string))));
expected.push(+negStr(string));
actual.push(_.toNumber(Object(wrapWS(string))));
expected.push(+string);
actual.push(_.toNumber(Object(wrapWS(posStr(string)))));
expected.push(+posStr(string));
actual.push(_.toNumber(Object(wrapWS(negStr(string)))));
expected.push(+negStr(string));
assert.deepEqual(actual, expected);
});
});
var toNumberAdvancedStrings = [{
string: '0b101010',
value: 42
}, {
string: '0o12345',
value: 5349
}, {
string: '0x1a2b3c',
value: 1715004
}];
lodashStable.each(toNumberAdvancedStrings, function (item) {
QUnit.test('should convert basic string literals and objects accurately', function(assert) {
assert.expect(1);
var actual = [],
expected = [];
actual.push(_.toNumber(item.string));
expected.push(item.value);
actual.push(_.toNumber(posStr(item.string)));
expected.push(NaN);
actual.push(_.toNumber(negStr(item.string)));
expected.push(NaN);
actual.push(_.toNumber(wrapWS(item.string)));
expected.push(item.value);
actual.push(_.toNumber(wrapWS(posStr(item.string))));
expected.push(NaN);
actual.push(_.toNumber(wrapWS(negStr(item.string))));
expected.push(NaN);
actual.push(_.toNumber(item.string.toUpperCase()));
expected.push(item.value);
actual.push(_.toNumber(posStr(item.string.toUpperCase())));
expected.push(NaN);
actual.push(_.toNumber(negStr(item.string.toUpperCase())));
expected.push(NaN);
actual.push(_.toNumber(wrapWS(item.string.toUpperCase())));
expected.push(item.value);
actual.push(_.toNumber(wrapWS(posStr(item.string.toUpperCase()))));
expected.push(NaN);
actual.push(_.toNumber(wrapWS(negStr(item.string.toUpperCase()))));
expected.push(NaN);
actual.push(_.toNumber(Object(item.string)));
expected.push(item.value);
actual.push(_.toNumber(Object(posStr(item.string))));
expected.push(NaN);
actual.push(_.toNumber(Object(negStr(item.string))));
expected.push(NaN);
actual.push(_.toNumber(Object(wrapWS(item.string))));
expected.push(item.value);
actual.push(_.toNumber(Object(wrapWS(posStr(item.string)))));
expected.push(NaN);
actual.push(_.toNumber(Object(wrapWS(negStr(item.string)))));
expected.push(NaN);
actual.push(_.toNumber(Object(item.string.toUpperCase())));
expected.push(item.value);
actual.push(_.toNumber(Object(posStr(item.string.toUpperCase()))));
expected.push(NaN);
actual.push(_.toNumber(Object(negStr(item.string.toUpperCase()))));
expected.push(NaN);
actual.push(_.toNumber(Object(wrapWS(item.string.toUpperCase()))));
expected.push(item.value);
actual.push(_.toNumber(Object(wrapWS(posStr(item.string.toUpperCase())))));
expected.push(NaN);
actual.push(_.toNumber(Object(wrapWS(negStr(item.string.toUpperCase())))));
expected.push(NaN);
assert.deepEqual(actual, expected);
});
});
var toNumberInvalidAdvanceStrings = ['0b', '0o', '0x', '0b1010102', '0o123458', '0x1a2b3x'];
lodashStable.each(toNumberInvalidAdvanceStrings, function (string) {
QUnit.test('invalid binary, octal and hex string literals and objects should be `NaN`', function(assert) {
assert.expect(1);
var actual = [];
actual.push(_.toNumber(string));
actual.push(_.toNumber(posStr(string)));
actual.push(_.toNumber(negStr(string)));
actual.push(_.toNumber(wrapWS(string)));
actual.push(_.toNumber(wrapWS(posStr(string))));
actual.push(_.toNumber(wrapWS(negStr(string))));
actual.push(_.toNumber(Object(string)));
actual.push(_.toNumber(Object(posStr(string))));
actual.push(_.toNumber(Object(negStr(string))));
actual.push(_.toNumber(Object(wrapWS(string))));
actual.push(_.toNumber(Object(wrapWS(posStr(string)))));
actual.push(_.toNumber(Object(wrapWS(negStr(string)))));
var expected = _.fill(Array(actual.length), NaN);
assert.deepEqual(actual, expected);
});
});
QUnit.test('should convert boolean literals and objects', function(assert) {
assert.expect(1); assert.expect(1);
var actual = [], var values = falsey.concat(whitespace),
expected = []; expected = lodashStable.map(values, Number);
actual.push(1 / _.toNumber(false)); var actual = lodashStable.map(values, function(value, index) {
expected.push(Infinity); return index ? _.toNumber(value) : _.toNumber();
actual.push(_.toNumber(true)); });
expected.push(1);
actual.push(1 / _.toNumber(new Boolean(false)));
expected.push(Infinity);
actual.push(_.toNumber(new Boolean(true)));
expected.push(1);
actual.push(_.toNumber('false'));
expected.push(NaN);
actual.push(_.toNumber('true'));
expected.push(NaN);
assert.deepEqual(actual, expected); assert.deepEqual(actual, expected);
}); });
QUnit.test('should convert dates', function(assert) { QUnit.test('should preserve sign of `0`', function(assert) {
assert.expect(1); assert.expect(1);
var now = new Date(), var values = [0, '0', -0, '-0'],
actual = [_.toNumber(now), _.toNumber(new Date(MAX_INTEGER))], expected = [[0, Infinity], [0, Infinity], [-0, -Infinity], [-0, -Infinity]];
expected = [now.getTime(), NaN];
var actual = lodashStable.map(values, function(value) {
var result = _.toNumber(value);
return [result, 1 / result];
});
assert.deepEqual(actual, expected); assert.deepEqual(actual, expected);
}); });
QUnit.test('should convert RegExp literals and objects', function(assert) { QUnit.test('should convert number primitives and objects to numbers', function(assert) {
assert.expect(1); assert.expect(1);
var actual = [_.toNumber(/abc/i), _.toNumber(new RegExp('abc', 'i'))], var values = [2, 1.2, MAX_SAFE_INTEGER, MAX_INTEGER, Infinity, NaN];
expected = [NaN, NaN];
var expected = lodashStable.map(values, function(value) {
return [value, value, -value, -value];
});
var actual = lodashStable.map(values, function(value) {
return lodashStable.flattenDeep(
lodashStable.times(2, function(index) {
var other = index ? -value : value;
return [
_.toNumber(other),
_.toNumber(Object(other))
];
})
)
});
assert.deepEqual(actual, expected); assert.deepEqual(actual, expected);
}) });
QUnit.test('other objects', function(assert) { QUnit.test('should convert string primitives and objects to numbers', function(assert) {
assert.expect(1); assert.expect(1);
var actual = [], var values = [
expected = []; '10', '1.234567890', (MAX_SAFE_INTEGER + ''),
'1e+308', '1e308', '1E+308', '1E308',
'5e-324', '5E-324',
'Infinity', 'NaN'
];
actual.push(_.toNumber({})); var expected = lodashStable.map(values, function(value) {
expected.push(NaN); var n = +value;
actual.push(_.toNumber({ return [n, n, n, n, n, n, -n, -n];
valueOf: '1.1' });
}));
expected.push(NaN); var actual = lodashStable.map(values, function(value) {
actual.push(_.toNumber({ return lodashStable.flattenDeep(
valueOf: '1.1', lodashStable.map([identity, pad, positive, negative], function(func) {
toString: function () { return [
return '2.2'; _.toNumber(func(value)),
} _.toNumber(Object(func(value)))
})); ];
expected.push(2.2); })
actual.push(_.toNumber({ );
valueOf: function () { });
return '1.1';
}, assert.deepEqual(actual, expected);
toString: '2.2' });
}));
expected.push(1.1); QUnit.test('should convert binary and octal strings to numbers', function(assert) {
actual.push(_.toNumber({ assert.expect(1);
valueOf: function () {
return '1.1'; var numbers = [42, 5349, 1715004],
}, transforms = [identity, pad],
toString: function () { values = ['0b101010', '0o12345', '0x1a2b3c'];
return '2.2';
}, var expected = lodashStable.map(numbers, function(n) {
})); return lodashStable.times(8, lodashStable.constant(n));
expected.push(1.1); });
actual.push(_.toNumber({
valueOf: function () { var actual = lodashStable.map(values, function(value) {
return '-0x1a2b3c'; return lodashStable.flattenDeep(
} lodashStable.times(2, function(index) {
})); var other = index ? value.toUpperCase() : value;
expected.push(NaN); return lodashStable.map(transforms, function(mod) {
actual.push(_.toNumber({ return [
toString: function () { _.toNumber(mod(other)),
return '-0x1a2b3c'; _.toNumber(Object(mod(other)))
}, ];
})); })
expected.push(NaN); })
actual.push(_.toNumber({ );
valueOf: function () { });
return '0o12345';
} assert.deepEqual(actual, expected);
})); });
expected.push(5349);
actual.push(_.toNumber({ QUnit.test('should convert invalid binary and octal strings to `NaN`', function(assert) {
toString: function () { assert.expect(1);
return '0o12345';
}, var transforms = [identity, pad, positive, negative],
})); values = ['0b', '0o', '0x', '0b1010102', '0o123458', '0x1a2b3x'];
expected.push(5349);
actual.push(_.toNumber({ var expected = lodashStable.map(values, function(n) {
valueOf: function () { return lodashStable.times(16, lodashStable.constant(NaN));
return '0b101010'; });
}
})); var actual = lodashStable.map(values, function(value) {
expected.push(42); return lodashStable.flattenDeep(
actual.push(_.toNumber({ lodashStable.times(2, function(index) {
toString: function () { var other = index ? value.toUpperCase() : value;
return '0b101010'; return lodashStable.map(transforms, function(mod) {
}, return [
})); _.toNumber(mod(value)),
expected.push(42); _.toNumber(Object(mod(value)))
actual.push(1 / _.toNumber([])); ];
expected.push(Infinity); })
actual.push(_.toNumber([1])); })
expected.push(1); );
actual.push(_.toNumber([1, 2])); });
expected.push(NaN);
assert.deepEqual(actual, expected);
});
QUnit.test('should coerce objects to numbers', function(assert) {
assert.expect(1);
var values = [
{},
[],
[1],
[1, 2],
{ 'valueOf': '1.1' },
{ 'valueOf': '1.1', 'toString': lodashStable.constant('2.2') },
{ 'valueOf': lodashStable.constant('1.1'), 'toString': '2.2' },
{ 'valueOf': lodashStable.constant('1.1'), 'toString': lodashStable.constant('2.2') },
{ 'valueOf': lodashStable.constant('-0x1a2b3c') },
{ 'toString': lodashStable.constant('-0x1a2b3c') },
{ 'valueOf': lodashStable.constant('0o12345') },
{ 'toString': lodashStable.constant('0o12345') },
{ 'valueOf': lodashStable.constant('0b101010') },
{ 'toString': lodashStable.constant('0b101010') }
];
var expected = [
NaN, 0, 1, NaN,
NaN, 2.2, 1.1, 1.1,
NaN, NaN,
5349, 5349,
42, 42
];
var actual = lodashStable.map(values, function(value) {
return _.toNumber(value);
});
assert.deepEqual(actual, expected); assert.deepEqual(actual, expected);
}); });