Skip to content

Commit

Permalink
Add tests for Temporal.Calendar.p*.yearMonthFromFields
Browse files Browse the repository at this point in the history
(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
FrankYFTang authored and jessealama committed Apr 13, 2022
1 parent 1a516bc commit 2b3369a
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 10 deletions.
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}`);
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ const yearMonthFromFields = Temporal.Calendar.prototype.yearMonthFromFields;

assert.sameValue(typeof yearMonthFromFields, "function");

assert.throws(TypeError, () => yearMonthFromFields.call(undefined), "undefined");
assert.throws(TypeError, () => yearMonthFromFields.call(null), "null");
assert.throws(TypeError, () => yearMonthFromFields.call(true), "true");
assert.throws(TypeError, () => yearMonthFromFields.call(""), "empty string");
assert.throws(TypeError, () => yearMonthFromFields.call(Symbol()), "symbol");
assert.throws(TypeError, () => yearMonthFromFields.call(1), "1");
assert.throws(TypeError, () => yearMonthFromFields.call({}), "plain object");
assert.throws(TypeError, () => yearMonthFromFields.call(Temporal.Calendar), "Temporal.Calendar");
assert.throws(TypeError, () => yearMonthFromFields.call(Temporal.Calendar.prototype), "Temporal.Calendar.prototype");
const arg = { year: 2021, month: 1 };

assert.throws(TypeError, () => yearMonthFromFields.call(undefined, arg), "undefined");
assert.throws(TypeError, () => yearMonthFromFields.call(null, arg), "null");
assert.throws(TypeError, () => yearMonthFromFields.call(true, arg), "true");
assert.throws(TypeError, () => yearMonthFromFields.call("", arg), "empty string");
assert.throws(TypeError, () => yearMonthFromFields.call(Symbol(), arg), "symbol");
assert.throws(TypeError, () => yearMonthFromFields.call(1, arg), "1");
assert.throws(TypeError, () => yearMonthFromFields.call({}, arg), "plain object");
assert.throws(TypeError, () => yearMonthFromFields.call(Temporal.Calendar, arg), "Temporal.Calendar");
assert.throws(TypeError, () => yearMonthFromFields.call(Temporal.Calendar.prototype, arg), "Temporal.Calendar.prototype");
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");
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: Throw a TypeError if the fields is not an object
features: [Symbol, Temporal]
---*/

const tests = [undefined, null, false, "string", Symbol("sym"), Math.PI, 42n];
const tests = [undefined, null, true, false, "string", Symbol("sym"), Math.PI, Infinity, NaN, 42n];
const iso = Temporal.Calendar.from("iso8601");
for (const fields of tests) {
assert.throws(TypeError, () => iso.yearMonthFromFields(fields, {}));
Expand Down
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`);
});
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");
}
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");
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`
);
});

0 comments on commit 2b3369a

Please sign in to comment.