From ae76c3ee718d8a33405291c1c0e093c7f7e94a6d Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 1 Nov 2015 13:43:38 -0800 Subject: [PATCH] Cleanup `_.toNumber` test. --- test/test.js | 438 ++++++++++++++++++--------------------------------- 1 file changed, 150 insertions(+), 288 deletions(-) diff --git a/test/test.js b/test/test.js index 3f43144fc..03f8e810d 100644 --- a/test/test.js +++ b/test/test.js @@ -19376,322 +19376,184 @@ QUnit.module('lodash.toNumber'); (function() { - QUnit.test('null, empty strings and undefined should convert to `0` and `NaN`', function(assert) { - 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) { + function negative(string) { return '-' + string; } - function posStr(string) { - return '+' + string; - } - - function wrapWS(string) { + function pad(string) { return whitespace + string + whitespace; } - var toNumberBasicStrings = ['10', '1.234567890', '9007199254740991', '1e+308', '1e308', '1E+308', '1E308', '5e-324', '5E-324', 'Infinity', 'NaN']; - lodashStable.each(toNumberNumbers, function (string) { - QUnit.test('should convert basic string literals and objects accurately', function(assert) { - assert.expect(1); + function positive(string) { + return '+' + string; + } - var actual = [], - 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) { + QUnit.test('should convert empty values to `0` or `NaN`', function(assert) { assert.expect(1); - var actual = [], - expected = []; + var values = falsey.concat(whitespace), + expected = lodashStable.map(values, Number); - actual.push(1 / _.toNumber(false)); - expected.push(Infinity); - 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); + var actual = lodashStable.map(values, function(value, index) { + return index ? _.toNumber(value) : _.toNumber(); + }); assert.deepEqual(actual, expected); }); - QUnit.test('should convert dates', function(assert) { + QUnit.test('should preserve sign of `0`', function(assert) { assert.expect(1); - var now = new Date(), - actual = [_.toNumber(now), _.toNumber(new Date(MAX_INTEGER))], - expected = [now.getTime(), NaN]; + var values = [0, '0', -0, '-0'], + expected = [[0, Infinity], [0, Infinity], [-0, -Infinity], [-0, -Infinity]]; + + var actual = lodashStable.map(values, function(value) { + var result = _.toNumber(value); + return [result, 1 / result]; + }); 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); - var actual = [_.toNumber(/abc/i), _.toNumber(new RegExp('abc', 'i'))], - expected = [NaN, NaN]; + var values = [2, 1.2, MAX_SAFE_INTEGER, MAX_INTEGER, Infinity, 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); - }) + }); - QUnit.test('other objects', function(assert) { + QUnit.test('should convert string primitives and objects to numbers', function(assert) { assert.expect(1); - var actual = [], - expected = []; + var values = [ + '10', '1.234567890', (MAX_SAFE_INTEGER + ''), + '1e+308', '1e308', '1E+308', '1E308', + '5e-324', '5E-324', + 'Infinity', 'NaN' + ]; - actual.push(_.toNumber({})); - expected.push(NaN); - actual.push(_.toNumber({ - valueOf: '1.1' - })); - expected.push(NaN); - actual.push(_.toNumber({ - valueOf: '1.1', - toString: function () { - return '2.2'; - } - })); - expected.push(2.2); - actual.push(_.toNumber({ - valueOf: function () { - return '1.1'; - }, - toString: '2.2' - })); - expected.push(1.1); - actual.push(_.toNumber({ - valueOf: function () { - return '1.1'; - }, - toString: function () { - return '2.2'; - }, - })); - expected.push(1.1); - actual.push(_.toNumber({ - valueOf: function () { - return '-0x1a2b3c'; - } - })); - expected.push(NaN); - actual.push(_.toNumber({ - toString: function () { - return '-0x1a2b3c'; - }, - })); - expected.push(NaN); - actual.push(_.toNumber({ - valueOf: function () { - return '0o12345'; - } - })); - expected.push(5349); - actual.push(_.toNumber({ - toString: function () { - return '0o12345'; - }, - })); - expected.push(5349); - actual.push(_.toNumber({ - valueOf: function () { - return '0b101010'; - } - })); - expected.push(42); - actual.push(_.toNumber({ - toString: function () { - return '0b101010'; - }, - })); - expected.push(42); - actual.push(1 / _.toNumber([])); - expected.push(Infinity); - actual.push(_.toNumber([1])); - expected.push(1); - actual.push(_.toNumber([1, 2])); - expected.push(NaN); + var expected = lodashStable.map(values, function(value) { + var n = +value; + return [n, n, n, n, n, n, -n, -n]; + }); + + var actual = lodashStable.map(values, function(value) { + return lodashStable.flattenDeep( + lodashStable.map([identity, pad, positive, negative], function(func) { + return [ + _.toNumber(func(value)), + _.toNumber(Object(func(value))) + ]; + }) + ); + }); + + assert.deepEqual(actual, expected); + }); + + QUnit.test('should convert binary and octal strings to numbers', function(assert) { + assert.expect(1); + + var numbers = [42, 5349, 1715004], + transforms = [identity, pad], + values = ['0b101010', '0o12345', '0x1a2b3c']; + + var expected = lodashStable.map(numbers, function(n) { + return lodashStable.times(8, lodashStable.constant(n)); + }); + + var actual = lodashStable.map(values, function(value) { + return lodashStable.flattenDeep( + lodashStable.times(2, function(index) { + var other = index ? value.toUpperCase() : value; + return lodashStable.map(transforms, function(mod) { + return [ + _.toNumber(mod(other)), + _.toNumber(Object(mod(other))) + ]; + }) + }) + ); + }); + + assert.deepEqual(actual, expected); + }); + + QUnit.test('should convert invalid binary and octal strings to `NaN`', function(assert) { + assert.expect(1); + + var transforms = [identity, pad, positive, negative], + values = ['0b', '0o', '0x', '0b1010102', '0o123458', '0x1a2b3x']; + + var expected = lodashStable.map(values, function(n) { + return lodashStable.times(16, lodashStable.constant(NaN)); + }); + + var actual = lodashStable.map(values, function(value) { + return lodashStable.flattenDeep( + lodashStable.times(2, function(index) { + var other = index ? value.toUpperCase() : value; + return lodashStable.map(transforms, function(mod) { + return [ + _.toNumber(mod(value)), + _.toNumber(Object(mod(value))) + ]; + }) + }) + ); + }); + + 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); });