overflow.js (5606B)
1 // |reftest| skip-if(!this.hasOwnProperty('Temporal')) -- Temporal is not enabled unconditionally 2 // Copyright (C) 2021 Igalia, S.L. All rights reserved. 3 // This code is governed by the BSD license found in the LICENSE file. 4 5 /*--- 6 esid: sec-temporal.plainmonthday.from 7 description: Handling for overflow option 8 includes: [temporalHelpers.js] 9 features: [Temporal] 10 ---*/ 11 12 const validValues = [ 13 new Temporal.PlainMonthDay(5, 2), 14 "05-02", 15 ]; 16 validValues.forEach((value) => { 17 const constrain = Temporal.PlainMonthDay.from(value, { overflow: "constrain" }); 18 TemporalHelpers.assertPlainMonthDay(constrain, "M05", 2, "overflow is ignored: constrain"); 19 20 const reject = Temporal.PlainMonthDay.from(value, { overflow: "reject" }); 21 TemporalHelpers.assertPlainMonthDay(reject, "M05", 2, "overflow is ignored: reject"); 22 }); 23 24 const propertyBag1 = { year: 2000, month: 13, day: 34 }; 25 const result1 = Temporal.PlainMonthDay.from(propertyBag1, { overflow: "constrain" }); 26 TemporalHelpers.assertPlainMonthDay(result1, "M12", 31, "default overflow is constrain"); 27 assert.throws(RangeError, () => Temporal.PlainMonthDay.from(propertyBag1, { overflow: "reject" }), 28 "invalid property bag: reject"); 29 30 const propertyBag2 = { month: 1, day: 32 }; 31 const result2 = Temporal.PlainMonthDay.from(propertyBag2, { overflow: "constrain" }); 32 TemporalHelpers.assertPlainMonthDay(result2, "M01", 31, "default overflow is constrain"); 33 assert.throws(RangeError, () => Temporal.PlainMonthDay.from(propertyBag2, { overflow: "reject" }), 34 "invalid property bag: reject"); 35 36 assert.throws(RangeError, () => Temporal.PlainMonthDay.from("13-34", { overflow: "constrain" }), 37 "invalid ISO string: constrain"); 38 assert.throws(RangeError, () => Temporal.PlainMonthDay.from("13-34", { overflow: "reject" }), 39 "invalid ISO string: reject"); 40 41 const opt = { overflow: "constrain" }; 42 43 let result = Temporal.PlainMonthDay.from({ year: 2021, month: 13, day: 1 }, opt); 44 TemporalHelpers.assertPlainMonthDay(result, "M12", 1, "month 13 is constrained to 12"); 45 46 result = Temporal.PlainMonthDay.from({ year: 2021, month: 999999, day: 500 }, opt); 47 TemporalHelpers.assertPlainMonthDay(result, "M12", 31, "month 999999 is constrained to 12 and day 500 is constrained to 31"); 48 49 [-99999, -1, 0].forEach((month) => { 50 assert.throws( 51 RangeError, 52 () => Temporal.PlainMonthDay.from({ year: 2021, month, day: 1 }, opt), 53 `Month ${month} is out of range for 2021 even with overflow: constrain` 54 ); 55 }); 56 57 TemporalHelpers.ISOMonths.forEach(({ month, monthCode, daysInMonth }) => { 58 const day = daysInMonth + 1; 59 60 result = Temporal.PlainMonthDay.from({ month, day }, opt); 61 TemporalHelpers.assertPlainMonthDay(result, monthCode, daysInMonth, 62 `day is constrained from ${day} to ${daysInMonth} in month ${month}`); 63 64 result = Temporal.PlainMonthDay.from({ month, day: 9001 }, opt); 65 TemporalHelpers.assertPlainMonthDay(result, monthCode, daysInMonth, 66 `day is constrained to ${daysInMonth} in month ${month}`); 67 68 result = Temporal.PlainMonthDay.from({ monthCode, day }, opt); 69 TemporalHelpers.assertPlainMonthDay(result, monthCode, daysInMonth, 70 `day is constrained from ${day} to ${daysInMonth} in monthCode ${monthCode}`); 71 72 result = Temporal.PlainMonthDay.from({ monthCode, day: 9001 }, opt); 73 TemporalHelpers.assertPlainMonthDay(result, monthCode, daysInMonth, 74 `day is constrained to ${daysInMonth} in monthCode ${monthCode}`); 75 }); 76 77 [ ["month", 2], ["monthCode", "M02"] ].forEach(([ name, value ]) => { 78 result = Temporal.PlainMonthDay.from({ year: 2020, [name]: value, day: 30 }, opt); 79 TemporalHelpers.assertPlainMonthDay(result, "M02", 29, `${name} ${value} is constrained to 29 in leap year 2020`); 80 81 result = Temporal.PlainMonthDay.from({ year: 2021, [name]: value, day: 29 }, opt); 82 TemporalHelpers.assertPlainMonthDay(result, "M02", 28, `${name} ${value} is constrained to 28 in common year 2021`); 83 }); 84 85 [-1, 0, 13, 9995].forEach((month) => { 86 assert.throws( 87 RangeError, 88 () => Temporal.PlainMonthDay.from({year: 2021, month, day: 5}, { overflow: "reject" }), 89 `Month ${month} is out of range for 2021 with overflow: reject` 90 ); 91 }); 92 93 [-1, 0, 32, 999].forEach((day) => { 94 assert.throws( 95 RangeError, 96 () => Temporal.PlainMonthDay.from({ year: 2021, month: 12, day }, { overflow: "reject" }), 97 `Day ${day} is out of range for 2021-12 with overflow: reject` 98 ); 99 assert.throws( 100 RangeError, 101 () => Temporal.PlainMonthDay.from({ monthCode: "M12", day }, { overflow: "reject" }), 102 `Day ${day} is out of range for 2021-M12 with overflow: reject` 103 ); 104 }); 105 106 TemporalHelpers.ISOMonths.forEach(({ month, monthCode, daysInMonth }) => { 107 const day = daysInMonth + 1; 108 assert.throws(RangeError, 109 () => Temporal.PlainMonthDay.from({ month, day }, { overflow: "reject" }), 110 `Day ${day} is out of range for month ${month} with overflow: reject`); 111 assert.throws(RangeError, 112 () => Temporal.PlainMonthDay.from({ monthCode, day }, { overflow: "reject" }), 113 `Day ${day} is out of range for monthCode ${monthCode} with overflow: reject`); 114 }); 115 116 [ ["month", 2], ["monthCode", "M02"] ].forEach(([ name, value ]) => { 117 assert.throws(RangeError, 118 () => Temporal.PlainMonthDay.from({ year: 2020, [name]: value, day: 30 }, { overflow: "reject" }), 119 `Day 30 is out of range for ${name} ${value} in leap year 2020 with overflow: reject`); 120 assert.throws(RangeError, 121 () => Temporal.PlainMonthDay.from({ year: 2021, [name]: value, day: 29 }, { overflow: "reject" }), 122 `Day 29 is out of range for ${name} ${value} in common year 2021 with overflow: reject`); 123 }); 124 125 reportCompare(0, 0);