Test PlainYearMonth constructor, from.

This commit is contained in:
Ms2ger 2022-01-25 11:21:17 +01:00 committed by Rick Waldron
parent 543c8ee938
commit 158d0bf35f
17 changed files with 283 additions and 5 deletions

View File

@ -0,0 +1,13 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainyearmonth
description: PlainYearMonth constructor works
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const ym = new Temporal.PlainYearMonth(1976, 11);
assert.sameValue(typeof ym, "object");
TemporalHelpers.assertPlainYearMonth(ym, 1976, 11, "M11");

View File

@ -0,0 +1,17 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainyearmonth.from
description: A number argument is stringified
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const plainYearMonth = Temporal.PlainYearMonth.from(201906);
TemporalHelpers.assertPlainYearMonth(plainYearMonth, 2019, 6, "M06");
const fields = plainYearMonth.getISOFields();
assert.sameValue(fields.calendar.id, "iso8601");
assert.sameValue(fields.isoDay, 1, "isoDay");
assert.sameValue(fields.isoMonth, 6, "isoMonth");
assert.sameValue(fields.isoYear, 2019, "isoYear");

View File

@ -0,0 +1,47 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainyearmonth.from
description: An object argument
includes: [temporalHelpers.js]
features: [Temporal]
---*/
TemporalHelpers.assertPlainYearMonth(Temporal.PlainYearMonth.from({ year: 2019, monthCode: "M11" }),
2019, 11, "M11", "Only monthCode");
TemporalHelpers.assertPlainYearMonth(Temporal.PlainYearMonth.from({ year: 2019, month: 11 }),
2019, 11, "M11", "Only month");
assert.throws(RangeError,
() => Temporal.PlainYearMonth.from({ year: 2019, month: 11, monthCode: "M12" }),
"Mismatch between month and monthCode");
const monthDayItem = { year: 2019, month: 11, get day() { throw new Test262Error("should not read the day property") } };
TemporalHelpers.assertPlainYearMonth(Temporal.PlainYearMonth.from(monthDayItem),
2019, 11, "M11", "month with day");
const monthCodeDayItem = { year: 2019, monthCode: "M11", get day() { throw new Test262Error("should not read the day property") } };
TemporalHelpers.assertPlainYearMonth(Temporal.PlainYearMonth.from(monthCodeDayItem),
2019, 11, "M11", "monthCode with day");
assert.throws(TypeError,
() => Temporal.PlainYearMonth.from({}),
"No properties");
assert.throws(TypeError,
() => Temporal.PlainYearMonth.from({ year: 2019 }),
"Only year");
assert.throws(TypeError,
() => Temporal.PlainYearMonth.from({ year: 2019, months: 6 }),
"Year and plural 'months'");
assert.throws(TypeError,
() => Temporal.PlainYearMonth.from({ month: 6 }),
"Only month");
assert.throws(TypeError,
() => Temporal.PlainYearMonth.from({ monthCode: "M06" }),
"Only monthCode");
assert.throws(TypeError,
() => Temporal.PlainYearMonth.from({ year: undefined, month: 6 }),
"year explicit undefined");
TemporalHelpers.assertPlainYearMonth(Temporal.PlainYearMonth.from({ year: 1976, month: 11, months: 12 }),
1976, 11, "M11", "Plural property ignored");

View File

@ -0,0 +1,18 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainyearmonth.from
description: A PlainYearMonth argument is cloned
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const plainDate = Temporal.PlainDate.from("1976-11-18");
const plainYearMonth = Temporal.PlainYearMonth.from(plainDate);
TemporalHelpers.assertPlainYearMonth(plainYearMonth, 1976, 11, "M11");
const fields = plainYearMonth.getISOFields();
assert.sameValue(fields.calendar.id, "iso8601");
assert.sameValue(fields.isoDay, 1, "isoDay");
assert.sameValue(fields.isoMonth, 11, "isoMonth");
assert.sameValue(fields.isoYear, 1976, "isoYear");

View File

@ -0,0 +1,22 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainyearmonth.from
description: A PlainYearMonth argument is cloned
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const original = new Temporal.PlainYearMonth(2019, 11, undefined, 7);
const result = Temporal.PlainYearMonth.from(original);
assert.notSameValue(result, original);
for (const plainYearMonth of [original, result]) {
TemporalHelpers.assertPlainYearMonth(plainYearMonth, 2019, 11, "M11");
const fields = plainYearMonth.getISOFields();
assert.sameValue(fields.calendar.id, "iso8601");
assert.sameValue(fields.isoDay, 7, "isoDay");
assert.sameValue(fields.isoMonth, 11, "isoMonth");
assert.sameValue(fields.isoYear, 2019, "isoYear");
}

View File

@ -0,0 +1,11 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainyearmonth.from
description: An invalid ISO string is never supported
features: [Temporal]
---*/
assert.throws(RangeError, () => Temporal.PlainYearMonth.from("2020-13", { overflow: "reject" }));
assert.throws(RangeError, () => Temporal.PlainYearMonth.from("2020-13", { overflow: "constrain" }));

View File

@ -0,0 +1,10 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainyearmonth.from
description: RangeError thrown if a string with trailing junk is used as a PlainYearMonth
features: [Temporal]
---*/
assert.throws(RangeError, () => Temporal.PlainYearMonth.from("1976-11junk"));

View File

@ -8,10 +8,48 @@ includes: [temporalHelpers.js]
features: [Temporal]
---*/
const plainYearMonth = Temporal.PlainYearMonth.from("1970-12");
TemporalHelpers.assertPlainYearMonth(plainYearMonth, 1970, 12, "M12");
const inputs = [
"1976-11",
"1976-11-10",
"1976-11-01T09:00:00+00:00",
"1976-11-01T00:00:00+05:00",
"197611",
"+00197611",
"1976-11-18T15:23:30.1\u221202:00",
"1976-11-18T152330.1+00:00",
"19761118T15:23:30.1+00:00",
"1976-11-18T15:23:30.1+0000",
"1976-11-18T152330.1+0000",
"19761118T15:23:30.1+0000",
"19761118T152330.1+00:00",
"19761118T152330.1+0000",
"+001976-11-18T152330.1+00:00",
"+0019761118T15:23:30.1+00:00",
"+001976-11-18T15:23:30.1+0000",
"+001976-11-18T152330.1+0000",
"+0019761118T15:23:30.1+0000",
"+0019761118T152330.1+00:00",
"+0019761118T152330.1+0000",
"1976-11-18T15:23",
"1976-11-18T15",
"1976-11-18",
];
for (const input of inputs) {
const plainYearMonth = Temporal.PlainYearMonth.from(input);
TemporalHelpers.assertPlainYearMonth(plainYearMonth, 1976, 11, "M11");
const fields = plainYearMonth.getISOFields();
assert.sameValue(fields.calendar.id, "iso8601");
assert.sameValue(fields.isoDay, 1, "isoDay");
assert.sameValue(fields.isoMonth, 11, "isoMonth");
assert.sameValue(fields.isoYear, 1976, "isoYear");
}
const plainYearMonth = Temporal.PlainYearMonth.from("\u2212009999-11");
TemporalHelpers.assertPlainYearMonth(plainYearMonth, -9999, 11, "M11");
const fields = plainYearMonth.getISOFields();
assert.sameValue(fields.calendar.id, "iso8601");
assert.sameValue(fields.isoDay, 1, "isoDay");
assert.sameValue(fields.isoMonth, 12, "isoMonth");
assert.sameValue(fields.isoYear, 1970, "isoYear");
assert.sameValue(fields.isoMonth, 11, "isoMonth");
assert.sameValue(fields.isoYear, -9999, "isoYear");
assert.sameValue(plainYearMonth.toString(), "-009999-11");

View File

@ -0,0 +1,21 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainyearmonth.from
description: TypeError thrown when a primitive is passed as the options argument
features: [Temporal]
---*/
const items = [
{ year: 2000, month: 11 },
"2000-11",
new Temporal.PlainYearMonth(2000, 11),
];
const values = [null, true, "hello", Symbol("foo"), 1, 1n];
for (const item of items) {
for (const badOptions of values) {
assert.throws(TypeError, () => Temporal.PlainYearMonth.from(item, badOptions));
}
}

View File

@ -0,0 +1,14 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainyearmonth.from
description: Reject value for overflow option
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const propertyBag = { year: 2000, month: 13 };
const plainYearMonth = Temporal.PlainYearMonth.from(propertyBag, { overflow: "constrain" });
TemporalHelpers.assertPlainYearMonth(plainYearMonth, 2000, 12, "M12", "default overflow is constrain");

View File

@ -28,5 +28,7 @@ const validValues = [
"2000-05",
];
validValues.forEach((value) => {
assert.throws(RangeError, () => Temporal.PlainYearMonth.from(value, { overflow: "other string" }));
["", "CONSTRAIN", "balance", "other string", "constra\u0131n", 3, null].forEach((overflow) => {
assert.throws(RangeError, () => Temporal.PlainYearMonth.from(value, { overflow }));
});
});

View File

@ -0,0 +1,11 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainyearmonth.from
description: Reject value for overflow option
features: [Temporal]
---*/
const bad = { year: 2019, month: 13 };
assert.throws(RangeError, () => Temporal.PlainYearMonth.from(bad, { overflow: "reject" }));

View File

@ -32,6 +32,8 @@ validValues.forEach((value) => {
TemporalHelpers.assertPlainYearMonth(explicit, 2000, 5, "M05", "overflow is ignored");
const implicit = Temporal.PlainYearMonth.from(value, {});
TemporalHelpers.assertPlainYearMonth(implicit, 2000, 5, "M05", "overflow is ignored");
const lambda = Temporal.PlainYearMonth.from(value, () => {});
TemporalHelpers.assertPlainYearMonth(lambda, 2000, 5, "M05", "overflow is ignored");
});
const propertyBag = { year: 2000, month: 13 };
@ -39,3 +41,5 @@ const explicit = Temporal.PlainYearMonth.from(propertyBag, { overflow: undefined
TemporalHelpers.assertPlainYearMonth(explicit, 2000, 12, "M12", "default overflow is constrain");
const implicit = Temporal.PlainYearMonth.from(propertyBag, {});
TemporalHelpers.assertPlainYearMonth(implicit, 2000, 12, "M12", "default overflow is constrain");
const lambda = Temporal.PlainYearMonth.from(propertyBag, () => {});
TemporalHelpers.assertPlainYearMonth(lambda, 2000, 12, "M12", "default overflow is constrain");

View File

@ -0,0 +1,11 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-get-temporal.plainyearmonth.prototype.daysinmonth
description: daysInMonth works
features: [Temporal]
---*/
const ym = new Temporal.PlainYearMonth(1976, 11);
assert.sameValue(ym.daysInMonth, 30);

View File

@ -0,0 +1,11 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-get-temporal.plainyearmonth.prototype.daysinyear
description: daysInYear works
features: [Temporal]
---*/
const ym = new Temporal.PlainYearMonth(1976, 11);
assert.sameValue(ym.daysInYear, 366);

View File

@ -0,0 +1,11 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-get-temporal.plainyearmonth.prototype.monthsinyear
description: monthsInYear works
features: [Temporal]
---*/
const ym = new Temporal.PlainYearMonth(1976, 11);
assert.sameValue(ym.monthsInYear, 12);

View File

@ -0,0 +1,17 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainyearmonth.from
description: An object argument
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const monthDayItem = { calendar: "gregory", era: "ce", eraYear: 2019, month: 11, get day() { throw new Test262Error("should not read the day property") } };
TemporalHelpers.assertPlainYearMonth(Temporal.PlainYearMonth.from(monthDayItem),
2019, 11, "M11", "month with day", "ce", 2019);
const monthCodeDayItem = { calendar: "gregory", era: "ce", eraYear: 2019, monthCode: "M11", get day() { throw new Test262Error("should not read the day property") } };
TemporalHelpers.assertPlainYearMonth(Temporal.PlainYearMonth.from(monthCodeDayItem),
2019, 11, "M11", "monthCode with day", "ce", 2019);