1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:17:46 +00:00

LibJS: Convert all remaining non-Array tests to the new system :)

This commit is contained in:
Matthew Olsson 2020-07-05 17:26:26 -07:00 committed by Andreas Kling
parent 918f4affd5
commit 15de2eda2b
72 changed files with 2394 additions and 1998 deletions

View file

@ -1,9 +0,0 @@
load("test-common.js");
try {
assert(BigInt.asIntN.length === 2);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -1,9 +0,0 @@
load("test-common.js");
try {
assert(BigInt.asUintN.length === 2);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -1,78 +1,68 @@
load("test-common.js");
describe("correct behavior", () => {
test("basic functionality", () => {
expect(BigInt).toHaveLength(1);
expect(BigInt.name).toBe("BigInt");
});
try {
assert(BigInt.length === 1);
assert(BigInt.name === "BigInt");
test("constructor with numbers", () => {
expect(BigInt(0)).toBe(0n);
expect(BigInt(1)).toBe(1n);
expect(BigInt(+1)).toBe(1n);
expect(BigInt(-1)).toBe(-1n);
expect(BigInt(123n)).toBe(123n);
});
assert(BigInt(0) === 0n);
assert(BigInt(1) === 1n);
assert(BigInt(+1) === 1n);
assert(BigInt(-1) === -1n);
assert(BigInt("") === 0n);
assert(BigInt("0") === 0n);
assert(BigInt("1") === 1n);
assert(BigInt("+1") === 1n);
assert(BigInt("-1") === -1n);
assert(BigInt("-1") === -1n);
assert(BigInt([]) === 0n);
assert(BigInt("42") === 42n);
assert(BigInt(" \n 00100 \n ") === 100n);
assert(BigInt(123n) === 123n);
assert(
BigInt("3323214327642987348732109829832143298746432437532197321") ===
test("constructor with strings", () => {
expect(BigInt("")).toBe(0n);
expect(BigInt("0")).toBe(0n);
expect(BigInt("1")).toBe(1n);
expect(BigInt("+1")).toBe(1n);
expect(BigInt("-1")).toBe(-1n);
expect(BigInt("-1")).toBe(-1n);
expect(BigInt("42")).toBe(42n);
expect(BigInt(" \n 00100 \n ")).toBe(100n);
expect(BigInt("3323214327642987348732109829832143298746432437532197321")).toBe(
3323214327642987348732109829832143298746432437532197321n
);
);
});
assertThrowsError(
() => {
test("constructor with objects", () => {
expect(BigInt([])).toBe(0n);
});
});
describe("errors", () => {
test('cannot be constructed with "new"', () => {
expect(() => {
new BigInt();
},
{
error: TypeError,
message: "BigInt is not a constructor",
}
);
[null, undefined, Symbol()].forEach(value => {
assertThrowsError(
() => {
BigInt(value);
},
{
error: TypeError,
message:
typeof value === "symbol"
? "Cannot convert symbol to BigInt"
: `Cannot convert ${value} to BigInt`,
}
);
}).toThrowWithMessage(TypeError, "BigInt is not a constructor");
});
["foo", "123n", "1+1", {}, function () {}].forEach(value => {
assertThrowsError(
() => {
test("invalid arguments", () => {
expect(() => {
BigInt(null);
}).toThrowWithMessage(TypeError, "Cannot convert null to BigInt");
expect(() => {
BigInt(undefined);
}).toThrowWithMessage(TypeError, "Cannot convert undefined to BigInt");
expect(() => {
BigInt(Symbol());
}).toThrowWithMessage(TypeError, "Cannot convert symbol to BigInt");
["foo", "123n", "1+1", {}, function () {}].forEach(value => {
expect(() => {
BigInt(value);
},
{
error: SyntaxError,
message: `Invalid value for BigInt: ${value}`,
}
);
}).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
});
});
[1.23, Infinity, -Infinity, NaN].forEach(value => {
assertThrowsError(
() => {
test("invalid numeric arguments", () => {
[1.23, Infinity, -Infinity, NaN].forEach(value => {
expect(() => {
BigInt(value);
},
{
error: RangeError,
message: "BigInt argument must be an integer",
}
);
}).toThrowWithMessage(RangeError, "BigInt argument must be an integer");
});
});
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
});

View file

@ -1,21 +1,10 @@
load("test-common.js");
test("basic functionality", () => {
expect(BigInt.prototype.toLocaleString).toHaveLength(0);
expect(BigInt(123).toLocaleString()).toBe("123");
});
try {
assert(BigInt.prototype.toLocaleString.length === 0);
assertThrowsError(
() => {
BigInt.prototype.toLocaleString.call("foo");
},
{
error: TypeError,
message: "Not a BigInt object",
}
);
assert(BigInt(123).toLocaleString() === "123");
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
test("calling with non-BigInt |this|", () => {
expect(() => {
BigInt.prototype.toLocaleString.call("foo");
}).toThrowWithMessage(TypeError, "Not a BigInt object");
});

View file

@ -1,21 +1,10 @@
load("test-common.js");
test("basic functionality", () => {
expect(BigInt.prototype.toString).toHaveLength(0);
expect(BigInt(123).toString()).toBe("123");
});
try {
assert(BigInt.prototype.toString.length === 0);
assertThrowsError(
() => {
BigInt.prototype.toString.call("foo");
},
{
error: TypeError,
message: "Not a BigInt object",
}
);
assert(BigInt(123).toString() === "123");
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
test("calling with non-BigInt |this|", () => {
expect(() => {
BigInt.prototype.toString.call("foo");
}).toThrowWithMessage(TypeError, "Not a BigInt object");
});

View file

@ -1,23 +1,12 @@
load("test-common.js");
try {
assert(BigInt.prototype.valueOf.length === 0);
assertThrowsError(
() => {
BigInt.prototype.valueOf.call("foo");
},
{
error: TypeError,
message: "Not a BigInt object",
}
);
assert(typeof BigInt(123).valueOf() === "bigint");
test("basic functionality", () => {
expect(BigInt.prototype.valueOf).toHaveLength(0);
expect(typeof BigInt(123).valueOf()).toBe("bigint");
// FIXME: Uncomment once we support Object() with argument
// assert(typeof Object(123n).valueOf() === "bigint");
// expect(typeof Object(123n).valueOf()).toBe("bigint");
});
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
test("calling with non-BigInt |this|", () => {
expect(() => {
BigInt.prototype.valueOf.call("foo");
}).toThrowWithMessage(TypeError, "Not a BigInt object");
});

View file

@ -1,74 +1,80 @@
load("test-common.js");
describe("correct behavior", () => {
test("typeof bigint", () => {
expect(typeof 1n).toBe("bigint");
});
try {
var bigint = 123n;
test("bigint string coersion", () => {
expect("" + 123n).toBe("123");
});
assert(typeof bigint === "bigint");
assert(-bigint === -123n);
assert("" + bigint === "123");
test("arithmetic operators", () => {
let bigint = 123n;
expect(-bigint).toBe(-123n);
assertThrowsError(
() => {
+bigint;
},
{
error: TypeError,
message: "Cannot convert BigInt to number",
}
);
expect(12n + 34n).toBe(46n);
expect(12n - 34n).toBe(-22n);
expect(8n * 12n).toBe(96n);
expect(123n / 10n).toBe(12n);
expect(2n ** 3n).toBe(8n);
expect(5n % 3n).toBe(2n);
expect(
45977665298704210987n +
(714320987142450987412098743217984576n / 4598741987421098765327980n) * 987498743n
).toBe(199365500239020623962n);
});
assert(12n + 34n === 46n);
assert(12n - 34n === -22n);
assert(8n * 12n === 96n);
assert(123n / 10n === 12n);
assert(2n ** 3n === 8n);
assert(5n % 3n === 2n);
assert(
45977665298704210987n +
(714320987142450987412098743217984576n / 4598741987421098765327980n) * 987498743n ===
199365500239020623962n
);
test("bitwise operators", () => {
expect(12n & 5n).toBe(4n);
expect(1n | 2n).toBe(3n);
expect(5n ^ 3n).toBe(6n);
expect(~1n).toBe(-2n);
});
assert((12n & 5n) === 4n);
assert((1n | 2n) === 3n);
assert((5n ^ 3n) === 6n);
assert(~1n === -2n);
test("increment operators", () => {
let bigint = 1n;
expect(bigint++).toBe(1n);
expect(bigint).toBe(2n);
expect(bigint--).toBe(2n);
expect(bigint).toBe(1n);
expect(++bigint).toBe(2n);
expect(bigint).toBe(2n);
expect(--bigint).toBe(1n);
expect(bigint).toBe(1n);
});
bigint = 1n;
assert(bigint++ === 1n);
assert(bigint === 2n);
assert(bigint-- === 2n);
assert(bigint === 1n);
assert(++bigint === 2n);
assert(bigint === 2n);
assert(--bigint === 1n);
assert(bigint === 1n);
test("weak equality operators", () => {
expect(1n == 1n).toBeTrue();
expect(1n == 1).toBeTrue();
expect(1 == 1n).toBeTrue();
expect(1n == 1.23).toBeFalse();
expect(1.23 == 1n).toBeFalse();
assert((1n == 1n) === true);
assert((1n == 1) === true);
assert((1 == 1n) === true);
assert((1n == 1.23) === false);
assert((1.23 == 1n) === false);
expect(1n != 1n).toBeFalse();
expect(1n != 1).toBeFalse();
expect(1 != 1n).toBeFalse();
expect(1n != 1.23).toBeTrue();
expect(1.23 != 1n).toBeTrue();
});
assert((1n != 1n) === false);
assert((1n != 1) === false);
assert((1 != 1n) === false);
assert((1n != 1.23) === true);
assert((1.23 != 1n) === true);
test("strong equality operators", () => {
expect(1n === 1n).toBeTrue();
expect(1n === 1).toBeFalse();
expect(1 === 1n).toBeFalse();
expect(1n === 1.23).toBeFalse();
expect(1.23 === 1n).toBeFalse();
assert((1n === 1n) === true);
assert((1n === 1) == false);
assert((1 === 1n) === false);
assert((1n === 1.23) === false);
assert((1.23 === 1n) === false);
expect(1n !== 1n).toBeFalse();
expect(1n !== 1).toBeTrue();
expect(1 !== 1n).toBeTrue();
expect(1n !== 1.23).toBeTrue();
expect(1.23 !== 1n).toBeTrue();
});
});
assert((1n !== 1n) === false);
assert((1n !== 1) === true);
assert((1 !== 1n) === true);
assert((1n !== 1.23) === true);
assert((1.23 !== 1n) === true);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
describe("errors", () => {
test("conversion to number", () => {
expect(() => {
+123n;
}).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
});
});

View file

@ -1,118 +1,31 @@
load("test-common.js");
try {
const doTest = (operatorName, executeOperation) => {
[1, null, undefined].forEach(value => {
assertThrowsError(
() => {
1n + value;
},
{
error: TypeError,
message: "Cannot use addition operator with BigInt and other type",
}
);
assertThrowsError(
() => {
1n - value;
},
{
error: TypeError,
message: "Cannot use subtraction operator with BigInt and other type",
}
);
assertThrowsError(
() => {
1n * value;
},
{
error: TypeError,
message: "Cannot use multiplication operator with BigInt and other type",
}
);
assertThrowsError(
() => {
1n / value;
},
{
error: TypeError,
message: "Cannot use division operator with BigInt and other type",
}
);
assertThrowsError(
() => {
1n % value;
},
{
error: TypeError,
message: "Cannot use modulo operator with BigInt and other type",
}
);
assertThrowsError(
() => {
1n ** value;
},
{
error: TypeError,
message: "Cannot use exponentiation operator with BigInt and other type",
}
);
assertThrowsError(
() => {
1n | value;
},
{
error: TypeError,
message: "Cannot use bitwise OR operator with BigInt and other type",
}
);
assertThrowsError(
() => {
1n & value;
},
{
error: TypeError,
message: "Cannot use bitwise AND operator with BigInt and other type",
}
);
assertThrowsError(
() => {
1n ^ value;
},
{
error: TypeError,
message: "Cannot use bitwise XOR operator with BigInt and other type",
}
);
assertThrowsError(
() => {
1n << value;
},
{
error: TypeError,
message: "Cannot use left-shift operator with BigInt and other type",
}
);
assertThrowsError(
() => {
1n >> value;
},
{
error: TypeError,
message: "Cannot use right-shift operator with BigInt and other type",
}
);
assertThrowsError(
() => {
1n >>> value;
},
{
error: TypeError,
message: "Cannot use unsigned right-shift operator with BigInt",
}
const messageSuffix = operatorName === "unsigned right-shift" ? "" : " and other type";
expect(() => {
executeOperation(1n, value);
}).toThrowWithMessage(
TypeError,
`Cannot use ${operatorName} operator with BigInt${messageSuffix}`
);
});
};
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
[
["addition", (a, b) => a + b],
["subtraction", (a, b) => a - b],
["multiplication", (a, b) => a * b],
["division", (a, b) => a / b],
["modulo", (a, b) => a % b],
["exponentiation", (a, b) => a ** b],
["bitwise OR", (a, b) => a | b],
["bitwise AND", (a, b) => a & b],
["bitwise XOR", (a, b) => a ^ b],
["left-shift", (a, b) => a << b],
["right-shift", (a, b) => a >> b],
["unsigned right-shift", (a, b) => a >>> b],
].forEach(testCase => {
test(`using ${testCase[0]} operator with BigInt and other type`, () => {
doTest(testCase[0], testCase[1]);
});
});

View file

@ -1,33 +1,23 @@
load("test-common.js");
// test("basic functionality", () => {
// const localSym = Symbol("foo");
// const globalSym = Symbol.for("foo");
try {
const localSym = Symbol("foo");
const globalSym = Symbol.for("foo");
// expect(localSym).not.toBe(globalSym);
// expect(localSym).not.toBe(Symbol("foo"));
// expect(globalSym).not.toBe(Symbol("foo"));
// expect(globalSym).toBe(Symbol.for("foo"));
// expect(localSym.toString()).toBe("Symbol(foo)");
// expect(globalSym.toString()).toBe("Symbol(foo)");
assert(localSym !== globalSym);
assert(localSym !== Symbol("foo"));
assert(globalSym !== Symbol("foo"));
assert(globalSym === Symbol.for("foo"));
assert(localSym.toString() === "Symbol(foo)");
assert(globalSym.toString() === "Symbol(foo)");
// expect(Symbol.for(1).description).toBe("1");
// expect(Symbol.for(true).description).toBe("true");
// expect(Symbol.for({}).description).toBe("[object Object]");
// expect(Symbol.for().description).toBe("undefined");
// expect(Symbol.for(null).description).toBe("null");
// });
assert(Symbol.for(1).description === "1");
assert(Symbol.for(true).description === "true");
assert(Symbol.for({}).description === "[object Object]");
assert(Symbol.for().description === "undefined");
assert(Symbol.for(null).description === "null");
assertThrowsError(
() => {
Symbol.for(Symbol());
},
{
error: TypeError,
message: "Cannot convert symbol to string",
}
);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
// test("symbol argument throws an error", () => {
// expect(() => {
// Symbol.for(Symbol());
// }).toThrowWithMessage(TypeError, "Cannot convert symbol to string");
// });

View file

@ -1,29 +1,19 @@
load("test-common.js");
try {
test("basic functionality", () => {
const s1 = Symbol("foo");
const s2 = Symbol("foo");
assert(s1 !== s2);
assert(s1.description === "foo");
assert(s2.description === "foo");
expect(s1).not.toBe(s2);
expect(s1.description).toBe("foo");
expect(s2.description).toBe("foo");
s1.description = "bar";
assert(s1.description === "foo");
expect(s1.description).toBe("foo");
assert(typeof s1 === "symbol");
expect(typeof s1).toBe("symbol");
});
assertThrowsError(
() => {
Symbol(Symbol("foo"));
},
{
error: TypeError,
message: "Cannot convert symbol to string",
}
);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
test("constructing symbol from symbol is an error", () => {
expect(() => {
Symbol(Symbol("foo"));
}).toThrowWithMessage(TypeError, "Cannot convert symbol to string");
});

View file

@ -1,34 +1,24 @@
load("test-common.js");
// test("basic functionality", () => {
// const localSym = Symbol("bar");
// const globalSym = Symbol.for("bar");
try {
const localSym = Symbol("foo");
const globalSym = Symbol.for("foo");
// expect(Symbol.keyFor(localSym)).toBeUndefined();
// expect(Symbol.keyFor(globalSym)).toBe("bar");
// });
assert(Symbol.keyFor(localSym) === undefined);
assert(Symbol.keyFor(globalSym) === "foo");
const testThrows = (value, str) => {
assertThrowsError(
() => {
Symbol.keyFor(value);
},
{
error: TypeError,
message: str + " is not a symbol",
}
);
};
testThrows(1, "1");
testThrows(null, "null");
testThrows(undefined, "undefined");
testThrows([], "[object Array]");
testThrows({}, "[object Object]");
testThrows(true, "true");
testThrows("foobar", "foobar");
testThrows(function () {}, "[object ScriptFunction]"); // FIXME: Better function stringification
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
// test("bad argument values", () => {
// [
// [1, "1"],
// [null, "null"],
// [undefined, "undefined"],
// [[], "[object Array]"],
// [{}, "[object Object]"],
// [true, "true"],
// ["foobar", "foobar"],
// [function () {}, "[object ScriptFunction]"], // FIXME: Better function stringification
// ].forEach(testCase => {
// expect(() => {
// Symbol.keyFor(testCase[0]);
// }).toThrowWithMessage(TypeError, `${testCase[1]} is not a symbol`);
// });
// });

View file

@ -1,33 +1,23 @@
load("test-common.js");
describe("correct behavior", () => {
test("basic functionality", () => {
const s1 = Symbol("baz");
// const s2 = Symbol.for("qux");
try {
const s1 = Symbol("foo");
const s2 = Symbol.for("bar");
expect(s1.toString()).toBe("Symbol(baz)");
// expect(s2.toString()).toBe("Symbol(qux)");
});
});
assert(s1.toString() === "Symbol(foo)");
assert(s2.toString() === "Symbol(bar)");
describe("errors", () => {
test("convert to string", () => {
expect(() => {
Symbol() + "";
}).toThrowWithMessage(TypeError, "Cannot convert symbol to string");
});
assertThrowsError(
() => {
s1 + "";
},
{
error: TypeError,
message: "Cannot convert symbol to string",
}
);
assertThrowsError(
() => {
s1 + 1;
},
{
error: TypeError,
message: "Cannot convert symbol to number",
}
);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
test("convert to number", () => {
expect(() => {
Symbol() + 1;
}).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
});
});

View file

@ -1,25 +1,15 @@
load("test-common.js");
test("basic functionality", () => {
const local = Symbol("foo");
// const global = Symbol.for("foo");
expect(local.valueOf()).toBe(local);
// expect(global.valueOf()).toBe(global);
try {
let local = Symbol("foo");
let global = Symbol.for("foo");
assert(local.valueOf() === local);
assert(global.valueOf() === global);
expect(Symbol.prototype.valueOf.call(local)).toBe(local);
// expect(Symbol.prototype.valueOf.call(global)).toBe(global);
});
assert(Symbol.prototype.valueOf.call(local) === local);
assert(Symbol.prototype.valueOf.call(global) === global);
assertThrowsError(
() => {
Symbol.prototype.valueOf.call("foo");
},
{
error: TypeError,
message: "Not a Symbol object",
}
);
console.log("PASS");
} catch (err) {
console.log("FAIL: " + err);
}
test("|this| must be a symbol", () => {
expect(() => {
Symbol.prototype.valueOf.call("foo");
}).toThrowWithMessage(TypeError, "Not a Symbol object");
});