Temporal: Test throwing on time-zone-to-calendar conversion and vice versa

This contains tests for the normative PR
https://github.com/tc39/proposal-temporal/pull/2433, which is to be
presented for consensus to TC39 in the upcoming plenary meeting. That PR
changes ToTemporalCalendar to throw when it encounters a Temporal.TimeZone
instance, and ToTemporalTimeZone to throw when it encounters a
Temporal.Calendar instance.
This commit is contained in:
Philip Chimento 2022-11-09 17:26:42 -08:00 committed by Philip Chimento
parent 9b5c38d109
commit 338063f80f
43 changed files with 61 additions and 0 deletions

View File

@ -15,10 +15,12 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [arg, description] of rangeErrorTests) {
assert.throws(RangeError, () => Temporal.Calendar.from(arg), `${description} does not convert to a valid ISO string`);
assert.throws(RangeError, () => Temporal.Calendar.from({ calendar: arg }), `${description} does not convert to a valid ISO string (in property bag)`);
}
const typeErrorTests = [
@ -27,4 +29,5 @@ const typeErrorTests = [
for (const [arg, description] of typeErrorTests) {
assert.throws(TypeError, () => Temporal.Calendar.from(arg), `${description} is not a valid object and does not convert to a string`);
assert.throws(TypeError, () => Temporal.Calendar.from({ calendar: arg }), `${description} is not a valid object and does not convert to a string (in property bag)`);
}

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [calendar, description] of rangeErrorTests) {

View File

@ -16,6 +16,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [calendar, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [calendar, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [calendar, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [calendar, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -17,10 +17,12 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [arg, description] of rangeErrorTests) {
assert.throws(RangeError, () => instance.toZonedDateTime({ calendar: arg, timeZone: "UTC" }), `${description} does not convert to a valid ISO string`);
assert.throws(RangeError, () => instance.toZonedDateTime({ calendar: { calendar: arg }, timeZone: "UTC" }), `${description} does not convert to a valid ISO string (in property bag)`);
}
const typeErrorTests = [
@ -29,4 +31,5 @@ const typeErrorTests = [
for (const [arg, description] of typeErrorTests) {
assert.throws(TypeError, () => instance.toZonedDateTime({ calendar: arg, timeZone: "UTC" }), `${description} is not a valid object and does not convert to a string`);
assert.throws(TypeError, () => instance.toZonedDateTime({ calendar: { calendar: arg }, timeZone: "UTC" }), `${description} is not a valid object and does not convert to a string (in property bag)`);
}

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -15,10 +15,12 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [arg, description] of rangeErrorTests) {
assert.throws(RangeError, () => Temporal.Now.plainDate(arg), `${description} does not convert to a valid ISO string`);
assert.throws(RangeError, () => Temporal.Now.plainDate({ calendar: arg }), `${description} does not convert to a valid ISO string (in property bag)`);
}
const typeErrorTests = [
@ -27,4 +29,5 @@ const typeErrorTests = [
for (const [arg, description] of typeErrorTests) {
assert.throws(TypeError, () => Temporal.Now.plainDate(arg), `${description} is not a valid object and does not convert to a string`);
assert.throws(TypeError, () => Temporal.Now.plainDate({ calendar: arg }), `${description} is not a valid object and does not convert to a string (in property bag)`);
}

View File

@ -16,6 +16,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -16,6 +16,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -15,10 +15,12 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [arg, description] of rangeErrorTests) {
assert.throws(RangeError, () => Temporal.Now.plainDateTime(arg), `${description} does not convert to a valid ISO string`);
assert.throws(RangeError, () => Temporal.Now.plainDateTime({ calendar: arg }), `${description} does not convert to a valid ISO string (in property bag)`);
}
const typeErrorTests = [
@ -27,4 +29,5 @@ const typeErrorTests = [
for (const [arg, description] of typeErrorTests) {
assert.throws(TypeError, () => Temporal.Now.plainDateTime(arg), `${description} is not a valid object and does not convert to a string`);
assert.throws(TypeError, () => Temporal.Now.plainDateTime({ calendar: arg }), `${description} is not a valid object and does not convert to a string (in property bag)`);
}

View File

@ -16,6 +16,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -16,6 +16,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -16,6 +16,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -15,10 +15,12 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [arg, description] of rangeErrorTests) {
assert.throws(RangeError, () => Temporal.Now.zonedDateTime(arg), `${description} does not convert to a valid ISO string`);
assert.throws(RangeError, () => Temporal.Now.zonedDateTime({ calendar: arg }), `${description} does not convert to a valid ISO string (in property bag)`);
}
const typeErrorTests = [
@ -27,4 +29,5 @@ const typeErrorTests = [
for (const [arg, description] of typeErrorTests) {
assert.throws(TypeError, () => Temporal.Now.zonedDateTime(arg), `${description} is not a valid object and does not convert to a string`);
assert.throws(TypeError, () => Temporal.Now.zonedDateTime({ calendar: arg }), `${description} is not a valid object and does not convert to a string (in property bag)`);
}

View File

@ -16,6 +16,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -16,6 +16,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -15,6 +15,7 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [calendar, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -17,10 +17,12 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [arg, description] of rangeErrorTests) {
assert.throws(RangeError, () => instance.withCalendar(arg), `${description} does not convert to a valid ISO string`);
assert.throws(RangeError, () => instance.withCalendar({ calendar: arg }), `${description} does not convert to a valid ISO string (in property bag)`);
}
const typeErrorTests = [
@ -29,4 +31,5 @@ const typeErrorTests = [
for (const [arg, description] of typeErrorTests) {
assert.throws(TypeError, () => instance.withCalendar(arg), `${description} is not a valid object and does not convert to a string`);
assert.throws(TypeError, () => instance.withCalendar({ calendar: arg }), `${description} is not a valid object and does not convert to a string (in property bag)`);
}

View File

@ -15,6 +15,7 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [calendar, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -17,10 +17,12 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [arg, description] of rangeErrorTests) {
assert.throws(RangeError, () => instance.withCalendar(arg), `${description} does not convert to a valid ISO string`);
assert.throws(RangeError, () => instance.withCalendar({ calendar: arg }), `${description} does not convert to a valid ISO string (in property bag)`);
}
const typeErrorTests = [
@ -29,4 +31,5 @@ const typeErrorTests = [
for (const [arg, description] of typeErrorTests) {
assert.throws(TypeError, () => instance.withCalendar(arg), `${description} is not a valid object and does not convert to a string`);
assert.throws(TypeError, () => instance.withCalendar({ calendar: arg }), `${description} is not a valid object and does not convert to a string (in property bag)`);
}

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -15,6 +15,7 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [calendar, description] of rangeErrorTests) {

View File

@ -16,6 +16,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -17,10 +17,12 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [arg, description] of rangeErrorTests) {
assert.throws(RangeError, () => instance.getPlainDateTimeFor(new Temporal.Instant(0n), arg), `${description} does not convert to a valid ISO string`);
assert.throws(RangeError, () => instance.getPlainDateTimeFor(new Temporal.Instant(0n), { calendar: arg }), `${description} does not convert to a valid ISO string (in property bag)`);
}
const typeErrorTests = [
@ -29,4 +31,5 @@ const typeErrorTests = [
for (const [arg, description] of typeErrorTests) {
assert.throws(TypeError, () => instance.getPlainDateTimeFor(new Temporal.Instant(0n), arg), `${description} is not a valid object and does not convert to a string`);
assert.throws(TypeError, () => instance.getPlainDateTimeFor(new Temporal.Instant(0n), { calendar: arg }), `${description} is not a valid object and does not convert to a string (in property bag)`);
}

View File

@ -17,6 +17,7 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [calendar, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -16,6 +16,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {

View File

@ -17,10 +17,12 @@ const rangeErrorTests = [
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
];
for (const [arg, description] of rangeErrorTests) {
assert.throws(RangeError, () => instance.withCalendar(arg), `${description} does not convert to a valid ISO string`);
assert.throws(RangeError, () => instance.withCalendar({ calendar: arg }), `${description} does not convert to a valid ISO string (in property bag)`);
}
const typeErrorTests = [
@ -29,4 +31,5 @@ const typeErrorTests = [
for (const [arg, description] of typeErrorTests) {
assert.throws(TypeError, () => instance.withCalendar(arg), `${description} is not a valid object and does not convert to a string`);
assert.throws(TypeError, () => instance.withCalendar({ calendar: arg }), `${description} is not a valid object and does not convert to a string (in property bag)`);
}

View File

@ -18,6 +18,7 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];
for (const [timeZone, description] of rangeErrorTests) {