1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:08:13 +00:00

LibJS: Convert remaining top-level tests to new system

This commit is contained in:
Matthew Olsson 2020-07-05 10:47:40 -07:00 committed by Andreas Kling
parent 6d58c48c2f
commit 918f4affd5
19 changed files with 518 additions and 497 deletions

View file

@ -1,104 +1,149 @@
load("test-common.js");
describe("correct behavior", () => {
test("numeric indexing", () => {
const o = { 1: 23 };
try {
var foo = "bar";
var computed = "computed";
var o = {
1: 23,
foo,
bar: "baz",
qux: true ? 10 : 20,
hello: "friends",
[1 + 2]: 42,
["I am a " + computed + " key"]: foo,
duplicate: "hello",
duplicate: "world",
};
assert(o[1] === 23);
assert(o[1n] === 23);
assert(o["1"] === 23);
assert(o.foo === "bar");
assert(o["foo"] === "bar");
assert(o.qux === 10), assert(o.hello === "friends");
assert(o["hello"] === "friends");
assert(o[3] === 42);
assert(o["I am a computed key"] === "bar");
assert(o.duplicate === "world");
o.baz = "test";
assert(o.baz === "test");
assert(o["baz"] === "test");
o[10] = "123";
assert(o[10] === "123");
assert(o["10"] === "123");
o[10n] = "123";
assert(o[10] === "123");
assert(o["10"] === "123");
o[-1] = "hello friends";
assert(o[-1] === "hello friends");
assert(o["-1"] === "hello friends");
expect(o[1]).toBe(23);
expect(o[1n]).toBe(23);
expect(o["1"]).toBe(23);
var math = { 3.14: "pi" };
assert(math["3.14"] === "pi");
// Note : this test doesn't pass yet due to floating-point literals being coerced to i32 on access
// assert(math[3.14] === "pi");
o[10] = "123";
expect(o[10]).toBe("123");
expect(o["10"]).toBe("123");
// This is also allowed! Watch out for syntax errors.
var o2 = { return: 1, yield: 1, for: 1, catch: 1, break: 1 };
assert(o2.return === 1);
assert(o2.yield === 1);
assert(o2.for === 1);
assert(o2.catch === 1);
assert(o2.break === 1);
o[10n] = "1234";
expect(o[10]).toBe("1234");
expect(o["10"]).toBe("1234");
});
var a;
var append = x => {
test("string indexing", () => {
let foo = "bar";
const o = {
foo,
bar: "baz",
qux: true ? 10 : 20,
hello: "friends",
};
expect(o.foo).toBe("bar");
expect(o["foo"]).toBe("bar");
expect(o.qux).toBe(10), expect(o.hello).toBe("friends");
expect(o["hello"]).toBe("friends");
});
test("computed properties", () => {
const foo = "bar";
const computed = "computed";
const o = {
[1 + 2]: 42,
[`I am a ${computed} key`]: foo,
};
expect(o[3]).toBe(42);
expect(o["I am a computed key"]).toBe("bar");
});
test("duplicate keys", () => {
const o = {
duplicate: "hello",
duplicate: "world",
};
expect(o.duplicate).toBe("world");
});
test("assigning after creation", () => {
const o = {};
o.baz = "test";
expect(o.baz).toBe("test");
expect(o["baz"]).toBe("test");
expect(o[-1]).toBeUndefined();
o[-1] = "hello friends";
expect(o[-1]).toBe("hello friends");
expect(o["-1"]).toBe("hello friends");
});
test("floating point keys", () => {
const math = { 3.14: "pi" };
expect(math["3.14"]).toBe("pi");
// FIXME: Floating point literals are coerced to i32
// expect(math[3.14]).toBe("pi");
});
test("keywords as property keys", () => {
const o2 = {
return: 1,
yield: 1,
for: 1,
catch: 1,
break: 1,
};
expect(o2.return).toBe(1);
expect(o2.yield).toBe(1);
expect(o2.for).toBe(1);
expect(o2.catch).toBe(1);
expect(o2.break).toBe(1);
});
test("prototypical inheritance", () => {
var base = {
getNumber() {
return 10;
},
};
var derived = {
getNumber() {
return 20 + super.getNumber();
},
};
Object.setPrototypeOf(derived, base);
expect(derived.getNumber()).toBe(30);
});
});
describe("side effects", () => {
let a;
const append = x => {
a.push(x);
};
a = [];
var o3 = { [append(1)]: 1, [append(2)]: 2, [append(3)]: 3 };
assert(a.length === 3);
assert(a[0] === 1);
assert(a[1] === 2);
assert(a[2] === 3);
assert(o3.undefined === 3);
test("computed key side effects", () => {
a = [];
const o3 = { [append(1)]: 1, [append(2)]: 2, [append(3)]: 3 };
expect(a).toHaveLength(3);
expect(a[0]).toBe(1);
expect(a[1]).toBe(2);
expect(a[2]).toBe(3);
expect(o3.undefined).toBe(3);
});
a = [];
var o4 = { test: append(1), test: append(2), test: append(3) };
assert(a.length === 3);
assert(a[0] === 1);
assert(a[1] === 2);
assert(a[2] === 3);
assert(o4.test === undefined);
test("value side effects", () => {
a = [];
const o4 = { test: append(1), test: append(2), test: append(3) };
expect(a).toHaveLength(3);
expect(a[0]).toBe(1);
expect(a[1]).toBe(2);
expect(a[2]).toBe(3);
expect(o4.test).toBeUndefined();
});
});
var base = {
getNumber() {
return 10;
},
};
var derived = {
getNumber() {
return 20 + super.getNumber();
},
};
Object.setPrototypeOf(derived, base);
assert(derived.getNumber() === 30);
assertIsSyntaxError("({ foo: function() { super.bar; } })");
assertIsSyntaxError("({ get ...foo })");
assertIsSyntaxError("({ get... foo })");
assertIsSyntaxError("({ get foo })");
assertIsSyntaxError("({ get foo: bar })");
assertIsSyntaxError("({ get [foo]: bar })");
assertIsSyntaxError("({ get ...[foo] })");
assertIsSyntaxError("({ get foo(bar) {} })");
assertIsSyntaxError("({ set foo() {} })");
assertIsSyntaxError("({ set foo(bar, baz) {} })");
assertIsSyntaxError("({ ...foo: bar })");
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
describe("errors", () => {
test("syntax errors", () => {
expect("({ foo: function() { super.bar; } })").not.toEval();
expect("({ get ...foo })").not.toEval();
expect("({ get... foo })").not.toEval();
expect("({ get foo })").not.toEval();
expect("({ get foo: bar })").not.toEval();
expect("({ get [foo]: bar })").not.toEval();
expect("({ get ...[foo] })").not.toEval();
expect("({ get foo(bar) {} })").not.toEval();
expect("({ set foo() {} })").not.toEval();
expect("({ set foo(bar, baz) {} })").not.toEval();
expect("({ ...foo: bar })").not.toEval();
});
});