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

LibJS: Add the Number.prototype.toFixed method

This commit is contained in:
Idan Horowitz 2021-06-19 17:02:42 +03:00 committed by Linus Groh
parent 5e53a690ac
commit c31392510a
5 changed files with 79 additions and 0 deletions

View file

@ -0,0 +1,44 @@
describe("correct behavior", () => {
test("length", () => {
expect(Number.prototype.toFixed).toHaveLength(1);
});
test("basic functionality", () => {
[
[0, 5, "0.00000"],
[Infinity, 6, "Infinity"],
[-Infinity, 7, "-Infinity"],
[NaN, 8, "NaN"],
[12.81646112, 3, "12.816"],
[84.23, 4, "84.2300"],
[3.00003, 5, "3.00003"],
// Numbers >= 1e+21
[1e21, 5, "1e+21"],
[1e22, 0, "1e+22"],
].forEach(testCase => {
expect(testCase[0].toFixed(testCase[1])).toBe(testCase[2]);
});
});
test("decimal fixed digits gets converted to int", () => {
expect((30.521).toFixed(1.9)).toBe("30.5");
expect((30.521).toFixed(2.2)).toBe("30.52");
});
});
test("errors", () => {
test("must be called with numeric |this|", () => {
[true, [], {}, Symbol("foo"), "bar", 1n].forEach(value => {
expect(() => Number.prototype.toFixed.call(value)).toThrowWithMessage(
TypeError,
"Not a Number object"
);
});
});
test("fixed digits RangeError", () => {
[-Infinity, -5, 105, Infinity, NaN].forEach(value => {
expect(() => (0).toFixed(value)).toThrow(RangeError);
});
});
});