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*.monthDayFromFields #3058

Closed
wants to merge 1 commit 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.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());
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

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

This one has a naming conflict with a test already in the proposal repo which still has to be copied over here: https://github.com/tc39/proposal-temporal/blob/main/polyfill/test/Calendar/prototype/monthDayFromFields/overflow-undefined.js
Consider combining them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so... what make the tests in those places not yet move into test262 yet?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about I change the file name and yield the file name for those in
https://github.com/tc39/proposal-temporal/blob/main/polyfill/test/

Copy link
Contributor

Choose a reason for hiding this comment

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

I outlined the plan for moving them over in #3002 (comment)
It would be really inconvenient for ensuring that we correctly and completely converted the legacy test suite, if we were to move everything over now to this repo and let it start to diverge.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, let me just abandon this PR and wait for @ptomato and @Ms2ger to move their test from Temporal repo instead.

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());
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.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());
});
Original file line number Diff line number Diff line change
@@ -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");
Original file line number Diff line number Diff line change
@@ -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");