mirror of
https://github.com/RGBCube/serenity
synced 2025-05-27 21:25:06 +00:00

We use strtod to convert a string to number after checking whether the string is [+-]Infinity, however strtod also checks for either 'inf' or 'infinity' in a case-insensitive. There are still valid cases for strtod to return infinity like 10e100000 so we just check if the "number" contains 'i' or 'I' in which case the strtod infinity is not valid.
60 lines
2.4 KiB
JavaScript
60 lines
2.4 KiB
JavaScript
test("length is 1", () => {
|
|
expect(Number).toHaveLength(1);
|
|
});
|
|
|
|
test("constructor without new", () => {
|
|
expect(typeof Number()).toBe("number");
|
|
expect(typeof new Number()).toBe("object");
|
|
|
|
expect(Number()).toBe(0);
|
|
expect(Number(123)).toBe(123);
|
|
expect(Number(-123)).toBe(-123);
|
|
expect(Number(123n)).toBe(123);
|
|
expect(Number(-123n)).toBe(-123);
|
|
expect(Number("42")).toBe(42);
|
|
expect(Number(null)).toBe(0);
|
|
expect(Number(true)).toBe(1);
|
|
expect(Number("Infinity")).toBe(Infinity);
|
|
expect(Number("+Infinity")).toBe(Infinity);
|
|
expect(Number("-Infinity")).toBe(-Infinity);
|
|
expect(Number("infinity")).toBeNaN();
|
|
expect(Number("-infinity")).toBeNaN();
|
|
expect(Number("INFINITY")).toBeNaN();
|
|
expect(Number("-INFINITY")).toBeNaN();
|
|
expect(Number("inf")).toBeNaN();
|
|
expect(Number(undefined)).toBeNaN();
|
|
expect(Number({})).toBeNaN();
|
|
expect(Number({ a: 1 })).toBeNaN();
|
|
expect(Number([1, 2, 3])).toBeNaN();
|
|
expect(Number("foo")).toBeNaN();
|
|
expect(Number("10e10000")).toBe(Infinity);
|
|
expect(Number("-10e10000")).toBe(-Infinity);
|
|
});
|
|
|
|
test("constructor with new", () => {
|
|
expect(typeof new Number()).toBe("object");
|
|
|
|
expect(new Number().valueOf()).toBe(0);
|
|
expect(new Number(123).valueOf()).toBe(123);
|
|
expect(new Number(-123).valueOf()).toBe(-123);
|
|
expect(new Number(123n).valueOf()).toBe(123);
|
|
expect(new Number(-123n).valueOf()).toBe(-123);
|
|
expect(new Number("42").valueOf()).toBe(42);
|
|
expect(new Number(null).valueOf()).toBe(0);
|
|
expect(new Number(true).valueOf()).toBe(1);
|
|
expect(new Number("Infinity").valueOf()).toBe(Infinity);
|
|
expect(new Number("+Infinity").valueOf()).toBe(Infinity);
|
|
expect(new Number("-Infinity").valueOf()).toBe(-Infinity);
|
|
expect(new Number("infinity").valueOf()).toBeNaN();
|
|
expect(new Number("-infinity").valueOf()).toBeNaN();
|
|
expect(new Number("INFINITY").valueOf()).toBeNaN();
|
|
expect(new Number("-INFINITY").valueOf()).toBeNaN();
|
|
expect(new Number("inf").valueOf()).toBeNaN();
|
|
expect(new Number(undefined).valueOf()).toBeNaN();
|
|
expect(new Number({}).valueOf()).toBeNaN();
|
|
expect(new Number({ a: 1 }).valueOf()).toBeNaN();
|
|
expect(new Number([1, 2, 3]).valueOf()).toBeNaN();
|
|
expect(new Number("foo").valueOf()).toBeNaN();
|
|
expect(new Number("10e10000").valueOf()).toBe(Infinity);
|
|
expect(new Number("-10e10000").valueOf()).toBe(-Infinity);
|
|
});
|