s-global.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. describe('global methods', function () {
  2. 'use strict';
  3. var foo = function foo() {};
  4. var functionsHaveNames = foo.name === 'foo';
  5. var ifFunctionsHaveNamesIt = functionsHaveNames ? it : xit;
  6. var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol';
  7. var ifSymbolsIt = hasSymbols ? it : xit;
  8. var is = function (x, y) {
  9. if (x === 0 && y === 0) {
  10. return 1 / x === 1 / y;
  11. }
  12. return x === y;
  13. };
  14. describe('parseInt', function () {
  15. /* eslint-disable radix */
  16. ifFunctionsHaveNamesIt('has the right name', function () {
  17. expect(parseInt.name).toBe('parseInt');
  18. });
  19. it('accepts a radix', function () {
  20. for (var i = 2; i <= 36; ++i) {
  21. expect(parseInt('10', i)).toBe(i);
  22. }
  23. });
  24. it('defaults the radix to 10 when the number does not start with 0x or 0X', function () {
  25. [
  26. '01',
  27. '08',
  28. '10',
  29. '42'
  30. ].forEach(function (str) {
  31. expect(parseInt(str)).toBe(parseInt(str, 10));
  32. });
  33. });
  34. it('defaults the radix to 16 when the number starts with 0x or 0X', function () {
  35. expect(parseInt('0x16')).toBe(parseInt('0x16', 16));
  36. expect(parseInt('0X16')).toBe(parseInt('0X16', 16));
  37. });
  38. it('ignores leading whitespace', function () {
  39. expect(parseInt(' 0x16')).toBe(parseInt('0x16', 16));
  40. expect(parseInt(' 42')).toBe(parseInt('42', 10));
  41. expect(parseInt(' 08')).toBe(parseInt('08', 10));
  42. var mvsIsWS = (/\s/).test('\u180E');
  43. if (mvsIsWS) {
  44. expect(parseInt('\u180E' + 1)).toBe(1);
  45. } else {
  46. expect(parseInt('\u180E' + 1)).toBeNaN();
  47. }
  48. var ws = '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF'
  49. .replace(/\S/g, ''); // remove the mongolian vowel separator (/u180E) on modern engines
  50. expect(parseInt(ws + '08')).toBe(parseInt('08', 10));
  51. expect(parseInt(ws + '0x16')).toBe(parseInt('0x16', 16));
  52. });
  53. it('defaults the radix properly when not a true number', function () {
  54. var fakeZero = { valueOf: function () { return 0; } };
  55. expect(parseInt('08', fakeZero)).toBe(parseInt('08', 10));
  56. expect(parseInt('0x16', fakeZero)).toBe(parseInt('0x16', 16));
  57. });
  58. it('allows sign-prefixed hex values', function () {
  59. expect(parseInt('-0xF')).toBe(-15);
  60. expect(parseInt('-0xF', 16)).toBe(-15);
  61. expect(parseInt('+0xF')).toBe(15);
  62. expect(parseInt('+0xF', 16)).toBe(15);
  63. });
  64. it('NaN parsing', function () {
  65. expect(parseInt()).toBeNaN();
  66. expect(parseInt(undefined)).toBeNaN();
  67. expect(parseInt(null)).toBeNaN();
  68. expect(parseInt(NaN)).toBeNaN();
  69. });
  70. ifSymbolsIt('throws on symbols', function () {
  71. expect(function () { parseInt(Symbol('')); }).toThrow();
  72. expect(function () { parseInt(Object(Symbol(''))); }).toThrow();
  73. });
  74. /* eslint-enable radix */
  75. });
  76. describe('parseFloat()', function () {
  77. it('works with zeroes', function () {
  78. expect(is(parseFloat('0'), 0) ? '+0' : '-0').toBe('+0');
  79. expect(is(parseFloat(' 0'), 0) ? '+0' : '-0').toBe('+0');
  80. expect(is(parseFloat('+0'), 0) ? '+0' : '-0').toBe('+0');
  81. expect(is(parseFloat(' +0'), 0) ? '+0' : '-0').toBe('+0');
  82. expect(is(parseFloat('-0'), -0) ? '-0' : '+0').toBe('-0');
  83. expect(is(parseFloat(' -0'), -0) ? '-0' : '+0').toBe('-0');
  84. });
  85. it('NaN parsing', function () {
  86. expect(parseFloat(undefined)).toBeNaN();
  87. expect(parseFloat(null)).toBeNaN();
  88. expect(parseFloat(NaN)).toBeNaN();
  89. });
  90. });
  91. });