1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
describe("correct behavior", () => {
test("basic functionality", () => {
expect(BigInt).toHaveLength(1);
expect(BigInt.name).toBe("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);
});
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
);
});
test("constructor with objects", () => {
expect(BigInt([])).toBe(0n);
});
});
describe("errors", () => {
test('cannot be constructed with "new"', () => {
expect(() => {
new BigInt();
}).toThrowWithMessage(TypeError, "BigInt is not a constructor");
});
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);
}).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
});
});
test("invalid numeric arguments", () => {
[1.23, Infinity, -Infinity, NaN].forEach(value => {
expect(() => {
BigInt(value);
}).toThrowWithMessage(RangeError, "Cannot convert non-integral number to BigInt");
});
});
});
|