Skip to content

Commit

Permalink
Expand some Duration#toString() tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger authored and rwaldron committed Mar 10, 2022
1 parent c6c31c8 commit 51ce1fa
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
24 changes: 24 additions & 0 deletions test/built-ins/Temporal/Duration/prototype/toString/balance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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: Verify that values are balanced correctly.
features: [Temporal]
---*/

assert.sameValue(
Temporal.Duration.from({ milliseconds: 3500 }).toString(),
"PT3.5S");
assert.sameValue(
Temporal.Duration.from({ microseconds: 3500 }).toString(),
"PT0.0035S");
assert.sameValue(
Temporal.Duration.from({ nanoseconds: 3500 }).toString(),
"PT0.0000035S");
assert.sameValue(
new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 1111, 1111, 1111).toString(),
"PT1.112112111S");
assert.sameValue(
Temporal.Duration.from({ seconds: 120, milliseconds: 3500 }).toString(),
"PT123.5S");
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ features: [Temporal]

const duration = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 987, 650, 0);

assert.throws(RangeError, () => duration.toString({ fractionalSecondDigits: "other string" }));
for (const fractionalSecondDigits of ["other string", "AUTO", "not-auto", "autos"]) {
assert.throws(RangeError, () => duration.toString({ fractionalSecondDigits }));
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ features: [Temporal]

const duration = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 987, 650, 0);

assert.throws(RangeError, () => duration.toString({ fractionalSecondDigits: -Infinity }));
assert.throws(RangeError, () => duration.toString({ fractionalSecondDigits: -1 }));
assert.throws(RangeError, () => duration.toString({ fractionalSecondDigits: 10 }));
assert.throws(RangeError, () => duration.toString({ fractionalSecondDigits: Infinity }));
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@ esid: sec-temporal.duration.prototype.tostring
description: Temporal.Duration.toString handles negative components
features: [Temporal]
---*/
const d = new Temporal.Duration(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
const expected = "-P1Y1M1W1DT1H1M1.001001001S";
assert.sameValue(d.toString(), expected, "toString with negative components");
assert.sameValue(
new Temporal.Duration(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1).toString(),
"-P1Y1M1W1DT1H1M1.001001001S");
assert.sameValue(
Temporal.Duration.from({ milliseconds: -250 }).toString(),
"-PT0.25S");
assert.sameValue(
Temporal.Duration.from({ milliseconds: -3500 }).toString(),
"-PT3.5S");
assert.sameValue(
Temporal.Duration.from({ microseconds: -250 }).toString(),
"-PT0.00025S");
assert.sameValue(
Temporal.Duration.from({ microseconds: -3500 }).toString(),
"-PT0.0035S");
assert.sameValue(
Temporal.Duration.from({ nanoseconds: -250 }).toString(),
"-PT0.00000025S");
assert.sameValue(
Temporal.Duration.from({ nanoseconds: -3500 }).toString(),
"-PT0.0000035S");
assert.sameValue(
new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, -1111, -1111, -1111).toString(),
"-PT1.112112111S");
assert.sameValue(
Temporal.Duration.from({ seconds: -120, milliseconds: -3500 }).toString(),
"-PT123.5S");
assert.sameValue(
Temporal.Duration.from({ weeks: -1, days: -1 }).toString(),
"-P1W1D");
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ features: [Temporal]
---*/

const duration = new Temporal.Duration(0, 0, 0, 0, 12, 34, 56, 123, 987, 500);
assert.throws(RangeError, () => duration.toString({ smallestUnit: "microsecond", roundingMode: "other string" }));
for (const roundingMode of ["other string", "cile", "CEIL", "ce\u0131l", "auto"]) {
assert.throws(RangeError, () => duration.toString({ smallestUnit: "microsecond", roundingMode }));
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ features: [Temporal]
---*/

const duration = new Temporal.Duration(0, 0, 0, 0, 12, 34, 56, 123, 987, 500);
assert.throws(RangeError, () => duration.toString({ smallestUnit: "other string" }));
const values = ["eras", "years", "months", "weeks", "days", "hours", "minutes", "nonsense", "other string", "mill\u0131seconds", "SECONDS"];
for (const smallestUnit of values) {
assert.throws(RangeError, () => duration.toString({ smallestUnit }));
}

0 comments on commit 51ce1fa

Please sign in to comment.