Skip to content

Commit

Permalink
Temporal: Improve existing tests for ZDT#toString timeZoneName option
Browse files Browse the repository at this point in the history
Based on the improvements just made to the calendarName option, improve
the tests for the timeZoneName option of ZonedDateTime.prototype.toString
as well.
  • Loading branch information
ptomato committed Oct 8, 2022
1 parent 3d0f112 commit 07582cd
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
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.zoneddatetime.prototype.tostring
description: If timeZoneName is "auto", the time zone ID should be included.
features: [Temporal]
---*/

const tests = [
["UTC", "1970-01-01T01:01:01.987654321+00:00[UTC]", "built-in UTC"],
["+01:00", "1970-01-01T02:01:01.987654321+01:00[+01:00]", "built-in offset"],
[{
getOffsetNanosecondsFor() { return 0; },
toString() { return "Etc/Custom"; },
}, "1970-01-01T01:01:01.987654321+00:00[Etc/Custom]", "custom"],
];

for (const [timeZone, expected, description] of tests) {
const date = new Temporal.ZonedDateTime(3661_987_654_321n, timeZone);
const result = date.toString({ timeZoneName: "auto" });
assert.sameValue(result, expected, `${description} time zone for timeZoneName = auto`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ features: [Temporal]
---*/

const datetime = new Temporal.ZonedDateTime(1_000_000_000_987_64_321n, "UTC");
assert.throws(RangeError, () => datetime.toString({ timeZoneName: "other string" }));
const invalidValues = ["NEVER", "sometimes", "other string", "auto\0"];

for (const timeZoneName of invalidValues) {
assert.throws(
RangeError,
() => datetime.toString({ timeZoneName }),
`${timeZoneName} is an invalid value for timeZoneName option`
);
}
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.zoneddatetime.prototype.tostring
description: If timeZoneName is "never", the time zone ID should be omitted.
features: [Temporal]
---*/

const tests = [
["UTC", "1970-01-01T01:01:01.987654321+00:00", "built-in UTC"],
["+01:00", "1970-01-01T02:01:01.987654321+01:00", "built-in offset"],
[{
getOffsetNanosecondsFor() { return 0; },
toString() { return "Etc/Custom"; },
}, "1970-01-01T01:01:01.987654321+00:00", "custom"],
];

for (const [timeZone, expected, description] of tests) {
const date = new Temporal.ZonedDateTime(3661_987_654_321n, timeZone);
const result = date.toString({ timeZoneName: "never" });
assert.sameValue(result, expected, `${description} time zone for timeZoneName = never`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ const datetime = new Temporal.ZonedDateTime(1_000_000_000_987_654_321n, "UTC");
const explicit = datetime.toString({ timeZoneName: undefined });
assert.sameValue(explicit, "2001-09-09T01:46:40.987654321+00:00[UTC]", "default timeZoneName option is auto");

// See options-undefined.js for {}
// See options-object.js for {} and options-undefined.js for absent
6 changes: 0 additions & 6 deletions test/staging/Temporal/ZonedDateTime/old/toString.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ features: [Temporal]
var zdt1 = Temporal.ZonedDateTime.from("1976-11-18T15:23+00:00[UTC]");
var fakeGregorian = { toString() { return "gregory" }};

// shows time zone if timeZoneName = auto
assert.sameValue(zdt1.toString({ timeZoneName: "auto" }), "1976-11-18T15:23:00+00:00[UTC]");

// omits time zone if timeZoneName = never
assert.sameValue(zdt1.toString({ timeZoneName: "never" }), "1976-11-18T15:23:00+00:00");

// shows offset if offset = auto
assert.sameValue(zdt1.toString({ offset: "auto" }), "1976-11-18T15:23:00+00:00[UTC]");

Expand Down

0 comments on commit 07582cd

Please sign in to comment.