1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:38:11 +00:00

LibJS: Implement Temporal.PlainMonthDay.from

This commit is contained in:
Luke Wilde 2021-09-10 03:44:34 +01:00 committed by Linus Groh
parent d9d1e25db3
commit 2d5b15295a
3 changed files with 91 additions and 0 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/TypeCasts.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
@ -28,6 +29,9 @@ void PlainMonthDayConstructor::initialize(GlobalObject& global_object)
define_direct_property(vm.names.prototype, global_object.temporal_plain_month_day_prototype(), 0);
define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.from, from, 1, attr);
}
// 10.1.1 Temporal.PlainMonthDay ( isoMonth, isoDay [ , calendarLike [ , referenceISOYear ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday
@ -89,4 +93,31 @@ Value PlainMonthDayConstructor::construct(FunctionObject& new_target)
return create_temporal_month_day(global_object, m, d, *calendar, ref, &new_target);
}
// 10.2.2 Temporal.PlainMonthDay.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.from
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayConstructor::from)
{
// 1. Set options to ? GetOptionsObject(options).
auto* options = get_options_object(global_object, vm.argument(1));
if (vm.exception())
return {};
auto item = vm.argument(0);
// 2. If Type(item) is Object and item has an [[InitializedTemporalMonthDay]] internal slot, then
if (item.is_object() && is<PlainMonthDay>(item.as_object())) {
// a. Perform ? ToTemporalOverflow(options).
(void)to_temporal_overflow(global_object, *options);
if (vm.exception())
return {};
auto& plain_month_day_object = static_cast<PlainMonthDay&>(item.as_object());
// b. Return ? CreateTemporalMonthDay(item.[[ISOMonth]], item.[[ISODay]], item.[[Calendar]], item.[[ISOYear]]).
return create_temporal_month_day(global_object, plain_month_day_object.iso_month(), plain_month_day_object.iso_day(), plain_month_day_object.calendar(), plain_month_day_object.iso_year());
}
// 3. Return ? ToTemporalMonthDay(item, options).
return to_temporal_month_day(global_object, item, options);
}
}

View file

@ -23,6 +23,8 @@ public:
private:
virtual bool has_constructor() const override { return true; }
JS_DECLARE_NATIVE_FUNCTION(from);
};
}

View file

@ -0,0 +1,58 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.PlainMonthDay.from).toHaveLength(1);
});
test("PlainDate instance argument", () => {
const plainDate = new Temporal.PlainDate(2021, 7, 6);
const plainMonthDay = Temporal.PlainMonthDay.from(plainDate);
expect(plainMonthDay.monthCode).toBe("M07");
expect(plainMonthDay.day).toBe(6);
});
test("PlainMonthDay instance argument", () => {
const plainMonthDay_ = new Temporal.PlainMonthDay(7, 6);
const plainMonthDay = Temporal.PlainMonthDay.from(plainMonthDay_);
expect(plainMonthDay.monthCode).toBe("M07");
expect(plainMonthDay.day).toBe(6);
});
test("ZonedDateTime instance argument", () => {
const timeZone = new Temporal.TimeZone("UTC");
const zonedDateTime = new Temporal.ZonedDateTime(1625614921000000000n, timeZone);
const plainMonthDay = Temporal.PlainMonthDay.from(zonedDateTime);
expect(plainMonthDay.monthCode).toBe("M07");
expect(plainMonthDay.day).toBe(6);
});
test("fields object argument", () => {
const object = {
month: 7,
day: 6,
};
const plainMonthDay = Temporal.PlainMonthDay.from(object);
expect(plainMonthDay.monthCode).toBe("M07");
expect(plainMonthDay.day).toBe(6);
});
// Un-skip once ParseISODateTime, ToTemporalMonthDay & ParseTemporalMonthDayString are fully implemented
test.skip("PlainMonthDay string argument", () => {
const plainMonthDay = Temporal.PlainMonthDay.from("2021-07-06T23:42:01Z");
expect(plainMonthDay.monthCode).toBe("M07");
expect(plainMonthDay.day).toBe(6);
});
});
describe("errors", () => {
test("missing fields", () => {
expect(() => {
Temporal.PlainMonthDay.from({});
}).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
expect(() => {
Temporal.PlainMonthDay.from({ month: 1 });
}).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
expect(() => {
Temporal.PlainMonthDay.from({ day: 1 });
}).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
});
});