Skip to content

Commit

Permalink
Temporal: Add coverage for validating month code syntax vs suitability
Browse files Browse the repository at this point in the history
Syntax is validated first. Only after the property bag is fully converted
into a Calendar Fields Record does the calendar validate whether the month
code actually exists in the year.

See tc39/proposal-temporal#2962
  • Loading branch information
ptomato authored and Ms2ger committed Oct 7, 2024
1 parent f8b72d9 commit bfe3534
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 3 deletions.
33 changes: 33 additions & 0 deletions test/built-ins/Temporal/PlainDate/from/monthcode-invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plaindate.from
description: Validation of monthCode
features: [Temporal]
---*/

["m1", "M1", "m01"].forEach((monthCode) => {
assert.throws(RangeError, () => Temporal.PlainDate.from({ year: 2021, monthCode, day: 17 }),
`monthCode '${monthCode}' is not well-formed`);
});

assert.throws(RangeError, () => Temporal.PlainDate.from({ year: 2021, month: 12, monthCode: "M11", day: 17 }),
"monthCode and month conflict");

["M00", "M19", "M99", "M13", "M00L", "M05L", "M13L"].forEach((monthCode) => {
assert.throws(RangeError, () => Temporal.PlainDate.from({ year: 2021, monthCode, day: 17 }),
`monthCode '${monthCode}' is not valid for ISO 8601 calendar`);
});

assert.throws(
RangeError,
() => Temporal.PlainDate.from({ day: 1, monthCode: "L99M", year: Symbol() }),
"Month code syntax is validated before year type is validated"
);

assert.throws(
TypeError,
() => Temporal.PlainDate.from({ day: 1, monthCode: "M99L", year: Symbol() }),
"Month code suitability is validated after year type is validated"
);
33 changes: 33 additions & 0 deletions test/built-ins/Temporal/PlainDateTime/from/monthcode-invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plaindatetime.from
description: Validation of monthCode
features: [Temporal]
---*/

["m1", "M1", "m01"].forEach((monthCode) => {
assert.throws(RangeError, () => Temporal.PlainDateTime.from({ year: 2021, monthCode, day: 17 }),
`monthCode '${monthCode}' is not well-formed`);
});

assert.throws(RangeError, () => Temporal.PlainDateTime.from({ year: 2021, month: 12, monthCode: "M11", day: 17 }),
"monthCode and month conflict");

["M00", "M19", "M99", "M13", "M00L", "M05L", "M13L"].forEach((monthCode) => {
assert.throws(RangeError, () => Temporal.PlainDateTime.from({ year: 2021, monthCode, day: 17 }),
`monthCode '${monthCode}' is not valid for ISO 8601 calendar`);
});

assert.throws(
RangeError,
() => Temporal.PlainDateTime.from({ day: 1, monthCode: "L99M", year: Symbol() }),
"Month code syntax is validated before year type is validated"
);

assert.throws(
TypeError,
() => Temporal.PlainDateTime.from({ day: 1, monthCode: "M99L", year: Symbol() }),
"Month code suitability is validated after year type is validated"
);
14 changes: 13 additions & 1 deletion test/built-ins/Temporal/PlainMonthDay/from/monthcode-invalid.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@ features: [Temporal]
assert.throws(RangeError, () => Temporal.PlainMonthDay.from({ year: 2021, month: 12, monthCode: "M11", day: 17 }),
"monthCode and month conflict");

["M00", "M19", "M99", "M13"].forEach((monthCode) => {
["M00", "M19", "M99", "M13", "M00L", "M05L", "M13L"].forEach((monthCode) => {
assert.throws(RangeError, () => Temporal.PlainMonthDay.from({ monthCode, day: 17 }),
`monthCode '${monthCode}' is not valid for ISO 8601 calendar`);
});

assert.throws(
RangeError,
() => Temporal.PlainMonthDay.from({ day: 1, monthCode: "L99M", year: Symbol() }),
"Month code syntax is validated before year type is validated"
);

assert.throws(
TypeError,
() => Temporal.PlainMonthDay.from({ day: 1, monthCode: "M99L", year: Symbol() }),
"Month code suitability is validated after year type is validated"
);
16 changes: 14 additions & 2 deletions test/built-ins/Temporal/PlainYearMonth/from/monthcode-invalid.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@ features: [Temporal]
assert.throws(RangeError, () => Temporal.PlainYearMonth.from({ year: 2021, month: 12, monthCode: "M11" }),
"monthCode and month conflict");

["M00", "M19", "M99", "M13"].forEach((monthCode) => {
["M00", "M19", "M99", "M13", "M00L", "M05L", "M13L"].forEach((monthCode) => {
assert.throws(RangeError, () => Temporal.PlainYearMonth.from({ year: 2021, monthCode }),
`monthCode '${monthCode}' is not valid for year 2021`);
`monthCode '${monthCode}' is not valid for year 2021 in ISO 8601 calendar`);
});

assert.throws(
RangeError,
() => Temporal.PlainYearMonth.from({ monthCode: "L99M", year: Symbol() }),
"Month code syntax is validated before year type is validated"
);

assert.throws(
TypeError,
() => Temporal.PlainYearMonth.from({ monthCode: "M99L", year: Symbol() }),
"Month code suitability is validated after year type is validated"
);
33 changes: 33 additions & 0 deletions test/built-ins/Temporal/ZonedDateTime/from/monthcode-invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.zoneddatetime.from
description: Validation of monthCode
features: [Temporal]
---*/

["m1", "M1", "m01"].forEach((monthCode) => {
assert.throws(RangeError, () => Temporal.ZonedDateTime.from({ year: 2021, monthCode, day: 17, timeZone: "UTC" }),
`monthCode '${monthCode}' is not well-formed`);
});

assert.throws(RangeError, () => Temporal.ZonedDateTime.from({ year: 2021, month: 12, monthCode: "M11", day: 17, timeZone: "UTC" }),
"monthCode and month conflict");

["M00", "M19", "M99", "M13", "M00L", "M05L", "M13L"].forEach((monthCode) => {
assert.throws(RangeError, () => Temporal.ZonedDateTime.from({ year: 2021, monthCode, day: 17, timeZone: "UTC" }),
`monthCode '${monthCode}' is not valid for ISO 8601 calendar`);
});

assert.throws(
RangeError,
() => Temporal.ZonedDateTime.from({ day: 1, monthCode: "L99M", year: Symbol(), timeZone: "UTC" }),
"Month code syntax is validated before year type is validated"
);

assert.throws(
TypeError,
() => Temporal.ZonedDateTime.from({ day: 1, monthCode: "M99L", year: Symbol(), timeZone: "UTC" }),
"Month code suitability is validated after year type is validated"
);

0 comments on commit bfe3534

Please sign in to comment.