mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-24 10:12:25 -05:00
LibJS: Implement Temporal.PlainDate.prototype.until/since
This commit is contained in:
parent
abc6b679c3
commit
3aa4cdd314
Notes:
github-actions[bot]
2024-11-23 13:47:46 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/3aa4cdd3149 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2513 Reviewed-by: https://github.com/shannonbooth ✅
6 changed files with 587 additions and 0 deletions
|
@ -316,6 +316,60 @@ i8 compare_iso_date(ISODate iso_date1, ISODate iso_date2)
|
|||
return 0;
|
||||
}
|
||||
|
||||
// 3.5.13 DifferenceTemporalPlainDate ( operation, temporalDate, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencetemporalplaindate
|
||||
ThrowCompletionOr<GC::Ref<Duration>> difference_temporal_plain_date(VM& vm, DurationOperation operation, PlainDate const& temporal_date, Value other_value, Value options)
|
||||
{
|
||||
// 1. Set other to ? ToTemporalDate(other).
|
||||
auto other = TRY(to_temporal_date(vm, other_value));
|
||||
|
||||
// 2. If CalendarEquals(temporalDate.[[Calendar]], other.[[Calendar]]) is false, throw a RangeError exception.
|
||||
if (!calendar_equals(temporal_date.calendar(), other->calendar()))
|
||||
return vm.throw_completion<RangeError>(ErrorType::TemporalDifferentCalendars);
|
||||
|
||||
// 3. Let resolvedOptions be ? GetOptionsObject(options).
|
||||
auto resolved_options = TRY(get_options_object(vm, options));
|
||||
|
||||
// 4. Let settings be ? GetDifferenceSettings(operation, resolvedOptions, DATE, « », DAY, DAY).
|
||||
auto settings = TRY(get_difference_settings(vm, operation, resolved_options, UnitGroup::Date, {}, Unit::Day, Unit::Day));
|
||||
|
||||
// 5. If CompareISODate(temporalDate.[[ISODate]], other.[[ISODate]]) = 0, then
|
||||
if (compare_iso_date(temporal_date.iso_date(), other->iso_date()) == 0) {
|
||||
// a. Return ! CreateTemporalDuration(0, 0, 0, 0, 0, 0, 0, 0, 0, 0).
|
||||
return MUST(create_temporal_duration(vm, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
|
||||
}
|
||||
|
||||
// 6. Let dateDifference be CalendarDateUntil(temporalDate.[[Calendar]], temporalDate.[[ISODate]], other.[[ISODate]], settings.[[LargestUnit]]).
|
||||
auto date_difference = calendar_date_until(vm, temporal_date.calendar(), temporal_date.iso_date(), other->iso_date(), settings.largest_unit);
|
||||
|
||||
// 7. Let duration be ! CombineDateAndTimeDuration(dateDifference, 0).
|
||||
auto duration = MUST(combine_date_and_time_duration(vm, date_difference, TimeDuration { 0 }));
|
||||
|
||||
// 8. If settings.[[SmallestUnit]] is not DAY or settings.[[RoundingIncrement]] ≠ 1, then
|
||||
if (settings.smallest_unit != Unit::Day || settings.rounding_increment != 1) {
|
||||
// a. Let isoDateTime be CombineISODateAndTimeRecord(temporalDate.[[ISODate]], MidnightTimeRecord()).
|
||||
auto iso_date_time = combine_iso_date_and_time_record(temporal_date.iso_date(), midnight_time_record());
|
||||
|
||||
// b. Let isoDateTimeOther be CombineISODateAndTimeRecord(other.[[ISODate]], MidnightTimeRecord()).
|
||||
auto iso_date_time_other = combine_iso_date_and_time_record(other->iso_date(), midnight_time_record());
|
||||
|
||||
// c. Let destEpochNs be GetUTCEpochNanoseconds(isoDateTimeOther).
|
||||
auto dest_epoch_ns = get_utc_epoch_nanoseconds(iso_date_time_other);
|
||||
|
||||
// d. Set duration to ? RoundRelativeDuration(duration, destEpochNs, isoDateTime, UNSET, temporalDate.[[Calendar]], settings.[[LargestUnit]], settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]]).
|
||||
duration = TRY(round_relative_duration(vm, move(duration), dest_epoch_ns, iso_date_time, {}, temporal_date.calendar(), settings.largest_unit, settings.rounding_increment, settings.smallest_unit, settings.rounding_mode));
|
||||
}
|
||||
|
||||
// 9. Let result be ! TemporalDurationFromInternal(duration, DAY).
|
||||
auto result = MUST(temporal_duration_from_internal(vm, duration, Unit::Day));
|
||||
|
||||
// 10. If operation is since, set result to CreateNegatedTemporalDuration(result).
|
||||
if (operation == DurationOperation::Since)
|
||||
result = create_negated_temporal_duration(vm, result);
|
||||
|
||||
// 11. Return result.
|
||||
return result;
|
||||
}
|
||||
|
||||
// 3.5.14 AddDurationToDate ( operation, temporalDate, temporalDurationLike, options ), https://tc39.es/proposal-temporal/#sec-temporal-adddurationtodate
|
||||
ThrowCompletionOr<GC::Ref<PlainDate>> add_duration_to_date(VM& vm, ArithmeticOperation operation, PlainDate const& temporal_date, Value temporal_duration_like, Value options)
|
||||
{
|
||||
|
|
|
@ -51,6 +51,7 @@ String pad_iso_year(i32 year);
|
|||
String temporal_date_to_string(PlainDate const&, ShowCalendar);
|
||||
bool iso_date_within_limits(ISODate);
|
||||
i8 compare_iso_date(ISODate, ISODate);
|
||||
ThrowCompletionOr<GC::Ref<Duration>> difference_temporal_plain_date(VM&, DurationOperation, PlainDate const&, Value other, Value options);
|
||||
ThrowCompletionOr<GC::Ref<PlainDate>> add_duration_to_date(VM&, ArithmeticOperation, PlainDate const&, Value temporal_duration_like, Value options);
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Temporal/Calendar.h>
|
||||
#include <LibJS/Runtime/Temporal/Duration.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainDatePrototype.h>
|
||||
|
||||
namespace JS::Temporal {
|
||||
|
@ -51,6 +52,8 @@ void PlainDatePrototype::initialize(Realm& realm)
|
|||
define_native_function(realm, vm.names.subtract, subtract, 1, attr);
|
||||
define_native_function(realm, vm.names.with, with, 1, attr);
|
||||
define_native_function(realm, vm.names.withCalendar, with_calendar, 1, attr);
|
||||
define_native_function(realm, vm.names.until, until, 1, attr);
|
||||
define_native_function(realm, vm.names.since, since, 1, attr);
|
||||
define_native_function(realm, vm.names.equals, equals, 1, attr);
|
||||
define_native_function(realm, vm.names.toString, to_string, 0, attr);
|
||||
define_native_function(realm, vm.names.toLocaleString, to_locale_string, 0, attr);
|
||||
|
@ -268,6 +271,34 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::with_calendar)
|
|||
return MUST(create_temporal_date(vm, temporal_date->iso_date(), calendar));
|
||||
}
|
||||
|
||||
// 3.3.25 Temporal.PlainDate.prototype.until ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.until
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::until)
|
||||
{
|
||||
auto other = vm.argument(0);
|
||||
auto options = vm.argument(1);
|
||||
|
||||
// 1. Let temporalDate be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
|
||||
auto temporal_date = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return ? DifferenceTemporalPlainDate(UNTIL, temporalDate, other, options).
|
||||
return TRY(difference_temporal_plain_date(vm, DurationOperation::Until, temporal_date, other, options));
|
||||
}
|
||||
|
||||
// 3.3.26 Temporal.PlainDate.prototype.since ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.since
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::since)
|
||||
{
|
||||
auto other = vm.argument(0);
|
||||
auto options = vm.argument(1);
|
||||
|
||||
// 1. Let temporalDate be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
|
||||
auto temporal_date = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return ? DifferenceTemporalPlainDate(SINCE, temporalDate, other, options).
|
||||
return TRY(difference_temporal_plain_date(vm, DurationOperation::Since, temporal_date, other, options));
|
||||
}
|
||||
|
||||
// 3.3.27 Temporal.PlainDate.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.equals
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::equals)
|
||||
{
|
||||
|
|
|
@ -43,6 +43,8 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(subtract);
|
||||
JS_DECLARE_NATIVE_FUNCTION(with);
|
||||
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
|
||||
JS_DECLARE_NATIVE_FUNCTION(until);
|
||||
JS_DECLARE_NATIVE_FUNCTION(since);
|
||||
JS_DECLARE_NATIVE_FUNCTION(equals);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
|
||||
|
|
|
@ -0,0 +1,249 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.PlainDate.prototype.since).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const dateOne = new Temporal.PlainDate(2021, 11, 14);
|
||||
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
|
||||
const sinceDuration = dateTwo.since(dateOne);
|
||||
|
||||
expect(sinceDuration.years).toBe(0);
|
||||
expect(sinceDuration.months).toBe(0);
|
||||
expect(sinceDuration.weeks).toBe(0);
|
||||
expect(sinceDuration.days).toBe(406);
|
||||
expect(sinceDuration.hours).toBe(0);
|
||||
expect(sinceDuration.minutes).toBe(0);
|
||||
expect(sinceDuration.seconds).toBe(0);
|
||||
expect(sinceDuration.milliseconds).toBe(0);
|
||||
expect(sinceDuration.microseconds).toBe(0);
|
||||
expect(sinceDuration.nanoseconds).toBe(0);
|
||||
});
|
||||
|
||||
test("equal dates", () => {
|
||||
const equalDateOne = new Temporal.PlainDate(1, 1, 1);
|
||||
const equalDateTwo = new Temporal.PlainDate(1, 1, 1);
|
||||
|
||||
const checkResults = result => {
|
||||
expect(result.years).toBe(0);
|
||||
expect(result.months).toBe(0);
|
||||
expect(result.weeks).toBe(0);
|
||||
expect(result.days).toBe(0);
|
||||
expect(result.hours).toBe(0);
|
||||
expect(result.minutes).toBe(0);
|
||||
expect(result.seconds).toBe(0);
|
||||
expect(result.milliseconds).toBe(0);
|
||||
expect(result.microseconds).toBe(0);
|
||||
expect(result.nanoseconds).toBe(0);
|
||||
};
|
||||
|
||||
checkResults(equalDateOne.since(equalDateOne));
|
||||
checkResults(equalDateTwo.since(equalDateTwo));
|
||||
checkResults(equalDateOne.since(equalDateTwo));
|
||||
checkResults(equalDateTwo.since(equalDateOne));
|
||||
});
|
||||
|
||||
test("negative direction", () => {
|
||||
const dateOne = new Temporal.PlainDate(2021, 11, 14);
|
||||
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
|
||||
const sinceDuration = dateOne.since(dateTwo);
|
||||
|
||||
expect(sinceDuration.years).toBe(0);
|
||||
expect(sinceDuration.months).toBe(0);
|
||||
expect(sinceDuration.weeks).toBe(0);
|
||||
expect(sinceDuration.days).toBe(-406);
|
||||
expect(sinceDuration.hours).toBe(0);
|
||||
expect(sinceDuration.minutes).toBe(0);
|
||||
expect(sinceDuration.seconds).toBe(0);
|
||||
expect(sinceDuration.milliseconds).toBe(0);
|
||||
expect(sinceDuration.microseconds).toBe(0);
|
||||
expect(sinceDuration.nanoseconds).toBe(0);
|
||||
});
|
||||
|
||||
test("largestUnit option", () => {
|
||||
const values = [
|
||||
["year", { years: 1, months: 1, days: 11 }],
|
||||
["month", { months: 13, days: 11 }],
|
||||
["week", { weeks: 58 }],
|
||||
["day", { days: 406 }],
|
||||
];
|
||||
|
||||
const dateOne = new Temporal.PlainDate(2021, 11, 14);
|
||||
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
|
||||
|
||||
for (const [largestUnit, durationLike] of values) {
|
||||
const singularOptions = { largestUnit };
|
||||
const pluralOptions = { largestUnit: `${largestUnit}s` };
|
||||
|
||||
const propertiesToCheck = Object.keys(durationLike);
|
||||
|
||||
// Positive direction
|
||||
const positiveSingularResult = dateTwo.since(dateOne, singularOptions);
|
||||
for (const property of propertiesToCheck)
|
||||
expect(positiveSingularResult[property]).toBe(durationLike[property]);
|
||||
|
||||
const positivePluralResult = dateTwo.since(dateOne, pluralOptions);
|
||||
for (const property of propertiesToCheck)
|
||||
expect(positivePluralResult[property]).toBe(durationLike[property]);
|
||||
|
||||
// Negative direction
|
||||
const negativeSingularResult = dateOne.since(dateTwo, singularOptions);
|
||||
for (const property of propertiesToCheck)
|
||||
expect(negativeSingularResult[property]).toBe(-durationLike[property]);
|
||||
|
||||
const negativePluralResult = dateOne.since(dateTwo, pluralOptions);
|
||||
for (const property of propertiesToCheck)
|
||||
expect(negativePluralResult[property]).toBe(-durationLike[property]);
|
||||
}
|
||||
});
|
||||
|
||||
test("PlainDate string argument", () => {
|
||||
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
|
||||
const sinceDuration = dateTwo.since("2021-11-14");
|
||||
|
||||
expect(sinceDuration.years).toBe(0);
|
||||
expect(sinceDuration.months).toBe(0);
|
||||
expect(sinceDuration.weeks).toBe(0);
|
||||
expect(sinceDuration.days).toBe(406);
|
||||
expect(sinceDuration.hours).toBe(0);
|
||||
expect(sinceDuration.minutes).toBe(0);
|
||||
expect(sinceDuration.seconds).toBe(0);
|
||||
expect(sinceDuration.milliseconds).toBe(0);
|
||||
expect(sinceDuration.microseconds).toBe(0);
|
||||
expect(sinceDuration.nanoseconds).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.PlainDate object", () => {
|
||||
expect(() => {
|
||||
Temporal.PlainDate.prototype.since.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDate");
|
||||
});
|
||||
|
||||
test("cannot compare dates from different calendars", () => {
|
||||
const dateOneWithCalendar = new Temporal.PlainDate(2021, 11, 14, "iso8601");
|
||||
const dateTwoWithCalendar = new Temporal.PlainDate(2022, 12, 25, "gregory");
|
||||
|
||||
expect(() => {
|
||||
dateOneWithCalendar.since(dateTwoWithCalendar);
|
||||
}).toThrowWithMessage(RangeError, "Cannot compare dates from two different calendars");
|
||||
});
|
||||
|
||||
test("disallowed units", () => {
|
||||
const dateOne = new Temporal.PlainDate(2021, 11, 14);
|
||||
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
|
||||
|
||||
const disallowedUnits = [
|
||||
"hour",
|
||||
"minute",
|
||||
"second",
|
||||
"millisecond",
|
||||
"microsecond",
|
||||
"nanosecond",
|
||||
];
|
||||
|
||||
for (const smallestUnit of disallowedUnits) {
|
||||
const singularSmallestUnitOptions = { smallestUnit };
|
||||
const pluralSmallestUnitOptions = { smallestUnit: `${smallestUnit}s` };
|
||||
|
||||
expect(() => {
|
||||
dateOne.since(dateTwo, singularSmallestUnitOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`${smallestUnit} is not a valid value for option smallestUnit`
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
dateOne.since(dateTwo, pluralSmallestUnitOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`${smallestUnit}s is not a valid value for option smallestUnit`
|
||||
);
|
||||
}
|
||||
|
||||
for (const largestUnit of disallowedUnits) {
|
||||
const singularLargestUnitOptions = { largestUnit };
|
||||
const pluralLargestUnitOptions = { largestUnit: `${largestUnit}s` };
|
||||
|
||||
expect(() => {
|
||||
dateOne.since(dateTwo, singularLargestUnitOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`${largestUnit} is not a valid value for option largestUnit`
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
dateOne.since(dateTwo, pluralLargestUnitOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`${largestUnit}s is not a valid value for option largestUnit`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("invalid unit range", () => {
|
||||
// smallestUnit -> disallowed largestUnits, see validate_temporal_unit_range
|
||||
// Note that all the "smallestUnits" are all the allowedUnits.
|
||||
const invalidRanges = [
|
||||
["year", ["month", "week", "day"]],
|
||||
["month", ["week", "day"]],
|
||||
["week", ["day"]],
|
||||
];
|
||||
|
||||
const dateOne = new Temporal.PlainDate(2021, 11, 14);
|
||||
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
|
||||
|
||||
for (const [smallestUnit, disallowedLargestUnits] of invalidRanges) {
|
||||
for (const disallowedUnit of disallowedLargestUnits) {
|
||||
const pluralSmallestUnit = `${smallestUnit}s`;
|
||||
const pluralDisallowedUnit = `${disallowedUnit}s`;
|
||||
|
||||
const singularSmallestSingularDisallowedOptions = {
|
||||
smallestUnit,
|
||||
largestUnit: disallowedUnit,
|
||||
};
|
||||
const singularSmallestPluralDisallowedOptions = {
|
||||
smallestUnit,
|
||||
largestUnit: pluralDisallowedUnit,
|
||||
};
|
||||
const pluralSmallestSingularDisallowedOptions = {
|
||||
smallestUnit: pluralSmallestUnit,
|
||||
largestUnit: disallowedUnit,
|
||||
};
|
||||
const pluralSmallestPluralDisallowedOptions = {
|
||||
smallestUnit: pluralSmallestUnit,
|
||||
largestUnit: pluralDisallowedUnit,
|
||||
};
|
||||
|
||||
expect(() => {
|
||||
dateOne.since(dateTwo, singularSmallestSingularDisallowedOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
dateOne.since(dateTwo, singularSmallestPluralDisallowedOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
dateOne.since(dateTwo, pluralSmallestSingularDisallowedOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
dateOne.since(dateTwo, pluralSmallestPluralDisallowedOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
|
@ -0,0 +1,250 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.PlainDate.prototype.until).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const dateOne = new Temporal.PlainDate(2021, 11, 14);
|
||||
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
|
||||
const untilDuration = dateOne.until(dateTwo);
|
||||
|
||||
expect(untilDuration.years).toBe(0);
|
||||
expect(untilDuration.months).toBe(0);
|
||||
expect(untilDuration.weeks).toBe(0);
|
||||
expect(untilDuration.days).toBe(406);
|
||||
expect(untilDuration.hours).toBe(0);
|
||||
expect(untilDuration.minutes).toBe(0);
|
||||
expect(untilDuration.seconds).toBe(0);
|
||||
expect(untilDuration.milliseconds).toBe(0);
|
||||
expect(untilDuration.microseconds).toBe(0);
|
||||
expect(untilDuration.nanoseconds).toBe(0);
|
||||
});
|
||||
|
||||
test("equal dates", () => {
|
||||
const equalDateOne = new Temporal.PlainDate(1, 1, 1);
|
||||
const equalDateTwo = new Temporal.PlainDate(1, 1, 1);
|
||||
|
||||
const checkResults = result => {
|
||||
expect(result.years).toBe(0);
|
||||
expect(result.months).toBe(0);
|
||||
expect(result.weeks).toBe(0);
|
||||
expect(result.days).toBe(0);
|
||||
expect(result.hours).toBe(0);
|
||||
expect(result.minutes).toBe(0);
|
||||
expect(result.seconds).toBe(0);
|
||||
expect(result.milliseconds).toBe(0);
|
||||
expect(result.microseconds).toBe(0);
|
||||
expect(result.nanoseconds).toBe(0);
|
||||
};
|
||||
|
||||
checkResults(equalDateOne.until(equalDateOne));
|
||||
checkResults(equalDateTwo.until(equalDateTwo));
|
||||
checkResults(equalDateOne.until(equalDateTwo));
|
||||
checkResults(equalDateTwo.until(equalDateOne));
|
||||
});
|
||||
|
||||
test("negative direction", () => {
|
||||
const dateOne = new Temporal.PlainDate(2021, 11, 14);
|
||||
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
|
||||
const untilDuration = dateTwo.until(dateOne);
|
||||
|
||||
expect(untilDuration.years).toBe(0);
|
||||
expect(untilDuration.months).toBe(0);
|
||||
expect(untilDuration.weeks).toBe(0);
|
||||
expect(untilDuration.days).toBe(-406);
|
||||
expect(untilDuration.hours).toBe(0);
|
||||
expect(untilDuration.minutes).toBe(0);
|
||||
expect(untilDuration.seconds).toBe(0);
|
||||
expect(untilDuration.milliseconds).toBe(0);
|
||||
expect(untilDuration.microseconds).toBe(0);
|
||||
expect(untilDuration.nanoseconds).toBe(0);
|
||||
});
|
||||
|
||||
test("largestUnit option", () => {
|
||||
const values = [
|
||||
["year", { years: 1, months: 1, days: 11 }],
|
||||
["month", { months: 13, days: 11 }],
|
||||
["week", { weeks: 58 }],
|
||||
["day", { days: 406 }],
|
||||
];
|
||||
|
||||
const dateOne = new Temporal.PlainDate(2021, 11, 14);
|
||||
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
|
||||
|
||||
for (const [largestUnit, durationLike] of values) {
|
||||
const singularOptions = { largestUnit };
|
||||
const pluralOptions = { largestUnit: `${largestUnit}s` };
|
||||
|
||||
const propertiesToCheck = Object.keys(durationLike);
|
||||
|
||||
// Positive direction
|
||||
const positiveSingularResult = dateOne.until(dateTwo, singularOptions);
|
||||
for (const property of propertiesToCheck)
|
||||
expect(positiveSingularResult[property]).toBe(durationLike[property]);
|
||||
|
||||
const positivePluralResult = dateOne.until(dateTwo, pluralOptions);
|
||||
for (const property of propertiesToCheck)
|
||||
expect(positivePluralResult[property]).toBe(durationLike[property]);
|
||||
|
||||
// Negative direction
|
||||
const negativeSingularResult = dateTwo.until(dateOne, singularOptions);
|
||||
for (const property of propertiesToCheck)
|
||||
expect(negativeSingularResult[property]).toBe(-durationLike[property]);
|
||||
|
||||
const negativePluralResult = dateTwo.until(dateOne, pluralOptions);
|
||||
for (const property of propertiesToCheck)
|
||||
expect(negativePluralResult[property]).toBe(-durationLike[property]);
|
||||
}
|
||||
});
|
||||
|
||||
test("PlainDate string argument", () => {
|
||||
const dateOne = new Temporal.PlainDate(2021, 11, 14);
|
||||
const untilDuration = dateOne.until("2022-12-25");
|
||||
|
||||
expect(untilDuration.years).toBe(0);
|
||||
expect(untilDuration.months).toBe(0);
|
||||
expect(untilDuration.weeks).toBe(0);
|
||||
expect(untilDuration.days).toBe(406);
|
||||
expect(untilDuration.hours).toBe(0);
|
||||
expect(untilDuration.minutes).toBe(0);
|
||||
expect(untilDuration.seconds).toBe(0);
|
||||
expect(untilDuration.milliseconds).toBe(0);
|
||||
expect(untilDuration.microseconds).toBe(0);
|
||||
expect(untilDuration.nanoseconds).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.PlainDate object", () => {
|
||||
expect(() => {
|
||||
Temporal.PlainDate.prototype.until.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDate");
|
||||
});
|
||||
|
||||
test("cannot compare dates from different calendars", () => {
|
||||
const dateOneWithCalendar = new Temporal.PlainDate(2021, 11, 14, "iso8601");
|
||||
const dateTwoWithCalendar = new Temporal.PlainDate(2022, 12, 25, "gregory");
|
||||
|
||||
expect(() => {
|
||||
dateOneWithCalendar.until(dateTwoWithCalendar);
|
||||
}).toThrowWithMessage(RangeError, "Cannot compare dates from two different calendars");
|
||||
});
|
||||
|
||||
test("disallowed units", () => {
|
||||
const dateOne = new Temporal.PlainDate(2021, 11, 14);
|
||||
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
|
||||
|
||||
const disallowedUnits = [
|
||||
"hour",
|
||||
"minute",
|
||||
"second",
|
||||
"millisecond",
|
||||
"microsecond",
|
||||
"nanosecond",
|
||||
];
|
||||
|
||||
for (const smallestUnit of disallowedUnits) {
|
||||
const singularSmallestUnitOptions = { smallestUnit };
|
||||
const pluralSmallestUnitOptions = { smallestUnit: `${smallestUnit}s` };
|
||||
|
||||
expect(() => {
|
||||
dateOne.until(dateTwo, singularSmallestUnitOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`${smallestUnit} is not a valid value for option smallestUnit`
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
dateOne.until(dateTwo, pluralSmallestUnitOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`${smallestUnit}s is not a valid value for option smallestUnit`
|
||||
);
|
||||
}
|
||||
|
||||
for (const largestUnit of disallowedUnits) {
|
||||
const singularLargestUnitOptions = { largestUnit };
|
||||
const pluralLargestUnitOptions = { largestUnit: `${largestUnit}s` };
|
||||
|
||||
expect(() => {
|
||||
dateOne.until(dateTwo, singularLargestUnitOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`${largestUnit} is not a valid value for option largestUnit`
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
dateOne.until(dateTwo, pluralLargestUnitOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`${largestUnit}s is not a valid value for option largestUnit`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("invalid unit range", () => {
|
||||
// smallestUnit -> disallowed largestUnits, see validate_temporal_unit_range
|
||||
// Note that all the "smallestUnits" are all the allowedUnits.
|
||||
const invalidRanges = [
|
||||
["year", ["month", "week", "day"]],
|
||||
["month", ["week", "day"]],
|
||||
["week", ["day"]],
|
||||
];
|
||||
|
||||
const dateOne = new Temporal.PlainDate(2021, 11, 14);
|
||||
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
|
||||
|
||||
for (const [smallestUnit, disallowedLargestUnits] of invalidRanges) {
|
||||
const pluralSmallestUnit = `${smallestUnit}s`;
|
||||
|
||||
for (const disallowedUnit of disallowedLargestUnits) {
|
||||
const pluralDisallowedUnit = `${disallowedUnit}s`;
|
||||
|
||||
const singularSmallestSingularDisallowedOptions = {
|
||||
smallestUnit,
|
||||
largestUnit: disallowedUnit,
|
||||
};
|
||||
const singularSmallestPluralDisallowedOptions = {
|
||||
smallestUnit,
|
||||
largestUnit: pluralDisallowedUnit,
|
||||
};
|
||||
const pluralSmallestSingularDisallowedOptions = {
|
||||
smallestUnit: pluralSmallestUnit,
|
||||
largestUnit: disallowedUnit,
|
||||
};
|
||||
const pluralSmallestPluralDisallowedOptions = {
|
||||
smallestUnit: pluralSmallestUnit,
|
||||
largestUnit: pluralDisallowedUnit,
|
||||
};
|
||||
|
||||
expect(() => {
|
||||
dateOne.until(dateTwo, singularSmallestSingularDisallowedOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
dateOne.until(dateTwo, singularSmallestPluralDisallowedOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
dateOne.until(dateTwo, pluralSmallestSingularDisallowedOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
dateOne.until(dateTwo, pluralSmallestPluralDisallowedOptions);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue