diff --git a/test/built-ins/Temporal/PlainDate/prototype/with/calendar-invalid-return.js b/test/built-ins/Temporal/PlainDate/prototype/with/calendar-invalid-return.js new file mode 100644 index 0000000000..8f78233807 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/with/calendar-invalid-return.js @@ -0,0 +1,36 @@ +// 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.plaindate.prototype.with +description: Throw when the returned value is not a PlainDate. +features: [Temporal] +---*/ + +class CustomCalendar extends Temporal.Calendar { + constructor(value) { + super("iso8601"); + this.value = value; + } + dateFromFields() { + return this.value; + } +} + +const tests = [ + [undefined], + [null, "null"], + [true], + ["2000-05"], + [Symbol()], + [200005], + [200005n], + [{}, "plain object"], + [() => {}, "lambda"], + [Temporal.PlainDate, "Temporal.PlainDate"], + [Temporal.PlainDate.prototype, "Temporal.PlainDate.prototype"], +]; +for (const [test, description = typeof test] of tests) { + const plainDate = new Temporal.PlainDate(2000, 5, 2, new CustomCalendar(test)); + assert.throws(TypeError, () => plainDate.with({ year: 1 }), `Expected error with ${description}`); +} diff --git a/test/built-ins/Temporal/PlainDate/prototype/with/custom.js b/test/built-ins/Temporal/PlainDate/prototype/with/custom.js new file mode 100644 index 0000000000..e772c7dfc0 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/with/custom.js @@ -0,0 +1,33 @@ +// 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.plaindate.prototype.with +description: Basic tests with custom calendar +includes: [compareArray.js,temporalHelpers.js] +features: [Temporal] +---*/ + +const result = new Temporal.PlainDate(1920, 5, 3); +const options = {}; +let calls = 0; +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + dateFromFields(...args) { + ++calls; + assert.sameValue(args.length, 2, "Two arguments"); + assert.sameValue(typeof args[0], "object", "First argument: type"); + assert.sameValue(args[0].day, 18, "First argument: day"); + assert.sameValue(args[0].month, 11, "First argument: month"); + assert.sameValue(args[0].monthCode, "M11", "First argument: monthCode"); + assert.sameValue(args[0].year, 43, "First argument: year"); + assert.sameValue(args[1], options, "Second argument"); + return result; + } +} +const calendar = new CustomCalendar(); +const plainDate = new Temporal.PlainDate(1976, 11, 18, calendar); +assert.sameValue(plainDate.with({ year: 43 }, options), result); +assert.sameValue(calls, 1);