summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/functions/arrow-functions.js
diff options
context:
space:
mode:
Diffstat (limited to 'Libraries/LibJS/Tests/functions/arrow-functions.js')
-rw-r--r--Libraries/LibJS/Tests/functions/arrow-functions.js134
1 files changed, 81 insertions, 53 deletions
diff --git a/Libraries/LibJS/Tests/functions/arrow-functions.js b/Libraries/LibJS/Tests/functions/arrow-functions.js
index 6f9afc632d..3b3ab09ed1 100644
--- a/Libraries/LibJS/Tests/functions/arrow-functions.js
+++ b/Libraries/LibJS/Tests/functions/arrow-functions.js
@@ -1,69 +1,101 @@
-load("test-common.js");
+test("no arguments", () => {
+ let getNumber = () => {
+ return 42;
+ };
+ expect(getNumber()).toBe(42);
-try {
- let getNumber = () => 42;
- assert(getNumber() === 42);
+ getNumber = () => 42;
+ expect(getNumber()).toBe(42);
+ getNumber = () => {
+ return 99;
+ };
+ expect(getNumber()).toBe(99);
getNumber = () => 99;
- assert(getNumber() === 99);
+ expect(getNumber()).toBe(99);
+});
+test("arguments", () => {
let add = (a, b) => a + b;
- assert(add(2, 3) === 5);
+ expect(add(2, 3)).toBe(5);
const addBlock = (a, b) => {
let res = a + b;
return res;
};
- assert(addBlock(5, 4) === 9);
+ expect(addBlock(5, 4)).toBe(9);
+});
+test("inside an array", () => {
let chompy = [x => x, 2];
- assert(chompy.length === 2);
- assert(chompy[0](1) === 1);
+ expect(chompy).toHaveLength(2);
+ expect(chompy[0](1)).toBe(1);
+});
+test("return object literal", () => {
const makeObject = (a, b) => ({ a, b });
const obj = makeObject(33, 44);
- assert(typeof obj === "object");
- assert(obj.a === 33);
- assert(obj.b === 44);
+ expect(typeof obj).toBe("object");
+ expect(obj.a).toBe(33);
+ expect(obj.b).toBe(44);
+});
+test("return undefined", () => {
let returnUndefined = () => {};
- assert(typeof returnUndefined() === "undefined");
+ expect(returnUndefined()).toBeUndefined();
+});
+test("return array literal", () => {
const makeArray = (a, b) => [a, b];
const array = makeArray("3", { foo: 4 });
- assert(array[0] === "3");
- assert(array[1].foo === 4);
+ expect(array[0]).toBe("3");
+ expect(array[1].foo).toBe(4);
+});
+test("return numeric expression", () => {
let square = x => x * x;
- assert(square(3) === 9);
+ expect(square(3)).toBe(9);
let squareBlock = x => {
return x * x;
};
- assert(squareBlock(4) === 16);
+ expect(squareBlock(4)).toBe(16);
+});
+test("return called arrow function expression", () => {
const message = (who => "Hello " + who)("friends!");
- assert(message === "Hello friends!");
+ expect(message).toBe("Hello friends!");
const sum = ((x, y, z) => x + y + z)(1, 2, 3);
- assert(sum === 6);
+ expect(sum).toBe(6);
const product = ((x, y, z) => {
let res = x * y * z;
return res;
})(5, 4, 2);
- assert(product === 40);
+ expect(product).toBe(40);
const half = (x => {
return x / 2;
})(10);
- assert(half === 5);
-
- var foo, bar;
+ expect(half).toBe(5);
+});
+
+test("currying", () => {
+ let add = a => b => a + b;
+ expect(typeof add(1)).toBe("function");
+ expect(typeof add(1, 2)).toBe("function");
+ expect(add(1)(2)).toBe(3);
+});
+
+test("with comma operator", () => {
+ let foo, bar;
(foo = bar), baz => {};
- assert(foo === undefined);
- assert(bar === undefined);
+ expect(foo).toBe(undefined);
+ expect(bar).toBe(undefined);
+});
+test("arrow functions in objects", () => {
function FooBar() {
this.x = {
y: () => this,
@@ -73,50 +105,46 @@ try {
};
}
- 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",
- }
- );
+ const foobar = new FooBar();
+ expect(foobar.x.y()).toBe(foobar);
+ expect(foobar.x.z()).toBe(foobar.x);
+});
+test("strict mode propogation", () => {
(() => {
"use strict";
- assert(isStrictMode());
+ expect(isStrictMode()).toBeTrue();
(() => {
- assert(isStrictMode());
+ expect(isStrictMode()).toBeTrue();
})();
})();
(() => {
"use strict";
- assert(isStrictMode());
+ expect(isStrictMode()).toBeTrue();
})();
(() => {
- assert(!isStrictMode());
+ expect(isStrictMode()).toBeFalse();
(() => {
"use strict";
- assert(isStrictMode());
+ expect(isStrictMode()).toBeTrue();
})();
- assert(!isStrictMode());
+ expect(isStrictMode()).toBeFalse();
})();
-
- console.log("PASS");
-} catch {
- console.log("FAIL");
-}
+});
+
+test("no prototype", () => {
+ let foo = () => {};
+ expect(foo).not.toHaveProperty("prototype");
+});
+
+test("cannot be constructed", () => {
+ let foo = () => {};
+ expect(() => {
+ new foo();
+ }).toThrowWithMessage(TypeError, "foo is not a constructor");
+});