Skip to content

Commit

Permalink
Add test to check nfOptions.roundingMode for seconds / microseconds…
Browse files Browse the repository at this point in the history
… / nanoseconds is `"trunc"` (#4115)

* Test that `nfOptions.roundingMode` for seconds / microseconds / nanoseconds is `"trunc"`

* Add license header

* Use `Intl.NumberFormat` instead of `testintl.js` harness

* Use correct harness
  • Loading branch information
sosukesuzuki authored Jun 27, 2024
1 parent 8a2229c commit 27b2551
Showing 1 changed file with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (C) 2024 Sosuke Suzuki. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-Intl.DurationFormat.prototype.format
description: |
nfOptions.roundingMode for seconds / microseconds / nanoseconds should be "trunc"
info: |
1.1.14 PartitionDurationFormatPattern
(...)
4. While numericUnitFound is false, repeat for each row in Table 2 in table order, except the header row:
e. If style is "numeric" or "2-digit", then
i. Append FormatNumericUnits(durationFormat, duration, unit, signDisplayed) to result.
(...)
f.Else,
(...)
ii. If unit is "seconds", "milliseconds", or "microseconds", then
(...)
f. Perform ! CreateDataPropertyOrThrow(nfOpts, "roundingMode", "trunc").
1.1.12 FormatNumericUnits
(...)
18. If secondsFormatted is true, then
a. Append FormatNumericSeconds(durationFormat, secondsValue, minutesFormatted, signDisplayed) to numericPartsList.
(...)
1.1.11 FormatNumericSeconds
(...)
15. Perform ! CreateDataPropertyOrThrow(nfOpts, "roundingMode", "trunc").
(...)
locale: [en]
features: [Intl.DurationFormat]
---*/

const durations = [
// 1
{
fractionalDigits: 0,
numericValue: 1.5,
duration: {
seconds: 1,
milliseconds: 500,
},
},
// 0.001
{
fractionalDigits: 3,
numericValue: 0.0015,
duration: {
milliseconds: 1,
microseconds: 500,
}
},
// 0.000001
{
fractionalDigits: 6,
numericValue: 0.0000015,
duration: {
microseconds: 1,
nanoseconds: 500
}
}
];

for (const { numericValue, fractionalDigits, duration } of durations) {
const df = new Intl.DurationFormat("en", { seconds: "numeric", fractionalDigits });
const nf = new Intl.NumberFormat("en", { maximumFractionDigits: fractionalDigits, roundingMode: "trunc" });
const expected = nf.format(numericValue);
assert.sameValue(df.format(duration), expected, 'Intl.DurationFormat should format seconds, milliseconds and microseconds with `roundingMode: "trunc"`');
}

0 comments on commit 27b2551

Please sign in to comment.