From 07582cd3b7a0cc8caa2b7d29301306836bd8f303 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Fri, 7 Oct 2022 18:27:57 -0700 Subject: [PATCH] Temporal: Improve existing tests for ZDT#toString timeZoneName option Based on the improvements just made to the calendarName option, improve the tests for the timeZoneName option of ZonedDateTime.prototype.toString as well. --- .../prototype/toString/timezonename-auto.js | 23 +++++++++++++++++++ .../toString/timezonename-invalid-string.js | 10 +++++++- .../prototype/toString/timezonename-never.js | 23 +++++++++++++++++++ .../toString/timezonename-undefined.js | 2 +- .../Temporal/ZonedDateTime/old/toString.js | 6 ----- 5 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-auto.js create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-never.js diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-auto.js b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-auto.js new file mode 100644 index 00000000000..10e97af7086 --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-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: 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`); +} diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-invalid-string.js b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-invalid-string.js index 94067c25900..dbf82a82102 100644 --- a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-invalid-string.js +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-invalid-string.js @@ -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` + ); +} diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-never.js b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-never.js new file mode 100644 index 00000000000..632dff2ccb2 --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-never.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: 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`); +} diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-undefined.js b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-undefined.js index 7973c987d74..3e86590b67a 100644 --- a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-undefined.js +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/timezonename-undefined.js @@ -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 diff --git a/test/staging/Temporal/ZonedDateTime/old/toString.js b/test/staging/Temporal/ZonedDateTime/old/toString.js index 0f86a4cdcd3..7703f05e7b9 100644 --- a/test/staging/Temporal/ZonedDateTime/old/toString.js +++ b/test/staging/Temporal/ZonedDateTime/old/toString.js @@ -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]");