From 7099acc88244deea6db6222f1dd2a80261de9f9c Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Mon, 2 May 2022 16:12:20 +0200 Subject: [PATCH] Temporal: Add some tests for Duration#round. Co-authored-by: Philip Chimento --- .../round/largestunit-smallestunit-default.js | 38 +++++++++++++++++++ .../largestunit-smallestunit-mismatch.js | 20 ++++++++++ .../Duration/prototype/round/smallestunit.js | 34 +++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-default.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-mismatch.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/smallestunit.js diff --git a/test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-default.js b/test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-default.js new file mode 100644 index 0000000000..efef710a08 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-default.js @@ -0,0 +1,38 @@ +// Copyright (C) 2022 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: assumes a different default for largestUnit if smallestUnit is larger than the default +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const relativeTo = Temporal.PlainDate.from("2020-01-01"); +const almostYear = Temporal.Duration.from({ days: 364 }); +TemporalHelpers.assertDuration(almostYear.round({ smallestUnit: "years", relativeTo }), + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years"); +const almostMonth = Temporal.Duration.from({ days: 27 }); +TemporalHelpers.assertDuration(almostMonth.round({ smallestUnit: "months", relativeTo }), + 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "months"); +const almostWeek = Temporal.Duration.from({ days: 6 }); +TemporalHelpers.assertDuration(almostWeek.round({ smallestUnit: "weeks", relativeTo }), + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "weeks"); +const almostDay = Temporal.Duration.from({ seconds: 86399 }); +TemporalHelpers.assertDuration(almostDay.round({ smallestUnit: "days" }), + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "days"); +const almostHour = Temporal.Duration.from({ seconds: 3599 }); +TemporalHelpers.assertDuration(almostHour.round({ smallestUnit: "hours" }), + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "hours"); +const almostMinute = Temporal.Duration.from({ seconds: 59 }); +TemporalHelpers.assertDuration(almostMinute.round({ smallestUnit: "minutes" }), + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "minutes"); +const almostSecond = Temporal.Duration.from({ nanoseconds: 999999999 }); +TemporalHelpers.assertDuration(almostSecond.round({ smallestUnit: "seconds" }), + 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "seconds"); +const almostMillisecond = Temporal.Duration.from({ nanoseconds: 999999 }); +TemporalHelpers.assertDuration(almostMillisecond.round({ smallestUnit: "milliseconds" }), + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "milliseconds"); +const almostMicrosecond = Temporal.Duration.from({ nanoseconds: 999 }); +TemporalHelpers.assertDuration(almostMicrosecond.round({ smallestUnit: "microseconds" }), + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "microseconds"); diff --git a/test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-mismatch.js b/test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-mismatch.js new file mode 100644 index 0000000000..7517cb336b --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-mismatch.js @@ -0,0 +1,20 @@ +// Copyright (C) 2021 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: RangeError thrown when smallestUnit is larger than largestUnit +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'); +const units = ["years", "months", "weeks", "days", "hours", "minutes", "seconds", "milliseconds", "microseconds", "nanoseconds"]; +for (let largestIdx = 1; largestIdx < units.length; largestIdx++) { + for (let smallestIdx = 0; smallestIdx < largestIdx; smallestIdx++) { + const largestUnit = units[largestIdx]; + const smallestUnit = units[smallestIdx]; + assert.throws(RangeError, () => d.round({ largestUnit, smallestUnit, relativeTo }), + `${smallestUnit} > ${largestUnit}`); + } +} diff --git a/test/built-ins/Temporal/Duration/prototype/round/smallestunit.js b/test/built-ins/Temporal/Duration/prototype/round/smallestunit.js new file mode 100644 index 0000000000..8f62f8928a --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/smallestunit.js @@ -0,0 +1,34 @@ +// Copyright (C) 2022 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: smallestUnit should be taken into account +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const d = Temporal.Duration.from({ + days: 1, + hours: 2, + minutes: 3, + seconds: 4, + milliseconds: 5, + microseconds: 6, + nanoseconds: 7 +}); +const tests = { + 'day': [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], + 'hour': [0, 0, 0, 1, 2, 0, 0, 0, 0, 0], + 'minute': [0, 0, 0, 1, 2, 3, 0, 0, 0, 0], + 'second': [0, 0, 0, 1, 2, 3, 4, 0, 0, 0], + 'millisecond': [0, 0, 0, 1, 2, 3, 4, 5, 0, 0], + 'microsecond': [0, 0, 0, 1, 2, 3, 4, 5, 6, 0], + 'nanosecond': [0, 0, 0, 1, 2, 3, 4, 5, 6, 7], +}; +for (const [smallestUnit, expected] of Object.entries(tests)) { + TemporalHelpers.assertDuration(d.round(smallestUnit), ...expected, + `"${smallestUnit}" should work as argument`); + TemporalHelpers.assertDuration(d.round({ smallestUnit }), ...expected, + `"${smallestUnit}" should work in option bag`); +}