Version 1
[yaffs-website] / node_modules / es5-shim / tests / spec / s-global.js
1 /* global describe, it, xit, expect */
2
3 describe('global methods', function () {
4     'use strict';
5
6     var foo = function foo() {};
7     var functionsHaveNames = foo.name === 'foo';
8     var ifFunctionsHaveNamesIt = functionsHaveNames ? it : xit;
9
10     var is = function (x, y) {
11         if (x === 0 && y === 0) {
12             return 1 / x === 1 / y;
13         }
14         return x === y;
15     };
16
17     describe('parseInt', function () {
18         /* eslint-disable radix */
19
20         ifFunctionsHaveNamesIt('has the right name', function () {
21             expect(parseInt.name).toBe('parseInt');
22         });
23
24         it('accepts a radix', function () {
25             for (var i = 2; i <= 36; ++i) {
26                 expect(parseInt('10', i)).toBe(i);
27             }
28         });
29
30         it('defaults the radix to 10 when the number does not start with 0x or 0X', function () {
31             [
32                 '01',
33                 '08',
34                 '10',
35                 '42'
36             ].forEach(function (str) {
37                 expect(parseInt(str)).toBe(parseInt(str, 10));
38             });
39         });
40
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));
44         });
45
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));
50
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' +
53                 '\u2029\uFEFF';
54             expect(parseInt(ws + '08')).toBe(parseInt('08', 10));
55             expect(parseInt(ws + '0x16')).toBe(parseInt('0x16', 16));
56         });
57
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));
62         });
63
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);
69         });
70
71         it('NaN parsing', function () {
72             expect(parseInt(undefined)).toBeNaN();
73             expect(parseInt(null)).toBeNaN();
74             expect(parseInt(NaN)).toBeNaN();
75         });
76         /* eslint-enable radix */
77     });
78
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');
87         });
88
89         it('NaN parsing', function () {
90             expect(parseFloat(undefined)).toBeNaN();
91             expect(parseFloat(null)).toBeNaN();
92             expect(parseFloat(NaN)).toBeNaN();
93         });
94     });
95 });