From 3905c0c80afb5d6cf0c433fa9232d63f42ef6141 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Fri, 8 Apr 2022 11:39:40 -0700 Subject: [PATCH] Expand toString() rounding tests from PlainTime to cover other types This takes the tests of the rounding functionality of Temporal.PlainTime.p.toString() and adds similar tests covering the equivalent functionality to Duration, Instant, PlainDateTime, and ZonedDateTime: all the types that have rounding and precision controls for how they output their subsecond values. It also takes the opportunity to improve the existing PlainTime tests: - fractionalseconddigits-auto.js: More descriptive variable names. Added assertion messages. - fractionalseconddigits-number.js: Ditto. - rounding-cross-midnight.js: Use constructor directly to remove coupling with from(). - roundingmode-*.js: Add additional tests for specifying the precision using fractionalSecondDigits. - smallestunit-fractionalseconddigits.js: Add assertion messages. --- .../toString/fractionalseconddigits-auto.js | 21 +++++++++++ .../toString/fractionalseconddigits-number.js | 28 ++++++++++++++ .../prototype/toString/roundingmode-ceil.js | 34 +++++++++++++++++ .../prototype/toString/roundingmode-floor.js | 34 +++++++++++++++++ .../toString/roundingmode-halfExpand.js | 34 +++++++++++++++++ .../prototype/toString/roundingmode-trunc.js | 34 +++++++++++++++++ .../smallestunit-fractionalseconddigits.js | 29 +++++++++++++++ .../toString/fractionalseconddigits-auto.js | 23 ++++++++++++ .../toString/fractionalseconddigits-number.js | 33 +++++++++++++++++ .../toString/rounding-cross-midnight.js | 13 +++++++ .../prototype/toString/roundingmode-ceil.js | 37 +++++++++++++++++++ .../prototype/toString/roundingmode-floor.js | 37 +++++++++++++++++++ .../toString/roundingmode-halfExpand.js | 37 +++++++++++++++++++ .../prototype/toString/roundingmode-trunc.js | 37 +++++++++++++++++++ .../smallestunit-fractionalseconddigits.js | 30 +++++++++++++++ .../toString/fractionalseconddigits-auto.js | 23 ++++++++++++ .../toString/fractionalseconddigits-number.js | 33 +++++++++++++++++ .../toString/rounding-cross-midnight.js | 13 +++++++ .../prototype/toString/roundingmode-ceil.js | 37 +++++++++++++++++++ .../prototype/toString/roundingmode-floor.js | 37 +++++++++++++++++++ .../toString/roundingmode-halfExpand.js | 37 +++++++++++++++++++ .../prototype/toString/roundingmode-trunc.js | 37 +++++++++++++++++++ .../smallestunit-fractionalseconddigits.js | 30 +++++++++++++++ .../toString/fractionalseconddigits-auto.js | 16 +++++--- .../toString/fractionalseconddigits-number.js | 37 ++++++++++++------- .../toString/rounding-cross-midnight.js | 2 +- .../prototype/toString/roundingmode-ceil.js | 28 +++++++++++--- .../prototype/toString/roundingmode-floor.js | 28 +++++++++++--- .../toString/roundingmode-halfExpand.js | 28 +++++++++++--- .../prototype/toString/roundingmode-trunc.js | 28 +++++++++++--- .../smallestunit-fractionalseconddigits.js | 5 +-- .../toString/fractionalseconddigits-auto.js | 23 ++++++++++++ .../toString/fractionalseconddigits-number.js | 33 +++++++++++++++++ .../toString/rounding-cross-midnight.js | 13 +++++++ .../prototype/toString/roundingmode-ceil.js | 37 +++++++++++++++++++ .../prototype/toString/roundingmode-floor.js | 37 +++++++++++++++++++ .../toString/roundingmode-halfExpand.js | 37 +++++++++++++++++++ .../prototype/toString/roundingmode-trunc.js | 37 +++++++++++++++++++ .../smallestunit-fractionalseconddigits.js | 30 +++++++++++++++ 39 files changed, 1084 insertions(+), 43 deletions(-) create mode 100644 test/built-ins/Temporal/Duration/prototype/toString/fractionalseconddigits-auto.js create mode 100644 test/built-ins/Temporal/Duration/prototype/toString/fractionalseconddigits-number.js create mode 100644 test/built-ins/Temporal/Duration/prototype/toString/roundingmode-ceil.js create mode 100644 test/built-ins/Temporal/Duration/prototype/toString/roundingmode-floor.js create mode 100644 test/built-ins/Temporal/Duration/prototype/toString/roundingmode-halfExpand.js create mode 100644 test/built-ins/Temporal/Duration/prototype/toString/roundingmode-trunc.js create mode 100644 test/built-ins/Temporal/Duration/prototype/toString/smallestunit-fractionalseconddigits.js create mode 100644 test/built-ins/Temporal/Instant/prototype/toString/fractionalseconddigits-auto.js create mode 100644 test/built-ins/Temporal/Instant/prototype/toString/fractionalseconddigits-number.js create mode 100644 test/built-ins/Temporal/Instant/prototype/toString/rounding-cross-midnight.js create mode 100644 test/built-ins/Temporal/Instant/prototype/toString/roundingmode-ceil.js create mode 100644 test/built-ins/Temporal/Instant/prototype/toString/roundingmode-floor.js create mode 100644 test/built-ins/Temporal/Instant/prototype/toString/roundingmode-halfExpand.js create mode 100644 test/built-ins/Temporal/Instant/prototype/toString/roundingmode-trunc.js create mode 100644 test/built-ins/Temporal/Instant/prototype/toString/smallestunit-fractionalseconddigits.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-auto.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-number.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/toString/rounding-cross-midnight.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-ceil.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-floor.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-halfExpand.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-trunc.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/toString/smallestunit-fractionalseconddigits.js create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/toString/fractionalseconddigits-auto.js create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/toString/fractionalseconddigits-number.js create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/toString/rounding-cross-midnight.js create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-ceil.js create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-floor.js create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-halfExpand.js create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-trunc.js create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/toString/smallestunit-fractionalseconddigits.js diff --git a/test/built-ins/Temporal/Duration/prototype/toString/fractionalseconddigits-auto.js b/test/built-ins/Temporal/Duration/prototype/toString/fractionalseconddigits-auto.js new file mode 100644 index 0000000000..941b9dce93 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/toString/fractionalseconddigits-auto.js @@ -0,0 +1,21 @@ +// 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.tostring +description: auto value for fractionalSecondDigits option +features: [Temporal] +---*/ + +const wholeSeconds = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7); +const subSeconds = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 987, 650); + +const tests = [ + [wholeSeconds, "P1Y2M3W4DT5H6M7S"], + [subSeconds, "P1Y2M3W4DT5H6M7.98765S"], +]; + +for (const [duration, expected] of tests) { + assert.sameValue(duration.toString(), expected, "default is to emit seconds and drop trailing zeroes"); + assert.sameValue(duration.toString({ fractionalSecondDigits: "auto" }), expected, "auto is the default"); +} diff --git a/test/built-ins/Temporal/Duration/prototype/toString/fractionalseconddigits-number.js b/test/built-ins/Temporal/Duration/prototype/toString/fractionalseconddigits-number.js new file mode 100644 index 0000000000..764986cb2e --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/toString/fractionalseconddigits-number.js @@ -0,0 +1,28 @@ +// 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.tostring +description: Number for fractionalSecondDigits option +features: [Temporal] +---*/ + +const wholeSeconds = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7); +const subSeconds = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 987, 650); + +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 0 }), "P1Y2M3W4DT5H6M7S", + "truncates 4 decimal places to 0"); +assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 2 }), "P1Y2M3W4DT5H6M7.00S", + "pads whole seconds to 2 decimal places"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 2 }), "P1Y2M3W4DT5H6M7.98S", + "truncates 4 decimal places to 2"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 3 }), "P1Y2M3W4DT5H6M7.987S", + "truncates 4 decimal places to 3"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 6 }), "P1Y2M3W4DT5H6M7.987650S", + "pads 4 decimal places to 6"); +assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 7 }), "P1Y2M3W4DT5H6M7.0000000S", + "pads whole seconds to 7 decimal places"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 7 }), "P1Y2M3W4DT5H6M7.9876500S", + "pads 4 decimal places to 7"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 9 }), "P1Y2M3W4DT5H6M7.987650000S", + "pads 4 decimal places to 9"); diff --git a/test/built-ins/Temporal/Duration/prototype/toString/roundingmode-ceil.js b/test/built-ins/Temporal/Duration/prototype/toString/roundingmode-ceil.js new file mode 100644 index 0000000000..cdef0c5274 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/toString/roundingmode-ceil.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.tostring +description: ceil value for roundingMode option +features: [Temporal] +---*/ + +const duration = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 123, 987, 500); + +const result1 = duration.toString({ smallestUnit: "microsecond", roundingMode: "ceil" }); +assert.sameValue(result1, "P1Y2M3W4DT5H6M7.123988S", + "roundingMode is ceil (with 6 digits from smallestUnit)"); + +const result2 = duration.toString({ fractionalSecondDigits: 6, roundingMode: "ceil" }); +assert.sameValue(result2, "P1Y2M3W4DT5H6M7.123988S", + "roundingMode is ceil (with 6 digits from fractionalSecondDigits)"); + +const result3 = duration.toString({ smallestUnit: "millisecond", roundingMode: "ceil" }); +assert.sameValue(result3, "P1Y2M3W4DT5H6M7.124S", + "roundingMode is ceil (with 3 digits from smallestUnit)"); + +const result4 = duration.toString({ fractionalSecondDigits: 3, roundingMode: "ceil" }); +assert.sameValue(result4, "P1Y2M3W4DT5H6M7.124S", + "roundingMode is ceil (with 3 digits from fractionalSecondDigits)"); + +const result5 = duration.toString({ smallestUnit: "second", roundingMode: "ceil" }); +assert.sameValue(result5, "P1Y2M3W4DT5H6M8S", + "roundingMode is ceil (with 0 digits from smallestUnit)"); + +const result6 = duration.toString({ fractionalSecondDigits: 0, roundingMode: "ceil" }); +assert.sameValue(result6, "P1Y2M3W4DT5H6M8S", + "roundingMode is ceil (with 0 digits from fractionalSecondDigits)"); diff --git a/test/built-ins/Temporal/Duration/prototype/toString/roundingmode-floor.js b/test/built-ins/Temporal/Duration/prototype/toString/roundingmode-floor.js new file mode 100644 index 0000000000..e1ee18b5c8 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/toString/roundingmode-floor.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.tostring +description: floor value for roundingMode option +features: [Temporal] +---*/ + +const duration = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 123, 987, 500); + +const result1 = duration.toString({ smallestUnit: "microsecond", roundingMode: "floor" }); +assert.sameValue(result1, "P1Y2M3W4DT5H6M7.123987S", + "roundingMode is floor (with 6 digits from smallestUnit)"); + +const result2 = duration.toString({ fractionalSecondDigits: 6, roundingMode: "floor" }); +assert.sameValue(result2, "P1Y2M3W4DT5H6M7.123987S", + "roundingMode is floor (with 6 digits from fractionalSecondDigits)"); + +const result3 = duration.toString({ smallestUnit: "millisecond", roundingMode: "floor" }); +assert.sameValue(result3, "P1Y2M3W4DT5H6M7.123S", + "roundingMode is floor (with 3 digits from smallestUnit)"); + +const result4 = duration.toString({ fractionalSecondDigits: 3, roundingMode: "floor" }); +assert.sameValue(result4, "P1Y2M3W4DT5H6M7.123S", + "roundingMode is floor (with 3 digits from fractionalSecondDigits)"); + +const result5 = duration.toString({ smallestUnit: "second", roundingMode: "floor" }); +assert.sameValue(result5, "P1Y2M3W4DT5H6M7S", + "roundingMode is floor (with 0 digits from smallestUnit)"); + +const result6 = duration.toString({ fractionalSecondDigits: 0, roundingMode: "floor" }); +assert.sameValue(result6, "P1Y2M3W4DT5H6M7S", + "roundingMode is floor (with 0 digits from fractionalSecondDigits)"); diff --git a/test/built-ins/Temporal/Duration/prototype/toString/roundingmode-halfExpand.js b/test/built-ins/Temporal/Duration/prototype/toString/roundingmode-halfExpand.js new file mode 100644 index 0000000000..9a11468a12 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/toString/roundingmode-halfExpand.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.tostring +description: halfExpand value for roundingMode option +features: [Temporal] +---*/ + +const duration = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 123, 987, 500); + +const result1 = duration.toString({ smallestUnit: "microsecond", roundingMode: "halfExpand" }); +assert.sameValue(result1, "P1Y2M3W4DT5H6M7.123988S", + "roundingMode is halfExpand (with 6 digits from smallestUnit)"); + +const result2 = duration.toString({ fractionalSecondDigits: 6, roundingMode: "halfExpand" }); +assert.sameValue(result2, "P1Y2M3W4DT5H6M7.123988S", + "roundingMode is halfExpand (with 6 digits from fractionalSecondDigits)"); + +const result3 = duration.toString({ smallestUnit: "millisecond", roundingMode: "halfExpand" }); +assert.sameValue(result3, "P1Y2M3W4DT5H6M7.124S", + "roundingMode is halfExpand (with 3 digits from smallestUnit)"); + +const result4 = duration.toString({ fractionalSecondDigits: 3, roundingMode: "halfExpand" }); +assert.sameValue(result4, "P1Y2M3W4DT5H6M7.124S", + "roundingMode is halfExpand (with 3 digits from fractionalSecondDigits)"); + +const result5 = duration.toString({ smallestUnit: "second", roundingMode: "halfExpand" }); +assert.sameValue(result5, "P1Y2M3W4DT5H6M7S", + "roundingMode is halfExpand (with 0 digits from smallestUnit)"); + +const result6 = duration.toString({ fractionalSecondDigits: 0, roundingMode: "halfExpand" }); +assert.sameValue(result6, "P1Y2M3W4DT5H6M7S", + "roundingMode is halfExpand (with 0 digits from fractionalSecondDigits)"); diff --git a/test/built-ins/Temporal/Duration/prototype/toString/roundingmode-trunc.js b/test/built-ins/Temporal/Duration/prototype/toString/roundingmode-trunc.js new file mode 100644 index 0000000000..3b66fb5201 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/toString/roundingmode-trunc.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.tostring +description: trunc value for roundingMode option +features: [Temporal] +---*/ + +const duration = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 123, 987, 500); + +const result1 = duration.toString({ smallestUnit: "microsecond", roundingMode: "trunc" }); +assert.sameValue(result1, "P1Y2M3W4DT5H6M7.123987S", + "roundingMode is trunc (with 6 digits from smallestUnit)"); + +const result2 = duration.toString({ fractionalSecondDigits: 6, roundingMode: "trunc" }); +assert.sameValue(result2, "P1Y2M3W4DT5H6M7.123987S", + "roundingMode is trunc (with 6 digits from fractionalSecondDigits)"); + +const result3 = duration.toString({ smallestUnit: "millisecond", roundingMode: "trunc" }); +assert.sameValue(result3, "P1Y2M3W4DT5H6M7.123S", + "roundingMode is trunc (with 3 digits from smallestUnit)"); + +const result4 = duration.toString({ fractionalSecondDigits: 3, roundingMode: "trunc" }); +assert.sameValue(result4, "P1Y2M3W4DT5H6M7.123S", + "roundingMode is trunc (with 3 digits from fractionalSecondDigits)"); + +const result5 = duration.toString({ smallestUnit: "second", roundingMode: "trunc" }); +assert.sameValue(result5, "P1Y2M3W4DT5H6M7S", + "roundingMode is trunc (with 0 digits from smallestUnit)"); + +const result6 = duration.toString({ fractionalSecondDigits: 0, roundingMode: "trunc" }); +assert.sameValue(result6, "P1Y2M3W4DT5H6M7S", + "roundingMode is trunc (with 0 digits from fractionalSecondDigits)"); diff --git a/test/built-ins/Temporal/Duration/prototype/toString/smallestunit-fractionalseconddigits.js b/test/built-ins/Temporal/Duration/prototype/toString/smallestunit-fractionalseconddigits.js new file mode 100644 index 0000000000..19d1052380 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/toString/smallestunit-fractionalseconddigits.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.duration.prototype.tostring +description: fractionalSecondDigits option is not used with smallestUnit present +features: [Temporal] +---*/ + +const duration = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 789, 999, 999); +const tests = [ + ["second", "P1Y2M3W4DT5H6M7S"], + ["millisecond", "P1Y2M3W4DT5H6M7.789S"], + ["microsecond", "P1Y2M3W4DT5H6M7.789999S"], + ["nanosecond", "P1Y2M3W4DT5H6M7.789999999S"], +]; + +for (const [smallestUnit, expected] of tests) { + const string = duration.toString({ + smallestUnit, + get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") } + }); + assert.sameValue(string, expected, `smallestUnit: "${smallestUnit}" overrides fractionalSecondDigits`); +} + +assert.throws(RangeError, () => duration.toString({ + smallestUnit: "hour", + get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") } +}), "hour is an invalid smallestUnit but still overrides fractionalSecondDigits"); diff --git a/test/built-ins/Temporal/Instant/prototype/toString/fractionalseconddigits-auto.js b/test/built-ins/Temporal/Instant/prototype/toString/fractionalseconddigits-auto.js new file mode 100644 index 0000000000..5e37b7ffd5 --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/toString/fractionalseconddigits-auto.js @@ -0,0 +1,23 @@ +// 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.instant.prototype.tostring +description: auto value for fractionalSecondDigits option +features: [BigInt, Temporal] +---*/ + +const zeroSeconds = new Temporal.Instant(0n); +const wholeSeconds = new Temporal.Instant(30_000_000_000n); +const subSeconds = new Temporal.Instant(30_123_400_000n); + +const tests = [ + [zeroSeconds, "1970-01-01T00:00:00Z"], + [wholeSeconds, "1970-01-01T00:00:30Z"], + [subSeconds, "1970-01-01T00:00:30.1234Z"], +]; + +for (const [instant, expected] of tests) { + assert.sameValue(instant.toString(), expected, "default is to emit seconds and drop trailing zeroes"); + assert.sameValue(instant.toString({ fractionalSecondDigits: "auto" }), expected, "auto is the default"); +} diff --git a/test/built-ins/Temporal/Instant/prototype/toString/fractionalseconddigits-number.js b/test/built-ins/Temporal/Instant/prototype/toString/fractionalseconddigits-number.js new file mode 100644 index 0000000000..e6ba963405 --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/toString/fractionalseconddigits-number.js @@ -0,0 +1,33 @@ +// 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.instant.prototype.tostring +description: Number for fractionalSecondDigits option +features: [BigInt, Temporal] +---*/ + +const zeroSeconds = new Temporal.Instant(0n); +const wholeSeconds = new Temporal.Instant(30_000_000_000n); +const subSeconds = new Temporal.Instant(30_123_400_000n); + +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 0 }), "1970-01-01T00:00:30Z", + "truncates 4 decimal places to 0"); +assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 2 }), "1970-01-01T00:00:00.00Z", + "pads zero seconds to 2 decimal places"); +assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 2 }), "1970-01-01T00:00:30.00Z", + "pads whole seconds to 2 decimal places"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 2 }), "1970-01-01T00:00:30.12Z", + "truncates 4 decimal places to 2"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 3 }), "1970-01-01T00:00:30.123Z", + "truncates 4 decimal places to 3"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 6 }), "1970-01-01T00:00:30.123400Z", + "pads 4 decimal places to 6"); +assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 7 }), "1970-01-01T00:00:00.0000000Z", + "pads zero seconds to 7 decimal places"); +assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 7 }), "1970-01-01T00:00:30.0000000Z", + "pads whole seconds to 7 decimal places"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 7 }), "1970-01-01T00:00:30.1234000Z", + "pads 4 decimal places to 7"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 9 }), "1970-01-01T00:00:30.123400000Z", + "pads 4 decimal places to 9"); diff --git a/test/built-ins/Temporal/Instant/prototype/toString/rounding-cross-midnight.js b/test/built-ins/Temporal/Instant/prototype/toString/rounding-cross-midnight.js new file mode 100644 index 0000000000..923671d489 --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/toString/rounding-cross-midnight.js @@ -0,0 +1,13 @@ +// 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.instant.prototype.tostring +description: Rounding can cross midnight +features: [Temporal] +---*/ + +const instant = new Temporal.Instant(946_684_799_999_999_999n); // one nanosecond before 2000-01-01T00:00:00 +for (const roundingMode of ["ceil", "halfExpand"]) { + assert.sameValue(instant.toString({ fractionalSecondDigits: 8, roundingMode }), "2000-01-01T00:00:00.00000000Z"); +} diff --git a/test/built-ins/Temporal/Instant/prototype/toString/roundingmode-ceil.js b/test/built-ins/Temporal/Instant/prototype/toString/roundingmode-ceil.js new file mode 100644 index 0000000000..f4cd9f0a80 --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/toString/roundingmode-ceil.js @@ -0,0 +1,37 @@ +// 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.instant.prototype.tostring +description: ceil value for roundingMode option +features: [Temporal] +---*/ + +const instant = new Temporal.Instant(1_000_000_000_123_987_500n); + +const result1 = instant.toString({ smallestUnit: "microsecond", roundingMode: "ceil" }); +assert.sameValue(result1, "2001-09-09T01:46:40.123988Z", + "roundingMode is ceil (with 6 digits from smallestUnit)"); + +const result2 = instant.toString({ fractionalSecondDigits: 6, roundingMode: "ceil" }); +assert.sameValue(result2, "2001-09-09T01:46:40.123988Z", + "roundingMode is ceil (with 6 digits from fractionalSecondDigits)"); + +const result3 = instant.toString({ smallestUnit: "millisecond", roundingMode: "ceil" }); +assert.sameValue(result3, "2001-09-09T01:46:40.124Z", + "roundingMode is ceil (with 3 digits from smallestUnit)"); + +const result4 = instant.toString({ fractionalSecondDigits: 3, roundingMode: "ceil" }); +assert.sameValue(result4, "2001-09-09T01:46:40.124Z", + "roundingMode is ceil (with 3 digits from fractionalSecondDigits)"); + +const result5 = instant.toString({ smallestUnit: "second", roundingMode: "ceil" }); +assert.sameValue(result5, "2001-09-09T01:46:41Z", + "roundingMode is ceil (with 0 digits from smallestUnit)"); + +const result6 = instant.toString({ fractionalSecondDigits: 0, roundingMode: "ceil" }); +assert.sameValue(result6, "2001-09-09T01:46:41Z", + "roundingMode is ceil (with 0 digits from fractionalSecondDigits)"); + +const result7 = instant.toString({ smallestUnit: "minute", roundingMode: "ceil" }); +assert.sameValue(result7, "2001-09-09T01:47Z", "roundingMode is ceil (round to minute)"); diff --git a/test/built-ins/Temporal/Instant/prototype/toString/roundingmode-floor.js b/test/built-ins/Temporal/Instant/prototype/toString/roundingmode-floor.js new file mode 100644 index 0000000000..d50326568b --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/toString/roundingmode-floor.js @@ -0,0 +1,37 @@ +// 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.instant.prototype.tostring +description: floor value for roundingMode option +features: [Temporal] +---*/ + +const instant = new Temporal.Instant(1_000_000_000_123_987_500n); + +const result1 = instant.toString({ smallestUnit: "microsecond", roundingMode: "floor" }); +assert.sameValue(result1, "2001-09-09T01:46:40.123987Z", + "roundingMode is floor (with 6 digits from smallestUnit)"); + +const result2 = instant.toString({ fractionalSecondDigits: 6, roundingMode: "floor" }); +assert.sameValue(result2, "2001-09-09T01:46:40.123987Z", + "roundingMode is floor (with 6 digits from fractionalSecondDigits)"); + +const result3 = instant.toString({ smallestUnit: "millisecond", roundingMode: "floor" }); +assert.sameValue(result3, "2001-09-09T01:46:40.123Z", + "roundingMode is floor (with 3 digits from smallestUnit)"); + +const result4 = instant.toString({ fractionalSecondDigits: 3, roundingMode: "floor" }); +assert.sameValue(result4, "2001-09-09T01:46:40.123Z", + "roundingMode is floor (with 3 digits from fractionalSecondDigits)"); + +const result5 = instant.toString({ smallestUnit: "second", roundingMode: "floor" }); +assert.sameValue(result5, "2001-09-09T01:46:40Z", + "roundingMode is floor (with 0 digits from smallestUnit)"); + +const result6 = instant.toString({ fractionalSecondDigits: 0, roundingMode: "floor" }); +assert.sameValue(result6, "2001-09-09T01:46:40Z", + "roundingMode is floor (with 0 digits from fractionalSecondDigits)"); + +const result7 = instant.toString({ smallestUnit: "minute", roundingMode: "floor" }); +assert.sameValue(result7, "2001-09-09T01:46Z", "roundingMode is floor (round to minute)"); diff --git a/test/built-ins/Temporal/Instant/prototype/toString/roundingmode-halfExpand.js b/test/built-ins/Temporal/Instant/prototype/toString/roundingmode-halfExpand.js new file mode 100644 index 0000000000..7b8aa7e8fa --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/toString/roundingmode-halfExpand.js @@ -0,0 +1,37 @@ +// 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.instant.prototype.tostring +description: halfExpand value for roundingMode option +features: [Temporal] +---*/ + +const instant = new Temporal.Instant(1_000_000_000_123_987_500n); + +const result1 = instant.toString({ smallestUnit: "microsecond", roundingMode: "halfExpand" }); +assert.sameValue(result1, "2001-09-09T01:46:40.123988Z", + "roundingMode is halfExpand (with 6 digits from smallestUnit)"); + +const result2 = instant.toString({ fractionalSecondDigits: 6, roundingMode: "halfExpand" }); +assert.sameValue(result2, "2001-09-09T01:46:40.123988Z", + "roundingMode is halfExpand (with 6 digits from fractionalSecondDigits)"); + +const result3 = instant.toString({ smallestUnit: "millisecond", roundingMode: "halfExpand" }); +assert.sameValue(result3, "2001-09-09T01:46:40.124Z", + "roundingMode is halfExpand (with 3 digits from smallestUnit)"); + +const result4 = instant.toString({ fractionalSecondDigits: 3, roundingMode: "halfExpand" }); +assert.sameValue(result4, "2001-09-09T01:46:40.124Z", + "roundingMode is halfExpand (with 3 digits from fractionalSecondDigits)"); + +const result5 = instant.toString({ smallestUnit: "second", roundingMode: "halfExpand" }); +assert.sameValue(result5, "2001-09-09T01:46:40Z", + "roundingMode is halfExpand (with 0 digits from smallestUnit)"); + +const result6 = instant.toString({ fractionalSecondDigits: 0, roundingMode: "halfExpand" }); +assert.sameValue(result6, "2001-09-09T01:46:40Z", + "roundingMode is halfExpand (with 0 digits from fractionalSecondDigits)"); + +const result7 = instant.toString({ smallestUnit: "minute", roundingMode: "halfExpand" }); +assert.sameValue(result7, "2001-09-09T01:47Z", "roundingMode is halfExpand (round to minute)"); diff --git a/test/built-ins/Temporal/Instant/prototype/toString/roundingmode-trunc.js b/test/built-ins/Temporal/Instant/prototype/toString/roundingmode-trunc.js new file mode 100644 index 0000000000..944c479c16 --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/toString/roundingmode-trunc.js @@ -0,0 +1,37 @@ +// 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.instant.prototype.tostring +description: trunc value for roundingMode option +features: [Temporal] +---*/ + +const instant = new Temporal.Instant(1_000_000_000_123_987_500n); + +const result1 = instant.toString({ smallestUnit: "microsecond", roundingMode: "trunc" }); +assert.sameValue(result1, "2001-09-09T01:46:40.123987Z", + "roundingMode is trunc (with 6 digits from smallestUnit)"); + +const result2 = instant.toString({ fractionalSecondDigits: 6, roundingMode: "trunc" }); +assert.sameValue(result2, "2001-09-09T01:46:40.123987Z", + "roundingMode is trunc (with 6 digits from fractionalSecondDigits)"); + +const result3 = instant.toString({ smallestUnit: "millisecond", roundingMode: "trunc" }); +assert.sameValue(result3, "2001-09-09T01:46:40.123Z", + "roundingMode is trunc (with 3 digits from smallestUnit)"); + +const result4 = instant.toString({ fractionalSecondDigits: 3, roundingMode: "trunc" }); +assert.sameValue(result4, "2001-09-09T01:46:40.123Z", + "roundingMode is trunc (with 3 digits from fractionalSecondDigits)"); + +const result5 = instant.toString({ smallestUnit: "second", roundingMode: "trunc" }); +assert.sameValue(result5, "2001-09-09T01:46:40Z", + "roundingMode is trunc (with 0 digits from smallestUnit)"); + +const result6 = instant.toString({ fractionalSecondDigits: 0, roundingMode: "trunc" }); +assert.sameValue(result6, "2001-09-09T01:46:40Z", + "roundingMode is trunc (with 0 digits from fractionalSecondDigits)"); + +const result7 = instant.toString({ smallestUnit: "minute", roundingMode: "trunc" }); +assert.sameValue(result7, "2001-09-09T01:46Z", "roundingMode is trunc (round to minute)"); diff --git a/test/built-ins/Temporal/Instant/prototype/toString/smallestunit-fractionalseconddigits.js b/test/built-ins/Temporal/Instant/prototype/toString/smallestunit-fractionalseconddigits.js new file mode 100644 index 0000000000..73d8f9aaf9 --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/toString/smallestunit-fractionalseconddigits.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.instant.prototype.tostring +description: fractionalSecondDigits option is not used with smallestUnit present +features: [Temporal] +---*/ + +const instant = new Temporal.Instant(56_789_999_999n); +const tests = [ + ["minute", "1970-01-01T00:00Z"], + ["second", "1970-01-01T00:00:56Z"], + ["millisecond", "1970-01-01T00:00:56.789Z"], + ["microsecond", "1970-01-01T00:00:56.789999Z"], + ["nanosecond", "1970-01-01T00:00:56.789999999Z"], +]; + +for (const [smallestUnit, expected] of tests) { + const string = instant.toString({ + smallestUnit, + get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") } + }); + assert.sameValue(string, expected, `smallestUnit: "${smallestUnit}" overrides fractionalSecondDigits`); +} + +assert.throws(RangeError, () => instant.toString({ + smallestUnit: "hour", + get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") } +}), "hour is an invalid smallestUnit but still overrides fractionalSecondDigits"); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-auto.js b/test/built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-auto.js new file mode 100644 index 0000000000..2567d04275 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-auto.js @@ -0,0 +1,23 @@ +// 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.tostring +description: auto value for fractionalSecondDigits option +features: [Temporal] +---*/ + +const zeroSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23); +const wholeSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30); +const subSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 400); + +const tests = [ + [zeroSeconds, "1976-11-18T15:23:00"], + [wholeSeconds, "1976-11-18T15:23:30"], + [subSeconds, "1976-11-18T15:23:30.1234"], +]; + +for (const [datetime, expected] of tests) { + assert.sameValue(datetime.toString(), expected, "default is to emit seconds and drop trailing zeroes"); + assert.sameValue(datetime.toString({ fractionalSecondDigits: "auto" }), expected, "auto is the default"); +} diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-number.js b/test/built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-number.js new file mode 100644 index 0000000000..843ef74f78 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-number.js @@ -0,0 +1,33 @@ +// 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.tostring +description: Number for fractionalSecondDigits option +features: [Temporal] +---*/ + +const zeroSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23); +const wholeSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30); +const subSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 400); + +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 0 }), "1976-11-18T15:23:30", + "truncates 4 decimal places to 0"); +assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 2 }), "1976-11-18T15:23:00.00", + "pads zero seconds to 2 decimal places"); +assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 2 }), "1976-11-18T15:23:30.00", + "pads whole seconds to 2 decimal places"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 2 }), "1976-11-18T15:23:30.12", + "truncates 4 decimal places to 2"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 3 }), "1976-11-18T15:23:30.123", + "truncates 4 decimal places to 3"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 6 }), "1976-11-18T15:23:30.123400", + "pads 4 decimal places to 6"); +assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 7 }), "1976-11-18T15:23:00.0000000", + "pads zero seconds to 7 decimal places"); +assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 7 }), "1976-11-18T15:23:30.0000000", + "pads whole seconds to 7 decimal places"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 7 }), "1976-11-18T15:23:30.1234000", + "pads 4 decimal places to 7"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 9 }), "1976-11-18T15:23:30.123400000", + "pads 4 decimal places to 9"); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/toString/rounding-cross-midnight.js b/test/built-ins/Temporal/PlainDateTime/prototype/toString/rounding-cross-midnight.js new file mode 100644 index 0000000000..fe29b3b9c5 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/toString/rounding-cross-midnight.js @@ -0,0 +1,13 @@ +// 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.tostring +description: Rounding can cross midnight +features: [Temporal] +---*/ + +const plainDateTime = new Temporal.PlainDateTime(1999, 12, 31, 23, 59, 59, 999, 999, 999); // one nanosecond before 2000-01-01T00:00:00 +for (const roundingMode of ["ceil", "halfExpand"]) { + assert.sameValue(plainDateTime.toString({ fractionalSecondDigits: 8, roundingMode }), "2000-01-01T00:00:00.00000000"); +} diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-ceil.js b/test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-ceil.js new file mode 100644 index 0000000000..762b3a5cd3 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-ceil.js @@ -0,0 +1,37 @@ +// 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.tostring +description: ceil value for roundingMode option +features: [Temporal] +---*/ + +const datetime = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 123, 987, 500); + +const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "ceil" }); +assert.sameValue(result1, "2000-05-02T12:34:56.123988", + "roundingMode is ceil (with 6 digits from smallestUnit)"); + +const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "ceil" }); +assert.sameValue(result2, "2000-05-02T12:34:56.123988", + "roundingMode is ceil (with 6 digits from fractionalSecondDigits)"); + +const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "ceil" }); +assert.sameValue(result3, "2000-05-02T12:34:56.124", + "roundingMode is ceil (with 3 digits from smallestUnit)"); + +const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "ceil" }); +assert.sameValue(result4, "2000-05-02T12:34:56.124", + "roundingMode is ceil (with 3 digits from fractionalSecondDigits)"); + +const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "ceil" }); +assert.sameValue(result5, "2000-05-02T12:34:57", + "roundingMode is ceil (with 0 digits from smallestUnit)"); + +const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "ceil" }); +assert.sameValue(result6, "2000-05-02T12:34:57", + "roundingMode is ceil (with 0 digits from fractionalSecondDigits)"); + +const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "ceil" }); +assert.sameValue(result7, "2000-05-02T12:35", "roundingMode is ceil (round to minute)"); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-floor.js b/test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-floor.js new file mode 100644 index 0000000000..4899d4cfa2 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-floor.js @@ -0,0 +1,37 @@ +// 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.tostring +description: floor value for roundingMode option +features: [Temporal] +---*/ + +const datetime = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 123, 987, 500); + +const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "floor" }); +assert.sameValue(result1, "2000-05-02T12:34:56.123987", + "roundingMode is floor (with 6 digits from smallestUnit)"); + +const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "floor" }); +assert.sameValue(result2, "2000-05-02T12:34:56.123987", + "roundingMode is floor (with 6 digits from fractionalSecondDigits)"); + +const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "floor" }); +assert.sameValue(result3, "2000-05-02T12:34:56.123", + "roundingMode is floor (with 3 digits from smallestUnit)"); + +const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "floor" }); +assert.sameValue(result4, "2000-05-02T12:34:56.123", + "roundingMode is floor (with 3 digits from fractionalSecondDigits)"); + +const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "floor" }); +assert.sameValue(result5, "2000-05-02T12:34:56", + "roundingMode is floor (with 0 digits from smallestUnit)"); + +const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "floor" }); +assert.sameValue(result6, "2000-05-02T12:34:56", + "roundingMode is floor (with 0 digits from fractionalSecondDigits)"); + +const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "floor" }); +assert.sameValue(result7, "2000-05-02T12:34", "roundingMode is floor (round to minute)"); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-halfExpand.js b/test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-halfExpand.js new file mode 100644 index 0000000000..9dcad46112 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-halfExpand.js @@ -0,0 +1,37 @@ +// 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.tostring +description: halfExpand value for roundingMode option +features: [Temporal] +---*/ + +const datetime = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 123, 987, 500); + +const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "halfExpand" }); +assert.sameValue(result1, "2000-05-02T12:34:56.123988", + "roundingMode is halfExpand (with 6 digits from smallestUnit)"); + +const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "halfExpand" }); +assert.sameValue(result2, "2000-05-02T12:34:56.123988", + "roundingMode is halfExpand (with 6 digits from fractionalSecondDigits)"); + +const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "halfExpand" }); +assert.sameValue(result3, "2000-05-02T12:34:56.124", + "roundingMode is halfExpand (with 3 digits from smallestUnit)"); + +const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "halfExpand" }); +assert.sameValue(result4, "2000-05-02T12:34:56.124", + "roundingMode is halfExpand (with 3 digits from fractionalSecondDigits)"); + +const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "halfExpand" }); +assert.sameValue(result5, "2000-05-02T12:34:56", + "roundingMode is halfExpand (with 0 digits from smallestUnit)"); + +const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "halfExpand" }); +assert.sameValue(result6, "2000-05-02T12:34:56", + "roundingMode is halfExpand (with 0 digits from fractionalSecondDigits)"); + +const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "halfExpand" }); +assert.sameValue(result7, "2000-05-02T12:35", "roundingMode is halfExpand (round to minute)"); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-trunc.js b/test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-trunc.js new file mode 100644 index 0000000000..fa3e86319f --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-trunc.js @@ -0,0 +1,37 @@ +// 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.tostring +description: trunc value for roundingMode option +features: [Temporal] +---*/ + +const datetime = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 123, 987, 500); + +const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "trunc" }); +assert.sameValue(result1, "2000-05-02T12:34:56.123987", + "roundingMode is trunc (with 6 digits from smallestUnit)"); + +const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "trunc" }); +assert.sameValue(result2, "2000-05-02T12:34:56.123987", + "roundingMode is trunc (with 6 digits from fractionalSecondDigits)"); + +const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "trunc" }); +assert.sameValue(result3, "2000-05-02T12:34:56.123", + "roundingMode is trunc (with 3 digits from smallestUnit)"); + +const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "trunc" }); +assert.sameValue(result4, "2000-05-02T12:34:56.123", + "roundingMode is trunc (with 3 digits from fractionalSecondDigits)"); + +const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "trunc" }); +assert.sameValue(result5, "2000-05-02T12:34:56", + "roundingMode is trunc (with 0 digits from smallestUnit)"); + +const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "trunc" }); +assert.sameValue(result6, "2000-05-02T12:34:56", + "roundingMode is trunc (with 0 digits from fractionalSecondDigits)"); + +const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "trunc" }); +assert.sameValue(result7, "2000-05-02T12:34", "roundingMode is trunc (round to minute)"); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/toString/smallestunit-fractionalseconddigits.js b/test/built-ins/Temporal/PlainDateTime/prototype/toString/smallestunit-fractionalseconddigits.js new file mode 100644 index 0000000000..9918d045fc --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/toString/smallestunit-fractionalseconddigits.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.tostring +description: fractionalSecondDigits option is not used with smallestUnit present +features: [Temporal] +---*/ + +const datetime = new Temporal.PlainDateTime(1976, 11, 18, 12, 34, 56, 789, 999, 999); +const tests = [ + ["minute", "1976-11-18T12:34"], + ["second", "1976-11-18T12:34:56"], + ["millisecond", "1976-11-18T12:34:56.789"], + ["microsecond", "1976-11-18T12:34:56.789999"], + ["nanosecond", "1976-11-18T12:34:56.789999999"], +]; + +for (const [smallestUnit, expected] of tests) { + const string = datetime.toString({ + smallestUnit, + get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") } + }); + assert.sameValue(string, expected, `smallestUnit: "${smallestUnit}" overrides fractionalSecondDigits`); +} + +assert.throws(RangeError, () => datetime.toString({ + smallestUnit: "hour", + get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") } +}), "hour is an invalid smallestUnit but still overrides fractionalSecondDigits"); diff --git a/test/built-ins/Temporal/PlainTime/prototype/toString/fractionalseconddigits-auto.js b/test/built-ins/Temporal/PlainTime/prototype/toString/fractionalseconddigits-auto.js index 2bff43e90a..2c90ffaa47 100644 --- a/test/built-ins/Temporal/PlainTime/prototype/toString/fractionalseconddigits-auto.js +++ b/test/built-ins/Temporal/PlainTime/prototype/toString/fractionalseconddigits-auto.js @@ -7,13 +7,17 @@ description: auto value for fractionalSecondDigits option features: [Temporal] ---*/ +const zeroSeconds = new Temporal.PlainTime(15, 23); +const wholeSeconds = new Temporal.PlainTime(15, 23, 30); +const subSeconds = new Temporal.PlainTime(15, 23, 30, 123, 400); + const tests = [ - ["15:23", "15:23:00"], - ["15:23:30", "15:23:30"], - ["15:23:30.1234", "15:23:30.1234"], + [zeroSeconds, "15:23:00"], + [wholeSeconds, "15:23:30"], + [subSeconds, "15:23:30.1234"], ]; -for (const [input, expected] of tests) { - const plainTime = Temporal.PlainTime.from(input); - assert.sameValue(plainTime.toString({ fractionalSecondDigits: "auto" }), expected); +for (const [time, expected] of tests) { + assert.sameValue(time.toString(), expected, "default is to emit seconds and drop trailing zeroes"); + assert.sameValue(time.toString({ fractionalSecondDigits: "auto" }), expected, "auto is the default"); } diff --git a/test/built-ins/Temporal/PlainTime/prototype/toString/fractionalseconddigits-number.js b/test/built-ins/Temporal/PlainTime/prototype/toString/fractionalseconddigits-number.js index 5973c53400..0ea1eb838e 100644 --- a/test/built-ins/Temporal/PlainTime/prototype/toString/fractionalseconddigits-number.js +++ b/test/built-ins/Temporal/PlainTime/prototype/toString/fractionalseconddigits-number.js @@ -7,16 +7,27 @@ description: Number for fractionalSecondDigits option features: [Temporal] ---*/ -const t1 = Temporal.PlainTime.from("15:23"); -const t2 = Temporal.PlainTime.from("15:23:30"); -const t3 = Temporal.PlainTime.from("15:23:30.1234"); -assert.sameValue(t3.toString({ fractionalSecondDigits: 0 }), "15:23:30"); -assert.sameValue(t1.toString({ fractionalSecondDigits: 2 }), "15:23:00.00"); -assert.sameValue(t2.toString({ fractionalSecondDigits: 2 }), "15:23:30.00"); -assert.sameValue(t3.toString({ fractionalSecondDigits: 2 }), "15:23:30.12"); -assert.sameValue(t3.toString({ fractionalSecondDigits: 3 }), "15:23:30.123"); -assert.sameValue(t3.toString({ fractionalSecondDigits: 6 }), "15:23:30.123400"); -assert.sameValue(t1.toString({ fractionalSecondDigits: 7 }), "15:23:00.0000000"); -assert.sameValue(t2.toString({ fractionalSecondDigits: 7 }), "15:23:30.0000000"); -assert.sameValue(t3.toString({ fractionalSecondDigits: 7 }), "15:23:30.1234000"); -assert.sameValue(t3.toString({ fractionalSecondDigits: 9 }), "15:23:30.123400000"); +const zeroSeconds = new Temporal.PlainTime(15, 23); +const wholeSeconds = new Temporal.PlainTime(15, 23, 30); +const subSeconds = new Temporal.PlainTime(15, 23, 30, 123, 400); + +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 0 }), "15:23:30", + "truncates 4 decimal places to 0"); +assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 2 }), "15:23:00.00", + "pads zero seconds to 2 decimal places"); +assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 2 }), "15:23:30.00", + "pads whole seconds to 2 decimal places"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 2 }), "15:23:30.12", + "truncates 4 decimal places to 2"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 3 }), "15:23:30.123", + "truncates 4 decimal places to 3"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 6 }), "15:23:30.123400", + "pads 4 decimal places to 6"); +assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 7 }), "15:23:00.0000000", + "pads zero seconds to 7 decimal places"); +assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 7 }), "15:23:30.0000000", + "pads whole seconds to 7 decimal places"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 7 }), "15:23:30.1234000", + "pads 4 decimal places to 7"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 9 }), "15:23:30.123400000", + "pads 4 decimal places to 9"); diff --git a/test/built-ins/Temporal/PlainTime/prototype/toString/rounding-cross-midnight.js b/test/built-ins/Temporal/PlainTime/prototype/toString/rounding-cross-midnight.js index 4c67d223aa..d580f6f5e2 100644 --- a/test/built-ins/Temporal/PlainTime/prototype/toString/rounding-cross-midnight.js +++ b/test/built-ins/Temporal/PlainTime/prototype/toString/rounding-cross-midnight.js @@ -7,7 +7,7 @@ description: Rounding can cross midnight features: [Temporal] ---*/ -const plainTime = Temporal.PlainTime.from("23:59:59.999999999"); +const plainTime = new Temporal.PlainTime(23, 59, 59, 999, 999, 999); // one nanosecond before 00:00:00 for (const roundingMode of ["ceil", "halfExpand"]) { assert.sameValue(plainTime.toString({ fractionalSecondDigits: 8, roundingMode }), "00:00:00.00000000"); } diff --git a/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-ceil.js b/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-ceil.js index 4f6552b87b..97006c66ff 100644 --- a/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-ceil.js +++ b/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-ceil.js @@ -10,10 +10,28 @@ features: [Temporal] const time = new Temporal.PlainTime(12, 34, 56, 123, 987, 500); const result1 = time.toString({ smallestUnit: "microsecond", roundingMode: "ceil" }); -assert.sameValue(result1, "12:34:56.123988", "roundingMode is ceil"); +assert.sameValue(result1, "12:34:56.123988", + "roundingMode is ceil (with 6 digits from smallestUnit)"); -const result2 = time.toString({ smallestUnit: "millisecond", roundingMode: "ceil" }); -assert.sameValue(result2, "12:34:56.124", "roundingMode is ceil"); +const result2 = time.toString({ fractionalSecondDigits: 6, roundingMode: "ceil" }); +assert.sameValue(result2, "12:34:56.123988", + "roundingMode is ceil (with 6 digits from fractionalSecondDigits)"); -const result3 = time.toString({ smallestUnit: "second", roundingMode: "ceil" }); -assert.sameValue(result3, "12:34:57", "roundingMode is ceil"); +const result3 = time.toString({ smallestUnit: "millisecond", roundingMode: "ceil" }); +assert.sameValue(result3, "12:34:56.124", + "roundingMode is ceil (with 3 digits from smallestUnit)"); + +const result4 = time.toString({ fractionalSecondDigits: 3, roundingMode: "ceil" }); +assert.sameValue(result4, "12:34:56.124", + "roundingMode is ceil (with 3 digits from fractionalSecondDigits)"); + +const result5 = time.toString({ smallestUnit: "second", roundingMode: "ceil" }); +assert.sameValue(result5, "12:34:57", + "roundingMode is ceil (with 0 digits from smallestUnit)"); + +const result6 = time.toString({ fractionalSecondDigits: 0, roundingMode: "ceil" }); +assert.sameValue(result6, "12:34:57", + "roundingMode is ceil (with 0 digits from fractionalSecondDigits)"); + +const result7 = time.toString({ smallestUnit: "minute", roundingMode: "ceil" }); +assert.sameValue(result7, "12:35", "roundingMode is ceil (round to minute)"); diff --git a/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-floor.js b/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-floor.js index 63b30d3cc5..528d8cd0c8 100644 --- a/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-floor.js +++ b/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-floor.js @@ -10,10 +10,28 @@ features: [Temporal] const time = new Temporal.PlainTime(12, 34, 56, 123, 987, 500); const result1 = time.toString({ smallestUnit: "microsecond", roundingMode: "floor" }); -assert.sameValue(result1, "12:34:56.123987", "roundingMode is floor"); +assert.sameValue(result1, "12:34:56.123987", + "roundingMode is floor (with 6 digits from smallestUnit)"); -const result2 = time.toString({ smallestUnit: "millisecond", roundingMode: "floor" }); -assert.sameValue(result2, "12:34:56.123", "roundingMode is floor"); +const result2 = time.toString({ fractionalSecondDigits: 6, roundingMode: "floor" }); +assert.sameValue(result2, "12:34:56.123987", + "roundingMode is floor (with 6 digits from fractionalSecondDigits)"); -const result3 = time.toString({ smallestUnit: "second", roundingMode: "floor" }); -assert.sameValue(result3, "12:34:56", "roundingMode is floor"); +const result3 = time.toString({ smallestUnit: "millisecond", roundingMode: "floor" }); +assert.sameValue(result3, "12:34:56.123", + "roundingMode is floor (with 3 digits from smallestUnit)"); + +const result4 = time.toString({ fractionalSecondDigits: 3, roundingMode: "floor" }); +assert.sameValue(result4, "12:34:56.123", + "roundingMode is floor (with 3 digits from fractionalSecondDigits)"); + +const result5 = time.toString({ smallestUnit: "second", roundingMode: "floor" }); +assert.sameValue(result5, "12:34:56", + "roundingMode is floor (with 0 digits from smallestUnit)"); + +const result6 = time.toString({ fractionalSecondDigits: 0, roundingMode: "floor" }); +assert.sameValue(result6, "12:34:56", + "roundingMode is floor (with 0 digits from fractionalSecondDigits)"); + +const result7 = time.toString({ smallestUnit: "minute", roundingMode: "floor" }); +assert.sameValue(result7, "12:34", "roundingMode is floor (round to minute)"); diff --git a/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-halfExpand.js b/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-halfExpand.js index cd7b8e4673..35a65a8666 100644 --- a/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-halfExpand.js +++ b/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-halfExpand.js @@ -10,10 +10,28 @@ features: [Temporal] const time = new Temporal.PlainTime(12, 34, 56, 123, 987, 500); const result1 = time.toString({ smallestUnit: "microsecond", roundingMode: "halfExpand" }); -assert.sameValue(result1, "12:34:56.123988", "roundingMode is halfExpand"); +assert.sameValue(result1, "12:34:56.123988", + "roundingMode is halfExpand (with 6 digits from smallestUnit)"); -const result2 = time.toString({ smallestUnit: "millisecond", roundingMode: "halfExpand" }); -assert.sameValue(result2, "12:34:56.124", "roundingMode is halfExpand"); +const result2 = time.toString({ fractionalSecondDigits: 6, roundingMode: "halfExpand" }); +assert.sameValue(result2, "12:34:56.123988", + "roundingMode is halfExpand (with 6 digits from fractionalSecondDigits)"); -const result3 = time.toString({ smallestUnit: "second", roundingMode: "halfExpand" }); -assert.sameValue(result3, "12:34:56", "roundingMode is halfExpand"); +const result3 = time.toString({ smallestUnit: "millisecond", roundingMode: "halfExpand" }); +assert.sameValue(result3, "12:34:56.124", + "roundingMode is halfExpand (with 3 digits from smallestUnit)"); + +const result4 = time.toString({ fractionalSecondDigits: 3, roundingMode: "halfExpand" }); +assert.sameValue(result4, "12:34:56.124", + "roundingMode is halfExpand (with 3 digits from fractionalSecondDigits)"); + +const result5 = time.toString({ smallestUnit: "second", roundingMode: "halfExpand" }); +assert.sameValue(result5, "12:34:56", + "roundingMode is halfExpand (with 0 digits from smallestUnit)"); + +const result6 = time.toString({ fractionalSecondDigits: 0, roundingMode: "halfExpand" }); +assert.sameValue(result6, "12:34:56", + "roundingMode is halfExpand (with 0 digits from fractionalSecondDigits)"); + +const result7 = time.toString({ smallestUnit: "minute", roundingMode: "halfExpand" }); +assert.sameValue(result7, "12:35", "roundingMode is halfExpand (round to minute)"); diff --git a/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-trunc.js b/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-trunc.js index e25101caf4..d3496b266b 100644 --- a/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-trunc.js +++ b/test/built-ins/Temporal/PlainTime/prototype/toString/roundingmode-trunc.js @@ -10,10 +10,28 @@ features: [Temporal] const time = new Temporal.PlainTime(12, 34, 56, 123, 987, 500); const result1 = time.toString({ smallestUnit: "microsecond", roundingMode: "trunc" }); -assert.sameValue(result1, "12:34:56.123987", "roundingMode is trunc"); +assert.sameValue(result1, "12:34:56.123987", + "roundingMode is trunc (with 6 digits from smallestUnit)"); -const result2 = time.toString({ smallestUnit: "millisecond", roundingMode: "trunc" }); -assert.sameValue(result2, "12:34:56.123", "roundingMode is trunc"); +const result2 = time.toString({ fractionalSecondDigits: 6, roundingMode: "trunc" }); +assert.sameValue(result2, "12:34:56.123987", + "roundingMode is trunc (with 6 digits from fractionalSecondDigits)"); -const result3 = time.toString({ smallestUnit: "second", roundingMode: "trunc" }); -assert.sameValue(result3, "12:34:56", "roundingMode is trunc"); +const result3 = time.toString({ smallestUnit: "millisecond", roundingMode: "trunc" }); +assert.sameValue(result3, "12:34:56.123", + "roundingMode is trunc (with 3 digits from smallestUnit)"); + +const result4 = time.toString({ fractionalSecondDigits: 3, roundingMode: "trunc" }); +assert.sameValue(result4, "12:34:56.123", + "roundingMode is trunc (with 3 digits from fractionalSecondDigits)"); + +const result5 = time.toString({ smallestUnit: "second", roundingMode: "trunc" }); +assert.sameValue(result5, "12:34:56", + "roundingMode is trunc (with 0 digits from smallestUnit)"); + +const result6 = time.toString({ fractionalSecondDigits: 0, roundingMode: "trunc" }); +assert.sameValue(result6, "12:34:56", + "roundingMode is trunc (with 0 digits from fractionalSecondDigits)"); + +const result7 = time.toString({ smallestUnit: "minute", roundingMode: "trunc" }); +assert.sameValue(result7, "12:34", "roundingMode is trunc (round to minute)"); diff --git a/test/built-ins/Temporal/PlainTime/prototype/toString/smallestunit-fractionalseconddigits.js b/test/built-ins/Temporal/PlainTime/prototype/toString/smallestunit-fractionalseconddigits.js index 06ab3baac6..318041efa0 100644 --- a/test/built-ins/Temporal/PlainTime/prototype/toString/smallestunit-fractionalseconddigits.js +++ b/test/built-ins/Temporal/PlainTime/prototype/toString/smallestunit-fractionalseconddigits.js @@ -21,11 +21,10 @@ for (const [smallestUnit, expected] of tests) { smallestUnit, get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") } }); - assert.sameValue(string, expected, smallestUnit); + assert.sameValue(string, expected, `smallestUnit: "${smallestUnit}" overrides fractionalSecondDigits`); } assert.throws(RangeError, () => time.toString({ smallestUnit: "hour", get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") } -})); - +}), "hour is an invalid smallestUnit but still overrides fractionalSecondDigits"); diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/fractionalseconddigits-auto.js b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/fractionalseconddigits-auto.js new file mode 100644 index 0000000000..bffb928951 --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/fractionalseconddigits-auto.js @@ -0,0 +1,23 @@ +// 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.zoneddatetime.prototype.tostring +description: auto value for fractionalSecondDigits option +features: [BigInt, Temporal] +---*/ + +const zeroSeconds = new Temporal.ZonedDateTime(0n, "UTC"); +const wholeSeconds = new Temporal.ZonedDateTime(30_000_000_000n, "UTC"); +const subSeconds = new Temporal.ZonedDateTime(30_123_400_000n, "UTC"); + +const tests = [ + [zeroSeconds, "1970-01-01T00:00:00+00:00[UTC]"], + [wholeSeconds, "1970-01-01T00:00:30+00:00[UTC]"], + [subSeconds, "1970-01-01T00:00:30.1234+00:00[UTC]"], +]; + +for (const [datetime, expected] of tests) { + assert.sameValue(datetime.toString(), expected, "default is to emit seconds and drop trailing zeroes"); + assert.sameValue(datetime.toString({ fractionalSecondDigits: "auto" }), expected, "auto is the default"); +} diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/fractionalseconddigits-number.js b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/fractionalseconddigits-number.js new file mode 100644 index 0000000000..0063031f20 --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/fractionalseconddigits-number.js @@ -0,0 +1,33 @@ +// 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.zoneddatetime.prototype.tostring +description: Number for fractionalSecondDigits option +features: [BigInt, Temporal] +---*/ + +const zeroSeconds = new Temporal.ZonedDateTime(0n, "UTC"); +const wholeSeconds = new Temporal.ZonedDateTime(30_000_000_000n, "UTC"); +const subSeconds = new Temporal.ZonedDateTime(30_123_400_000n, "UTC"); + +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 0 }), "1970-01-01T00:00:30+00:00[UTC]", + "truncates 4 decimal places to 0"); +assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 2 }), "1970-01-01T00:00:00.00+00:00[UTC]", + "pads zero seconds to 2 decimal places"); +assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 2 }), "1970-01-01T00:00:30.00+00:00[UTC]", + "pads whole seconds to 2 decimal places"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 2 }), "1970-01-01T00:00:30.12+00:00[UTC]", + "truncates 4 decimal places to 2"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 3 }), "1970-01-01T00:00:30.123+00:00[UTC]", + "truncates 4 decimal places to 3"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 6 }), "1970-01-01T00:00:30.123400+00:00[UTC]", + "pads 4 decimal places to 6"); +assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 7 }), "1970-01-01T00:00:00.0000000+00:00[UTC]", + "pads zero seconds to 7 decimal places"); +assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 7 }), "1970-01-01T00:00:30.0000000+00:00[UTC]", + "pads whole seconds to 7 decimal places"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 7 }), "1970-01-01T00:00:30.1234000+00:00[UTC]", + "pads 4 decimal places to 7"); +assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 9 }), "1970-01-01T00:00:30.123400000+00:00[UTC]", + "pads 4 decimal places to 9"); diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/rounding-cross-midnight.js b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/rounding-cross-midnight.js new file mode 100644 index 0000000000..a55db0f240 --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/rounding-cross-midnight.js @@ -0,0 +1,13 @@ +// 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.zoneddatetime.prototype.tostring +description: Rounding can cross midnight +features: [Temporal] +---*/ + +const zonedDateTime = new Temporal.ZonedDateTime(946_684_799_999_999_999n, "UTC"); // one nanosecond before 2000-01-01T00:00:00 +for (const roundingMode of ["ceil", "halfExpand"]) { + assert.sameValue(zonedDateTime.toString({ fractionalSecondDigits: 8, roundingMode }), "2000-01-01T00:00:00.00000000+00:00[UTC]"); +} diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-ceil.js b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-ceil.js new file mode 100644 index 0000000000..cd9c1af1ed --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-ceil.js @@ -0,0 +1,37 @@ +// 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.zoneddatetime.prototype.tostring +description: ceil value for roundingMode option +features: [Temporal] +---*/ + +const datetime = new Temporal.ZonedDateTime(1_000_000_000_123_987_500n, "UTC"); + +const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "ceil" }); +assert.sameValue(result1, "2001-09-09T01:46:40.123988+00:00[UTC]", + "roundingMode is ceil (with 6 digits from smallestUnit)"); + +const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "ceil" }); +assert.sameValue(result2, "2001-09-09T01:46:40.123988+00:00[UTC]", + "roundingMode is ceil (with 6 digits from fractionalSecondDigits)"); + +const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "ceil" }); +assert.sameValue(result3, "2001-09-09T01:46:40.124+00:00[UTC]", + "roundingMode is ceil (with 3 digits from smallestUnit)"); + +const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "ceil" }); +assert.sameValue(result4, "2001-09-09T01:46:40.124+00:00[UTC]", + "roundingMode is ceil (with 3 digits from fractionalSecondDigits)"); + +const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "ceil" }); +assert.sameValue(result5, "2001-09-09T01:46:41+00:00[UTC]", + "roundingMode is ceil (with 0 digits from smallestUnit)"); + +const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "ceil" }); +assert.sameValue(result6, "2001-09-09T01:46:41+00:00[UTC]", + "roundingMode is ceil (with 0 digits from fractionalSecondDigits)"); + +const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "ceil" }); +assert.sameValue(result7, "2001-09-09T01:47+00:00[UTC]", "roundingMode is ceil (round to minute)"); diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-floor.js b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-floor.js new file mode 100644 index 0000000000..fec4286c46 --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-floor.js @@ -0,0 +1,37 @@ +// 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.zoneddatetime.prototype.tostring +description: floor value for roundingMode option +features: [Temporal] +---*/ + +const datetime = new Temporal.ZonedDateTime(1_000_000_000_123_987_500n, "UTC"); + +const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "floor" }); +assert.sameValue(result1, "2001-09-09T01:46:40.123987+00:00[UTC]", + "roundingMode is floor (with 6 digits from smallestUnit)"); + +const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "floor" }); +assert.sameValue(result2, "2001-09-09T01:46:40.123987+00:00[UTC]", + "roundingMode is floor (with 6 digits from fractionalSecondDigits)"); + +const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "floor" }); +assert.sameValue(result3, "2001-09-09T01:46:40.123+00:00[UTC]", + "roundingMode is floor (with 3 digits from smallestUnit)"); + +const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "floor" }); +assert.sameValue(result4, "2001-09-09T01:46:40.123+00:00[UTC]", + "roundingMode is floor (with 3 digits from fractionalSecondDigits)"); + +const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "floor" }); +assert.sameValue(result5, "2001-09-09T01:46:40+00:00[UTC]", + "roundingMode is floor (with 0 digits from smallestUnit)"); + +const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "floor" }); +assert.sameValue(result6, "2001-09-09T01:46:40+00:00[UTC]", + "roundingMode is floor (with 0 digits from fractionalSecondDigits)"); + +const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "floor" }); +assert.sameValue(result7, "2001-09-09T01:46+00:00[UTC]", "roundingMode is floor (round to minute)"); diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-halfExpand.js b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-halfExpand.js new file mode 100644 index 0000000000..a4a989cb6e --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-halfExpand.js @@ -0,0 +1,37 @@ +// 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.zoneddatetime.prototype.tostring +description: halfExpand value for roundingMode option +features: [Temporal] +---*/ + +const datetime = new Temporal.ZonedDateTime(1_000_000_000_123_987_500n, "UTC"); + +const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "halfExpand" }); +assert.sameValue(result1, "2001-09-09T01:46:40.123988+00:00[UTC]", + "roundingMode is halfExpand (with 6 digits from smallestUnit)"); + +const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "halfExpand" }); +assert.sameValue(result2, "2001-09-09T01:46:40.123988+00:00[UTC]", + "roundingMode is halfExpand (with 6 digits from fractionalSecondDigits)"); + +const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "halfExpand" }); +assert.sameValue(result3, "2001-09-09T01:46:40.124+00:00[UTC]", + "roundingMode is halfExpand (with 3 digits from smallestUnit)"); + +const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "halfExpand" }); +assert.sameValue(result4, "2001-09-09T01:46:40.124+00:00[UTC]", + "roundingMode is halfExpand (with 3 digits from fractionalSecondDigits)"); + +const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "halfExpand" }); +assert.sameValue(result5, "2001-09-09T01:46:40+00:00[UTC]", + "roundingMode is halfExpand (with 0 digits from smallestUnit)"); + +const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "halfExpand" }); +assert.sameValue(result6, "2001-09-09T01:46:40+00:00[UTC]", + "roundingMode is halfExpand (with 0 digits from fractionalSecondDigits)"); + +const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "halfExpand" }); +assert.sameValue(result7, "2001-09-09T01:47+00:00[UTC]", "roundingMode is halfExpand (round to minute)"); diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-trunc.js b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-trunc.js new file mode 100644 index 0000000000..8fc3ca0dca --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/roundingmode-trunc.js @@ -0,0 +1,37 @@ +// 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.zoneddatetime.prototype.tostring +description: trunc value for roundingMode option +features: [Temporal] +---*/ + +const datetime = new Temporal.ZonedDateTime(1_000_000_000_123_987_500n, "UTC"); + +const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "trunc" }); +assert.sameValue(result1, "2001-09-09T01:46:40.123987+00:00[UTC]", + "roundingMode is trunc (with 6 digits from smallestUnit)"); + +const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "trunc" }); +assert.sameValue(result2, "2001-09-09T01:46:40.123987+00:00[UTC]", + "roundingMode is trunc (with 6 digits from fractionalSecondDigits)"); + +const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "trunc" }); +assert.sameValue(result3, "2001-09-09T01:46:40.123+00:00[UTC]", + "roundingMode is trunc (with 3 digits from smallestUnit)"); + +const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "trunc" }); +assert.sameValue(result4, "2001-09-09T01:46:40.123+00:00[UTC]", + "roundingMode is trunc (with 3 digits from fractionalSecondDigits)"); + +const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "trunc" }); +assert.sameValue(result5, "2001-09-09T01:46:40+00:00[UTC]", + "roundingMode is trunc (with 0 digits from smallestUnit)"); + +const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "trunc" }); +assert.sameValue(result6, "2001-09-09T01:46:40+00:00[UTC]", + "roundingMode is trunc (with 0 digits from fractionalSecondDigits)"); + +const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "trunc" }); +assert.sameValue(result7, "2001-09-09T01:46+00:00[UTC]", "roundingMode is trunc (round to minute)"); diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/smallestunit-fractionalseconddigits.js b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/smallestunit-fractionalseconddigits.js new file mode 100644 index 0000000000..7845c10bfb --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/smallestunit-fractionalseconddigits.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.zoneddatetime.prototype.tostring +description: fractionalSecondDigits option is not used with smallestUnit present +features: [Temporal] +---*/ + +const datetime = new Temporal.ZonedDateTime(56_789_999_999n, "UTC"); +const tests = [ + ["minute", "1970-01-01T00:00+00:00[UTC]"], + ["second", "1970-01-01T00:00:56+00:00[UTC]"], + ["millisecond", "1970-01-01T00:00:56.789+00:00[UTC]"], + ["microsecond", "1970-01-01T00:00:56.789999+00:00[UTC]"], + ["nanosecond", "1970-01-01T00:00:56.789999999+00:00[UTC]"], +]; + +for (const [smallestUnit, expected] of tests) { + const string = datetime.toString({ + smallestUnit, + get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") } + }); + assert.sameValue(string, expected, `smallestUnit: "${smallestUnit}" overrides fractionalSecondDigits`); +} + +assert.throws(RangeError, () => datetime.toString({ + smallestUnit: "hour", + get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") } +}), "hour is an invalid smallestUnit but still overrides fractionalSecondDigits");