Skip to content

Commit

Permalink
Ensure that non-string TimeZone IDs will throw
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
justingrant authored and Ms2ger committed Jul 25, 2023
1 parent 92a9eca commit e8bbfe7
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 }));
});
Original file line number Diff line number Diff line change
@@ -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" }));
});
Original file line number Diff line number Diff line change
@@ -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);
});
Original file line number Diff line number Diff line change
@@ -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());
});
Original file line number Diff line number Diff line change
@@ -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());
});
Original file line number Diff line number Diff line change
@@ -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());
});
Original file line number Diff line number Diff line change
@@ -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" }));
});

0 comments on commit e8bbfe7

Please sign in to comment.