summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/functions/arrow-functions.js
blob: 6f9afc632de08fc6af0b66510e0c4b5db028f925 (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
113
114
115
116
117
118
119
120
121
122
load("test-common.js");

try {
  let getNumber = () => 42;
  assert(getNumber() === 42);

  getNumber = () => 99;
  assert(getNumber() === 99);

  let add = (a, b) => a + b;
  assert(add(2, 3) === 5);

  const addBlock = (a, b) => {
    let res = a + b;
    return res;
  };
  assert(addBlock(5, 4) === 9);

  let chompy = [x => x, 2];
  assert(chompy.length === 2);
  assert(chompy[0](1) === 1);

  const makeObject = (a, b) => ({ a, b });
  const obj = makeObject(33, 44);
  assert(typeof obj === "object");
  assert(obj.a === 33);
  assert(obj.b === 44);

  let returnUndefined = () => {};
  assert(typeof returnUndefined() === "undefined");

  const makeArray = (a, b) => [a, b];
  const array = makeArray("3", { foo: 4 });
  assert(array[0] === "3");
  assert(array[1].foo === 4);

  let square = x => x * x;
  assert(square(3) === 9);

  let squareBlock = x => {
    return x * x;
  };
  assert(squareBlock(4) === 16);

  const message = (who => "Hello " + who)("friends!");
  assert(message === "Hello friends!");

  const sum = ((x, y, z) => x + y + z)(1, 2, 3);
  assert(sum === 6);

  const product = ((x, y, z) => {
    let res = x * y * z;
    return res;
  })(5, 4, 2);
  assert(product === 40);

  const half = (x => {
    return x / 2;
  })(10);
  assert(half === 5);

  var foo, bar;
  (foo = bar), baz => {};
  assert(foo === undefined);
  assert(bar === undefined);

  function FooBar() {
    this.x = {
      y: () => this,
      z: function () {
        return (() => this)();
      },
    };
  }

  var foobar = new FooBar();
  assert(foobar.x.y() === foobar);
  assert(foobar.x.z() === foobar.x);

  var Baz = () => {};

  assert(Baz.prototype === undefined);

  assertThrowsError(
    () => {
      new Baz();
    },
    {
      error: TypeError,
      message: "Baz is not a constructor",
    }
  );

  (() => {
    "use strict";
    assert(isStrictMode());

    (() => {
      assert(isStrictMode());
    })();
  })();

  (() => {
    "use strict";
    assert(isStrictMode());
  })();

  (() => {
    assert(!isStrictMode());

    (() => {
      "use strict";
      assert(isStrictMode());
    })();

    assert(!isStrictMode());
  })();

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