summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/function-default-parameters.js
blob: ab537928b0df204ba702eae08e700f50c2ae9efc (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
load("test-common.js");

try {
    function func1(a, b = 1) {
        return a + b;
    }

    const arrowFunc1 = (a, b = 1) => a + b;

    assert(func1(4, 5) === 9);
    assert(func1(4) === 5);
    assert(func1(4, undefined) === 5);
    assert(Number.isNaN(func1()));

    assert(arrowFunc1(4, 5) === 9);
    assert(arrowFunc1(4) === 5);
    assert(arrowFunc1(4, undefined) === 5);
    assert(Number.isNaN(arrowFunc1()));

    function func2(a = 6) {
        return typeof a;
    }

    const arrowFunc2 = (a = 6) => typeof a;

    assert(func2() === "number");
    assert(func2(5) === "number");
    assert(func2(undefined) === "number");
    assert(func2(false) === "boolean");
    assert(func2(null) === "object");
    assert(func2({}) === "object");

    assert(arrowFunc2() === "number");
    assert(arrowFunc2(5) === "number");
    assert(arrowFunc2(undefined) === "number");
    assert(arrowFunc2(false) === "boolean");
    assert(arrowFunc2(null) === "object");
    assert(arrowFunc2({}) === "object");

    function func3(a = 5, b) {
        return a + b;
    }

    const arrowFunc3 = (a = 5, b) => a + b;

    assert(func3(4, 5) === 9);
    assert(func3(undefined, 4) === 9);
    assert(Number.isNaN(func3()));

    assert(arrowFunc3(4, 5) === 9);
    assert(arrowFunc3(undefined, 4) === 9);
    assert(Number.isNaN(arrowFunc3()));

    function func4(a, b = a) {
        return a + b;
    }

    const arrowFunc4 = (a, b = a) => a + b;

    assert(func4(4, 5) === 9);
    assert(func4(4) === 8);
    assert(func4("hf") === "hfhf");
    assert(func4(true) === 2);
    assert(Number.isNaN(func4()));

    assert(arrowFunc4(4, 5) === 9);
    assert(arrowFunc4(4) === 8);
    assert(arrowFunc4("hf") === "hfhf");
    assert(arrowFunc4(true) === 2);
    assert(Number.isNaN(arrowFunc4()));

    function func5(a = function() { return 5; }) { 
      return a();
    }

    const arrowFunc5 = (a = function() { return 5; }) => a();

    assert(func5() === 5);
    assert(func5(function() { return 10; }) === 10);
    assert(func5(() => 10) === 10);
    assert(arrowFunc5() === 5);
    assert(arrowFunc5(function() { return 10; }) === 10);
    assert(arrowFunc5(() => 10) === 10);

    function func6(a = () => 5) {
      return a();
    }

    const arrowFunc6 = (a = () => 5) => a();
    
    assert(func6() === 5);
    assert(func6(function() { return 10; }) === 10);
    assert(func6(() => 10) === 10);
    assert(arrowFunc6() === 5);
    assert(arrowFunc6(function() { return 10; }) === 10);
    assert(arrowFunc6(() => 10) === 10);

    function func7(a = { foo: "bar" }) {
      return a.foo;
    }

    const arrowFunc7 = (a = { foo: "bar" }) => a.foo

    assert(func7() === "bar");
    assert(func7({ foo: "baz" }) === "baz");
    assert(arrowFunc7() === "bar");
    assert(arrowFunc7({ foo: "baz" }) === "baz");

    console.log("PASS");
} catch (e) {
    console.log("FAIL: " + e);
}