From 713ddf66e0bbfff12f33713d9e6e53997b6e60d9 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Wed, 30 Oct 2024 11:32:37 -0700 Subject: [PATCH] Temporal: Move some tests out of round.js under staging into separate files under test/built-ins (#4280) Co-authored-by: Philip Chimento --- ...ccepts-datetime-strings-for-relative-to.js | 28 + ...lances-days-up-to-both-years-and-months.js | 16 + ...wn-differently-depending-on-relative-to.js | 33 + ...up-differently-depending-on-relative-to.js | 53 ++ ...balances-up-to-next-unit-after-rounding.js | 17 + .../round/casts-relative-to-to-plain-date.js | 24 + .../days-24-hours-relative-to-plain-date.js | 15 + ...ys-24-hours-relative-to-zoned-date-time.js | 15 + .../Duration/prototype/round/days-24-hours.js | 14 + ...t-non-string-primitives-for-relative-to.js | 21 + ...ks-if-largest-unit-is-larger-than-weeks.js | 16 + ...ions-do-not-balance-beyond-largest-unit.js | 22 + .../prototype/round/half-expand-is-default.js | 21 + .../prototype/round/invalid-increments.js | 75 +++ ...ot-required-to-round-non-calendar-units.js | 32 + ...-rounding-durations-with-calendar-units.js | 21 + ...ive-to-required-to-round-calendar-units.js | 22 + ...relativeTo-ignores-incorrect-properties.js | 21 + .../round/relativeTo-required-properties.js | 32 + .../prototype/round/rounding-increments.js | 54 ++ .../round/rounding-relative-to-date.js | 40 ++ .../prototype/round/singular-units.js | 156 +++++ .../round/succeeds-with-largest-unit-auto.js | 14 + ...r-largestUnit-nor-smallestUnit-is-given.js | 21 + ...-offset-for-zoned-date-time-relative-to.js | 15 + .../prototype/round/valid-increments.js | 86 +++ test/staging/Temporal/Duration/old/round.js | 620 ------------------ 27 files changed, 884 insertions(+), 620 deletions(-) create mode 100644 test/built-ins/Temporal/Duration/prototype/round/accepts-datetime-strings-for-relative-to.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/balances-days-up-to-both-years-and-months.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/balances-down-differently-depending-on-relative-to.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/balances-up-differently-depending-on-relative-to.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/balances-up-to-next-unit-after-rounding.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/casts-relative-to-to-plain-date.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/days-24-hours-relative-to-plain-date.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/days-24-hours-relative-to-zoned-date-time.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/days-24-hours.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/does-not-accept-non-string-primitives-for-relative-to.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/does-not-balance-up-to-weeks-if-largest-unit-is-larger-than-weeks.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/durations-do-not-balance-beyond-largest-unit.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/half-expand-is-default.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/invalid-increments.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/relative-to-not-required-to-round-non-calendar-units.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/relative-to-required-for-rounding-durations-with-calendar-units.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/relative-to-required-to-round-calendar-units.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/relativeTo-ignores-incorrect-properties.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/relativeTo-required-properties.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/rounding-increments.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/rounding-relative-to-date.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/singular-units.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/succeeds-with-largest-unit-auto.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/throws-if-neither-largestUnit-nor-smallestUnit-is-given.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/throws-on-wrong-offset-for-zoned-date-time-relative-to.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/valid-increments.js delete mode 100644 test/staging/Temporal/Duration/old/round.js diff --git a/test/built-ins/Temporal/Duration/prototype/round/accepts-datetime-strings-for-relative-to.js b/test/built-ins/Temporal/Duration/prototype/round/accepts-datetime-strings-for-relative-to.js new file mode 100644 index 0000000000..ad1fce4dcb --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/accepts-datetime-strings-for-relative-to.js @@ -0,0 +1,28 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: round() accepts datetime strings or fields for relativeTo. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const d = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5); + +[ + "2020-01-01", + "20200101", + "2020-01-01T00:00:00.000000000", + { + year: 2020, + month: 1, + day: 1 + } +].forEach(relativeTo => { + TemporalHelpers.assertDuration(d.round({ + smallestUnit: "seconds", + relativeTo + }), 5, 6, 0, 10, 5, 5, 5, 0, 0, 0) +}); + diff --git a/test/built-ins/Temporal/Duration/prototype/round/balances-days-up-to-both-years-and-months.js b/test/built-ins/Temporal/Duration/prototype/round/balances-days-up-to-both-years-and-months.js new file mode 100644 index 0000000000..0a3ae3e4d4 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/balances-days-up-to-both-years-and-months.js @@ -0,0 +1,16 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Balances days up to both years and months. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const twoYears = new Temporal.Duration(0, 11, 0, 396, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(twoYears.round({ + largestUnit: "years", + relativeTo: new Temporal.PlainDate(2017, 1, 1) +}), 2, 0, 0, 0, 0, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/round/balances-down-differently-depending-on-relative-to.js b/test/built-ins/Temporal/Duration/prototype/round/balances-down-differently-depending-on-relative-to.js new file mode 100644 index 0000000000..6fcb73e4d4 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/balances-down-differently-depending-on-relative-to.js @@ -0,0 +1,33 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Balances down differently depending on relativeTo. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const oneYear = new Temporal.Duration(1, 0, 0, 0, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(oneYear.round({ + largestUnit: "days", + relativeTo: new Temporal.PlainDate(2019, 1, 1) +}), 0, 0, 0, 365, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(oneYear.round({ + largestUnit: "days", + relativeTo: new Temporal.PlainDate(2019, 7, 1) +}), 0, 0, 0, 366, 0, 0, 0, 0, 0, 0); + +const minusYear = new Temporal.Duration(-1, 0, 0, 0, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(minusYear.round({ + largestUnit: "days", + relativeTo: new Temporal.PlainDate(2020, 1, 1) +}), 0, 0, 0, -365, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(minusYear.round({ + largestUnit: "days", + relativeTo: new Temporal.PlainDate(2020, 7, 1) +}), 0, 0, 0, -366, 0, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/round/balances-up-differently-depending-on-relative-to.js b/test/built-ins/Temporal/Duration/prototype/round/balances-up-differently-depending-on-relative-to.js new file mode 100644 index 0000000000..9c097e9fad --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/balances-up-differently-depending-on-relative-to.js @@ -0,0 +1,53 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Balances up differently depending on relativeTo. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const fortyDays = new Temporal.Duration(0, 0, 0, 40, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(fortyDays.round({ + largestUnit: "years", + relativeTo: new Temporal.PlainDate(2020, 1, 1) +}), 0, 1, 0, 9, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(fortyDays.round({ + largestUnit: "years", + relativeTo: new Temporal.PlainDate(2020, 2, 1) +}), 0, 1, 0, 11, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(fortyDays.round({ + largestUnit: "years", + relativeTo: new Temporal.PlainDate(2020, 3, 1) +}), 0, 1, 0, 9, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(fortyDays.round({ + largestUnit: "years", + relativeTo: new Temporal.PlainDate(2020, 4, 1) +}), 0, 1, 0, 10, 0, 0, 0, 0, 0, 0); + +const minusForty = new Temporal.Duration(0, 0, 0, -40, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(minusForty.round({ + largestUnit: "years", + relativeTo: new Temporal.PlainDate(2020, 1, 1) +}), 0, -1, 0, -9, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(minusForty.round({ + largestUnit: "years", + relativeTo: new Temporal.PlainDate(2020, 2, 1) +}), 0, -1, 0, -9, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(minusForty.round({ + largestUnit: "years", + relativeTo: new Temporal.PlainDate(2020, 3, 1) +}), 0, -1, 0, -11, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(minusForty.round({ + largestUnit: "years", + relativeTo: new Temporal.PlainDate(2020, 4, 1) +}), 0, -1, 0, -9, 0, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/round/balances-up-to-next-unit-after-rounding.js b/test/built-ins/Temporal/Duration/prototype/round/balances-up-to-next-unit-after-rounding.js new file mode 100644 index 0000000000..6827e6670c --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/balances-up-to-next-unit-after-rounding.js @@ -0,0 +1,17 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Balances up to the next unit after rounding. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const almostWeek = new Temporal.Duration(0, 0, 0, 6, 20, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(almostWeek.round({ + largestUnit: "weeks", + smallestUnit: "days", + relativeTo: new Temporal.PlainDate(2020, 1, 1) +}), 0, 0, 1, 0, 0, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/round/casts-relative-to-to-plain-date.js b/test/built-ins/Temporal/Duration/prototype/round/casts-relative-to-to-plain-date.js new file mode 100644 index 0000000000..acaa9b7ac9 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/casts-relative-to-to-plain-date.js @@ -0,0 +1,24 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: round() casts relativeTo to PlainDate if possible +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const hours25 = new Temporal.Duration(0, 0, 0, 0, 25, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(hours25.round({ largestUnit: "days", + relativeTo: "2019-11-02" + }), + 0, 0, 0, 1, 1, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(hours25.round({ largestUnit: "days", + relativeTo: { + year: 2019, + month: 11, + day: 2 + }}), + 0, 0, 0, 1, 1, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/round/days-24-hours-relative-to-plain-date.js b/test/built-ins/Temporal/Duration/prototype/round/days-24-hours-relative-to-plain-date.js new file mode 100644 index 0000000000..3382bd9486 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/days-24-hours-relative-to-plain-date.js @@ -0,0 +1,15 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Days are 24 hours if relativeTo is PlainDate +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const hours25 = new Temporal.Duration(0, 0, 0, 0, 25, 0, 0, 0, 0, 0); +const relativeTo = new Temporal.PlainDate(2017, 1, 1); + +TemporalHelpers.assertDuration(hours25.round({ largestUnit: "days", relativeTo }), + 0, 0, 0, 1, 1, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/round/days-24-hours-relative-to-zoned-date-time.js b/test/built-ins/Temporal/Duration/prototype/round/days-24-hours-relative-to-zoned-date-time.js new file mode 100644 index 0000000000..78f9d2d9a8 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/days-24-hours-relative-to-zoned-date-time.js @@ -0,0 +1,15 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Days are 24 hours if relativeTo is ZonedDateTime and duration encompasses no DST change +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const hours25 = new Temporal.Duration(0, 0, 0, 0, 25, 0, 0, 0, 0, 0); +const relativeTo = new Temporal.ZonedDateTime(1_000_000_000_000_000_000n, "+04:30"); + +TemporalHelpers.assertDuration(hours25.round({ largestUnit: "days", relativeTo }), + 0, 0, 0, 1, 1, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/round/days-24-hours.js b/test/built-ins/Temporal/Duration/prototype/round/days-24-hours.js new file mode 100644 index 0000000000..d67f61a1c1 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/days-24-hours.js @@ -0,0 +1,14 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Days are 24 hours if relativeTo not given. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const hours25 = new Temporal.Duration(0, 0, 0, 0, 25, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(hours25.round({ largestUnit: "days" }), + 0, 0, 0, 1, 1, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/round/does-not-accept-non-string-primitives-for-relative-to.js b/test/built-ins/Temporal/Duration/prototype/round/does-not-accept-non-string-primitives-for-relative-to.js new file mode 100644 index 0000000000..562c4490d7 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/does-not-accept-non-string-primitives-for-relative-to.js @@ -0,0 +1,21 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: round() does not accept non-string primitives for relativeTo +features: [Temporal] +---*/ + +const d = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5); + +[ + 20200101, + 20200101n, + null, + true, +].forEach(relativeTo => { + assert.throws( + TypeError, () => d.round({ smallestUnit: "seconds", relativeTo}) + ); +}); diff --git a/test/built-ins/Temporal/Duration/prototype/round/does-not-balance-up-to-weeks-if-largest-unit-is-larger-than-weeks.js b/test/built-ins/Temporal/Duration/prototype/round/does-not-balance-up-to-weeks-if-largest-unit-is-larger-than-weeks.js new file mode 100644 index 0000000000..84ef54c8ce --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/does-not-balance-up-to-weeks-if-largest-unit-is-larger-than-weeks.js @@ -0,0 +1,16 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Does not balance up to weeks if largestUnit is larger than weeks. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const monthAlmostWeek = new Temporal.Duration(0, 1, 0, 6, 20, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(monthAlmostWeek.round({ + smallestUnit: "days", + relativeTo: new PlainDate(2020, 1, 1) +}), 0, 1, 0, 7, 0, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/round/durations-do-not-balance-beyond-largest-unit.js b/test/built-ins/Temporal/Duration/prototype/round/durations-do-not-balance-beyond-largest-unit.js new file mode 100644 index 0000000000..6f39c1d458 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/durations-do-not-balance-beyond-largest-unit.js @@ -0,0 +1,22 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Durations do not balance beyond their current largest unit by default. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const fortyDays = new Temporal.Duration(0, 0, 0, 40, 0, 0, 0, 0, 0, 0); +const one85Minutes = new Temporal.Duration(0, 0, 0, 0, 185, 0, 0, 0, 0, 0); +const relativeTo = new Temporal.PlainDate(2020, 1, 1); + +TemporalHelpers.assertDuration(fortyDays.round({ + smallestUnit: "seconds", + relativeTo +}), 0, 0, 0, 40, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(one85Minutes.round({ + smallestUnit: "seconds", +}), 0, 0, 0, 0, 185, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/round/half-expand-is-default.js b/test/built-ins/Temporal/Duration/prototype/round/half-expand-is-default.js new file mode 100644 index 0000000000..c19f08cdf8 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/half-expand-is-default.js @@ -0,0 +1,21 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: halfExpand is the default. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const d = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5); +const relativeTo = new Temporal.PlainDate(2020, 1, 1); + +TemporalHelpers.assertDuration(d.round({ + smallestUnit: "years", + relativeTo +}), 6, 0, 0, 0, 0, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(d.negated().round({ + smallestUnit: "years", + relativeTo +}), -6, 0, 0, 0, 0, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/round/invalid-increments.js b/test/built-ins/Temporal/Duration/prototype/round/invalid-increments.js new file mode 100644 index 0000000000..e7d9bfe826 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/invalid-increments.js @@ -0,0 +1,75 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Test various invalid rounding increments. +features: [Temporal] +---*/ + +const d = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5); +const relativeTo = Temporal.PlainDate.from("2020-01-01"); + +// throws on increments that do not divide evenly into the next highest +assert.throws(RangeError, () => d.round({ + relativeTo, + smallestUnit: "hours", + roundingIncrement: 11 +})); +assert.throws(RangeError, () => d.round({ + relativeTo, + smallestUnit: "minutes", + roundingIncrement: 29 +})); +assert.throws(RangeError, () => d.round({ + relativeTo, + smallestUnit: "seconds", + roundingIncrement: 29 +})); +assert.throws(RangeError, () => d.round({ + relativeTo, + smallestUnit: "milliseconds", + roundingIncrement: 29 +})); +assert.throws(RangeError, () => d.round({ + relativeTo, + smallestUnit: "microseconds", + roundingIncrement: 29 +})); +assert.throws(RangeError, () => d.round({ + relativeTo, + smallestUnit: "nanoseconds", + roundingIncrement: 29 +})); + +// throws on increments that are equal to the next highest +assert.throws(RangeError, () => d.round({ + relativeTo, + smallestUnit: "hours", + roundingIncrement: 24 +})); +assert.throws(RangeError, () => d.round({ + relativeTo, + smallestUnit: "minutes", + roundingIncrement: 60 +})); +assert.throws(RangeError, () => d.round({ + relativeTo, + smallestUnit: "seconds", + roundingIncrement: 60 +})); +assert.throws(RangeError, () => d.round({ + relativeTo, + smallestUnit: "milliseconds", + roundingIncrement: 1000 +})); +assert.throws(RangeError, () => d.round({ + relativeTo, + smallestUnit: "microseconds", + roundingIncrement: 1000 +})); +assert.throws(RangeError, () => d.round({ + relativeTo, + smallestUnit: "nanoseconds", + roundingIncrement: 1000 +})); diff --git a/test/built-ins/Temporal/Duration/prototype/round/relative-to-not-required-to-round-non-calendar-units.js b/test/built-ins/Temporal/Duration/prototype/round/relative-to-not-required-to-round-non-calendar-units.js new file mode 100644 index 0000000000..d36d6f64f0 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/relative-to-not-required-to-round-non-calendar-units.js @@ -0,0 +1,32 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: relativeTo is not required to round non-calendar units in durations without calendar units. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const d2 = new Temporal.Duration(0, 0, 0, 5, 5, 5, 5, 5, 5, 5); + +// String params + +TemporalHelpers.assertDuration(d2.round("days"), 0, 0, 0, 5, 0, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(d2.round("hours"), 0, 0, 0, 5, 5, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(d2.round("minutes"), 0, 0, 0, 5, 5, 5, 0, 0, 0, 0); +TemporalHelpers.assertDuration(d2.round("seconds"), 0, 0, 0, 5, 5, 5, 5, 0, 0, 0); +TemporalHelpers.assertDuration(d2.round("milliseconds"), 0, 0, 0, 5, 5, 5, 5, 5, 0, 0); +TemporalHelpers.assertDuration(d2.round("microseconds"), 0, 0, 0, 5, 5, 5, 5, 5, 5, 0); +TemporalHelpers.assertDuration(d2.round("nanoseconds"), 0, 0, 0, 5, 5, 5, 5, 5, 5, 5); + +// Object params + +TemporalHelpers.assertDuration(d2.round({ smallestUnit: "days" }), 0, 0, 0, 5, 0, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(d2.round({ smallestUnit: "hours" }), 0, 0, 0, 5, 5, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(d2.round({ smallestUnit: "minutes" }), 0, 0, 0, 5, 5, 5, 0, 0, 0, 0); +TemporalHelpers.assertDuration(d2.round({ smallestUnit: "seconds" }), 0, 0, 0, 5, 5, 5, 5, 0, 0, 0); +TemporalHelpers.assertDuration(d2.round({ smallestUnit: "milliseconds" }), 0, 0, 0, 5, 5, 5, 5, 5, 0, 0); +TemporalHelpers.assertDuration(d2.round({ smallestUnit: "microseconds" }), 0, 0, 0, 5, 5, 5, 5, 5, 5, 0); +TemporalHelpers.assertDuration(d2.round({ smallestUnit: "nanoseconds" }), 0, 0, 0, 5, 5, 5, 5, 5, 5, 5); + diff --git a/test/built-ins/Temporal/Duration/prototype/round/relative-to-required-for-rounding-durations-with-calendar-units.js b/test/built-ins/Temporal/Duration/prototype/round/relative-to-required-for-rounding-durations-with-calendar-units.js new file mode 100644 index 0000000000..f65a2e05c4 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/relative-to-required-for-rounding-durations-with-calendar-units.js @@ -0,0 +1,21 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: relativeTo is required for rounding durations with calendar units +features: [Temporal] +---*/ + +const d = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5); + +assert.throws(RangeError, () => d.round({ largestUnit: "years" })); +assert.throws(RangeError, () => d.round({ largestUnit: "months" })); +assert.throws(RangeError, () => d.round({ largestUnit: "weeks" })); +assert.throws(RangeError, () => d.round({ largestUnit: "days" })); +assert.throws(RangeError, () => d.round({ largestUnit: "hours" })); +assert.throws(RangeError, () => d.round({ largestUnit: "minutes" })); +assert.throws(RangeError, () => d.round({ largestUnit: "seconds" })); +assert.throws(RangeError, () => d.round({ largestUnit: "milliseconds" })); +assert.throws(RangeError, () => d.round({ largestUnit: "microseconds" })); +assert.throws(RangeError, () => d.round({ largestUnit: "nanoseconds" })); diff --git a/test/built-ins/Temporal/Duration/prototype/round/relative-to-required-to-round-calendar-units.js b/test/built-ins/Temporal/Duration/prototype/round/relative-to-required-to-round-calendar-units.js new file mode 100644 index 0000000000..ac316c4c6d --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/relative-to-required-to-round-calendar-units.js @@ -0,0 +1,22 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: relativeTo is required to round calendar units even in durations without calendar units. +features: [Temporal] +---*/ + +const d2 = new Temporal.Duration(0, 0, 0, 5, 5, 5, 5, 5, 5, 5); + +// String params + +assert.throws(RangeError, () => d2.round("years")); +assert.throws(RangeError, () => d2.round("months")); +assert.throws(RangeError, () => d2.round("weeks")); + +// Object params + +assert.throws(RangeError, () => d2.round({ smallestUnit: "years" })); +assert.throws(RangeError, () => d2.round({ smallestUnit: "months" })); +assert.throws(RangeError, () => d2.round({ smallestUnit: "weeks" })); diff --git a/test/built-ins/Temporal/Duration/prototype/round/relativeTo-ignores-incorrect-properties.js b/test/built-ins/Temporal/Duration/prototype/round/relativeTo-ignores-incorrect-properties.js new file mode 100644 index 0000000000..30df4a3557 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/relativeTo-ignores-incorrect-properties.js @@ -0,0 +1,21 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Incorrectly-spelled properties are ignored in relativeTo. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const oneMonth = Temporal.Duration.from({ months: 1 }); + +TemporalHelpers.assertDuration(oneMonth.round({largestUnit: "days", + relativeTo: { + year: 2020, + month: 1, + day: 1, + months: 2 + }}), + 0, 0, 0, 31, 0, 0, 0, 0, 0, 0); + diff --git a/test/built-ins/Temporal/Duration/prototype/round/relativeTo-required-properties.js b/test/built-ins/Temporal/Duration/prototype/round/relativeTo-required-properties.js new file mode 100644 index 0000000000..02747b8d24 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/relativeTo-required-properties.js @@ -0,0 +1,32 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: relativeTo object must contain at least the required correctly-spelled properties +features: [Temporal] +---*/ + +const hours25 = new Temporal.Duration(0, 0, 0, 0, 25, 0, 0, 0, 0, 0); + +assert.throws(TypeError, () => hours25.round({ + largestUnit: "days", + relativeTo: { + month: 11, + day: 3 + } +})); +assert.throws(TypeError, () => hours25.round({ + largestUnit: "days", + relativeTo: { + year: 2019, + month: 11 + } +})); +assert.throws(TypeError, () => hours25.round({ + largestUnit: "days", + relativeTo: { + year: 2019, + day: 3 + } +})); diff --git a/test/built-ins/Temporal/Duration/prototype/round/rounding-increments.js b/test/built-ins/Temporal/Duration/prototype/round/rounding-increments.js new file mode 100644 index 0000000000..48365e1433 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/rounding-increments.js @@ -0,0 +1,54 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Test various rounding increments. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const d = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5); +const relativeTo = new Temporal.PlainDate(2020, 1, 1); + +// Rounds to an increment of hours +TemporalHelpers.assertDuration(d.round({ + smallestUnit: "hours", + roundingIncrement: 3, + relativeTo +}), 5, 6, 0, 10, 6, 0, 0, 0, 0, 0); + +// Rounds to an increment of minutes +TemporalHelpers.assertDuration(d.round({ + smallestUnit: "minutes", + roundingIncrement: 30, + relativeTo +}), 5, 6, 0, 10, 5, 0, 0, 0, 0, 0); + +// Rounds to an increment of seconds +TemporalHelpers.assertDuration(d.round({ + smallestUnit: "seconds", + roundingIncrement: 15, + relativeTo +}), 5, 6, 0, 10, 5, 5, 0, 0, 0, 0); + +// Rounds to an increment of milliseconds +TemporalHelpers.assertDuration(d.round({ + smallestUnit: "milliseconds", + roundingIncrement: 10, + relativeTo +}), 5, 6, 0, 10, 5, 5, 5, 10, 0, 0); + +// Rounds to an increment of microseconds +TemporalHelpers.assertDuration(d.round({ + smallestUnit: "microseconds", + roundingIncrement: 10, + relativeTo +}), 5, 6, 0, 10, 5, 5, 5, 5, 10, 0); + +// Rounds to an increment of nanoseconds +TemporalHelpers.assertDuration(d.round({ + smallestUnit: "nanoseconds", + roundingIncrement: 10, + relativeTo +}), 5, 6, 0, 10, 5, 5, 5, 5, 5, 10); diff --git a/test/built-ins/Temporal/Duration/prototype/round/rounding-relative-to-date.js b/test/built-ins/Temporal/Duration/prototype/round/rounding-relative-to-date.js new file mode 100644 index 0000000000..3c00976f86 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/rounding-relative-to-date.js @@ -0,0 +1,40 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Test that round() counts the correct number of days when rounding relative to a date. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const days = new Temporal.Duration(0, 0, 0, 45, 0, 0, 0, 0, 0, 0); +const yearAndHalf = new Temporal.Duration(0, 0, 0, 547, 12, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(days.round({ + relativeTo: new Temporal.PlainDate(2019, 1, 1) + smallestUnit: "months"}), 0, 2, 0, 0, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(days.negated().round({ + relativeTo: new Temporal.PlainDate(2019, 2, 15) + smallestUnit: "months"}), 0, -1, 0, 0, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(yearAndHalf.round({ + relativeTo: new Temporal.PlainDate(2018, 1, 1) + smallestUnit: "years"}), 2, 0, 0, 0, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(yearAndHalf.round({ + relativeTo: new Temporal.PlainDate(2018, 7, 1) + smallestUnit: "years"}), 1, 0, 0, 0, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(yearAndHalf.round({ + relativeTo: new Temporal.PlainDate(2019, 1, 1) + smallestUnit: "years"}), 1, 0, 0, 0, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(yearAndHalf.round({ + relativeTo: new Temporal.PlainDate(2020, 1, 1) + smallestUnit: "years"}), 1, 0, 0, 0, 0, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(yearAndHalf.round({ + relativeTo: new Temporal.PlainDate(2020, 7, 1) + smallestUnit: "years"}), 2, 0, 0, 0, 0, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/round/singular-units.js b/test/built-ins/Temporal/Duration/prototype/round/singular-units.js new file mode 100644 index 0000000000..0b264f32bc --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/singular-units.js @@ -0,0 +1,156 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Test that round() accepts singular units. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const d = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5); +const relativeTo = new Temporal.PlainDate(2020, 1, 1); + +TemporalHelpers.assertDurationsEqual(d.round({ + largestUnit: "year", + relativeTo +}), d.round({ + largestUnit: "years", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + smallestUnit: "year", + relativeTo +}), d.round({ + smallestUnit: "years", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + largestUnit: "month", + relativeTo +}), d.round({ + largestUnit: "months", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + smallestUnit: "month", + relativeTo +}), d.round({ + smallestUnit: "months", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + largestUnit: "day", + relativeTo +}), d.round({ + largestUnit: "days", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + smallestUnit: "day", + relativeTo +}), d.round({ + smallestUnit: "days", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + largestUnit: "hour", + relativeTo +}), d.round({ + largestUnit: "hours", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + smallestUnit: "hour", + relativeTo +}), d.round({ + smallestUnit: "hours", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + largestUnit: "minute", + relativeTo +}), d.round({ + largestUnit: "minutes", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + smallestUnit: "minute", + relativeTo +}), d.round({ + smallestUnit: "minutes", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + largestUnit: "second", + relativeTo +}), d.round({ + largestUnit: "seconds", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + smallestUnit: "second", + relativeTo +}), d.round({ + smallestUnit: "seconds", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + largestUnit: "millisecond", + relativeTo +}), d.round({ + largestUnit: "milliseconds", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + smallestUnit: "millisecond", + relativeTo +}), d.round({ + smallestUnit: "milliseconds", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + largestUnit: "microsecond", + relativeTo +}), d.round({ + largestUnit: "microseconds", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + smallestUnit: "microsecond", + relativeTo +}), d.round({ + smallestUnit: "microseconds", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + largestUnit: "nanosecond", + relativeTo +}), d.round({ + largestUnit: "nanoseconds", + relativeTo +})); + +TemporalHelpers.assertDurationsEqual(d.round({ + smallestUnit: "nanosecond", + relativeTo +}), d.round({ + smallestUnit: "nanoseconds", + relativeTo +})); diff --git a/test/built-ins/Temporal/Duration/prototype/round/succeeds-with-largest-unit-auto.js b/test/built-ins/Temporal/Duration/prototype/round/succeeds-with-largest-unit-auto.js new file mode 100644 index 0000000000..41b40b9851 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/succeeds-with-largest-unit-auto.js @@ -0,0 +1,14 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: round() succeeds with largestUnit 'auto'. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const hours25 = new Temporal.Duration(0, 0, 0, 0, 25, 0, 0, 0, 0, 0); + +TemporalHelpers.assertDuration(hours25.round({ largestUnit: "auto" }), + 0, 0, 0, 0, 25, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/round/throws-if-neither-largestUnit-nor-smallestUnit-is-given.js b/test/built-ins/Temporal/Duration/prototype/round/throws-if-neither-largestUnit-nor-smallestUnit-is-given.js new file mode 100644 index 0000000000..fc83e5a97e --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/throws-if-neither-largestUnit-nor-smallestUnit-is-given.js @@ -0,0 +1,21 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: round() throws if neither largestUnit nor smallestUnit is given +features: [Temporal] +---*/ + +const d = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5); +const hoursOnly = new Temporal.Duration(0, 0, 0, 0, 1); + +[ + {}, + () => { + }, + { roundingMode: "ceil" } +].forEach(roundTo => { + assert.throws(RangeError, () => d.round(roundTo)); + assert.throws(RangeError, () => hoursOnly.round(roundTo)); +}); diff --git a/test/built-ins/Temporal/Duration/prototype/round/throws-on-wrong-offset-for-zoned-date-time-relative-to.js b/test/built-ins/Temporal/Duration/prototype/round/throws-on-wrong-offset-for-zoned-date-time-relative-to.js new file mode 100644 index 0000000000..1dc6590b3f --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/throws-on-wrong-offset-for-zoned-date-time-relative-to.js @@ -0,0 +1,15 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: round() throws on wrong offset for ZonedDateTime relativeTo string +features: [Temporal] +---*/ + +const d = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5); + +assert.throws(RangeError, () => d.round({ + smallestUnit: "seconds", + relativeTo: "1971-01-01T00:00+02:00[-00:44:30]" +})); diff --git a/test/built-ins/Temporal/Duration/prototype/round/valid-increments.js b/test/built-ins/Temporal/Duration/prototype/round/valid-increments.js new file mode 100644 index 0000000000..012f77952c --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/valid-increments.js @@ -0,0 +1,86 @@ +// Copyright (C) 2018 Bloomberg LP. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Test valid rounding increments (correct divisibility). +features: [Temporal] +---*/ + +const d = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5); +const relativeTo = new Temporal.PlainDate(2020, 1, 1); + +// valid hour increments divide into 24 +[ + 1, + 2, + 3, + 4, + 6, + 8, + 12 +].forEach(roundingIncrement => { + const options = { + smallestUnit: "hours", + roundingIncrement, + relativeTo + }; + assert(d.round(options) instanceof Temporal.Duration); +}); +[ + "minutes", + "seconds" +].forEach(smallestUnit => { + // valid minutes/seconds increments divide into 60 + [ + 1, + 2, + 3, + 4, + 5, + 6, + 10, + 12, + 15, + 20, + 30 + ].forEach(roundingIncrement => { + const roundTo = { + smallestUnit, + roundingIncrement, + relativeTo + }; + assert(d.round(roundTo) instanceof Temporal.Duration); + }); + }); +[ + "milliseconds", + "microseconds", + "nanoseconds" +].forEach(smallestUnit => { + // valid milliseconds/microseconds/nanoseconds increments divide into 1000 + [ + 1, + 2, + 4, + 5, + 8, + 10, + 20, + 25, + 40, + 50, + 100, + 125, + 200, + 250, + 500 + ].forEach(roundingIncrement => { + const roundTo = { + smallestUnit, + roundingIncrement, + relativeTo + }; + assert(d.round(roundTo) instanceof Temporal.Duration); + }); + }); diff --git a/test/staging/Temporal/Duration/old/round.js b/test/staging/Temporal/Duration/old/round.js deleted file mode 100644 index 6191db6799..0000000000 --- a/test/staging/Temporal/Duration/old/round.js +++ /dev/null @@ -1,620 +0,0 @@ -// Copyright (C) 2018 Bloomberg LP. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-temporal-duration-objects -description: Temporal.Duration.prototype.round() works as expected -features: [Temporal] ----*/ - -var d = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5); -var d2 = new Temporal.Duration(0, 0, 0, 5, 5, 5, 5, 5, 5, 5); -var relativeTo = Temporal.PlainDate.from("2020-01-01"); - -// succeeds with largestUnit: 'auto' -assert.sameValue(`${ Temporal.Duration.from({ hours: 25 }).round({ largestUnit: "auto" }) }`, "PT25H"); -var hours25 = new Temporal.Duration(0, 0, 0, 0, 25); - -// days are 24 hours if relativeTo not given -assert.sameValue(`${ hours25.round({ largestUnit: "days" }) }`, "P1DT1H"); - -// days are 24 hours if relativeTo is PlainDate -var relativeTo = Temporal.PlainDate.from("2017-01-01"); -assert.sameValue(`${ hours25.round({ - largestUnit: "days", - relativeTo -}) }`, "P1DT1H"); - -// days are 24 hours if relativeTo is ZonedDateTime, and duration encompasses no DST change -var relativeTo = Temporal.ZonedDateTime.from("2017-01-01T00:00[+04:30]"); -assert.sameValue(`${ hours25.round({ - largestUnit: "days", - relativeTo -}) }`, "P1DT1H"); - -// casts relativeTo to PlainDate if possible -assert.sameValue(`${ hours25.round({ - largestUnit: "days", - relativeTo: "2019-11-02" -}) }`, "P1DT1H"); -assert.sameValue(`${ hours25.round({ - largestUnit: "days", - relativeTo: { - year: 2019, - month: 11, - day: 2 - } -}) }`, "P1DT1H"); - -// accepts datetime strings or fields for relativeTo -[ - "2020-01-01", - "20200101", - "2020-01-01T00:00:00.000000000", - { - year: 2020, - month: 1, - day: 1 - } -].forEach(relativeTo => { - assert.sameValue(`${ d.round({ - smallestUnit: "seconds", - relativeTo - }) }`, "P5Y6M10DT5H5M5S"); -}); - -// does not accept non-string primitives for relativeTo -[ - 20200101, - 20200101n, - null, - true, -].forEach(relativeTo => { - assert.throws( - TypeError, () => d.round({ smallestUnit: "seconds", relativeTo}) - ); -}); - -// throws on wrong offset for ZonedDateTime relativeTo string -assert.throws(RangeError, () => d.round({ - smallestUnit: "seconds", - relativeTo: "1971-01-01T00:00+02:00[-00:44:30]" -})); - -// relativeTo object must contain at least the required correctly-spelled properties -assert.throws(TypeError, () => hours25.round({ - largestUnit: "days", - relativeTo: { - month: 11, - day: 3 - } -})); -assert.throws(TypeError, () => hours25.round({ - largestUnit: "days", - relativeTo: { - year: 2019, - month: 11 - } -})); -assert.throws(TypeError, () => hours25.round({ - largestUnit: "days", - relativeTo: { - year: 2019, - day: 3 - } -})); - -// incorrectly-spelled properties are ignored in relativeTo -var oneMonth = Temporal.Duration.from({ months: 1 }); -assert.sameValue(`${ oneMonth.round({ - largestUnit: "days", - relativeTo: { - year: 2020, - month: 1, - day: 1, - months: 2 - } -}) }`, "P31D"); - -// throws if neither one of largestUnit or smallestUnit is given -var hoursOnly = new Temporal.Duration(0, 0, 0, 0, 1); -[ - {}, - () => { - }, - { roundingMode: "ceil" } -].forEach(roundTo => { - assert.throws(RangeError, () => d.round(roundTo)); - assert.throws(RangeError, () => hoursOnly.round(roundTo)); -}); - -// relativeTo not required to round non-calendar units in durations w/o calendar units (string param) -assert.sameValue(`${ d2.round("days") }`, "P5D"); -assert.sameValue(`${ d2.round("hours") }`, "P5DT5H"); -assert.sameValue(`${ d2.round("minutes") }`, "P5DT5H5M"); -assert.sameValue(`${ d2.round("seconds") }`, "P5DT5H5M5S"); -assert.sameValue(`${ d2.round("milliseconds") }`, "P5DT5H5M5.005S"); -assert.sameValue(`${ d2.round("microseconds") }`, "P5DT5H5M5.005005S"); -assert.sameValue(`${ d2.round("nanoseconds") }`, "P5DT5H5M5.005005005S"); - -// relativeTo is required to round calendar units even in durations w/o calendar units (string param) -assert.throws(RangeError, () => d2.round("years")); -assert.throws(RangeError, () => d2.round("months")); -assert.throws(RangeError, () => d2.round("weeks")); - -// relativeTo not required to round non-calendar units in durations w/o calendar units (object param) -assert.sameValue(`${ d2.round({ smallestUnit: "days" }) }`, "P5D"); -assert.sameValue(`${ d2.round({ smallestUnit: "hours" }) }`, "P5DT5H"); -assert.sameValue(`${ d2.round({ smallestUnit: "minutes" }) }`, "P5DT5H5M"); -assert.sameValue(`${ d2.round({ smallestUnit: "seconds" }) }`, "P5DT5H5M5S"); -assert.sameValue(`${ d2.round({ smallestUnit: "milliseconds" }) }`, "P5DT5H5M5.005S"); -assert.sameValue(`${ d2.round({ smallestUnit: "microseconds" }) }`, "P5DT5H5M5.005005S"); -assert.sameValue(`${ d2.round({ smallestUnit: "nanoseconds" }) }`, "P5DT5H5M5.005005005S"); - -// relativeTo is required to round calendar units even in durations w/o calendar units (object param) -assert.throws(RangeError, () => d2.round({ smallestUnit: "years" })); -assert.throws(RangeError, () => d2.round({ smallestUnit: "months" })); -assert.throws(RangeError, () => d2.round({ smallestUnit: "weeks" })); - -// relativeTo is required for rounding durations with calendar units -assert.throws(RangeError, () => d.round({ largestUnit: "years" })); -assert.throws(RangeError, () => d.round({ largestUnit: "months" })); -assert.throws(RangeError, () => d.round({ largestUnit: "weeks" })); -assert.throws(RangeError, () => d.round({ largestUnit: "days" })); -assert.throws(RangeError, () => d.round({ largestUnit: "hours" })); -assert.throws(RangeError, () => d.round({ largestUnit: "minutes" })); -assert.throws(RangeError, () => d.round({ largestUnit: "seconds" })); -assert.throws(RangeError, () => d.round({ largestUnit: "milliseconds" })); -assert.throws(RangeError, () => d.round({ largestUnit: "microseconds" })); -assert.throws(RangeError, () => d.round({ largestUnit: "nanoseconds" })); - -// durations do not balance beyond their current largest unit by default -var relativeTo = Temporal.PlainDate.from("2020-01-01"); -var fortyDays = Temporal.Duration.from({ days: 40 }); -assert.sameValue(`${ fortyDays.round({ smallestUnit: "seconds" }) }`, "P40D"); - -// halfExpand is the default -assert.sameValue(`${ d.round({ - smallestUnit: "years", - relativeTo -}) }`, "P6Y"); -assert.sameValue(`${ d.negated().round({ - smallestUnit: "years", - relativeTo -}) }`, "-P6Y"); - -// balances up differently depending on relativeTo -var fortyDays = Temporal.Duration.from({ days: 40 }); -assert.sameValue(`${ fortyDays.round({ - largestUnit: "years", - relativeTo: "2020-01-01" -}) }`, "P1M9D"); -assert.sameValue(`${ fortyDays.round({ - largestUnit: "years", - relativeTo: "2020-02-01" -}) }`, "P1M11D"); -assert.sameValue(`${ fortyDays.round({ - largestUnit: "years", - relativeTo: "2020-03-01" -}) }`, "P1M9D"); -assert.sameValue(`${ fortyDays.round({ - largestUnit: "years", - relativeTo: "2020-04-01" -}) }`, "P1M10D"); -var minusForty = Temporal.Duration.from({ days: -40 }); -assert.sameValue(`${ minusForty.round({ - largestUnit: "years", - relativeTo: "2020-02-01" -}) }`, "-P1M9D"); -assert.sameValue(`${ minusForty.round({ - largestUnit: "years", - relativeTo: "2020-01-01" -}) }`, "-P1M9D"); -assert.sameValue(`${ minusForty.round({ - largestUnit: "years", - relativeTo: "2020-03-01" -}) }`, "-P1M11D"); -assert.sameValue(`${ minusForty.round({ - largestUnit: "years", - relativeTo: "2020-04-01" -}) }`, "-P1M9D"); - -// balances up to the next unit after rounding -var almostWeek = Temporal.Duration.from({ - days: 6, - hours: 20 -}); -assert.sameValue(`${ almostWeek.round({ - largestUnit: "weeks", - smallestUnit: "days", - relativeTo: "2020-01-01" -}) }`, "P1W"); - -// balances days up to both years and months -var twoYears = Temporal.Duration.from({ - months: 11, - days: 396 -}); -assert.sameValue(`${ twoYears.round({ - largestUnit: "years", - relativeTo: "2017-01-01" -}) }`, "P2Y"); - -// does not balance up to weeks if largestUnit is larger than weeks -var monthAlmostWeek = Temporal.Duration.from({ - months: 1, - days: 6, - hours: 20 -}); -assert.sameValue(`${ monthAlmostWeek.round({ - smallestUnit: "days", - relativeTo: "2020-01-01" -}) }`, "P1M7D"); - -// balances down differently depending on relativeTo -var oneYear = Temporal.Duration.from({ years: 1 }); -assert.sameValue(`${ oneYear.round({ - largestUnit: "days", - relativeTo: "2019-01-01" -}) }`, "P365D"); -assert.sameValue(`${ oneYear.round({ - largestUnit: "days", - relativeTo: "2019-07-01" -}) }`, "P366D"); -var minusYear = Temporal.Duration.from({ years: -1 }); -assert.sameValue(`${ minusYear.round({ - largestUnit: "days", - relativeTo: "2020-01-01" -}) }`, "-P365D"); -assert.sameValue(`${ minusYear.round({ - largestUnit: "days", - relativeTo: "2020-07-01" -}) }`, "-P366D"); - -// rounds to an increment of hours -assert.sameValue(`${ d.round({ - smallestUnit: "hours", - roundingIncrement: 3, - relativeTo -}) }`, "P5Y6M10DT6H"); - -// rounds to an increment of minutes -assert.sameValue(`${ d.round({ - smallestUnit: "minutes", - roundingIncrement: 30, - relativeTo -}) }`, "P5Y6M10DT5H"); - -// rounds to an increment of seconds -assert.sameValue(`${ d.round({ - smallestUnit: "seconds", - roundingIncrement: 15, - relativeTo -}) }`, "P5Y6M10DT5H5M"); - -// rounds to an increment of milliseconds -assert.sameValue(`${ d.round({ - smallestUnit: "milliseconds", - roundingIncrement: 10, - relativeTo -}) }`, "P5Y6M10DT5H5M5.01S"); - -// rounds to an increment of microseconds -assert.sameValue(`${ d.round({ - smallestUnit: "microseconds", - roundingIncrement: 10, - relativeTo -}) }`, "P5Y6M10DT5H5M5.00501S"); - -// rounds to an increment of nanoseconds -assert.sameValue(`${ d.round({ - smallestUnit: "nanoseconds", - roundingIncrement: 10, - relativeTo -}) }`, "P5Y6M10DT5H5M5.00500501S"); - -// valid hour increments divide into 24 -[ - 1, - 2, - 3, - 4, - 6, - 8, - 12 -].forEach(roundingIncrement => { - var options = { - smallestUnit: "hours", - roundingIncrement, - relativeTo - }; - assert(d.round(options) instanceof Temporal.Duration); -}); -[ - "minutes", - "seconds" -].forEach(smallestUnit => { - // valid minutes/seconds increments divide into 60 - [ - 1, - 2, - 3, - 4, - 5, - 6, - 10, - 12, - 15, - 20, - 30 - ].forEach(roundingIncrement => { - var roundTo = { - smallestUnit, - roundingIncrement, - relativeTo - }; - assert(d.round(roundTo) instanceof Temporal.Duration); - }); - }); -[ - "milliseconds", - "microseconds", - "nanoseconds" -].forEach(smallestUnit => { - // valid milliseconds/microseconds/nanoseconds increments divide into 1000 - [ - 1, - 2, - 4, - 5, - 8, - 10, - 20, - 25, - 40, - 50, - 100, - 125, - 200, - 250, - 500 - ].forEach(roundingIncrement => { - var roundTo = { - smallestUnit, - roundingIncrement, - relativeTo - }; - assert(d.round(roundTo) instanceof Temporal.Duration); - }); - }); - -// throws on increments that do not divide evenly into the next highest -assert.throws(RangeError, () => d.round({ - relativeTo, - smallestUnit: "hours", - roundingIncrement: 11 -})); -assert.throws(RangeError, () => d.round({ - relativeTo, - smallestUnit: "minutes", - roundingIncrement: 29 -})); -assert.throws(RangeError, () => d.round({ - relativeTo, - smallestUnit: "seconds", - roundingIncrement: 29 -})); -assert.throws(RangeError, () => d.round({ - relativeTo, - smallestUnit: "milliseconds", - roundingIncrement: 29 -})); -assert.throws(RangeError, () => d.round({ - relativeTo, - smallestUnit: "microseconds", - roundingIncrement: 29 -})); -assert.throws(RangeError, () => d.round({ - relativeTo, - smallestUnit: "nanoseconds", - roundingIncrement: 29 -})); - -// throws on increments that are equal to the next highest -assert.throws(RangeError, () => d.round({ - relativeTo, - smallestUnit: "hours", - roundingIncrement: 24 -})); -assert.throws(RangeError, () => d.round({ - relativeTo, - smallestUnit: "minutes", - roundingIncrement: 60 -})); -assert.throws(RangeError, () => d.round({ - relativeTo, - smallestUnit: "seconds", - roundingIncrement: 60 -})); -assert.throws(RangeError, () => d.round({ - relativeTo, - smallestUnit: "milliseconds", - roundingIncrement: 1000 -})); -assert.throws(RangeError, () => d.round({ - relativeTo, - smallestUnit: "microseconds", - roundingIncrement: 1000 -})); -assert.throws(RangeError, () => d.round({ - relativeTo, - smallestUnit: "nanoseconds", - roundingIncrement: 1000 -})); - -// accepts singular units -assert.sameValue(`${ d.round({ - largestUnit: "year", - relativeTo -}) }`, `${ d.round({ - largestUnit: "years", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - smallestUnit: "year", - relativeTo -}) }`, `${ d.round({ - smallestUnit: "years", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - largestUnit: "month", - relativeTo -}) }`, `${ d.round({ - largestUnit: "months", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - smallestUnit: "month", - relativeTo -}) }`, `${ d.round({ - smallestUnit: "months", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - largestUnit: "day", - relativeTo -}) }`, `${ d.round({ - largestUnit: "days", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - smallestUnit: "day", - relativeTo -}) }`, `${ d.round({ - smallestUnit: "days", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - largestUnit: "hour", - relativeTo -}) }`, `${ d.round({ - largestUnit: "hours", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - smallestUnit: "hour", - relativeTo -}) }`, `${ d.round({ - smallestUnit: "hours", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - largestUnit: "minute", - relativeTo -}) }`, `${ d.round({ - largestUnit: "minutes", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - smallestUnit: "minute", - relativeTo -}) }`, `${ d.round({ - smallestUnit: "minutes", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - largestUnit: "second", - relativeTo -}) }`, `${ d.round({ - largestUnit: "seconds", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - smallestUnit: "second", - relativeTo -}) }`, `${ d.round({ - smallestUnit: "seconds", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - largestUnit: "millisecond", - relativeTo -}) }`, `${ d.round({ - largestUnit: "milliseconds", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - smallestUnit: "millisecond", - relativeTo -}) }`, `${ d.round({ - smallestUnit: "milliseconds", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - largestUnit: "microsecond", - relativeTo -}) }`, `${ d.round({ - largestUnit: "microseconds", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - smallestUnit: "microsecond", - relativeTo -}) }`, `${ d.round({ - smallestUnit: "microseconds", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - largestUnit: "nanosecond", - relativeTo -}) }`, `${ d.round({ - largestUnit: "nanoseconds", - relativeTo -}) }`); -assert.sameValue(`${ d.round({ - smallestUnit: "nanosecond", - relativeTo -}) }`, `${ d.round({ - smallestUnit: "nanoseconds", - relativeTo -}) }`); - -// counts the correct number of days when rounding relative to a date -var days = Temporal.Duration.from({ days: 45 }); -assert.sameValue(`${ days.round({ - relativeTo: "2019-01-01", - smallestUnit: "months" -}) }`, "P2M"); -assert.sameValue(`${ days.negated().round({ - relativeTo: "2019-02-15", - smallestUnit: "months" -}) }`, "-P1M"); -var yearAndHalf = Temporal.Duration.from({ - days: 547, - hours: 12 -}); -assert.sameValue(`${ yearAndHalf.round({ - relativeTo: "2018-01-01", - smallestUnit: "years" -}) }`, "P2Y"); -assert.sameValue(`${ yearAndHalf.round({ - relativeTo: "2018-07-01", - smallestUnit: "years" -}) }`, "P1Y"); -assert.sameValue(`${ yearAndHalf.round({ - relativeTo: "2019-01-01", - smallestUnit: "years" -}) }`, "P1Y"); -assert.sameValue(`${ yearAndHalf.round({ - relativeTo: "2019-07-01", - smallestUnit: "years" -}) }`, "P1Y"); -assert.sameValue(`${ yearAndHalf.round({ - relativeTo: "2020-01-01", - smallestUnit: "years" -}) }`, "P1Y"); -assert.sameValue(`${ yearAndHalf.round({ - relativeTo: "2020-07-01", - smallestUnit: "years" -}) }`, "P2Y");