Ensure that non-string TimeZone IDs will throw

This commit fills a test hole found by Codecov: we weren't checking for
the case of a custom time zone whose `id` property returns a non-string
value.

Note that the `id` property is only read in a few places, all on the
ZonedDateTime prototype: `until`, `since`, `equals`, `toString`,
`toLocaleString`, `toJSON`, and `timeZoneId`.
This commit is contained in:
Justin Grant 2023-07-21 22:40:45 -07:00 committed by Ms2ger
parent 92a9eca159
commit e8bbfe704f
7 changed files with 261 additions and 0 deletions

View File

@ -0,0 +1,37 @@
// Copyright (C) 2023 Justin Grant. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.equals
description: TypeError thrown if time zone reports an id that is not a String
features: [Temporal]
---*/
class CustomTimeZone extends Temporal.TimeZone {
constructor(id) {
super("UTC");
this._id = id;
}
get id() {
return this._id;
}
}
[
undefined,
null,
true,
-1000,
Symbol(),
3600_000_000_000n,
{},
{
valueOf() {
return 3600_000_000_000;
}
}
].forEach((wrongId) => {
const timeZone = new CustomTimeZone(wrongId);
const datetime = new Temporal.ZonedDateTime(0n, "UTC");
assert.throws(TypeError, () => datetime.equals({ year: 1970, month: 1, day: 1, timeZone }));
});

View File

@ -0,0 +1,38 @@
// Copyright (C) 2023 Justin Grant. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.since
description: TypeError thrown if time zone reports an id that is not a String
features: [Temporal]
---*/
class CustomTimeZone extends Temporal.TimeZone {
constructor(id) {
super("UTC");
this._id = id;
}
get id() {
return this._id;
}
}
[
undefined,
null,
true,
-1000,
Symbol(),
3600_000_000_000n,
{},
{
valueOf() {
return 3600_000_000_000;
}
}
].forEach((wrongId) => {
const timeZone = new CustomTimeZone(wrongId);
const datetime = new Temporal.ZonedDateTime(1_000_000_000_987_654_321n, "UTC");
const properties = { year: 2004, month: 11, day: 9, hour: 11, minute: 33, second: 20, timeZone };
assert.throws(TypeError, () => datetime.since(properties, { largestUnit: "days" }));
});

View File

@ -0,0 +1,37 @@
// Copyright (C) 2023 Justin Grant. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.equals
description: TypeError thrown if time zone reports an id that is not a String
features: [Temporal]
---*/
class CustomTimeZone extends Temporal.TimeZone {
constructor(id) {
super("UTC");
this._id = id;
}
get id() {
return this._id;
}
}
[
undefined,
null,
true,
-1000,
Symbol(),
3600_000_000_000n,
{},
{
valueOf() {
return 3600_000_000_000;
}
}
].forEach((wrongId) => {
const timeZone = new CustomTimeZone(wrongId);
const zdt = Temporal.ZonedDateTime.from({ year: 1970, month: 1, day: 1, timeZone });
assert.throws(TypeError, () => zdt.timeZoneId);
});

View File

@ -0,0 +1,37 @@
// Copyright (C) 2023 Justin Grant. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.equals
description: TypeError thrown if time zone reports an id that is not a String
features: [Temporal]
---*/
class CustomTimeZone extends Temporal.TimeZone {
constructor(id) {
super("UTC");
this._id = id;
}
get id() {
return this._id;
}
}
[
undefined,
null,
true,
-1000,
Symbol(),
3600_000_000_000n,
{},
{
valueOf() {
return 3600_000_000_000;
}
}
].forEach((wrongId) => {
const timeZone = new CustomTimeZone(wrongId);
const zdt = Temporal.ZonedDateTime.from({ year: 1970, month: 1, day: 1, timeZone });
assert.throws(TypeError, () => zdt.toJSON());
});

View File

@ -0,0 +1,37 @@
// Copyright (C) 2023 Justin Grant. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.equals
description: TypeError thrown if time zone reports an id that is not a String
features: [Temporal]
---*/
class CustomTimeZone extends Temporal.TimeZone {
constructor(id) {
super("UTC");
this._id = id;
}
get id() {
return this._id;
}
}
[
undefined,
null,
true,
-1000,
Symbol(),
3600_000_000_000n,
{},
{
valueOf() {
return 3600_000_000_000;
}
}
].forEach((wrongId) => {
const timeZone = new CustomTimeZone(wrongId);
const zdt = Temporal.ZonedDateTime.from({ year: 1970, month: 1, day: 1, timeZone });
assert.throws(TypeError, () => zdt.toLocaleString());
});

View File

@ -0,0 +1,37 @@
// Copyright (C) 2023 Justin Grant. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.equals
description: TypeError thrown if time zone reports an id that is not a String
features: [Temporal]
---*/
class CustomTimeZone extends Temporal.TimeZone {
constructor(id) {
super("UTC");
this._id = id;
}
get id() {
return this._id;
}
}
[
undefined,
null,
true,
-1000,
Symbol(),
3600_000_000_000n,
{},
{
valueOf() {
return 3600_000_000_000;
}
}
].forEach((wrongId) => {
const timeZone = new CustomTimeZone(wrongId);
const zdt = Temporal.ZonedDateTime.from({ year: 1970, month: 1, day: 1, timeZone });
assert.throws(TypeError, () => zdt.toString());
});

View File

@ -0,0 +1,38 @@
// Copyright (C) 2023 Justin Grant. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.until
description: TypeError thrown if time zone reports an id that is not a String
features: [Temporal]
---*/
class CustomTimeZone extends Temporal.TimeZone {
constructor(id) {
super("UTC");
this._id = id;
}
get id() {
return this._id;
}
}
[
undefined,
null,
true,
-1000,
Symbol(),
3600_000_000_000n,
{},
{
valueOf() {
return 3600_000_000_000;
}
}
].forEach((wrongId) => {
const timeZone = new CustomTimeZone(wrongId);
const datetime = new Temporal.ZonedDateTime(1_000_000_000_987_654_321n, "UTC");
const properties = { year: 2004, month: 11, day: 9, hour: 11, minute: 33, second: 20, timeZone };
assert.throws(TypeError, () => datetime.until(properties, { largestUnit: "days" }));
});