Skip to content

Commit

Permalink
Temporal: Tests for round() and since()/until() using the same code path
Browse files Browse the repository at this point in the history
This should produce all the same results (except for a change to weeks
balancing in round(), which is now more consistent with since()/until())
but leads to different observable user code calls.

See tc39/proposal-temporal#2742
  • Loading branch information
ptomato committed Apr 2, 2024
1 parent 9531185 commit 01dcb15
Show file tree
Hide file tree
Showing 32 changed files with 326 additions and 319 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,26 @@ const relativeTo = new Temporal.ZonedDateTime(0n, timeZone, calendar);
// Rounding with smallestUnit a calendar unit.
// The calls come from these paths:
// Duration.round() ->
// RoundDuration ->
// MoveRelativeZonedDateTime -> AddZonedDateTime -> calendar.dateAdd()
// MoveRelativeDate -> calendar.dateAdd()
// BalanceDateDurationRelative -> calendar.dateAdd()
// AddZonedDateTime -> calendar.dateAdd()
// DifferenceZonedDateTimeWithRounding ->
// RoundDuration ->
// MoveRelativeZonedDateTime -> AddZonedDateTime -> calendar.dateAdd()
// MoveRelativeDate -> calendar.dateAdd() (2x)
// BalanceDateDurationRelative -> calendar.dateAdd()

const instance1 = new Temporal.Duration(1, 1, 1, 1, 1);
instance1.round({ smallestUnit: "weeks", relativeTo });
assert.sameValue(calendar.dateAddCallCount, 4, "rounding with calendar smallestUnit");

// Rounding with a non-default largestUnit to cover the path in
// UnbalanceDurationRelative where larger units are converted into smaller
// units; and with a smallestUnit larger than days to cover the path in
// RoundDuration where days are converted into larger units.
// The calls come from these paths:
// Duration.round() ->
// UnbalanceDurationRelative -> MoveRelativeDate -> calendar.dateAdd()
// RoundDuration -> MoveRelativeDate -> calendar.dateAdd() (2x)
// BalanceDateDurationRelative -> calendar.dateAdd()
// MoveRelativeZonedDateTime -> AddZonedDateTime -> calendar.dateAdd()

calendar.dateAddCallCount = 0;

const instance2 = new Temporal.Duration(0, 1, 1, 1);
instance2.round({ largestUnit: "weeks", smallestUnit: "weeks", relativeTo });
assert.sameValue(calendar.dateAddCallCount, 6, "rounding with non-default largestUnit and calendar smallestUnit");
const instance = new Temporal.Duration(1, 1, 1, 1, 1);
instance.round({ smallestUnit: "weeks", relativeTo });
assert.sameValue(calendar.dateAddCallCount, 5, "rounding with calendar smallestUnit");

// Rounding with smallestUnit days only.
// The calls come from these paths:
// Duration.round() ->
// RoundDuration ->
// MoveRelativeZonedDateTime -> AddZonedDateTime -> calendar.dateAdd()
// BalanceDateDurationRelative -> calendar.dateAdd()
// AddZonedDateTime -> calendar.dateAdd()
// DifferenceZonedDateTimeWithRounding ->
// RoundDuration -> MoveRelativeZonedDateTime -> AddZonedDateTime -> calendar.dateAdd()
// BalanceDateDurationRelative -> calendar.dateAdd()

calendar.dateAddCallCount = 0;

const instance3 = new Temporal.Duration(1, 1, 1, 1, 1);
instance3.round({ smallestUnit: "days", relativeTo });
instance.round({ smallestUnit: "days", relativeTo });
assert.sameValue(calendar.dateAddCallCount, 3, "rounding with days smallestUnit");
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ TemporalHelpers.checkCalendarDateUntilLargestUnitSingular(
duration.round({ largestUnit, roundingIncrement: 2, roundingMode: 'ceil', relativeTo });
},
{
years: ["year", "year"],
months: ["month", "month"],
weeks: ["week", "week"],
years: ["year", "year", "year"],
months: ["month", "month", "month"],
weeks: ["week", "week", "week"],
days: [],
hours: [],
minutes: [],
Expand All @@ -123,7 +123,7 @@ TemporalHelpers.checkCalendarDateUntilLargestUnitSingular(
},
{
years: ["year"],
months: ["month", "month"],
months: ["month"],
weeks: ["week"],
days: [],
hours: [],
Expand All @@ -146,9 +146,9 @@ TemporalHelpers.checkCalendarDateUntilLargestUnitSingular(
const relativeTo = new Temporal.ZonedDateTime(1_000_000_000_000_000_000n, "UTC", calendar);
duration.round({ largestUnit, smallestUnit: largestUnit, relativeTo });
}, {
years: ["year"],
months: ["month"],
weeks: ["week", "week"],
years: ["year", "year"],
months: ["month", "month"],
weeks: ["week", "week", "week"],
days: []
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ const calendar = new CalendarDateUntilObservable("iso8601");
const relativeTo = new Temporal.PlainDate(2018, 10, 12, calendar);

const expected = [
"call dateUntil", // UnbalanceDateDurationRelative
"call dateUntil", // BalanceDateDurationRelative
"call dateUntil", // DifferencePlainDateTimeWithRounding -> DifferenceISODateTime
];

const years = new Temporal.Duration(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ info: |
features: [Temporal]
---*/

const duration = Temporal.Duration.from({ days: 1 });
const duration = new Temporal.Duration(0, 0, 1, 1, 1);

const dayLengthNs = 86400000000000n;
const dayInstant = new Temporal.Instant(dayLengthNs);
Expand All @@ -35,10 +35,11 @@ const relativeTo = new Temporal.ZonedDateTime(0n, timeZone);
assert.throws(RangeError, () => duration.round({ smallestUnit: "days", relativeTo }), "indefinite loop is prevented");
assert.sameValue(calls, 5, "getPossibleInstantsFor is not called indefinitely");
// Expected calls:
// RoundDuration -> MoveRelativeZonedDateTime -> AddZonedDateTime (1)
// BalanceTimeDurationRelative ->
// AddZonedDateTime (2)
// NormalizedTimeDurationToDays ->
// AddDaysToZonedDateTime (3, step 12)
// AddDaysToZonedDateTime (4, step 15)
// AddDaysToZonedDateTime (5, step 18.d)
// AddZonedDateTime (1)
// DifferenceZonedDateTimeWithRounding ->
// DifferenceZonedDateTime -> GetInstantFor (2)
// RoundDuration ->
// MoveRelativeZonedDateTime -> AddZonedDateTime (3)
// NormalizedTimeDurationToDays ->
// AddDaysToZonedDateTime (5, step 18)
// AddDaysToZonedDateTime (6, step 21.d)
Loading

0 comments on commit 01dcb15

Please sign in to comment.