Test PlainYearMonth.prototype.with.

This commit is contained in:
Ms2ger 2022-02-01 11:02:39 +01:00 committed by Rick Waldron
parent 7215b9387f
commit 6ba72412ab
7 changed files with 84 additions and 1 deletions

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.prototype.with
description: Throw if the argument has a calendar field
features: [Temporal]
---*/
const ym = Temporal.PlainYearMonth.from("2019-10");
assert.throws(TypeError, () => ym.with({ year: 2021, calendar: "iso8601" }));

View File

@ -0,0 +1,12 @@
// 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.prototype.with
description: TypeError thrown when argument doesn't contain any of the supported properties
features: [Temporal]
---*/
const ym = Temporal.PlainYearMonth.from("2019-10");
assert.throws(TypeError, () => ym.with({}), "No properties");
assert.throws(TypeError, () => ym.with({ months: 12 }), "Only plural 'months' property");

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.prototype.with
description: Throw if the argument has a timeZone field
features: [Temporal]
---*/
const ym = Temporal.PlainYearMonth.from("2019-10");
assert.throws(TypeError, () => ym.with({ year: 2021, timeZone: "UTC" }));

View File

@ -0,0 +1,23 @@
// 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.prototype.with
description: Basic tests for with
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const ym = Temporal.PlainYearMonth.from("2019-10");
TemporalHelpers.assertPlainYearMonth(ym.with({ year: 2020 }), 2020, 10, "M10", "year");
TemporalHelpers.assertPlainYearMonth(ym.with({ month: 9 }), 2019, 9, "M09", "month");
TemporalHelpers.assertPlainYearMonth(ym.with({ monthCode: "M09" }), 2019, 9, "M09", "monthCode");
assert.throws(RangeError, () => ym.with({ month: 9, monthCode: "M10" }), "month/monthCode mismatch");
TemporalHelpers.assertPlainYearMonth(ym.with({ month: 1, years: 2020 }), 2019, 1, "M01", "plural 'years'");
const withDay = ym.with({ year: 2019, get day() { throw new Test262Error("should not read the day property") } });
TemporalHelpers.assertPlainYearMonth(withDay, 2019, 10, "M10", "day property");
assert.sameValue(withDay.getISOFields().isoDay, 1);

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.prototype.with
description: TypeError thrown when options argument is a primitive
features: [BigInt, Symbol, Temporal]
---*/
const values = [
null,
true,
"2021-01",
Symbol(),
1,
2n,
];
const ym = Temporal.PlainYearMonth.from("2019-10");
values.forEach((value) => {
assert.throws(TypeError, () => ym.with({ year: 2020 }, value), `TypeError on wrong argument type ${typeof value}`);
});

View File

@ -17,4 +17,6 @@ features: [Temporal]
---*/ ---*/
const yearmonth = new Temporal.PlainYearMonth(2000, 5); const yearmonth = new Temporal.PlainYearMonth(2000, 5);
assert.throws(RangeError, () => yearmonth.with({ month: 8 }, { overflow: "other string" })); for (const overflow of ["", "CONSTRAIN", "balance", "other string", "constra\u0131n"]) {
assert.throws(RangeError, () => yearmonth.with({ month: 8 }, { overflow }));
}

View File

@ -22,3 +22,5 @@ const explicit = yearmonth.with({ month: 15 }, { overflow: undefined });
TemporalHelpers.assertPlainYearMonth(explicit, 2000, 12, "M12", "default overflow is constrain"); TemporalHelpers.assertPlainYearMonth(explicit, 2000, 12, "M12", "default overflow is constrain");
const implicit = yearmonth.with({ month: 15 }, {}); const implicit = yearmonth.with({ month: 15 }, {});
TemporalHelpers.assertPlainYearMonth(implicit, 2000, 12, "M12", "default overflow is constrain"); TemporalHelpers.assertPlainYearMonth(implicit, 2000, 12, "M12", "default overflow is constrain");
const lambda = yearmonth.with({ month: 15 }, () => {});
TemporalHelpers.assertPlainYearMonth(lambda, 2000, 12, "M12", "default overflow is constrain");