From 569b947186e140cd507fd4651517cfd4f93ea1dd Mon Sep 17 00:00:00 2001 From: Jesse Alama Date: Tue, 12 Apr 2022 17:36:03 +0200 Subject: [PATCH] Temporal: Port Demitasse PlainDateTime `round` tests (#3478) Co-authored-by: Ms2ger --- .../PlainDateTime/prototype/round/balance.js | 19 +++++++ .../round/roundingincrement-divides.js | 53 +++++++++++++++++++ .../roundingincrement-does-not-divide.js | 18 +++++++ .../round/roundingincrement-one-day.js | 17 ++++++ .../prototype/round/roundingmode-basic.js | 47 ++++++++++++++++ .../round/roundingmode-ceil-basic.js | 30 +++++++++++ .../round/roundingmode-floor-basic.js | 29 ++++++++++ .../round/roundingmode-halfexpand-basic.js | 30 +++++++++++ .../roundingmode-halfexpand-is-default.js | 31 +++++++++++ .../round/roundingmode-trunc-basic.js | 29 ++++++++++ ...hrows-argument-object-insufficient-data.js | 16 ++++++ .../prototype/round/throws-argument-object.js | 16 ++++++ .../prototype/round/throws-no-argument.js | 16 ++++++ .../prototype/round/throws-undefined.js | 16 ++++++ 14 files changed, 367 insertions(+) create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/balance.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/roundingincrement-divides.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/roundingincrement-does-not-divide.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/roundingincrement-one-day.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-basic.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-ceil-basic.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-floor-basic.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-halfexpand-basic.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-halfexpand-is-default.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-trunc-basic.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/throws-argument-object-insufficient-data.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/throws-argument-object.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/throws-no-argument.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/throws-undefined.js diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/balance.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/balance.js new file mode 100644 index 00000000000..37a7947b6e3 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/balance.js @@ -0,0 +1,19 @@ +// 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.plaindatetime.prototype.round +description: Rounding balances to the next smallest unit +features: [Temporal] +includes: [temporalHelpers.js] +---*/ + +const dt = new Temporal.PlainDateTime(1976, 11, 18, 23, 59, 59, 999, 999, 999); + +["day", "hour", "minute", "second", "millisecond", "microsecond"].forEach((smallestUnit) => { + TemporalHelpers.assertPlainDateTime( + dt.round({ smallestUnit }), + 1976, 11, "M11", 19, 0, 0, 0, 0, 0, 0, + `balances to next ${smallestUnit}` + ); +}); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingincrement-divides.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingincrement-divides.js new file mode 100644 index 00000000000..304046e331d --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingincrement-divides.js @@ -0,0 +1,53 @@ +// 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.plaindatetime.prototype.round +description: Rounding increment should properly divide the relevant time unit +features: [Temporal] +---*/ + +const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789); + +[1, 2, 3, 4, 6, 8, 12].forEach((roundingIncrement) => { + assert.sameValue( + dt.round({ smallestUnit: "hour", roundingIncrement }) instanceof Temporal.PlainDateTime, + true, + `valid hour increments divide into 24 (rounding increment = ${roundingIncrement})`); +}); + +["minute", "second"].forEach((smallestUnit) => { + [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30].forEach((roundingIncrement) => { + assert.sameValue( + dt.round({ smallestUnit, roundingIncrement }) instanceof Temporal.PlainDateTime, + true, + `valid ${smallestUnit} increments divide into 60 (rounding increment = ${roundingIncrement})` + ); + }); +}); + +["millisecond", "microsecond", "nanosecond"].forEach((smallestUnit) => { + [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 250, 500].forEach((roundingIncrement) => { + assert.sameValue( + dt.round({ smallestUnit, roundingIncrement }) instanceof Temporal.PlainDateTime, + true, + `valid ${smallestUnit} increments divide into 1000 (rounding increment = ${roundingIncrement})`); + }); +}); + +const nextIncrements = { + "hour": 24, + "minute": 60, + "second": 60, + "millisecond": 1000, + "microsecond": 1000, + "nanosecond": 1000 +}; + +Object.entries(nextIncrements).forEach(([unit, next]) => { + assert.throws( + RangeError, + () => dt.round({ smallestUnit: unit, roundingIncrement: next }), + `throws on increments that are equal to the next highest (unit = ${unit}, increment = ${next})` + ); +}); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingincrement-does-not-divide.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingincrement-does-not-divide.js new file mode 100644 index 00000000000..82ab14b519c --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingincrement-does-not-divide.js @@ -0,0 +1,18 @@ +// 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.plaindatetime.prototype.round +description: Throw exception if the rounding unit does not properly divide the relevant time unit +features: [Temporal] +---*/ + +const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789); +const units = ["day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond"]; +units.forEach((unit) => { + assert.throws( + RangeError, + () => dt.round({ smallestUnit: unit, roundingIncrement: 29 }), + `throws on increments that do not divide evenly into the next highest (unit = ${unit})` + ); +}); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingincrement-one-day.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingincrement-one-day.js new file mode 100644 index 00000000000..71470b4bb9c --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingincrement-one-day.js @@ -0,0 +1,17 @@ +// 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.plaindatetime.prototype.round +description: One day is a valid rounding increment +features: [Temporal] +includes: [temporalHelpers.js] +---*/ + +const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789); + +TemporalHelpers.assertPlainDateTime( + dt.round({ smallestUnit: "day", roundingIncrement: 1 }), + 1976, 11, "M11", 19, 0, 0, 0, 0, 0, 0, + "1 day is a valid increment" +); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-basic.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-basic.js new file mode 100644 index 00000000000..d434095df9a --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-basic.js @@ -0,0 +1,47 @@ +// 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.plaindatetime.prototype.round +description: Basic checks for rounding mode +features: [Temporal] +includes: [temporalHelpers.js] +---*/ + +const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789); + +TemporalHelpers.assertPlainDateTime( + dt.round({ smallestUnit: "hour", roundingIncrement: 4 }), + 1976, 11, "M11", 18, 16, 0, 0, 0, 0, 0, + "rounds to an increment of hours" +); + +TemporalHelpers.assertPlainDateTime( + dt.round({ smallestUnit: "minute", roundingIncrement: 15 }), + 1976, 11, "M11", 18, 14, 30, 0, 0, 0, 0, + "rounds to an increment of minutes" +); + +TemporalHelpers.assertPlainDateTime( + dt.round({ smallestUnit: "second", roundingIncrement: 30 }), + 1976, 11, "M11", 18, 14, 23, 30, 0, 0, 0, + "rounds to an increment of seconds" +); + +TemporalHelpers.assertPlainDateTime( + dt.round({ smallestUnit: "millisecond", roundingIncrement: 10 }), + 1976, 11, "M11", 18, 14, 23, 30, 120, 0, 0, + "rounds to an increment of milliseconds" +); + +TemporalHelpers.assertPlainDateTime( + dt.round({ smallestUnit: "microsecond", roundingIncrement: 10 }), + 1976, 11, "M11", 18, 14, 23, 30, 123, 460, 0, + "rounds to an increment of microseconds" +); + +TemporalHelpers.assertPlainDateTime( + dt.round({ smallestUnit: "nanosecond", roundingIncrement: 10 }), + 1976, 11, "M11", 18, 14, 23, 30, 123, 456, 790, + "rounds to an increment of nanoseconds" +); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-ceil-basic.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-ceil-basic.js new file mode 100644 index 00000000000..3e290c93178 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-ceil-basic.js @@ -0,0 +1,30 @@ +// 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.plaindatetime.prototype.round +description: Basic checks for ceiling rounding mode +features: [Temporal] +includes: [temporalHelpers.js] +---*/ + +const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789); + +const incrementOneCeil = { + "day": [1976, 11, "M11", 19, 0, 0, 0, 0, 0, 0], + "hour": [1976, 11, "M11", 18, 15, 0, 0, 0, 0, 0], + "minute": [1976, 11, "M11", 18, 14, 24, 0, 0, 0, 0], + "second": [1976, 11, "M11", 18, 14, 23, 31, 0, 0, 0], + "millisecond": [1976, 11, "M11", 18, 14, 23, 30, 124, 0, 0], + "microsecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 457, 0], + "nanosecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 456, 789] +}; + +Object.entries(incrementOneCeil).forEach(([smallestUnit, expected]) => { + TemporalHelpers.assertPlainDateTime( + dt.round({ smallestUnit, roundingMode: "ceil" }), + ...expected, + `rounds up to ${smallestUnit} (ceil)`, + undefined + ); +}); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-floor-basic.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-floor-basic.js new file mode 100644 index 00000000000..8379b6100f8 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-floor-basic.js @@ -0,0 +1,29 @@ +// 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.plaindatetime.prototype.round +description: Basic checks for the floor rounding mode +features: [Temporal] +includes: [temporalHelpers.js] +---*/ + +const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789); + +const incrementOneFloor = { + "day": [1976, 11, "M11", 18, 0, 0, 0, 0, 0, 0], + "hour": [1976, 11, "M11", 18, 14, 0, 0, 0, 0, 0], + "minute": [1976, 11, "M11", 18, 14, 23, 0, 0, 0, 0], + "second": [1976, 11, "M11", 18, 14, 23, 30, 0, 0, 0], + "millisecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 0, 0], + "microsecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 456, 0], + "nanosecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 456, 789] +}; + +Object.entries(incrementOneFloor).forEach(([smallestUnit, expected]) => { + TemporalHelpers.assertPlainDateTime( + dt.round({ smallestUnit, roundingMode: "floor" }), + ...expected, + `rounds down to ${smallestUnit} (floor)` + ); +}); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-halfexpand-basic.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-halfexpand-basic.js new file mode 100644 index 00000000000..5cb4275c3aa --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-halfexpand-basic.js @@ -0,0 +1,30 @@ +// 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.plaindatetime.prototype.round +description: Basic checks for half-expand rounding mode +features: [Temporal] +includes: [temporalHelpers.js] +---*/ + +const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789); + +const incrementOneNearest = { + "day": [1976, 11, "M11", 19, 0, 0, 0, 0, 0, 0], + "hour": [1976, 11, "M11", 18, 14, 0, 0, 0, 0, 0], + "minute": [1976, 11, "M11", 18, 14, 24, 0, 0, 0, 0], + "second": [1976, 11, "M11", 18, 14, 23, 30, 0, 0, 0], + "millisecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 0, 0], + "microsecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 457, 0], + "nanosecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 456, 789] +}; + +Object.entries(incrementOneNearest).forEach(([smallestUnit, expected]) => { + TemporalHelpers.assertPlainDateTime( + dt.round({ smallestUnit, roundingMode: "halfExpand" }), + ...expected, + `rounds to nearest ${smallestUnit} (half-expand)`, + undefined + ); +}); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-halfexpand-is-default.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-halfexpand-is-default.js new file mode 100644 index 00000000000..47274d17459 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-halfexpand-is-default.js @@ -0,0 +1,31 @@ +// 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.plaindatetime.prototype.round +description: Half-expand is the default rounding mode +features: [Temporal] +includes: [temporalHelpers.js] +---*/ + +const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789); + +const units = { + "day": [1976, 11, "M11", 19, 0, 0, 0, 0, 0, 0], + "hour": [1976, 11, "M11", 18, 14, 0, 0, 0, 0, 0], + "minute": [1976, 11, "M11", 18, 14, 24, 0, 0, 0, 0], + "second": [1976, 11, "M11", 18, 14, 23, 30, 0, 0, 0], + "millisecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 0, 0], + "microsecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 457, 0], + "nanosecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 456, 789] +}; + +const expected = [1976, 11, "M11", 18, 0, 0, 0, 0, 0, 0]; + +Object.entries(units).forEach(([unit, expected]) => { + TemporalHelpers.assertPlainDateTime( + dt.round({ smallestUnit: unit }), + ...expected, + `halfExpand is the default (smallest unit = ${unit}, rounding mode absent)` + ); +}); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-trunc-basic.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-trunc-basic.js new file mode 100644 index 00000000000..c8f0a278488 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-trunc-basic.js @@ -0,0 +1,29 @@ +// 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.plaindatetime.prototype.round +description: Basic checks for truncation rounding mode +features: [Temporal] +includes: [temporalHelpers.js] +---*/ + +const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789); + +const incrementOneFloor = { + "day": [1976, 11, "M11", 18, 0, 0, 0, 0, 0, 0], + "hour": [1976, 11, "M11", 18, 14, 0, 0, 0, 0, 0], + "minute": [1976, 11, "M11", 18, 14, 23, 0, 0, 0, 0], + "second": [1976, 11, "M11", 18, 14, 23, 30, 0, 0, 0], + "millisecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 0, 0], + "microsecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 456, 0], + "nanosecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 456, 789] +}; + +Object.entries(incrementOneFloor).forEach(([smallestUnit, expected]) => { + TemporalHelpers.assertPlainDateTime( + dt.round({ smallestUnit, roundingMode: "trunc" }), + ...expected, + `truncates to ${smallestUnit}` + ); +}); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/throws-argument-object-insufficient-data.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/throws-argument-object-insufficient-data.js new file mode 100644 index 00000000000..7f57d1d220a --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/throws-argument-object-insufficient-data.js @@ -0,0 +1,16 @@ +// 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.plaindatetime.prototype.round +description: Throw if smallest unit is missing from argument +features: [Temporal] +---*/ + +const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789); + +assert.throws( + RangeError, + () => dt.round({ roundingIncrement: 1, roundingMode: "ceil" }), + "throws without required smallestUnit parameter" +); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/throws-argument-object.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/throws-argument-object.js new file mode 100644 index 00000000000..8ca7977a4e9 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/throws-argument-object.js @@ -0,0 +1,16 @@ +// 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.plaindatetime.prototype.round +description: Throw if argument is an empty plain object +features: [Temporal] +---*/ + +const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789); + +assert.throws( + RangeError, + () => dt.round({}), + "throws on empty object" +); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/throws-no-argument.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/throws-no-argument.js new file mode 100644 index 00000000000..d75fe284628 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/throws-no-argument.js @@ -0,0 +1,16 @@ +// 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.plaindatetime.prototype.round +description: Throw if no arguments at all are given +features: [Temporal] +---*/ + +const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789); + +assert.throws( + TypeError, + () => dt.round(), + "throws without any parameters" +); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/throws-undefined.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/throws-undefined.js new file mode 100644 index 00000000000..7b49ded8ddc --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/throws-undefined.js @@ -0,0 +1,16 @@ +// 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.plaindatetime.prototype.round +description: Throw if sole argument is undefined +features: [Temporal] +---*/ + +const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789); + +assert.throws( + TypeError, + () => dt.round(undefined), + "throws without undefined as sole parameter" +);