1 /* global describe, it, xit, expect */
3 describe('global methods', function () {
6 var foo = function foo() {};
7 var functionsHaveNames = foo.name === 'foo';
8 var ifFunctionsHaveNamesIt = functionsHaveNames ? it : xit;
10 var is = function (x, y) {
11 if (x === 0 && y === 0) {
12 return 1 / x === 1 / y;
17 describe('parseInt', function () {
18 /* eslint-disable radix */
20 ifFunctionsHaveNamesIt('has the right name', function () {
21 expect(parseInt.name).toBe('parseInt');
24 it('accepts a radix', function () {
25 for (var i = 2; i <= 36; ++i) {
26 expect(parseInt('10', i)).toBe(i);
30 it('defaults the radix to 10 when the number does not start with 0x or 0X', function () {
36 ].forEach(function (str) {
37 expect(parseInt(str)).toBe(parseInt(str, 10));
41 it('defaults the radix to 16 when the number starts with 0x or 0X', function () {
42 expect(parseInt('0x16')).toBe(parseInt('0x16', 16));
43 expect(parseInt('0X16')).toBe(parseInt('0X16', 16));
46 it('ignores leading whitespace', function () {
47 expect(parseInt(' 0x16')).toBe(parseInt('0x16', 16));
48 expect(parseInt(' 42')).toBe(parseInt('42', 10));
49 expect(parseInt(' 08')).toBe(parseInt('08', 10));
51 var ws = '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003' +
52 '\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028' +
54 expect(parseInt(ws + '08')).toBe(parseInt('08', 10));
55 expect(parseInt(ws + '0x16')).toBe(parseInt('0x16', 16));
58 it('defaults the radix properly when not a true number', function () {
59 var fakeZero = { valueOf: function () { return 0; } };
60 expect(parseInt('08', fakeZero)).toBe(parseInt('08', 10));
61 expect(parseInt('0x16', fakeZero)).toBe(parseInt('0x16', 16));
64 it('allows sign-prefixed hex values', function () {
65 expect(parseInt('-0xF')).toBe(-15);
66 expect(parseInt('-0xF', 16)).toBe(-15);
67 expect(parseInt('+0xF')).toBe(15);
68 expect(parseInt('+0xF', 16)).toBe(15);
71 it('NaN parsing', function () {
72 expect(parseInt(undefined)).toBeNaN();
73 expect(parseInt(null)).toBeNaN();
74 expect(parseInt(NaN)).toBeNaN();
76 /* eslint-enable radix */
79 describe('parseFloat()', function () {
80 it('works with zeroes', function () {
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 expect(is(parseFloat(' +0'), 0) ? '+0' : '-0').toBe('+0');
85 expect(is(parseFloat('-0'), -0) ? '-0' : '+0').toBe('-0');
86 expect(is(parseFloat(' -0'), -0) ? '-0' : '+0').toBe('-0');
89 it('NaN parsing', function () {
90 expect(parseFloat(undefined)).toBeNaN();
91 expect(parseFloat(null)).toBeNaN();
92 expect(parseFloat(NaN)).toBeNaN();