forked from tc39/test262
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for Temporal.Calendar.p*.yearMonthFromFields
(Philip, March 2022: This was originally Frank's PR tc39#3061. I did some reformatting, removed duplicate tests, and combined with some existing tests.)
- Loading branch information
1 parent
74c6747
commit 4f31969
Showing
8 changed files
with
239 additions
and
10 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
test/built-ins/Temporal/Calendar/prototype/yearMonthFromFields/basic.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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.yearmonthfromfields | ||
description: Temporal.Calendar.prototype.yearMonthFromFields 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 ? ISOYearMonthFromFields(fields, options). | ||
7. Return ? CreateTemporalYearMonth(result.[[Year]], result.[[Month]], calendar, result.[[ReferenceISODay]]). | ||
includes: [temporalHelpers.js] | ||
features: [Temporal] | ||
---*/ | ||
|
||
const cal = new Temporal.Calendar("iso8601") | ||
|
||
let result = cal.yearMonthFromFields({ year: 2021, month: 7 }); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 7, "M07", "year 2021, month 7"); | ||
result = cal.yearMonthFromFields({ year: 2021, month: 12 }); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", "year 2021, month 12"); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M07" }); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 7, "M07", "year 2021, monthCode M07"); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M12" }); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", "year 2021, monthCode M12"); | ||
|
||
["constrain", "reject"].forEach((overflow) => { | ||
const opt = { overflow }; | ||
result = cal.yearMonthFromFields({ year: 2021, month: 7 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 7, "M07", `year 2021, month 7, overflow ${overflow}`); | ||
result = cal.yearMonthFromFields({ year: 2021, month: 12 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", `year 2021, month 12, overflow ${overflow}`); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M07" }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 7, "M07", `year 2021, monthCode M07, overflow ${overflow}`); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M12" }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", `year 2021, monthCode M12, overflow ${overflow}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
test/built-ins/Temporal/Calendar/prototype/yearMonthFromFields/fields-missing-properties.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// 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.yearmonthfromfields | ||
description: Temporal.Calendar.prototype.yearMonthFromFields will throw TypeError with incorrect input data 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 ? ISOYearMonthFromFields(fields, options). | ||
7. Return ? CreateTemporalYearMonth(result.[[Year]], result.[[Month]], calendar, result.[[ReferenceISODay]]). | ||
features: [Temporal] | ||
---*/ | ||
|
||
const cal = new Temporal.Calendar("iso8601") | ||
|
||
assert.throws(TypeError, () => cal.yearMonthFromFields({}), "at least one correctly spelled property is required"); | ||
assert.throws(TypeError, () => cal.yearMonthFromFields({ month: 1 }), "year is required"); | ||
assert.throws(TypeError, () => cal.yearMonthFromFields({ year: 2021 }), "month or monthCode is required"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
test/built-ins/Temporal/Calendar/prototype/yearMonthFromFields/monthcode-invalid.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 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.yearmonthfromfields | ||
description: Throw RangeError for an out-of-range, conflicting, or ill-formed monthCode | ||
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 ? ISOYearMonthFromFields(fields, options). | ||
7. Return ? CreateTemporalYearMonth(result.[[Year]], result.[[Month]], calendar, result.[[ReferenceISODay]]). | ||
features: [Temporal] | ||
---*/ | ||
|
||
const cal = new Temporal.Calendar("iso8601"); | ||
|
||
["m1", "M1", "m01"].forEach((monthCode) => { | ||
assert.throws(RangeError, () => cal.yearMonthFromFields({ year: 2021, monthCode }), | ||
`monthCode '${monthCode}' is not well-formed`); | ||
}); | ||
|
||
assert.throws(RangeError, () => cal.yearMonthFromFields({ year: 2021, month: 12, monthCode: "M11" }), | ||
"monthCode and month conflict"); | ||
|
||
["M00", "M19", "M99", "M13"].forEach((monthCode) => { | ||
assert.throws(RangeError, () => cal.yearMonthFromFields({ year: 2021, monthCode }), | ||
`monthCode '${monthCode}' is not valid for year 2021`); | ||
}); |
17 changes: 17 additions & 0 deletions
17
test/built-ins/Temporal/Calendar/prototype/yearMonthFromFields/options-not-object.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// 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.yearmonthfromfields | ||
description: Throw a TypeError if options is not an object or undefined | ||
info: | | ||
5. Set options to ? GetOptionsObject(options). | ||
features: [Symbol, Temporal] | ||
---*/ | ||
|
||
const tests = [null, true, false, "string", Symbol("sym"), Math.PI, Infinity, NaN, 42n]; | ||
const iso = new Temporal.Calendar("iso8601"); | ||
for (const options of tests) { | ||
assert.throws(TypeError, () => iso.yearMonthFromFields({ year: 2021, month: 7 }, options), | ||
"options is not object"); | ||
} |
91 changes: 91 additions & 0 deletions
91
test/built-ins/Temporal/Calendar/prototype/yearMonthFromFields/overflow-constrain.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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.yearmonthfromfields | ||
description: Temporal.Calendar.prototype.yearMonthFromFields 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 ? ISOYearMonthFromFields(fields, options). | ||
7. Return ? CreateTemporalYearMonth(result.[[Year]], result.[[Month]], calendar, result.[[ReferenceISODay]]). | ||
includes: [temporalHelpers.js] | ||
features: [Temporal] | ||
---*/ | ||
|
||
const cal = new Temporal.Calendar("iso8601") | ||
const opt = { overflow: "constrain" }; | ||
|
||
let result = cal.yearMonthFromFields({ year: 2021, month: 1 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 1, "M01", "month 1 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, month: 2 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 2, "M02", "month 2 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, month: 3 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 3, "M03", "month 3 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, month: 4 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 4, "M04", "month 4 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, month: 5 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 5, "M05", "month 5 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, month: 6 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 6, "M06", "month 6 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, month: 7 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 7, "M07", "month 7 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, month: 8 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 8, "M08", "month 8 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, month: 9 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 9, "M09", "month 9 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, month: 10 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 10, "M10", "month 10 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, month: 11 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 11, "M11", "month 11 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, month: 12 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", "month 12 with overflow constrain"); | ||
|
||
assert.throws( | ||
RangeError, | ||
() => cal.yearMonthFromFields({ year: 2021, month: -99999 }, opt), | ||
"negative month -99999 is out of range even with overflow constrain" | ||
) | ||
assert.throws( | ||
RangeError, | ||
() => cal.yearMonthFromFields({ year: 2021, month: -1 }, opt), | ||
"negative month -1 is out of range even with overflow constrain" | ||
) | ||
assert.throws( | ||
RangeError, | ||
() => cal.yearMonthFromFields({ year: 2021, month: 0 }, opt), | ||
"month zero is out of range even with overflow constrain" | ||
) | ||
|
||
result = cal.yearMonthFromFields({ year: 2021, month: 13 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", "month 13 is constrained to 12"); | ||
result = cal.yearMonthFromFields({ year: 2021, month: 99999 }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", "month 99999 is constrained to 12"); | ||
|
||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M01" }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 1, "M01", "monthCode M01 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M02" }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 2, "M02", "monthCode M02 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M03" }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 3, "M03", "monthCode M03 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M04" }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 4, "M04", "monthCode M04 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M05" }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 5, "M05", "monthCode M05 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M06" }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 6, "M06", "monthCode M06 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M07" }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 7, "M07", "monthCode M07 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M08" }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 8, "M08", "monthCode M08 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M09" }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 9, "M09", "monthCode M09 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M10" }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 10, "M10", "monthCode M10 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M11" }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 11, "M11", "monthCode M11 with overflow constrain"); | ||
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M12" }, opt); | ||
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", "monthCode M12 with overflow constrain"); |
26 changes: 26 additions & 0 deletions
26
test/built-ins/Temporal/Calendar/prototype/yearMonthFromFields/overflow-reject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 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.yearmonthfromfields | ||
description: Throw RangeError for input data out of range with overflow reject | ||
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 ? ISOYearMonthFromFields(fields, options). | ||
7. Return ? CreateTemporalYearMonth(result.[[Year]], result.[[Month]], calendar, result.[[ReferenceISODay]]). | ||
features: [Temporal] | ||
---*/ | ||
|
||
const cal = new Temporal.Calendar("iso8601"); | ||
|
||
[-1, 0, 13, 9995].forEach((month) => { | ||
assert.throws( | ||
RangeError, | ||
() => cal.yearMonthFromFields({year: 2021, month, day: 5}, { overflow: "reject" }), | ||
`Month ${month} is out of range for 2021 with overflow: reject` | ||
); | ||
}); |