From d1b16d7d0e3d2fdb217645177fd5a5caaf2bd089 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Fri, 21 Oct 2022 14:53:42 -0700 Subject: [PATCH] Temporal: Move more collections of valid/invalid strings into TemporalHelpers The idea is to deduplicate more string tests into methods on this object, that return collections of valid and invalid strings. This adds collections of valid and invalid PlainYearMonth and PlainMonthDay strings. --- harness/temporalHelpers.js | 95 ++++++++++++++++++- .../PlainMonthDay/from/fields-string.js | 34 +------ .../from/argument-string-invalid.js | 7 +- .../PlainYearMonth/from/argument-string.js | 47 +++------ 4 files changed, 114 insertions(+), 69 deletions(-) diff --git a/harness/temporalHelpers.js b/harness/temporalHelpers.js index cbfbd555b8..bf0022457e 100644 --- a/harness/temporalHelpers.js +++ b/harness/temporalHelpers.js @@ -1798,6 +1798,47 @@ var TemporalHelpers = { * testing parsers. */ ISO: { + /* + * PlainMonthDay strings that are not valid. + */ + plainMonthDayStringsInvalid() { + return [ + "11-18junk", + ]; + }, + + /* + * PlainMonthDay strings that are valid and that should produce October 1st. + */ + plainMonthDayStringsValid() { + return [ + "10-01", + "1001", + "1965-10-01", + "1976-10-01T152330.1+00:00", + "19761001T15:23:30.1+00:00", + "1976-10-01T15:23:30.1+0000", + "1976-10-01T152330.1+0000", + "19761001T15:23:30.1+0000", + "19761001T152330.1+00:00", + "19761001T152330.1+0000", + "+001976-10-01T152330.1+00:00", + "+0019761001T15:23:30.1+00:00", + "+001976-10-01T15:23:30.1+0000", + "+001976-10-01T152330.1+0000", + "+0019761001T15:23:30.1+0000", + "+0019761001T152330.1+00:00", + "+0019761001T152330.1+0000", + "1976-10-01T15:23:00", + "1976-10-01T15:23", + "1976-10-01T15", + "1976-10-01", + "--10-01", + "--1001", + 1001, + ]; + }, + /* * PlainTime strings that may be mistaken for PlainMonthDay or * PlainYearMonth strings, and so require a time designator. @@ -1842,6 +1883,58 @@ var TemporalHelpers = { "2021-12[-12:00]", // HHMM-UU is ambiguous with YYYY-MM, but TZ disambiguates "202112[UTC]", // HHMMSS is ambiguous with YYYYMM, but TZ disambiguates ]; - } + }, + + /* + * PlainYearMonth-like strings that are not valid. + */ + plainYearMonthStringsInvalid() { + return [ + "2020-13", + ]; + }, + + /* + * PlainYearMonth-like strings that are valid and should produce November + * 1976 in the ISO 8601 calendar. + */ + plainYearMonthStringsValid() { + return [ + "1976-11", + "1976-11-10", + "1976-11-01T09:00:00+00:00", + "1976-11-01T00:00:00+05:00", + "197611", + "+00197611", + "1976-11-18T15:23:30.1\u221202:00", + "1976-11-18T152330.1+00:00", + "19761118T15:23:30.1+00:00", + "1976-11-18T15:23:30.1+0000", + "1976-11-18T152330.1+0000", + "19761118T15:23:30.1+0000", + "19761118T152330.1+00:00", + "19761118T152330.1+0000", + "+001976-11-18T152330.1+00:00", + "+0019761118T15:23:30.1+00:00", + "+001976-11-18T15:23:30.1+0000", + "+001976-11-18T152330.1+0000", + "+0019761118T15:23:30.1+0000", + "+0019761118T152330.1+00:00", + "+0019761118T152330.1+0000", + "1976-11-18T15:23", + "1976-11-18T15", + "1976-11-18", + ]; + }, + + /* + * PlainYearMonth-like strings that are valid and should produce November of + * the ISO year -9999. + */ + plainYearMonthStringsValidNegativeYear() { + return [ + "\u2212009999-11", + ]; + }, } }; diff --git a/test/built-ins/Temporal/PlainMonthDay/from/fields-string.js b/test/built-ins/Temporal/PlainMonthDay/from/fields-string.js index 8386dcd1cb..007d629ed4 100644 --- a/test/built-ins/Temporal/PlainMonthDay/from/fields-string.js +++ b/test/built-ins/Temporal/PlainMonthDay/from/fields-string.js @@ -8,39 +8,13 @@ includes: [compareArray.js, temporalHelpers.js] features: [Temporal] ---*/ -const tests = [ - "10-01", - "1001", - "1965-10-01", - "1976-10-01T152330.1+00:00", - "19761001T15:23:30.1+00:00", - "1976-10-01T15:23:30.1+0000", - "1976-10-01T152330.1+0000", - "19761001T15:23:30.1+0000", - "19761001T152330.1+00:00", - "19761001T152330.1+0000", - "+001976-10-01T152330.1+00:00", - "+0019761001T15:23:30.1+00:00", - "+001976-10-01T15:23:30.1+0000", - "+001976-10-01T152330.1+0000", - "+0019761001T15:23:30.1+0000", - "+0019761001T152330.1+00:00", - "+0019761001T152330.1+0000", - "1976-10-01T15:23:00", - "1976-10-01T15:23", - "1976-10-01T15", - "1976-10-01", - "--10-01", - "--1001", - 1001, -]; - -for (const argument of tests) { +for (const argument of TemporalHelpers.ISO.plainMonthDayStringsValid()) { const plainMonthDay = Temporal.PlainMonthDay.from(argument); assert.notSameValue(plainMonthDay, argument, `from ${argument} converts`); TemporalHelpers.assertPlainMonthDay(plainMonthDay, "M10", 1, `from ${argument}`); assert.sameValue(plainMonthDay.calendar.id, "iso8601", `from ${argument} calendar`); } -assert.throws(RangeError, () => Temporal.PlainMonthDay.from("11-18junk"), "no junk at end of string"); - +for (const arg of TemporalHelpers.ISO.plainMonthDayStringsInvalid()) { + assert.throws(RangeError, () => Temporal.PlainMonthDay.from(arg), `"${arg}" not a valid PlainMonthDay string`); +} diff --git a/test/built-ins/Temporal/PlainYearMonth/from/argument-string-invalid.js b/test/built-ins/Temporal/PlainYearMonth/from/argument-string-invalid.js index e99fd4efe5..694d54e69a 100644 --- a/test/built-ins/Temporal/PlainYearMonth/from/argument-string-invalid.js +++ b/test/built-ins/Temporal/PlainYearMonth/from/argument-string-invalid.js @@ -4,8 +4,11 @@ /*--- esid: sec-temporal.plainyearmonth.from description: An invalid ISO string is never supported +includes: [temporalHelpers.js] features: [Temporal] ---*/ -assert.throws(RangeError, () => Temporal.PlainYearMonth.from("2020-13", { overflow: "reject" })); -assert.throws(RangeError, () => Temporal.PlainYearMonth.from("2020-13", { overflow: "constrain" })); +for (const input of TemporalHelpers.ISO.plainYearMonthStringsInvalid()) { + assert.throws(RangeError, () => Temporal.PlainYearMonth.from(input, { overflow: "reject" })); + assert.throws(RangeError, () => Temporal.PlainYearMonth.from(input, { overflow: "constrain" })); +} diff --git a/test/built-ins/Temporal/PlainYearMonth/from/argument-string.js b/test/built-ins/Temporal/PlainYearMonth/from/argument-string.js index 01a841c61e..4d90936bcf 100644 --- a/test/built-ins/Temporal/PlainYearMonth/from/argument-string.js +++ b/test/built-ins/Temporal/PlainYearMonth/from/argument-string.js @@ -8,34 +8,7 @@ includes: [temporalHelpers.js] features: [Temporal] ---*/ -const inputs = [ - "1976-11", - "1976-11-10", - "1976-11-01T09:00:00+00:00", - "1976-11-01T00:00:00+05:00", - "197611", - "+00197611", - "1976-11-18T15:23:30.1\u221202:00", - "1976-11-18T152330.1+00:00", - "19761118T15:23:30.1+00:00", - "1976-11-18T15:23:30.1+0000", - "1976-11-18T152330.1+0000", - "19761118T15:23:30.1+0000", - "19761118T152330.1+00:00", - "19761118T152330.1+0000", - "+001976-11-18T152330.1+00:00", - "+0019761118T15:23:30.1+00:00", - "+001976-11-18T15:23:30.1+0000", - "+001976-11-18T152330.1+0000", - "+0019761118T15:23:30.1+0000", - "+0019761118T152330.1+00:00", - "+0019761118T152330.1+0000", - "1976-11-18T15:23", - "1976-11-18T15", - "1976-11-18", -]; - -for (const input of inputs) { +for (const input of TemporalHelpers.ISO.plainYearMonthStringsValid()) { const plainYearMonth = Temporal.PlainYearMonth.from(input); TemporalHelpers.assertPlainYearMonth(plainYearMonth, 1976, 11, "M11"); const fields = plainYearMonth.getISOFields(); @@ -45,11 +18,13 @@ for (const input of inputs) { assert.sameValue(fields.isoYear, 1976, "isoYear"); } -const plainYearMonth = Temporal.PlainYearMonth.from("\u2212009999-11"); -TemporalHelpers.assertPlainYearMonth(plainYearMonth, -9999, 11, "M11"); -const fields = plainYearMonth.getISOFields(); -assert.sameValue(fields.calendar.id, "iso8601"); -assert.sameValue(fields.isoDay, 1, "isoDay"); -assert.sameValue(fields.isoMonth, 11, "isoMonth"); -assert.sameValue(fields.isoYear, -9999, "isoYear"); -assert.sameValue(plainYearMonth.toString(), "-009999-11"); +for (const input of TemporalHelpers.ISO.plainYearMonthStringsValidNegativeYear()) { + const plainYearMonth = Temporal.PlainYearMonth.from(input); + TemporalHelpers.assertPlainYearMonth(plainYearMonth, -9999, 11, "M11"); + const fields = plainYearMonth.getISOFields(); + assert.sameValue(fields.calendar.id, "iso8601"); + assert.sameValue(fields.isoDay, 1, "isoDay"); + assert.sameValue(fields.isoMonth, 11, "isoMonth"); + assert.sameValue(fields.isoYear, -9999, "isoYear"); + assert.sameValue(plainYearMonth.toString(), "-009999-11"); +}