From 29e8305d0f9fc82547ece0209eb8a3c9491be0d5 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Tue, 18 Nov 2025 15:15:51 -0800 Subject: [PATCH] Temporal: Tests for duration rounding and totaling (#4662) * Temporal: Tests for duration rounding and totaling This provides coverage for the bug in https://github.com/tc39/proposal-temporal/issues/3168 * Update test/built-ins/Temporal/Duration/prototype/total/rounding-window.js --------- Co-authored-by: Philip Chimento --- .../prototype/round/rounding-window.js | 29 +++++++++++++++++++ .../prototype/total/rounding-window.js | 16 ++++++++++ 2 files changed, 45 insertions(+) create mode 100644 test/built-ins/Temporal/Duration/prototype/round/rounding-window.js create mode 100644 test/built-ins/Temporal/Duration/prototype/total/rounding-window.js diff --git a/test/built-ins/Temporal/Duration/prototype/round/rounding-window.js b/test/built-ins/Temporal/Duration/prototype/round/rounding-window.js new file mode 100644 index 0000000000..71c8065f56 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/rounding-window.js @@ -0,0 +1,29 @@ +// Copyright (C) 2025 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: See https://github.com/tc39/proposal-temporal/issues/3168 +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +var d = new Temporal.Duration(1, 0, 0, 0, 1); +var relativeTo = new Temporal.PlainDate(2020, 2, 29); +TemporalHelpers.assertDuration(d.round({ smallestUnit: 'years', relativeTo }), + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0); + +d = new Temporal.Duration(0, 1, 0, 0, 10); +relativeTo = new Temporal.PlainDate(2020, 1, 31); +TemporalHelpers.assertDuration(d.round({ smallestUnit: 'months', roundingMode: 'expand', relativeTo }), + 0, 2, 0, 0, 0, 0, 0, 0, 0, 0); + +d = new Temporal.Duration(2345, 0, 0, 0, 12); +relativeTo = new Temporal.PlainDate(2020, 2, 29) +TemporalHelpers.assertDuration(d.round({ smallestUnit: 'years', roundingMode: 'expand', relativeTo }), + 2346, 0, 0, 0, 0, 0, 0, 0, 0, 0); + +d = new Temporal.Duration(1); +relativeTo = new Temporal.PlainDate(2020, 2, 29) +TemporalHelpers.assertDuration(d.round({ smallestUnit: 'months', relativeTo }), + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/total/rounding-window.js b/test/built-ins/Temporal/Duration/prototype/total/rounding-window.js new file mode 100644 index 0000000000..4a6e3bbc21 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/total/rounding-window.js @@ -0,0 +1,16 @@ +// Copyright (C) 2025 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.total +description: See https://github.com/tc39/proposal-temporal/issues/3168 +features: [Temporal] +---*/ + +var d = new Temporal.Duration(1, 0, 0, 0, 1); +var relativeTo = new Temporal.PlainDate(2020, 2, 29); +assert.sameValue(d.total({ unit: 'years', relativeTo }), 1.0001141552511414); + +d = new Temporal.Duration(0, 1, 0, 0, 10); +relativeTo = new Temporal.PlainDate(2020, 1, 31); +assert.sameValue(d.total({ unit: 'months', relativeTo }), 1.0134408602150538);