summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/builtins/functions/parseFloat.js
blob: 20ed2633dd05f739b4252e3307ed014e75405eab (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
test("parsing numbers", () => {
    [
        [0, 0],
        [1, 1],
        [0.23, 0.23],
        [1.23, 1.23],
        [0.0123e2, 1.23],
        [1.23e4, 12300],
        [Infinity, Infinity],
    ].forEach(test => {
        expect(parseFloat(test[0])).toBe(test[1]);
        expect(parseFloat(+test[0])).toBe(test[1]);
        expect(parseFloat(-test[0])).toBe(-test[1]);
    });
});

test("parsing strings", () => {
    [
        ["0", 0],
        ["1", 1],
        [".23", 0.23],
        ["1.23", 1.23],
        ["0.0123E+2", 1.23],
        ["1.23e4", 12300],
        ["Infinity", Infinity],
    ].forEach(test => {
        expect(parseFloat(test[0])).toBe(test[1]);
        expect(parseFloat(`+${test[0]}`)).toBe(test[1]);
        expect(parseFloat(`-${test[0]}`)).toBe(-test[1]);
        expect(parseFloat(`${test[0]}foo`)).toBe(test[1]);
        expect(parseFloat(`+${test[0]}foo`)).toBe(test[1]);
        expect(parseFloat(`-${test[0]}foo`)).toBe(-test[1]);
        expect(parseFloat(`   \n  \t ${test[0]} \v  foo   `)).toBe(test[1]);
        expect(parseFloat(`   \r -${test[0]} \f \n\n  foo   `)).toBe(-test[1]);
        expect(parseFloat({ toString: () => test[0] })).toBe(test[1]);
    });
});

test("parsing NaN", () => {
    [
        "",
        [],
        [],
        true,
        false,
        null,
        undefined,
        NaN,
        "foo123",
        "foo+123",
        "fooInfinity",
        "foo+Infinity",
    ].forEach(value => {
        expect(parseFloat(value)).toBeNaN();
    });

    expect(parseFloat()).toBeNaN();
    expect(parseFloat("", 123, Infinity)).toBeNaN();
});