mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:07:45 +00:00
LibJS: Convert all remaining non-Array tests to the new system :)
This commit is contained in:
parent
918f4affd5
commit
15de2eda2b
72 changed files with 2394 additions and 1998 deletions
|
@ -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);
|
||||
expect(half).toBe(5);
|
||||
});
|
||||
|
||||
var foo, bar;
|
||||
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");
|
||||
});
|
||||
|
|
|
@ -1,17 +1,11 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
test("basic functionality", () => {
|
||||
function Foo() {
|
||||
this.x = 123;
|
||||
}
|
||||
|
||||
assert(Foo.prototype.constructor === Foo);
|
||||
expect(Foo.prototype.constructor).toBe(Foo);
|
||||
|
||||
var foo = new Foo();
|
||||
assert(foo.constructor === Foo);
|
||||
assert(foo.x === 123);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
const foo = new Foo();
|
||||
expect(foo.constructor).toBe(Foo);
|
||||
expect(foo.x).toBe(123);
|
||||
});
|
||||
|
|
|
@ -1,70 +1,44 @@
|
|||
load("test-common.js");
|
||||
test("calling non-function", () => {
|
||||
expect(() => {
|
||||
const a = true;
|
||||
a();
|
||||
}).toThrowWithMessage(TypeError, "true is not a function (evaluated from 'a')");
|
||||
});
|
||||
|
||||
try {
|
||||
assertThrowsError(
|
||||
() => {
|
||||
var b = true;
|
||||
b();
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "true is not a function (evaluated from 'b')",
|
||||
}
|
||||
test("calling number", () => {
|
||||
expect(() => {
|
||||
const a = 100 + 20 + 3;
|
||||
a();
|
||||
}).toThrowWithMessage(TypeError, "123 is not a function (evaluated from 'a')");
|
||||
});
|
||||
|
||||
test("calling undefined object key", () => {
|
||||
expect(() => {
|
||||
const o = {};
|
||||
o.a();
|
||||
}).toThrowWithMessage(TypeError, "undefined is not a function (evaluated from 'o.a')");
|
||||
});
|
||||
|
||||
test("calling object", () => {
|
||||
expect(() => {
|
||||
Math();
|
||||
}).toThrowWithMessage(TypeError, "[object MathObject] is not a function (evaluated from 'Math')");
|
||||
});
|
||||
|
||||
test("constructing object", () => {
|
||||
expect(() => {
|
||||
new Math();
|
||||
}).toThrowWithMessage(
|
||||
TypeError,
|
||||
"[object MathObject] is not a constructor (evaluated from 'Math')"
|
||||
);
|
||||
});
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
var n = 100 + 20 + 3;
|
||||
n();
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "123 is not a function (evaluated from 'n')",
|
||||
}
|
||||
test("constructing native function", () => {
|
||||
expect(() => {
|
||||
new isNaN();
|
||||
}).toThrowWithMessage(
|
||||
TypeError,
|
||||
"[object NativeFunction] is not a constructor (evaluated from 'isNaN')"
|
||||
);
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
var o = {};
|
||||
o.a();
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "undefined is not a function (evaluated from 'o.a')",
|
||||
}
|
||||
);
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
Math();
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "[object MathObject] is not a function (evaluated from 'Math')",
|
||||
}
|
||||
);
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
new Math();
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "[object MathObject] is not a constructor (evaluated from 'Math')",
|
||||
}
|
||||
);
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
new isNaN();
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "[object NativeFunction] is not a constructor (evaluated from 'isNaN')",
|
||||
}
|
||||
);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,75 +1,81 @@
|
|||
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) {
|
||||
test("single default parameter", () => {
|
||||
function func(a = 6) {
|
||||
return typeof a;
|
||||
}
|
||||
|
||||
const arrowFunc2 = (a = 6) => typeof a;
|
||||
const arrowFunc = (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");
|
||||
expect(func()).toBe("number");
|
||||
expect(func(5)).toBe("number");
|
||||
expect(func(undefined)).toBe("number");
|
||||
expect(func(false)).toBe("boolean");
|
||||
expect(func(null)).toBe("object");
|
||||
expect(func({})).toBe("object");
|
||||
|
||||
assert(arrowFunc2() === "number");
|
||||
assert(arrowFunc2(5) === "number");
|
||||
assert(arrowFunc2(undefined) === "number");
|
||||
assert(arrowFunc2(false) === "boolean");
|
||||
assert(arrowFunc2(null) === "object");
|
||||
assert(arrowFunc2({}) === "object");
|
||||
expect(arrowFunc()).toBe("number");
|
||||
expect(arrowFunc(5)).toBe("number");
|
||||
expect(arrowFunc(undefined)).toBe("number");
|
||||
expect(arrowFunc(false)).toBe("boolean");
|
||||
expect(arrowFunc(null)).toBe("object");
|
||||
expect(arrowFunc({})).toBe("object");
|
||||
});
|
||||
|
||||
function func3(a = 5, b) {
|
||||
test("two parameters, second one is default", () => {
|
||||
function func(a, b = 1) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
const arrowFunc3 = (a = 5, b) => a + b;
|
||||
const arrowFunc = (a, b = 1) => a + b;
|
||||
|
||||
assert(func3(4, 5) === 9);
|
||||
assert(func3(undefined, 4) === 9);
|
||||
assert(Number.isNaN(func3()));
|
||||
expect(func(4, 5)).toBe(9);
|
||||
expect(func(4)).toBe(5);
|
||||
expect(func(4, undefined)).toBe(5);
|
||||
expect(func()).toBeNaN();
|
||||
|
||||
assert(arrowFunc3(4, 5) === 9);
|
||||
assert(arrowFunc3(undefined, 4) === 9);
|
||||
assert(Number.isNaN(arrowFunc3()));
|
||||
expect(arrowFunc(4, 5)).toBe(9);
|
||||
expect(arrowFunc(4)).toBe(5);
|
||||
expect(arrowFunc(4, undefined)).toBe(5);
|
||||
expect(arrowFunc()).toBeNaN();
|
||||
});
|
||||
|
||||
function func4(a, b = a) {
|
||||
test("two parameters, first one is default", () => {
|
||||
function func(a = 5, b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
const arrowFunc4 = (a, b = a) => a + b;
|
||||
const arrowFunc = (a = 5, b) => a + b;
|
||||
|
||||
assert(func4(4, 5) === 9);
|
||||
assert(func4(4) === 8);
|
||||
assert(func4("hf") === "hfhf");
|
||||
assert(func4(true) === 2);
|
||||
assert(Number.isNaN(func4()));
|
||||
expect(func(4, 5)).toBe(9);
|
||||
expect(func(undefined, 4)).toBe(9);
|
||||
expect(func()).toBeNaN();
|
||||
|
||||
assert(arrowFunc4(4, 5) === 9);
|
||||
assert(arrowFunc4(4) === 8);
|
||||
assert(arrowFunc4("hf") === "hfhf");
|
||||
assert(arrowFunc4(true) === 2);
|
||||
assert(Number.isNaN(arrowFunc4()));
|
||||
expect(arrowFunc(4, 5)).toBe(9);
|
||||
expect(arrowFunc(undefined, 4)).toBe(9);
|
||||
expect(arrowFunc()).toBeNaN();
|
||||
});
|
||||
|
||||
function func5(
|
||||
test("default parameter references a previous parameter", () => {
|
||||
function func(a, b = a) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
const arrowFunc = (a, b = a) => a + b;
|
||||
|
||||
expect(func(4, 5)).toBe(9);
|
||||
expect(func(4)).toBe(8);
|
||||
expect(func("hf")).toBe("hfhf");
|
||||
expect(func(true)).toBe(2);
|
||||
expect(func()).toBeNaN();
|
||||
|
||||
expect(arrowFunc(4, 5)).toBe(9);
|
||||
expect(arrowFunc(4)).toBe(8);
|
||||
expect(arrowFunc("hf")).toBe("hfhf");
|
||||
expect(arrowFunc(true)).toBe(2);
|
||||
expect(arrowFunc()).toBeNaN();
|
||||
});
|
||||
|
||||
test("parameter with a function default value", () => {
|
||||
function func(
|
||||
a = function () {
|
||||
return 5;
|
||||
}
|
||||
|
@ -77,60 +83,61 @@ try {
|
|||
return a();
|
||||
}
|
||||
|
||||
const arrowFunc5 = (
|
||||
const arrowFunc = (
|
||||
a = function () {
|
||||
return 5;
|
||||
}
|
||||
) => a();
|
||||
|
||||
assert(func5() === 5);
|
||||
assert(
|
||||
func5(function () {
|
||||
expect(func()).toBe(5);
|
||||
expect(
|
||||
func(function () {
|
||||
return 10;
|
||||
}) === 10
|
||||
);
|
||||
assert(func5(() => 10) === 10);
|
||||
assert(arrowFunc5() === 5);
|
||||
assert(
|
||||
arrowFunc5(function () {
|
||||
return 10;
|
||||
}) === 10
|
||||
);
|
||||
assert(arrowFunc5(() => 10) === 10);
|
||||
})
|
||||
).toBe(10);
|
||||
expect(func(() => 10)).toBe(10);
|
||||
|
||||
function func6(a = () => 5) {
|
||||
expect(arrowFunc()).toBe(5);
|
||||
expect(
|
||||
arrowFunc(function () {
|
||||
return 10;
|
||||
})
|
||||
).toBe(10);
|
||||
expect(arrowFunc(() => 10)).toBe(10);
|
||||
});
|
||||
|
||||
test("parameter with an arrow function default vlaue", () => {
|
||||
function func(a = () => 5) {
|
||||
return a();
|
||||
}
|
||||
|
||||
const arrowFunc6 = (a = () => 5) => a();
|
||||
const arrowFunc = (a = () => 5) => a();
|
||||
|
||||
assert(func6() === 5);
|
||||
assert(
|
||||
func6(function () {
|
||||
expect(func()).toBe(5);
|
||||
expect(
|
||||
func(function () {
|
||||
return 10;
|
||||
}) === 10
|
||||
);
|
||||
assert(func6(() => 10) === 10);
|
||||
assert(arrowFunc6() === 5);
|
||||
assert(
|
||||
arrowFunc6(function () {
|
||||
})
|
||||
).toBe(10);
|
||||
expect(func(() => 10)).toBe(10);
|
||||
expect(arrowFunc()).toBe(5);
|
||||
expect(
|
||||
arrowFunc(function () {
|
||||
return 10;
|
||||
}) === 10
|
||||
);
|
||||
assert(arrowFunc6(() => 10) === 10);
|
||||
})
|
||||
).toBe(10);
|
||||
expect(arrowFunc(() => 10)).toBe(10);
|
||||
});
|
||||
|
||||
function func7(a = { foo: "bar" }) {
|
||||
test("parameter with an object default value", () => {
|
||||
function func(a = { foo: "bar" }) {
|
||||
return a.foo;
|
||||
}
|
||||
|
||||
const arrowFunc7 = (a = { foo: "bar" }) => a.foo;
|
||||
const arrowFunc = (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);
|
||||
}
|
||||
expect(func()).toBe("bar");
|
||||
expect(func({ foo: "baz" })).toBe("baz");
|
||||
expect(arrowFunc()).toBe("bar");
|
||||
expect(arrowFunc({ foo: "baz" })).toBe("baz");
|
||||
});
|
||||
|
|
|
@ -1,24 +1,29 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var callHoisted = hoisted();
|
||||
test("basic functionality", () => {
|
||||
let callHoisted = hoisted();
|
||||
function hoisted() {
|
||||
return true;
|
||||
return "foo";
|
||||
}
|
||||
assert(hoisted() === true);
|
||||
assert(callHoisted === true);
|
||||
expect(hoisted()).toBe("foo");
|
||||
expect(callHoisted).toBe("foo");
|
||||
});
|
||||
|
||||
// First two calls produce a ReferenceError, but the declarations should be hoisted
|
||||
test.skip("functions are hoisted across non-lexical scopes", () => {
|
||||
expect(scopedHoisted).toBeUndefined();
|
||||
expect(callScopedHoisted).toBeUndefined();
|
||||
{
|
||||
var callScopedHoisted = scopedHoisted();
|
||||
function scopedHoisted() {
|
||||
return "foo";
|
||||
}
|
||||
assert(scopedHoisted() === "foo");
|
||||
assert(callScopedHoisted === "foo");
|
||||
expect(scopedHoisted()).toBe("foo");
|
||||
expect(callScopedHoisted).toBe("foo");
|
||||
}
|
||||
assert(scopedHoisted() === "foo");
|
||||
assert(callScopedHoisted === "foo");
|
||||
expect(scopedHoisted()).toBe("foo");
|
||||
expect(callScopedHoisted).toBe("foo");
|
||||
});
|
||||
|
||||
test("functions are not hoisted across lexical scopes", () => {
|
||||
const test = () => {
|
||||
var iife = (function () {
|
||||
return declaredLater();
|
||||
|
@ -28,10 +33,7 @@ try {
|
|||
}
|
||||
return iife;
|
||||
};
|
||||
assert(typeof declaredLater === "undefined");
|
||||
assert(test() === "yay");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(() => declaredLater).toThrow(ReferenceError);
|
||||
expect(test()).toBe("yay");
|
||||
});
|
||||
|
|
|
@ -1,27 +1,23 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
test("basic functionality", () => {
|
||||
function foo() {}
|
||||
assert(foo.length === 0);
|
||||
assert((foo.length = 5) === 5);
|
||||
assert(foo.length === 0);
|
||||
expect(foo).toHaveLength(0);
|
||||
expect((foo.length = 5)).toBe(5);
|
||||
expect(foo).toHaveLength(0);
|
||||
|
||||
function bar(a, b, c) {}
|
||||
assert(bar.length === 3);
|
||||
assert((bar.length = 5) === 5);
|
||||
assert(bar.length === 3);
|
||||
expect(bar).toHaveLength(3);
|
||||
expect((bar.length = 5)).toBe(5);
|
||||
expect(bar).toHaveLength(3);
|
||||
});
|
||||
|
||||
test("functions with special parameter lists", () => {
|
||||
function baz(a, b = 1, c) {}
|
||||
assert(baz.length === 1);
|
||||
assert((baz.length = 5) === 5);
|
||||
assert(baz.length === 1);
|
||||
expect(baz).toHaveLength(1);
|
||||
expect((baz.length = 5)).toBe(5);
|
||||
expect(baz).toHaveLength(1);
|
||||
|
||||
function qux(a, b, ...c) {}
|
||||
assert(qux.length === 2);
|
||||
assert((qux.length = 5) === 5);
|
||||
assert(qux.length === 2);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(qux).toHaveLength(2);
|
||||
expect((qux.length = 2)).toBe(2);
|
||||
expect(qux).toHaveLength(2);
|
||||
});
|
||||
|
|
|
@ -1,15 +1,10 @@
|
|||
load("test-common.js");
|
||||
test("basic functionality", () => {
|
||||
function foo(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
function foo(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
try {
|
||||
assert(isNaN(foo()) === true);
|
||||
assert(isNaN(foo(1)) === true);
|
||||
assert(foo(2, 3) === 5);
|
||||
assert(foo(2, 3, 4) === 5);
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(foo()).toBeNaN();
|
||||
expect(foo(1)).toBeNaN();
|
||||
expect(foo(2, 3)).toBe(5);
|
||||
expect(foo(2, 3, 4)).toBe(5);
|
||||
});
|
||||
|
|
|
@ -1,45 +1,50 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(function () {}.name === "");
|
||||
|
||||
var foo = function () {};
|
||||
assert(foo.name === "foo");
|
||||
assert((foo.name = "bar") === "bar");
|
||||
assert(foo.name === "foo");
|
||||
|
||||
var a, b;
|
||||
a = b = function () {};
|
||||
assert(a.name === "b");
|
||||
assert(b.name === "b");
|
||||
|
||||
var arr = [function () {}, function () {}, function () {}];
|
||||
assert(arr[0].name === "arr");
|
||||
assert(arr[1].name === "arr");
|
||||
assert(arr[2].name === "arr");
|
||||
|
||||
var f;
|
||||
var o = { a: function () {} };
|
||||
assert(o.a.name === "a");
|
||||
f = o.a;
|
||||
assert(f.name === "a");
|
||||
assert(o.a.name === "a");
|
||||
o = { ...o, b: f };
|
||||
assert(o.a.name === "a");
|
||||
assert(o.b.name === "a");
|
||||
o.c = function () {};
|
||||
assert(o.c.name === "c");
|
||||
test("basic functionality", () => {
|
||||
expect(function () {}.name).toBe("");
|
||||
|
||||
function bar() {}
|
||||
assert(bar.name === "bar");
|
||||
assert((bar.name = "baz") === "baz");
|
||||
assert(bar.name === "bar");
|
||||
expect(bar.name).toBe("bar");
|
||||
expect((bar.name = "baz")).toBe("baz");
|
||||
expect(bar.name).toBe("bar");
|
||||
});
|
||||
|
||||
assert(console.log.name === "log");
|
||||
assert((console.log.name = "warn") === "warn");
|
||||
assert(console.log.name === "log");
|
||||
test("function assigned to variable", () => {
|
||||
let foo = function () {};
|
||||
expect(foo.name).toBe("foo");
|
||||
expect((foo.name = "bar")).toBe("bar");
|
||||
expect(foo.name).toBe("foo");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
let a, b;
|
||||
a = b = function () {};
|
||||
expect(a.name).toBe("b");
|
||||
expect(b.name).toBe("b");
|
||||
});
|
||||
|
||||
test("functions in array assigned to variable", () => {
|
||||
const arr = [function () {}, function () {}, function () {}];
|
||||
expect(arr[0].name).toBe("arr");
|
||||
expect(arr[1].name).toBe("arr");
|
||||
expect(arr[2].name).toBe("arr");
|
||||
});
|
||||
|
||||
test("functions in objects", () => {
|
||||
let f;
|
||||
let o = { a: function () {} };
|
||||
|
||||
expect(o.a.name).toBe("a");
|
||||
f = o.a;
|
||||
expect(f.name).toBe("a");
|
||||
expect(o.a.name).toBe("a");
|
||||
|
||||
o = { ...o, b: f };
|
||||
expect(o.a.name).toBe("a");
|
||||
expect(o.b.name).toBe("a");
|
||||
|
||||
o.c = function () {};
|
||||
expect(o.c.name).toBe("c");
|
||||
});
|
||||
|
||||
test("names of native functions", () => {
|
||||
expect(console.debug.name).toBe("debug");
|
||||
expect((console.debug.name = "warn")).toBe("warn");
|
||||
expect(console.debug.name).toBe("debug");
|
||||
});
|
||||
|
|
|
@ -1,57 +1,47 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
test("rest parameter with no arguments", () => {
|
||||
function foo(...a) {
|
||||
assert(a instanceof Array);
|
||||
assert(a.length === 0);
|
||||
expect(a).toBeInstanceOf(Array);
|
||||
expect(a).toHaveLength(0);
|
||||
}
|
||||
foo();
|
||||
});
|
||||
|
||||
function foo1(...a) {
|
||||
assert(a instanceof Array);
|
||||
assert(a.length === 4);
|
||||
assert(a[0] === "foo");
|
||||
assert(a[1] === 123);
|
||||
assert(a[2] === undefined);
|
||||
assert(a[3].foo === "bar");
|
||||
test("rest parameter with arguments", () => {
|
||||
function foo(...a) {
|
||||
expect(a).toEqual(["foo", 123, undefined, { foo: "bar" }]);
|
||||
}
|
||||
foo1("foo", 123, undefined, { foo: "bar" });
|
||||
foo("foo", 123, undefined, { foo: "bar" });
|
||||
});
|
||||
|
||||
function foo2(a, b, ...c) {
|
||||
assert(a === "foo");
|
||||
assert(b === 123);
|
||||
assert(c instanceof Array);
|
||||
assert(c.length === 0);
|
||||
test("rest parameter after normal parameters with no arguments", () => {
|
||||
function foo(a, b, ...c) {
|
||||
expect(a).toBe("foo");
|
||||
expect(b).toBe(123);
|
||||
expect(c).toEqual([]);
|
||||
}
|
||||
foo2("foo", 123);
|
||||
foo("foo", 123);
|
||||
});
|
||||
|
||||
function foo3(a, b, ...c) {
|
||||
assert(a === "foo");
|
||||
assert(b === 123);
|
||||
assert(c instanceof Array);
|
||||
assert(c.length === 2);
|
||||
assert(c[0] === undefined);
|
||||
assert(c[1].foo === "bar");
|
||||
test("rest parameter after normal parameters with arguments", () => {
|
||||
function foo(a, b, ...c) {
|
||||
expect(a).toBe("foo");
|
||||
expect(b).toBe(123);
|
||||
expect(c).toEqual([undefined, { foo: "bar" }]);
|
||||
}
|
||||
foo3("foo", 123, undefined, { foo: "bar" });
|
||||
foo("foo", 123, undefined, { foo: "bar" });
|
||||
});
|
||||
|
||||
var foo = (...a) => {
|
||||
assert(a instanceof Array);
|
||||
assert(a.length === 0);
|
||||
test("basic arrow function rest parameters", () => {
|
||||
let foo = (...a) => {
|
||||
expect(a).toBeInstanceOf(Array);
|
||||
expect(a).toHaveLength(0);
|
||||
};
|
||||
foo();
|
||||
|
||||
var foo = (a, b, ...c) => {
|
||||
assert(a === "foo");
|
||||
assert(b === 123);
|
||||
assert(c instanceof Array);
|
||||
assert(c.length === 2);
|
||||
assert(c[0] === undefined);
|
||||
assert(c[1].foo === "bar");
|
||||
foo = (a, b, ...c) => {
|
||||
expect(a).toBe("foo");
|
||||
expect(b).toBe(123);
|
||||
expect(c).toEqual([undefined, { foo: "bar" }]);
|
||||
};
|
||||
foo("foo", 123, undefined, { foo: "bar" });
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,30 +1,20 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
test("basic functionality", () => {
|
||||
const sum = (a, b, c) => a + b + c;
|
||||
const a = [1, 2, 3];
|
||||
|
||||
assert(sum(...a) === 6);
|
||||
assert(sum(1, ...a) === 4);
|
||||
assert(sum(...a, 10) === 6);
|
||||
expect(sum(...a)).toBe(6);
|
||||
expect(sum(1, ...a)).toBe(4);
|
||||
expect(sum(...a, 10)).toBe(6);
|
||||
|
||||
const foo = (a, b, c) => c;
|
||||
|
||||
const o = { bar: [1, 2, 3] };
|
||||
assert(foo(...o.bar) === 3);
|
||||
assert(foo(..."abc") === "c");
|
||||
expect(foo(...o.bar)).toBe(3);
|
||||
expect(foo(..."abc")).toBe("c");
|
||||
});
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
[...1];
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "1 is not iterable",
|
||||
}
|
||||
);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
test("spreading non iterable", () => {
|
||||
expect(() => {
|
||||
[...1];
|
||||
}).toThrowWithMessage(TypeError, "1 is not iterable");
|
||||
});
|
||||
|
|
|
@ -1,52 +1,53 @@
|
|||
load("test-common.js");
|
||||
// This file must not be formatted by prettier. Make sure your IDE
|
||||
// respects the .prettierignore file!
|
||||
|
||||
try {
|
||||
(function () {
|
||||
assert(!isStrictMode());
|
||||
test("non strict-mode by default", () => {
|
||||
expect(isStrictMode()).toBeFalse();
|
||||
});
|
||||
|
||||
test("use strict with double quotes", () => {
|
||||
"use strict";
|
||||
expect(isStrictMode()).toBeTrue();
|
||||
});
|
||||
|
||||
test("use strict with single quotes", () => {
|
||||
'use strict';
|
||||
expect(isStrictMode()).toBeTrue();
|
||||
});
|
||||
|
||||
test("use strict with backticks does not yield strict mode", () => {
|
||||
`use strict`;
|
||||
expect(isStrictMode()).toBeFalse();
|
||||
});
|
||||
|
||||
test("use strict with single quotes after statement does not yield strict mode code", () => {
|
||||
;'use strict';
|
||||
expect(isStrictMode()).toBeFalse();
|
||||
});
|
||||
|
||||
test("use strict with double quotes after statement does not yield strict mode code", () => {
|
||||
;"use strict";
|
||||
expect(isStrictMode()).toBeFalse();
|
||||
});
|
||||
|
||||
test("strict mode propogates down the scope chain", () => {
|
||||
"use strict";
|
||||
expect(isStrictMode()).toBeTrue();
|
||||
(function() {
|
||||
expect(isStrictMode()).toBeTrue();
|
||||
})();
|
||||
});
|
||||
|
||||
(function () {
|
||||
test("strict mode does not propogate up the scope chain", () => {
|
||||
expect(isStrictMode()).toBeFalse();
|
||||
(function() {
|
||||
"use strict";
|
||||
assert(isStrictMode());
|
||||
expect(isStrictMode()).toBeTrue();
|
||||
})();
|
||||
expect(isStrictMode()).toBeFalse();
|
||||
});
|
||||
|
||||
(function () {
|
||||
"use strict";
|
||||
assert(isStrictMode());
|
||||
})();
|
||||
|
||||
(function () {
|
||||
`use strict`;
|
||||
assert(!isStrictMode());
|
||||
})();
|
||||
|
||||
(function () {
|
||||
("use strict");
|
||||
assert(!isStrictMode());
|
||||
})();
|
||||
|
||||
(function () {
|
||||
("use strict");
|
||||
assert(!isStrictMode());
|
||||
})();
|
||||
|
||||
(function () {
|
||||
"use strict";
|
||||
(function () {
|
||||
assert(isStrictMode());
|
||||
})();
|
||||
})();
|
||||
|
||||
(function () {
|
||||
assert(!isStrictMode());
|
||||
(function () {
|
||||
"use strict";
|
||||
assert(isStrictMode());
|
||||
})();
|
||||
assert(!isStrictMode());
|
||||
})();
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
test('only the string "use strict" yields strict mode code', () => {
|
||||
"use stric";
|
||||
expect(isStrictMode()).toBeFalse();
|
||||
});
|
||||
|
|
|
@ -1,17 +1,16 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(typeof this === "object");
|
||||
assert(this === globalThis);
|
||||
test("basic functionality", () => {
|
||||
expect(typeof this).toBe("object");
|
||||
expect(this).toBe(globalThis);
|
||||
});
|
||||
|
||||
test("this inside instantiated functions is not globalThis", () => {
|
||||
let functionThis;
|
||||
function Foo() {
|
||||
this.x = 5;
|
||||
assert(typeof this === "object");
|
||||
assert(this.x === 5);
|
||||
functionThis = this;
|
||||
}
|
||||
|
||||
new Foo();
|
||||
console.log("PASS");
|
||||
} catch (err) {
|
||||
console.log("FAIL: " + err);
|
||||
}
|
||||
expect(typeof functionThis).toBe("object");
|
||||
expect(functionThis.x).toBe(5);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue