LibJS: Implement stringification Temporal.PlainTime prototypes

This commit is contained in:
Timothy Flynn 2024-11-23 11:10:35 -05:00 committed by Tim Flynn
commit ab3c825fa8
Notes: github-actions[bot] 2024-11-24 00:37:56 +00:00
7 changed files with 270 additions and 0 deletions

View file

@ -0,0 +1,18 @@
describe("correct behavior", () => {
test("length is 0", () => {
expect(Temporal.PlainTime.prototype.toJSON).toHaveLength(0);
});
test("basic functionality", () => {
const plainTime = new Temporal.PlainTime(18, 14, 47, 123, 456, 789);
expect(plainTime.toJSON()).toBe("18:14:47.123456789");
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainTime object", () => {
expect(() => {
Temporal.PlainTime.prototype.toJSON.call("foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainTime");
});
});

View file

@ -0,0 +1,18 @@
describe("correct behavior", () => {
test("length is 0", () => {
expect(Temporal.PlainTime.prototype.toLocaleString).toHaveLength(0);
});
test("basic functionality", () => {
const plainTime = new Temporal.PlainTime(18, 14, 47, 123, 456, 789);
expect(plainTime.toLocaleString()).toBe("18:14:47.123456789");
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainTime object", () => {
expect(() => {
Temporal.PlainTime.prototype.toLocaleString.call("foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainTime");
});
});

View file

@ -0,0 +1,59 @@
describe("correct behavior", () => {
test("length is 0", () => {
expect(Temporal.PlainTime.prototype.toString).toHaveLength(0);
});
test("basic functionality", () => {
const plainTime = new Temporal.PlainTime(18, 14, 47, 123, 456, 789);
expect(plainTime.toString()).toBe("18:14:47.123456789");
});
test("fractionalSecondDigits option", () => {
const plainTime = new Temporal.PlainTime(18, 14, 47, 123, 456);
const values = [
["auto", "18:14:47.123456"],
[0, "18:14:47"],
[1, "18:14:47.1"],
[2, "18:14:47.12"],
[3, "18:14:47.123"],
[4, "18:14:47.1234"],
[5, "18:14:47.12345"],
[6, "18:14:47.123456"],
[7, "18:14:47.1234560"],
[8, "18:14:47.12345600"],
[9, "18:14:47.123456000"],
];
for (const [fractionalSecondDigits, expected] of values) {
const options = { fractionalSecondDigits };
expect(plainTime.toString(options)).toBe(expected);
}
// Ignored when smallestUnit is given
expect(plainTime.toString({ smallestUnit: "minute", fractionalSecondDigits: 9 })).toBe(
"18:14"
);
});
test("smallestUnit option", () => {
const plainTime = new Temporal.PlainTime(18, 14, 47, 123, 456, 789);
const values = [
["minute", "18:14"],
["second", "18:14:47"],
["millisecond", "18:14:47.123"],
["microsecond", "18:14:47.123456"],
["nanosecond", "18:14:47.123456789"],
];
for (const [smallestUnit, expected] of values) {
const options = { smallestUnit };
expect(plainTime.toString(options)).toBe(expected);
}
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainTime object", () => {
expect(() => {
Temporal.PlainTime.prototype.toString.call("foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainTime");
});
});