Skip to content

Commit

Permalink
Expand toString() rounding tests from PlainTime to cover other types
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ptomato authored and Ms2ger committed Apr 13, 2022
1 parent 4ac16c2 commit 3905c0c
Show file tree
Hide file tree
Showing 39 changed files with 1,084 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -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");
}
Original file line number Diff line number Diff line change
@@ -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");
Original file line number Diff line number Diff line change
@@ -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)");
Original file line number Diff line number Diff line change
@@ -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)");
Original file line number Diff line number Diff line change
@@ -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)");
Original file line number Diff line number Diff line change
@@ -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)");
Original file line number Diff line number Diff line change
@@ -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");
Original file line number Diff line number Diff line change
@@ -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");
}
Original file line number Diff line number Diff line change
@@ -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");
Original file line number Diff line number Diff line change
@@ -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");
}
Original file line number Diff line number Diff line change
@@ -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)");
Original file line number Diff line number Diff line change
@@ -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)");
Loading

0 comments on commit 3905c0c

Please sign in to comment.