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

LibJS: Implement Temporal.Instant.fromEpochMicroseconds()

This commit is contained in:
Linus Groh 2021-07-09 12:10:16 +01:00
parent 66ff772254
commit 5872357b56
4 changed files with 75 additions and 0 deletions

View file

@ -0,0 +1,51 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.Instant.fromEpochMicroseconds).toHaveLength(1);
});
test("basic functionality", () => {
expect(Temporal.Instant.fromEpochMicroseconds(0n).epochMicroseconds).toBe(0n);
expect(Temporal.Instant.fromEpochMicroseconds(1n).epochMicroseconds).toBe(1n);
expect(Temporal.Instant.fromEpochMicroseconds(999_999_999n).epochMicroseconds).toBe(
999_999_999n
);
expect(
Temporal.Instant.fromEpochMicroseconds(8_640_000_000_000_000_000n).epochMicroseconds
).toBe(8_640_000_000_000_000_000n);
expect(Temporal.Instant.fromEpochMicroseconds(-0n).epochMicroseconds).toBe(0n);
expect(Temporal.Instant.fromEpochMicroseconds(-1n).epochMicroseconds).toBe(-1n);
expect(Temporal.Instant.fromEpochMicroseconds(-999_999_999n).epochMicroseconds).toBe(
-999_999_999n
);
expect(
Temporal.Instant.fromEpochMicroseconds(-8_640_000_000_000_000_000n).epochMicroseconds
).toBe(-8_640_000_000_000_000_000n);
});
});
test("errors", () => {
test("argument must be coercible to BigInt", () => {
expect(() => {
Temporal.Instant.fromEpochMicroseconds(123);
}).toThrowWithMessage(TypeError, "Cannot convert number to BigInt");
expect(() => {
Temporal.Instant.fromEpochMicroseconds("foo");
}).toThrowWithMessage(SyntaxError, "Invalid value for BigInt: foo");
});
test("out-of-range epoch microseconds value", () => {
expect(() => {
Temporal.Instant.fromEpochMicroseconds(8_640_000_000_000_000_001n);
}).toThrowWithMessage(
RangeError,
"Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
);
expect(() => {
Temporal.Instant.fromEpochMicroseconds(-8_640_000_000_000_000_001n);
}).toThrowWithMessage(
RangeError,
"Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
);
});
});