diff --git a/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/overflow-constrain.js b/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/overflow-constrain.js new file mode 100644 index 00000000000..5000832ddc0 --- /dev/null +++ b/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/overflow-constrain.js @@ -0,0 +1,46 @@ +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.calendar.prototype.monthdayfromfields +description: Temporal.Calendar.prototype.monthDayFromFields will return correctly with data and overflow set to 'constrain'. +info: | + 1. Let calendar be the this value. + 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]). + 3. Assert: calendar.[[Identifier]] is "iso8601". + 4. If Type(fields) is not Object, throw a TypeError exception. + 5. Set options to ? GetOptionsObject(options). + 6. Let result be ? ISOMonthDayFromFields(fields, options). + 7. Return ? CreateTemporalMonthDay(result.[[Month]], result.[[Day]], calendar, result.[[ReferenceISOYear]]). +features: [Temporal] +---*/ +let cal = new Temporal.Calendar("iso8601") + +let opt = {overflow: "constrain"}; +// Test overflow: "constrain" work properly +assert.sameValue("01-31", cal.monthDayFromFields({year: 2021, month: 1, day: 133}, opt).toJSON()); +assert.sameValue("02-28", cal.monthDayFromFields({year: 2021, month: 2, day: 133}, opt).toJSON()); +assert.sameValue("03-31", cal.monthDayFromFields({year: 2021, month: 3, day: 9033}, opt).toJSON()); +assert.sameValue("04-30", cal.monthDayFromFields({year: 2021, month: 4, day: 50}, opt).toJSON()); +assert.sameValue("05-31", cal.monthDayFromFields({year: 2021, month: 5, day: 77}, opt).toJSON()); +assert.sameValue("06-30", cal.monthDayFromFields({year: 2021, month: 6, day: 33}, opt).toJSON()); +assert.sameValue("07-31", cal.monthDayFromFields({year: 2021, month: 7, day: 33}, opt).toJSON()); +assert.sameValue("08-31", cal.monthDayFromFields({year: 2021, month: 8, day: 300}, opt).toJSON()); +assert.sameValue("09-30", cal.monthDayFromFields({year: 2021, month: 9, day: 400}, opt).toJSON()); +assert.sameValue("10-31", cal.monthDayFromFields({year: 2021, month: 10, day: 400}, opt).toJSON()); +assert.sameValue("11-30", cal.monthDayFromFields({year: 2021, month: 11, day: 400}, opt).toJSON()); +assert.sameValue("12-31", cal.monthDayFromFields({year: 2021, month: 12, day: 500}, opt).toJSON()); +assert.sameValue("12-31", cal.monthDayFromFields({year: 2021, month: 13, day: 500}, opt).toJSON()); +assert.sameValue("12-31", cal.monthDayFromFields({year: 2021, month: 999999, day: 500}, opt).toJSON()); +assert.sameValue("01-31", cal.monthDayFromFields({year: 2021, monthCode: "M01", day: 133}, opt).toJSON()); +assert.sameValue("02-29", cal.monthDayFromFields({year: 2021, monthCode: "M02", day: 133}, opt).toJSON()); +assert.sameValue("03-31", cal.monthDayFromFields({year: 2021, monthCode: "M03", day: 9033}, opt).toJSON()); +assert.sameValue("04-30", cal.monthDayFromFields({year: 2021, monthCode: "M04", day: 50}, opt).toJSON()); +assert.sameValue("05-31", cal.monthDayFromFields({year: 2021, monthCode: "M05", day: 77}, opt).toJSON()); +assert.sameValue("06-30", cal.monthDayFromFields({year: 2021, monthCode: "M06", day: 33}, opt).toJSON()); +assert.sameValue("07-31", cal.monthDayFromFields({year: 2021, monthCode: "M07", day: 33}, opt).toJSON()); +assert.sameValue("08-31", cal.monthDayFromFields({year: 2021, monthCode: "M08", day: 300}, opt).toJSON()); +assert.sameValue("09-30", cal.monthDayFromFields({year: 2021, monthCode: "M09", day: 400}, opt).toJSON()); +assert.sameValue("10-31", cal.monthDayFromFields({year: 2021, monthCode: "M10", day: 400}, opt).toJSON()); +assert.sameValue("11-30", cal.monthDayFromFields({year: 2021, monthCode: "M11", day: 400}, opt).toJSON()); +assert.sameValue("12-31", cal.monthDayFromFields({year: 2021, monthCode: "M12", day: 500}, opt).toJSON()); diff --git a/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/overflow-undefined.js b/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/overflow-undefined.js new file mode 100644 index 00000000000..88402fd6f51 --- /dev/null +++ b/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/overflow-undefined.js @@ -0,0 +1,45 @@ +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.calendar.prototype.monthdayfromfields +description: Temporal.Calendar.prototype.monthDayFromFields will return correctly with default overflow which constrain date range. +info: | + 1. Let calendar be the this value. + 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]). + 3. Assert: calendar.[[Identifier]] is "iso8601". + 4. If Type(fields) is not Object, throw a TypeError exception. + 5. Set options to ? GetOptionsObject(options). + 6. Let result be ? ISOMonthDayFromFields(fields, options). + 7. Return ? CreateTemporalMonthDay(result.[[Month]], result.[[Day]], calendar, result.[[ReferenceISOYear]]). +features: [Temporal] +---*/ +let cal = new Temporal.Calendar("iso8601") + +// Test default overflow (which is "constrain") work properly +assert.sameValue("01-31", cal.monthDayFromFields({year: 2021, month: 1, day: 133}).toJSON()); +assert.sameValue("02-28", cal.monthDayFromFields({year: 2021, month: 2, day: 133}).toJSON()); +assert.sameValue("03-31", cal.monthDayFromFields({year: 2021, month: 3, day: 9033}).toJSON()); +assert.sameValue("04-30", cal.monthDayFromFields({year: 2021, month: 4, day: 50}).toJSON()); +assert.sameValue("05-31", cal.monthDayFromFields({year: 2021, month: 5, day: 77}).toJSON()); +assert.sameValue("06-30", cal.monthDayFromFields({year: 2021, month: 6, day: 33}).toJSON()); +assert.sameValue("07-31", cal.monthDayFromFields({year: 2021, month: 7, day: 33}).toJSON()); +assert.sameValue("08-31", cal.monthDayFromFields({year: 2021, month: 8, day: 300}).toJSON()); +assert.sameValue("09-30", cal.monthDayFromFields({year: 2021, month: 9, day: 400}).toJSON()); +assert.sameValue("10-31", cal.monthDayFromFields({year: 2021, month: 10, day: 400}).toJSON()); +assert.sameValue("11-30", cal.monthDayFromFields({year: 2021, month: 11, day: 400}).toJSON()); +assert.sameValue("12-31", cal.monthDayFromFields({year: 2021, month: 12, day: 500}).toJSON()); +assert.sameValue("12-31", cal.monthDayFromFields({year: 2021, month: 13, day: 500}).toJSON()); +assert.sameValue("12-31", cal.monthDayFromFields({year: 2021, month: 999999, day: 500}).toJSON()); +assert.sameValue("01-31", cal.monthDayFromFields({year: 2021, monthCode: "M01", day: 133}).toJSON()); +assert.sameValue("02-29", cal.monthDayFromFields({year: 2021, monthCode: "M02", day: 133}).toJSON()); +assert.sameValue("03-31", cal.monthDayFromFields({year: 2021, monthCode: "M03", day: 9033}).toJSON()); +assert.sameValue("04-30", cal.monthDayFromFields({year: 2021, monthCode: "M04", day: 50}).toJSON()); +assert.sameValue("05-31", cal.monthDayFromFields({year: 2021, monthCode: "M05", day: 77}).toJSON()); +assert.sameValue("06-30", cal.monthDayFromFields({year: 2021, monthCode: "M06", day: 33}).toJSON()); +assert.sameValue("07-31", cal.monthDayFromFields({year: 2021, monthCode: "M07", day: 33}).toJSON()); +assert.sameValue("08-31", cal.monthDayFromFields({year: 2021, monthCode: "M08", day: 300}).toJSON()); +assert.sameValue("09-30", cal.monthDayFromFields({year: 2021, monthCode: "M09", day: 400}).toJSON()); +assert.sameValue("10-31", cal.monthDayFromFields({year: 2021, monthCode: "M10", day: 400}).toJSON()); +assert.sameValue("11-30", cal.monthDayFromFields({year: 2021, monthCode: "M11", day: 400}).toJSON()); +assert.sameValue("12-31", cal.monthDayFromFields({year: 2021, monthCode: "M12", day: 500}).toJSON()); diff --git a/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/simple.js b/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/simple.js new file mode 100644 index 00000000000..986c48cb91d --- /dev/null +++ b/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/simple.js @@ -0,0 +1,44 @@ +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.calendar.prototype.monthdayfromfields +description: Temporal.Calendar.prototype.monthDayFromFields will return correctly with valid data. +info: | + 1. Let calendar be the this value. + 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]). + 3. Assert: calendar.[[Identifier]] is "iso8601". + 4. If Type(fields) is not Object, throw a TypeError exception. + 5. Set options to ? GetOptionsObject(options). + 6. Let result be ? ISOMonthDayFromFields(fields, options). + 7. Return ? CreateTemporalMonthDay(result.[[Month]], result.[[Day]], calendar, result.[[ReferenceISOYear]]). +features: [Temporal] +---*/ +let cal = new Temporal.Calendar("iso8601") + +assert.sameValue("07-15", cal.monthDayFromFields({year: 2021, month: 7, day: 15}).toJSON()); +assert.sameValue("07-03", cal.monthDayFromFields({year: 2021, month: 7, day: 3}).toJSON()); +assert.sameValue("12-31", cal.monthDayFromFields({year: 2021, month: 12, day: 31}).toJSON()); + +// with monthCode, year 2021 is not used duration regulation, but use 1972 +assert.sameValue("07-15", cal.monthDayFromFields({year: 2021, monthCode: "M07", day: 15}).toJSON()); +assert.sameValue("07-03", cal.monthDayFromFields({year: 2021, monthCode: "M07", day: 3}).toJSON()); +assert.sameValue("12-31", cal.monthDayFromFields({year: 2021, monthCode: "M12", day: 31}).toJSON()); +// monthDay will use 1972 which is a leap year if monthCode is defined even if +// we pass in 2021 as reference year. +assert.sameValue("02-29", cal.monthDayFromFields({year: 2021, monthCode: "M02", day: 29}).toJSON()); + +["constrain", "reject"].forEach(function(overflow) { + let opt = {overflow}; + assert.sameValue("07-15", cal.monthDayFromFields({year: 2021, month: 7, day: 15}, opt).toJSON()); + assert.sameValue("07-03", cal.monthDayFromFields({year: 2021, month: 7, day: 3}, opt).toJSON()); + assert.sameValue("12-31", cal.monthDayFromFields({year: 2021, month: 12, day: 31}, opt).toJSON()); + + // with monthCode, year 2021 is not used duration regulation, but use 1972 + assert.sameValue("07-15", cal.monthDayFromFields({year: 2021, monthCode: "M07", day: 15}, opt).toJSON()); + assert.sameValue("07-03", cal.monthDayFromFields({year: 2021, monthCode: "M07", day: 3}, opt).toJSON()); + assert.sameValue("12-31", cal.monthDayFromFields({year: 2021, monthCode: "M12", day: 31}, opt).toJSON()); + // monthDay will use 1972 which is a leap year if monthCode is defined even if + // we pass in 2021 as reference year. + assert.sameValue("02-29", cal.monthDayFromFields({year: 2021, monthCode: "M02", day: 29}, opt).toJSON()); +}); diff --git a/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/throw-range-error.js b/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/throw-range-error.js new file mode 100644 index 00000000000..bc3ee15cc19 --- /dev/null +++ b/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/throw-range-error.js @@ -0,0 +1,121 @@ +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.calendar.prototype.monthdayfromfields +description: Temporal.Calendar.prototype.monthDayFromFields will throw RangeError with data value in wrong range. +info: | + 1. Let calendar be the this value. + 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]). + 3. Assert: calendar.[[Identifier]] is "iso8601". + 4. If Type(fields) is not Object, throw a TypeError exception. + 5. Set options to ? GetOptionsObject(options). + 6. Let result be ? ISOMonthDayFromFields(fields, options). + 7. Return ? CreateTemporalMonthDay(result.[[Month]], result.[[Day]], calendar, result.[[ReferenceISOYear]]). +features: [Temporal] +---*/ +let cal = new Temporal.Calendar("iso8601") + +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, monthCode: "m1", day: 17}), + "monthCode value is out of range."); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, monthCode: "M1", day: 17}), + "monthCode value is out of range."); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, monthCode: "m01", day: 17}), + "monthCode value is out of range."); + +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, month: 12, monthCode: "M11", day: 17}), + "monthCode value is out of range."); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, monthCode: "M00", day: 17}), + "monthCode value is out of range."); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, monthCode: "M19", day: 17}), + "monthCode value is out of range."); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, monthCode: "M99", day: 17}), + "monthCode value is out of range."); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, monthCode: "M13", day: 17}), + "monthCode value is out of range."); + +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, month: -1, day: 17}), + "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, month: -Infinity, day: 17}), + "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, month: 7, day: -17}), + "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, month: 7, day: -Infinity}), + "Invalid time value"); + +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, month: 12, day: 0}, {overflow: "reject"}), + "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, month: 12, day: 32}, {overflow: "reject"}), + "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, month: 1, day: 32}, {overflow: "reject"}), + "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, month: 2, day: 29}, {overflow: "reject"}), + "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, month: 6, day: 31}, {overflow: "reject"}), + "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, month: 9, day: 31}, {overflow: "reject"}), + "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, month: 0, day: 5}, {overflow: "reject"}), + "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields({year: 2021, month: 13, day: 5}, {overflow: "reject"}), + "Invalid time value"); + +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, monthCode: "M12", day: 0}, {overflow: "reject"}), + "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, monthCode: "M12", day: 32}, {overflow: "reject"}), + "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, monthCode: "M01", day: 32}, {overflow: "reject"}), + "Invalid time value"); + +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, monthCode: "M06", day: 31}, {overflow: "reject"}), + "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, monthCode: "M09", day: 31}, {overflow: "reject"}), + "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, monthCode: "M00", day: 5}, {overflow: "reject"}), + "monthCode value is out of range."); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, monthCode: "M13", day: 5}, {overflow: "reject"}), + "monthCode value is out of range."); + +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 12, day: 0}), "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 0, day: 3}), "Invalid time value"); + +// Check throw for the second arg +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 7, day: 13}, {overflow: "invalid"}), + "Value invalid out of range for Temporal.Calendar.prototype.monthDayFromFields options property overflow"); + +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 1, day: 32}, {overflow: "reject"}), "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 2, day: 29}, {overflow: "reject"}), "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 3, day: 32}, {overflow: "reject"}), "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 4, day: 31}, {overflow: "reject"}), "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 5, day: 32}, {overflow: "reject"}), "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 6, day: 31}, {overflow: "reject"}), "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 7, day: 32}, {overflow: "reject"}), "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 8, day: 32}, {overflow: "reject"}), "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 9, day: 31}, {overflow: "reject"}), "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 10, day: 32}, {overflow: "reject"}), "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 11, day: 31}, {overflow: "reject"}), "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 12, day: 32}, {overflow: "reject"}), "Invalid time value"); +assert.throws(RangeError, () => cal.monthDayFromFields( + {year: 2021, month: 13, day: 5}, {overflow: "reject"}), "Invalid time value"); diff --git a/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/throw-type-error.js b/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/throw-type-error.js new file mode 100644 index 00000000000..2977fe96c92 --- /dev/null +++ b/test/built-ins/Temporal/Calendar/prototype/monthDayFromFields/throw-type-error.js @@ -0,0 +1,39 @@ +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.calendar.prototype.monthdayfromfields +description: Temporal.Calendar.prototype.monthDayFromFields will throw TypeError with data in wrong type. +info: | + 1. Let calendar be the this value. + 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]). + 3. Assert: calendar.[[Identifier]] is "iso8601". + 4. If Type(fields) is not Object, throw a TypeError exception. + 5. Set options to ? GetOptionsObject(options). + 6. Let result be ? ISOMonthDayFromFields(fields, options). + 7. Return ? CreateTemporalMonthDay(result.[[Month]], result.[[Day]], calendar, result.[[ReferenceISOYear]]). +features: [Temporal] +---*/ +let cal = new Temporal.Calendar("iso8601") + +// Check throw for first arg +assert.throws(TypeError, () => cal.monthDayFromFields(), + "Temporal.Calendar.prototype.monthDayFromFields called on non-object"); +[undefined, true, false, 123, 456n, Symbol(), "string"].forEach( + function(fields) { + assert.throws(TypeError, () => cal.monthDayFromFields(fields), + "Temporal.Calendar.prototype.monthDayFromFields called on non-object"); + assert.throws(TypeError, () => cal.monthDayFromFields(fields, undefined), + "Temporal.Calendar.prototype.monthDayFromFields called on non-object"); + assert.throws(TypeError, () => cal.monthDayFromFields(fields, {overflow: "constrain"}), + "Temporal.Calendar.prototype.monthDayFromFields called on non-object"); + assert.throws(TypeError, () => cal.monthDayFromFields(fields, {overflow: "reject"}), + "Temporal.Calendar.prototype.monthDayFromFields called on non-object"); + }); + +assert.throws(TypeError, () => cal.monthDayFromFields({month: 1, day: 17}), + "invalid_argument"); +assert.throws(TypeError, () => cal.monthDayFromFields({year: 2021, day: 17}), + "invalid_argument"); +assert.throws(TypeError, () => cal.monthDayFromFields({year: 2021, month: 12}), + "invalid_argument");