Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporal: Add test for balancing up to weeks when year/month are present #4305

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.duration.prototype.round
description: Rounds up to weeks correctly when years and months are present.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const oneMonthOneDay = new Temporal.Duration(0, 1, 0, 1, 0, 0, 0, 0, 0, 0);
const oneYearOneMonthOneDay = new Temporal.Duration(1, 1, 0, 1, 0, 0, 0, 0, 0, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest something like

const relativeTo = new Temporal.PlainDate(2024, 1, 1);

and using that in the options bags throughout.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I've been looking at the existing tests in round/ and I think we might not have coverage of rounding a duration with only days up to weeks. So maybe add something like

const severalWeeksInDays = new Temporal.Duration(0, 0, 0, 29, 0, 0, 0, 0, 0, 0);

(I might have missed it if we already had coverage of this, though! There are a lot of tests in this folder)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and done.

const severalWeeksInDays = new Temporal.Duration(0, 0, 0, 29, 0, 0, 0, 0, 0, 0);
const relativeTo = new Temporal.PlainDate(2024, 1, 1);

// largestUnit must be included
assert.throws(RangeError, () => oneMonthOneDay.round({
relativeTo,
smallestUnit: 'weeks',
roundingIncrement: 99,
roundingMode: 'ceil'
}));

TemporalHelpers.assertDuration(oneMonthOneDay.round({
relativeTo,
largestUnit: 'weeks',
smallestUnit: 'weeks',
roundingIncrement: 99,
roundingMode: 'ceil'
}), 0, 0, 99, 0, 0, 0, 0, 0, 0, 0);

TemporalHelpers.assertDuration(oneMonthOneDay.round({
relativeTo,
largestUnit: 'weeks',
smallestUnit: 'weeks',
roundingIncrement: 6,
roundingMode: 'ceil'
}), 0, 0, 6, 0, 0, 0, 0, 0, 0, 0);

TemporalHelpers.assertDuration(oneYearOneMonthOneDay.round({
relativeTo,
largestUnit: 'weeks',
smallestUnit: 'weeks',
roundingIncrement: 99,
roundingMode: 'ceil'
}), 0, 0, 99, 0, 0, 0, 0, 0, 0, 0);

TemporalHelpers.assertDuration(oneYearOneMonthOneDay.round({
relativeTo,
largestUnit: 'weeks',
smallestUnit: 'weeks',
roundingIncrement: 57,
roundingMode: 'ceil'
}), 0, 0, 57, 0, 0, 0, 0, 0, 0, 0);

TemporalHelpers.assertDuration(severalWeeksInDays.round({
relativeTo,
largestUnit: 'weeks',
smallestUnit: 'weeks',
roundingIncrement: 5,
roundingMode: 'ceil'
}), 0, 0, 5, 0, 0, 0, 0, 0, 0, 0);

TemporalHelpers.assertDuration(severalWeeksInDays.round({
relativeTo,
largestUnit: 'weeks',
smallestUnit: 'weeks',
roundingIncrement: 8,
roundingMode: 'ceil'
}), 0, 0, 8, 0, 0, 0, 0, 0, 0, 0);
Loading