diff --git a/test/built-ins/Temporal/PlainDate/prototype/add/calendar-invalid-return.js b/test/built-ins/Temporal/PlainDate/prototype/add/calendar-invalid-return.js new file mode 100644 index 0000000000..982c09edf7 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/add/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.add +description: Throw when the returned value from the calendar's dateAdd method is not a PlainDate. +features: [Temporal] +---*/ + +class CustomCalendar extends Temporal.Calendar { + constructor(value) { + super("iso8601"); + this.value = value; + } + dateAdd() { + 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.add({ years: 1 }), `Expected error with ${description}`); +} diff --git a/test/built-ins/Temporal/PlainDate/prototype/add/custom.js b/test/built-ins/Temporal/PlainDate/prototype/add/custom.js new file mode 100644 index 0000000000..5e449dc052 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/add/custom.js @@ -0,0 +1,31 @@ +// 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.add +description: Basic tests with custom calendar +includes: [compareArray.js,temporalHelpers.js] +features: [Temporal] +---*/ + +const result = new Temporal.PlainDate(1920, 5, 3); +let calls = 0; +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + dateAdd(...args) { + ++calls; + assert.sameValue(args.length, 3, "Three arguments"); + assert.sameValue(args[0], plainDate, "First argument"); + TemporalHelpers.assertDuration(args[1], 43, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Second argument"); + assert.sameValue(typeof args[2], "object", "Third argument: type"); + assert.sameValue(Object.getPrototypeOf(args[2]), null, "Third argument: prototype"); + assert.compareArray(Object.keys(args[2]), [], "Third argument: keys"); + return result; + } +} +const calendar = new CustomCalendar(); +const plainDate = new Temporal.PlainDate(1976, 11, 18, calendar); +assert.sameValue(plainDate.add({ years: 43 }), result); +assert.sameValue(calls, 1); diff --git a/test/built-ins/Temporal/PlainDate/prototype/subtract/calendar-invalid-return.js b/test/built-ins/Temporal/PlainDate/prototype/subtract/calendar-invalid-return.js new file mode 100644 index 0000000000..574040aaa0 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/subtract/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.subtract +description: Throw when the returned value from the calendar's dateAdd method is not a PlainDate. +features: [Temporal] +---*/ + +class CustomCalendar extends Temporal.Calendar { + constructor(value) { + super("iso8601"); + this.value = value; + } + dateAdd() { + 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.subtract({ years: 1 }), `Expected error with ${description}`); +} diff --git a/test/built-ins/Temporal/PlainDate/prototype/subtract/custom.js b/test/built-ins/Temporal/PlainDate/prototype/subtract/custom.js new file mode 100644 index 0000000000..33098d557e --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/subtract/custom.js @@ -0,0 +1,31 @@ +// 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.subtract +description: Basic tests with custom calendar +includes: [compareArray.js,temporalHelpers.js] +features: [Temporal] +---*/ + +const result = new Temporal.PlainDate(1920, 5, 3); +let calls = 0; +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + dateAdd(...args) { + ++calls; + assert.sameValue(args.length, 3, "Three arguments"); + assert.sameValue(args[0], plainDate, "First argument"); + TemporalHelpers.assertDuration(args[1], -43, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Second argument"); + assert.sameValue(typeof args[2], "object", "Third argument: type"); + assert.sameValue(Object.getPrototypeOf(args[2]), null, "Third argument: prototype"); + assert.compareArray(Object.keys(args[2]), [], "Third argument: keys"); + return result; + } +} +const calendar = new CustomCalendar(); +const plainDate = new Temporal.PlainDate(1976, 11, 18, calendar); +assert.sameValue(plainDate.subtract({ years: 43 }), result); +assert.sameValue(calls, 1);