mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-24 02:08:58 +00:00
LibJS: Implement Temporal.PlainDateTime.prototype.with* methods
Includes with, withCalendar, and withPlainTime.
This commit is contained in:
parent
e3082b5bed
commit
990daaf63a
Notes:
github-actions[bot]
2024-11-24 10:45:09 +00:00
Author: https://github.com/trflynn89
Commit: 990daaf63a
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2544
7 changed files with 232 additions and 0 deletions
|
@ -0,0 +1,83 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.PlainDateTime.prototype.with).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const plainDateTime = new Temporal.PlainDateTime(1970, 1, 1);
|
||||
const values = [
|
||||
[{ year: 2021 }, new Temporal.PlainDateTime(2021, 1, 1)],
|
||||
[{ year: 2021, month: 7 }, new Temporal.PlainDateTime(2021, 7, 1)],
|
||||
[{ year: 2021, month: 7, day: 6 }, new Temporal.PlainDateTime(2021, 7, 6)],
|
||||
[{ year: 2021, monthCode: "M07", day: 6 }, new Temporal.PlainDateTime(2021, 7, 6)],
|
||||
[
|
||||
{ hour: 18, minute: 14, second: 47 },
|
||||
new Temporal.PlainDateTime(1970, 1, 1, 18, 14, 47),
|
||||
],
|
||||
[
|
||||
{
|
||||
year: 2021,
|
||||
month: 7,
|
||||
day: 6,
|
||||
hour: 18,
|
||||
minute: 14,
|
||||
second: 47,
|
||||
millisecond: 123,
|
||||
microsecond: 456,
|
||||
nanosecond: 789,
|
||||
},
|
||||
new Temporal.PlainDateTime(2021, 7, 6, 18, 14, 47, 123, 456, 789),
|
||||
],
|
||||
];
|
||||
for (const [arg, expected] of values) {
|
||||
expect(plainDateTime.with(arg).equals(expected)).toBeTrue();
|
||||
}
|
||||
|
||||
// Supplying the same values doesn't change the date/time, but still creates a new object
|
||||
const plainDateTimeLike = {
|
||||
year: plainDateTime.year,
|
||||
month: plainDateTime.month,
|
||||
day: plainDateTime.day,
|
||||
hour: plainDateTime.hour,
|
||||
minute: plainDateTime.minute,
|
||||
second: plainDateTime.second,
|
||||
millisecond: plainDateTime.millisecond,
|
||||
microsecond: plainDateTime.microsecond,
|
||||
nanosecond: plainDateTime.nanosecond,
|
||||
};
|
||||
expect(plainDateTime.with(plainDateTimeLike)).not.toBe(plainDateTime);
|
||||
expect(plainDateTime.with(plainDateTimeLike).equals(plainDateTime)).toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.PlainDateTime object", () => {
|
||||
expect(() => {
|
||||
Temporal.PlainDateTime.prototype.with.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDateTime");
|
||||
});
|
||||
|
||||
test("argument must be an object", () => {
|
||||
expect(() => {
|
||||
new Temporal.PlainDateTime(1970, 1, 1).with("foo");
|
||||
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
|
||||
expect(() => {
|
||||
new Temporal.PlainDateTime(1970, 1, 1).with(42);
|
||||
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
|
||||
});
|
||||
|
||||
test("argument must have one of 'day', 'hour', 'microsecond', 'millisecond', 'minute', 'month', 'monthCode', 'nanosecond', 'second', 'year'", () => {
|
||||
expect(() => {
|
||||
new Temporal.PlainDateTime(1970, 1, 1).with({});
|
||||
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
|
||||
});
|
||||
|
||||
test("argument must not have 'calendar' or 'timeZone'", () => {
|
||||
expect(() => {
|
||||
new Temporal.PlainDateTime(1970, 1, 1).with({ calendar: {} });
|
||||
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
|
||||
expect(() => {
|
||||
new Temporal.PlainDateTime(1970, 1, 1).with({ timeZone: {} });
|
||||
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
|
||||
});
|
||||
});
|
|
@ -0,0 +1,13 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.PlainDateTime.prototype.withCalendar).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const calendar = "gregory";
|
||||
const firstPlainDateTime = new Temporal.PlainDateTime(1, 2, 3);
|
||||
expect(firstPlainDateTime.calendarId).not.toBe(calendar);
|
||||
const secondPlainDateTime = firstPlainDateTime.withCalendar(calendar);
|
||||
expect(secondPlainDateTime.calendarId).toBe(calendar);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,20 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Temporal.PlainDateTime.prototype.withPlainTime).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const firstPlainDateTime = new Temporal.PlainDateTime(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
const plainTime = new Temporal.PlainTime(10, 11, 12, 13, 14, 15);
|
||||
const secondPlainDateTime = firstPlainDateTime.withPlainTime(plainTime);
|
||||
expect(secondPlainDateTime.year).toBe(1);
|
||||
expect(secondPlainDateTime.month).toBe(2);
|
||||
expect(secondPlainDateTime.day).toBe(3);
|
||||
expect(secondPlainDateTime.hour).toBe(10);
|
||||
expect(secondPlainDateTime.minute).toBe(11);
|
||||
expect(secondPlainDateTime.second).toBe(12);
|
||||
expect(secondPlainDateTime.millisecond).toBe(13);
|
||||
expect(secondPlainDateTime.microsecond).toBe(14);
|
||||
expect(secondPlainDateTime.nanosecond).toBe(15);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue