mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-15 23:09:05 +00:00
LibJS: Implement stringification Temporal.Instant prototypes
This commit is contained in:
parent
90820873a2
commit
615ad70030
Notes:
github-actions[bot]
2024-11-25 12:34:21 +00:00
Author: https://github.com/trflynn89
Commit: 615ad70030
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2557
Reviewed-by: https://github.com/shannonbooth ✅
11 changed files with 387 additions and 4 deletions
|
@ -0,0 +1,18 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Temporal.Instant.prototype.toJSON).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const instant = new Temporal.Instant(1625614921123456789n);
|
||||
expect(instant.toJSON()).toBe("2021-07-06T23:42:01.123456789Z");
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.Instant object", () => {
|
||||
expect(() => {
|
||||
Temporal.Instant.prototype.toJSON.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Instant");
|
||||
});
|
||||
});
|
|
@ -0,0 +1,18 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Temporal.Instant.prototype.toLocaleString).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const instant = new Temporal.Instant(1625614921123456789n);
|
||||
expect(instant.toLocaleString()).toBe("2021-07-06T23:42:01.123456789Z");
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.Instant object", () => {
|
||||
expect(() => {
|
||||
Temporal.Instant.prototype.toLocaleString.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Instant");
|
||||
});
|
||||
});
|
|
@ -0,0 +1,72 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Temporal.Instant.prototype.toString).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const instant = new Temporal.Instant(1625614921123456789n);
|
||||
expect(instant.toString()).toBe("2021-07-06T23:42:01.123456789Z");
|
||||
});
|
||||
|
||||
test("timeZone option", () => {
|
||||
const instant = new Temporal.Instant(1625614921123456789n);
|
||||
const options = { timeZone: "+01:30" };
|
||||
expect(instant.toString(options)).toBe("2021-07-07T01:12:01.123456789+01:30");
|
||||
});
|
||||
|
||||
test("fractionalSecondDigits option", () => {
|
||||
const instant = new Temporal.Instant(1625614921123456000n);
|
||||
const values = [
|
||||
["auto", "2021-07-06T23:42:01.123456Z"],
|
||||
[0, "2021-07-06T23:42:01Z"],
|
||||
[1, "2021-07-06T23:42:01.1Z"],
|
||||
[2, "2021-07-06T23:42:01.12Z"],
|
||||
[3, "2021-07-06T23:42:01.123Z"],
|
||||
[4, "2021-07-06T23:42:01.1234Z"],
|
||||
[5, "2021-07-06T23:42:01.12345Z"],
|
||||
[6, "2021-07-06T23:42:01.123456Z"],
|
||||
[7, "2021-07-06T23:42:01.1234560Z"],
|
||||
[8, "2021-07-06T23:42:01.12345600Z"],
|
||||
[9, "2021-07-06T23:42:01.123456000Z"],
|
||||
];
|
||||
for (const [fractionalSecondDigits, expected] of values) {
|
||||
const options = { fractionalSecondDigits };
|
||||
expect(instant.toString(options)).toBe(expected);
|
||||
}
|
||||
|
||||
// Ignored when smallestUnit is given
|
||||
expect(instant.toString({ smallestUnit: "minute", fractionalSecondDigits: 9 })).toBe(
|
||||
"2021-07-06T23:42Z"
|
||||
);
|
||||
});
|
||||
|
||||
test("smallestUnit option", () => {
|
||||
const instant = new Temporal.Instant(1625614921123456789n);
|
||||
const values = [
|
||||
["minute", "2021-07-06T23:42Z"],
|
||||
["second", "2021-07-06T23:42:01Z"],
|
||||
["millisecond", "2021-07-06T23:42:01.123Z"],
|
||||
["microsecond", "2021-07-06T23:42:01.123456Z"],
|
||||
["nanosecond", "2021-07-06T23:42:01.123456789Z"],
|
||||
];
|
||||
for (const [smallestUnit, expected] of values) {
|
||||
const options = { smallestUnit };
|
||||
expect(instant.toString(options)).toBe(expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.Instant object", () => {
|
||||
expect(() => {
|
||||
Temporal.Instant.prototype.toString.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Instant");
|
||||
});
|
||||
|
||||
test("custom time zone doesn't have a getOffsetNanosecondsFor function", () => {
|
||||
const instant = new Temporal.Instant(0n);
|
||||
expect(() => {
|
||||
instant.toString({ timeZone: null });
|
||||
}).toThrowWithMessage(TypeError, "Invalid time zone name 'null");
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue