Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Temporal.Calendar.p*.yearMonthFromFields #3061

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.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]]).
features: [Temporal]
---*/
let cal = new Temporal.Calendar("iso8601")

let opt = {overflow: "constrain"};
// Test overflow: "constrain" work properly
assert.sameValue("2021-01", cal.yearMonthFromFields({year: 2021, month: 1}, opt).toJSON());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using TemporalHelpers.assertPlainYearMonth

assert.sameValue("2021-02", cal.yearMonthFromFields({year: 2021, month: 2}, opt).toJSON());
assert.sameValue("2021-03", cal.yearMonthFromFields({year: 2021, month: 3}, opt).toJSON());
assert.sameValue("2021-04", cal.yearMonthFromFields({year: 2021, month: 4}, opt).toJSON());
assert.sameValue("2021-05", cal.yearMonthFromFields({year: 2021, month: 5}, opt).toJSON());
assert.sameValue("2021-06", cal.yearMonthFromFields({year: 2021, month: 6}, opt).toJSON());
assert.sameValue("2021-07", cal.yearMonthFromFields({year: 2021, month: 7}, opt).toJSON());
assert.sameValue("2021-08", cal.yearMonthFromFields({year: 2021, month: 8}, opt).toJSON());
assert.sameValue("2021-09", cal.yearMonthFromFields({year: 2021, month: 9}, opt).toJSON());
assert.sameValue("2021-10", cal.yearMonthFromFields({year: 2021, month: 10}, opt).toJSON());
assert.sameValue("2021-11", cal.yearMonthFromFields({year: 2021, month: 11}, opt).toJSON());
assert.sameValue("2021-12", cal.yearMonthFromFields({year: 2021, month: 12}, opt).toJSON());
assert.sameValue("2021-12", cal.yearMonthFromFields({year: 2021, month: 13}, opt).toJSON());
assert.sameValue("2021-12", cal.yearMonthFromFields({year: 2021, month: 999999}, opt).toJSON());
assert.sameValue("2021-01", cal.yearMonthFromFields({year: 2021, monthCode: "M01"}, opt).toJSON());
assert.sameValue("2021-02", cal.yearMonthFromFields({year: 2021, monthCode: "M02"}, opt).toJSON());
assert.sameValue("2021-03", cal.yearMonthFromFields({year: 2021, monthCode: "M03"}, opt).toJSON());
assert.sameValue("2021-04", cal.yearMonthFromFields({year: 2021, monthCode: "M04"}, opt).toJSON());
assert.sameValue("2021-05", cal.yearMonthFromFields({year: 2021, monthCode: "M05"}, opt).toJSON());
assert.sameValue("2021-06", cal.yearMonthFromFields({year: 2021, monthCode: "M06"}, opt).toJSON());
assert.sameValue("2021-07", cal.yearMonthFromFields({year: 2021, monthCode: "M07"}, opt).toJSON());
assert.sameValue("2021-08", cal.yearMonthFromFields({year: 2021, monthCode: "M08"}, opt).toJSON());
assert.sameValue("2021-09", cal.yearMonthFromFields({year: 2021, monthCode: "M09"}, opt).toJSON());
assert.sameValue("2021-10", cal.yearMonthFromFields({year: 2021, monthCode: "M10"}, opt).toJSON());
assert.sameValue("2021-11", cal.yearMonthFromFields({year: 2021, monthCode: "M11"}, opt).toJSON());
assert.sameValue("2021-12", cal.yearMonthFromFields({year: 2021, monthCode: "M12"}, opt).toJSON());
Original file line number Diff line number Diff line change
@@ -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.yearmonthfromfields
description: Temporal.Calendar.prototype.yearMonthFromFields will return correctly with default overflow which constrain date range.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]
---*/
let cal = new Temporal.Calendar("iso8601")

assert.sameValue("2021-01", cal.yearMonthFromFields({year: 2021, month: 1}).toJSON());
assert.sameValue("2021-02", cal.yearMonthFromFields({year: 2021, month: 2}).toJSON());
assert.sameValue("2021-03", cal.yearMonthFromFields({year: 2021, month: 3}).toJSON());
assert.sameValue("2021-04", cal.yearMonthFromFields({year: 2021, month: 4}).toJSON());
assert.sameValue("2021-05", cal.yearMonthFromFields({year: 2021, month: 5}).toJSON());
assert.sameValue("2021-06", cal.yearMonthFromFields({year: 2021, month: 6}).toJSON());
assert.sameValue("2021-07", cal.yearMonthFromFields({year: 2021, month: 7}).toJSON());
assert.sameValue("2021-08", cal.yearMonthFromFields({year: 2021, month: 8}).toJSON());
assert.sameValue("2021-09", cal.yearMonthFromFields({year: 2021, month: 9}).toJSON());
assert.sameValue("2021-10", cal.yearMonthFromFields({year: 2021, month: 10}).toJSON());
assert.sameValue("2021-11", cal.yearMonthFromFields({year: 2021, month: 11}).toJSON());
assert.sameValue("2021-12", cal.yearMonthFromFields({year: 2021, month: 12}).toJSON());
assert.sameValue("2021-12", cal.yearMonthFromFields({year: 2021, month: 13}).toJSON());
assert.sameValue("2021-12", cal.yearMonthFromFields({year: 2021, month: 999999}).toJSON());
assert.sameValue("2021-01", cal.yearMonthFromFields({year: 2021, monthCode: "M01"}).toJSON());
assert.sameValue("2021-02", cal.yearMonthFromFields({year: 2021, monthCode: "M02"}).toJSON());
assert.sameValue("2021-03", cal.yearMonthFromFields({year: 2021, monthCode: "M03"}).toJSON());
assert.sameValue("2021-04", cal.yearMonthFromFields({year: 2021, monthCode: "M04"}).toJSON());
assert.sameValue("2021-05", cal.yearMonthFromFields({year: 2021, monthCode: "M05"}).toJSON());
assert.sameValue("2021-06", cal.yearMonthFromFields({year: 2021, monthCode: "M06"}).toJSON());
assert.sameValue("2021-07", cal.yearMonthFromFields({year: 2021, monthCode: "M07"}).toJSON());
assert.sameValue("2021-08", cal.yearMonthFromFields({year: 2021, monthCode: "M08"}).toJSON());
assert.sameValue("2021-09", cal.yearMonthFromFields({year: 2021, monthCode: "M09"}).toJSON());
assert.sameValue("2021-10", cal.yearMonthFromFields({year: 2021, monthCode: "M10"}).toJSON());
assert.sameValue("2021-11", cal.yearMonthFromFields({year: 2021, monthCode: "M11"}).toJSON());
assert.sameValue("2021-12", cal.yearMonthFromFields({year: 2021, monthCode: "M12"}).toJSON());
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: 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]]).
features: [Temporal]
---*/
let cal = new Temporal.Calendar("iso8601")


assert.sameValue("2021-07", cal.yearMonthFromFields({year: 2021, month: 7}).toJSON());
assert.sameValue("2021-12", cal.yearMonthFromFields({year: 2021, month: 12}).toJSON());
assert.sameValue("2021-07", cal.yearMonthFromFields({year: 2021, monthCode: "M07"}).toJSON());
assert.sameValue("2021-12", cal.yearMonthFromFields({year: 2021, monthCode: "M12"}).toJSON());

["constrain", "reject"].forEach(function(overflow) {
let opt = {overflow};
assert.sameValue("2021-07", cal.yearMonthFromFields({year: 2021, month: 7}, opt).toJSON());
assert.sameValue("2021-12", cal.yearMonthFromFields({year: 2021, month: 12}, opt).toJSON());
assert.sameValue("2021-07", cal.yearMonthFromFields({year: 2021, monthCode: "M07"}, opt).toJSON());
assert.sameValue("2021-12", cal.yearMonthFromFields({year: 2021, monthCode: "M12"}, opt).toJSON());
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 RangeError while the input data value is out of 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 ? ISOYearMonthFromFields(fields, options).
7. Return ? CreateTemporalYearMonth(result.[[Year]], result.[[Month]], calendar, result.[[ReferenceISODay]]).
features: [Temporal]
---*/
let cal = new Temporal.Calendar("iso8601")

assert.throws(RangeError, () => cal.yearMonthFromFields({year: 2021, monthCode: "m1"}),
"monthCode value is out of range.");
assert.throws(RangeError, () => cal.yearMonthFromFields({year: 2021, monthCode: "M1"}),
"monthCode value is out of range.");
assert.throws(RangeError, () => cal.yearMonthFromFields({year: 2021, monthCode: "m01"}),
"monthCode value is out of range.");

assert.throws(RangeError, () => cal.yearMonthFromFields({year: 2021, month: 12, monthCode: "M11"}),
"monthCode value is out of range.");
assert.throws(RangeError, () => cal.yearMonthFromFields({year: 2021, monthCode: "M00"}),
"monthCode value is out of range.");
assert.throws(RangeError, () => cal.yearMonthFromFields({year: 2021, monthCode: "M19"}),
"monthCode value is out of range.");
assert.throws(RangeError, () => cal.yearMonthFromFields({year: 2021, monthCode: "M99"}),
"monthCode value is out of range.");
assert.throws(RangeError, () => cal.yearMonthFromFields({year: 2021, monthCode: "M13"}),
"monthCode value is out of range.");

assert.throws(RangeError, () => cal.yearMonthFromFields({year: 2021, month: -1}),
"Invalid time value");
assert.throws(RangeError, () => cal.yearMonthFromFields({year: 2021, month: -Infinity}),
"Invalid time value");

assert.throws(RangeError, () => cal.yearMonthFromFields({year: 2021, month: 0, day: 5}, {overflow: "reject"}),
"Invalid time value");
assert.throws(RangeError, () => cal.yearMonthFromFields({year: 2021, month: 13, day: 5}, {overflow: "reject"}),
"Invalid time value");

assert.throws(RangeError, () => cal.yearMonthFromFields(
{year: 2021, monthCode: "M00"}, {overflow: "reject"}),
"monthCode value is out of range.");
assert.throws(RangeError, () => cal.yearMonthFromFields(
{year: 2021, monthCode: "M13"}, {overflow: "reject"}),
"monthCode value is out of range.");

assert.throws(RangeError, () => cal.yearMonthFromFields(
{year: 2021, month: 0}), "Invalid time value");

// Check throw for the second arg
assert.throws(RangeError, () => cal.yearMonthFromFields(
{year: 2021, month: 7}, {overflow: "invalid"}),
"Value invalid out of range for Temporal.Calendar.prototype.yearMonthFromFields options property overflow");

assert.throws(RangeError, () => cal.yearMonthFromFields(
{year: 2021, month: 13}, {overflow: "reject"}), "Invalid time value");
assert.throws(RangeError, () => cal.yearMonthFromFields(
{year: 2021, month: 9995}, {overflow: "reject"}), "Invalid time value");
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 when fields is not object.
no [[InitializedTemporalCalendar]] in calendar.
info: |
4. If Type(fields) is not Object, throw a TypeError exception.
features: [Temporal]
---*/
let cal = new Temporal.Calendar("iso8601")

let notObjectList = [
null, undefined, "string", Symbol('efg'), true, false, Infinity, NaN, 123, 456n];
notObjectList.forEach(function(fields) {
assert.throws(TypeError, () => cal.yearMonthFromFields(fields),
"fields is not object");
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 from GetOptionsObject.
no [[InitializedTemporalCalendar]] in calendar.
info: |
5. Set options to ? GetOptionsObject(options).
features: [Temporal]
---*/
let cal = new Temporal.Calendar("iso8601")

let notObjectList = [
null, "string", Symbol('efg'), true, false, Infinity, NaN, 123, 456n];
notObjectList.forEach(function(options) {
assert.throws(TypeError, () => cal.yearMonthFromFields({year: 2021, month:7}, options),
"options is not object");
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test errors out for me.

Anyway, the brand check is covered more comprehensively by https://github.com/tc39/proposal-temporal/blob/main/polyfill/test/Calendar/prototype/yearMonthFromFields/branding.js, consider moving it over instead of this one.

// 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 from RequireInternalSlot if there are
no [[InitializedTemporalCalendar]] in calendar.
info: |
2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]).
features: [Temporal]
---*/
let cal = new Temporal.Calendar("iso8601")

let badCal = {yearMonthFromFields: cal.yearMonthFromFields};
// Check throw for first arg
assert.throws(TypeError, () => badCal.yearMonthFromFields({year: 2021, month: 1),
"calendar does not have [[InitializedTemporalCalendar]]");
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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]
---*/
let cal = new Temporal.Calendar("iso8601")

// Check throw for first arg
assert.throws(TypeError, () => cal.yearMonthFromFields(),
"Temporal.Calendar.prototype.yearMonthFromFields called on non-object");
[undefined, true, false, 123, 456n, Symbol(), "string"].forEach(
function(fields) {
assert.throws(TypeError, () => cal.yearMonthFromFields(fields),
"Temporal.Calendar.prototype.yearMonthFromFields called on non-object");
assert.throws(TypeError, () => cal.yearMonthFromFields(fields, undefined),
"Temporal.Calendar.prototype.yearMonthFromFields called on non-object");
assert.throws(TypeError, () => cal.yearMonthFromFields(fields, {overflow: "constrain"}),
"Temporal.Calendar.prototype.yearMonthFromFields called on non-object");
assert.throws(TypeError, () => cal.yearMonthFromFields(fields, {overflow: "reject"}),
"Temporal.Calendar.prototype.yearMonthFromFields called on non-object");
});

assert.throws(TypeError, () => cal.yearMonthFromFields({month: 1}),
"invalid_argument");
assert.throws(TypeError, () => cal.yearMonthFromFields({year: 2021}),
"invalid_argument");