ladybird/Libraries/LibJS/Tests/builtins/BigInt/BigInt.js

79 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-06-06 01:14:10 +01:00
load("test-common.js");
try {
assert(BigInt.length === 1);
assert(BigInt.name === "BigInt");
2020-06-06 01:14:10 +01:00
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") ===
3323214327642987348732109829832143298746432437532197321n
);
2020-06-06 01:14:10 +01:00
assertThrowsError(
() => {
new BigInt();
},
{
error: TypeError,
message: "BigInt is not a constructor",
}
);
2020-06-06 01:14:10 +01:00
[null, undefined, Symbol()].forEach(value => {
assertThrowsError(
() => {
BigInt(value);
},
{
error: TypeError,
message:
typeof value === "symbol"
? "Cannot convert symbol to BigInt"
: `Cannot convert ${value} to BigInt`,
}
);
});
2020-06-06 01:14:10 +01:00
["foo", "123n", "1+1", {}, function () {}].forEach(value => {
assertThrowsError(
() => {
BigInt(value);
},
{
error: SyntaxError,
message: `Invalid value for BigInt: ${value}`,
}
);
});
2020-06-06 01:14:10 +01:00
[1.23, Infinity, -Infinity, NaN].forEach(value => {
assertThrowsError(
() => {
BigInt(value);
},
{
error: RangeError,
message: "BigInt argument must be an integer",
}
);
});
2020-06-06 01:14:10 +01:00
console.log("PASS");
2020-06-06 01:14:10 +01:00
} catch (e) {
console.log("FAIL: " + e);
2020-06-06 01:14:10 +01:00
}