summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/to-number-basic.js
blob: 4509537839433be75765334cd0c7da77ff24bbaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
test("non-numeric primitives", () => {
    expect(+false).toBe(0);
    expect(-false).toBe(-0);
    expect(+true).toBe(1);
    expect(-true).toBe(-1);
    expect(+null).toBe(0);
    expect(-null).toBe(-0);
    expect(+undefined).toBeNaN();
    expect(-undefined).toBeNaN();
});

test("arrays", () => {
    expect(+[]).toBe(0);
    expect(-[]).toBe(-0);
    expect(+[,]).toBe(0);
    expect(-[,]).toBe(-0);
    expect(+[null]).toBe(0);
    expect(-[null]).toBe(-0);
    expect(+[undefined]).toBe(0);
    expect(-[undefined]).toBe(-0);
    expect(+[[[[[]]]]]).toBe(0);
    expect(-[[[[[]]]]]).toBe(-0);
    expect(+[[[[[42]]]]]).toBe(42);
    expect(-[[[[[42]]]]]).toBe(-42);

    expect(+[, , ,]).toBeNaN();
    expect(-[, , ,]).toBeNaN();
    expect(+[undefined, undefined]).toBeNaN();
    expect(-[undefined, undefined]).toBeNaN();
    expect(+[1, 2, 3]).toBeNaN();
    expect(-[1, 2, 3]).toBeNaN();
    expect(+[[[["foo"]]]]).toBeNaN();
    expect(-[[[["foo"]]]]).toBeNaN();
});

test("strings", () => {
    expect(+"").toBe(0);
    expect(-"").toBe(-0);
    expect(+"42").toBe(42);
    expect(-"42").toBe(-42);
    expect(+"1.23").toBe(1.23);
    expect(-"1.23").toBe(-1.23);

    expect(+"foo").toBeNaN();
    expect(-"foo").toBeNaN();
});

test("numbers", () => {
    expect(+42).toBe(42);
    expect(-42).toBe(-42);
    expect(+1.23).toBe(1.23);
    expect(-1.23).toBe(-1.23);
});

test("infinity", () => {
    expect(+"Infinity").toBe(Infinity);
    expect(+"+Infinity").toBe(Infinity);
    expect(+"-Infinity").toBe(-Infinity);
    expect(-"Infinity").toBe(-Infinity);
    expect(-"+Infinity").toBe(-Infinity);
    expect(-"-Infinity").toBe(Infinity);
});

test("space and space-like escapes", () => {
    expect(+"  \r  \t \n ").toBe(0);
    expect(+"  \n  \t    Infinity   \r   ").toBe(Infinity);
    expect(+"\r     \n1.23   \t\t\t  \n").toBe(1.23);
});

test("object literals", () => {
    expect(+{}).toBeNaN();
    expect(-{}).toBeNaN();
    expect(+{ a: 1 }).toBeNaN();
    expect(-{ a: 1 }).toBeNaN();
});