From 72c0c5e16350a76bd41f7a1ceb7702588a2a39c6 Mon Sep 17 00:00:00 2001 From: Guillaume Emont Date: Tue, 25 Apr 2023 16:48:04 +0200 Subject: [PATCH] Added new tests to reflect the change that daysInMonth is the count of days in a month This change affects how PlainYearMonth's add and subtract behave. They now ignore daysInMonth reimplementations in the calendar, and they call dateadd() a different number of times. See https://github.com/tc39/proposal-temporal/issues/1315 --- ...-dateadd-called-with-plaindate-instance.js | 18 +++++++++++++++ .../add/custom-daysInMonth-irrelevant.js | 23 +++++++++++++++++++ ...-dateadd-called-with-plaindate-instance.js | 18 +++++++++++++++ .../subtract/custom-daysInMonth-irrelevant.js | 23 +++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 test/built-ins/Temporal/PlainYearMonth/prototype/add/calendar-dateadd-called-with-plaindate-instance.js create mode 100644 test/built-ins/Temporal/PlainYearMonth/prototype/add/custom-daysInMonth-irrelevant.js create mode 100644 test/built-ins/Temporal/PlainYearMonth/prototype/subtract/calendar-dateadd-called-with-plaindate-instance.js create mode 100644 test/built-ins/Temporal/PlainYearMonth/prototype/subtract/custom-daysInMonth-irrelevant.js diff --git a/test/built-ins/Temporal/PlainYearMonth/prototype/add/calendar-dateadd-called-with-plaindate-instance.js b/test/built-ins/Temporal/PlainYearMonth/prototype/add/calendar-dateadd-called-with-plaindate-instance.js new file mode 100644 index 0000000000..a8dd88ce07 --- /dev/null +++ b/test/built-ins/Temporal/PlainYearMonth/prototype/add/calendar-dateadd-called-with-plaindate-instance.js @@ -0,0 +1,18 @@ +// Copyright (C) 2023 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plainyearmonth.prototype.add +description: Duration addition to PlainYearMonth calls Calendar.dateAdd the right number of times +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const calendar = TemporalHelpers.calendarDateAddPlainDateInstance(); +const instance = new Temporal.PlainYearMonth(1983, 3, calendar); +TemporalHelpers.assertPlainYearMonth(instance.add({days: 31}), 1983, 4, 'M04', "Adding 31 days to march in is8601 calendar") +assert.sameValue(calendar.dateAddCallCount, 1, "dateAdd called once with positive add"); + +calendar.dateAddCallCount = 0; +TemporalHelpers.assertPlainYearMonth(instance.add({days: -31}), 1983, 2, 'M02', "Adding -31 days to march in is8601 calendar") +assert.sameValue(calendar.dateAddCallCount, 3, "dateAdd called 3 times with negative add"); diff --git a/test/built-ins/Temporal/PlainYearMonth/prototype/add/custom-daysInMonth-irrelevant.js b/test/built-ins/Temporal/PlainYearMonth/prototype/add/custom-daysInMonth-irrelevant.js new file mode 100644 index 0000000000..e1834e58b5 --- /dev/null +++ b/test/built-ins/Temporal/PlainYearMonth/prototype/add/custom-daysInMonth-irrelevant.js @@ -0,0 +1,23 @@ +// Copyright (C) 2023 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plainyearmonth.prototype.add +description: Addition of a negative duration to a PlainYearMonth is not influenced by the implementation of daysInMonth() +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + daysInMonth(ym, ...args) { + return 15; + } +} + +const customCalendar = new CustomCalendar(); +const instance = new Temporal.PlainYearMonth(2023, 3, customCalendar); + +TemporalHelpers.assertPlainYearMonth(instance.add({days: -30}), 2023, 3, 'M03', "Adding -30 days from calendar reimplementing daysinMonth()") diff --git a/test/built-ins/Temporal/PlainYearMonth/prototype/subtract/calendar-dateadd-called-with-plaindate-instance.js b/test/built-ins/Temporal/PlainYearMonth/prototype/subtract/calendar-dateadd-called-with-plaindate-instance.js new file mode 100644 index 0000000000..037d83a78a --- /dev/null +++ b/test/built-ins/Temporal/PlainYearMonth/prototype/subtract/calendar-dateadd-called-with-plaindate-instance.js @@ -0,0 +1,18 @@ +// Copyright (C) 2023 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plainyearmonth.prototype.subtract +description: Duration subtraction from PlainYearMonth calls Calendar.dateAdd the right number of times +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const calendar = TemporalHelpers.calendarDateAddPlainDateInstance(); +const instance = new Temporal.PlainYearMonth(1983, 3, calendar); +TemporalHelpers.assertPlainYearMonth(instance.subtract({days: 31}), 1983, 2, 'M02', "Removing 31 days to march in is8601 calendar") +assert.sameValue(calendar.dateAddCallCount, 3, "dateAdd called 3 times with positive subtract"); + +calendar.dateAddCallCount = 0; +TemporalHelpers.assertPlainYearMonth(instance.subtract({days: -31}), 1983, 4, 'M04', "Removing -31 days to march in is8601 calendar") +assert.sameValue(calendar.dateAddCallCount, 1, "dateAdd called once with negative subtract"); diff --git a/test/built-ins/Temporal/PlainYearMonth/prototype/subtract/custom-daysInMonth-irrelevant.js b/test/built-ins/Temporal/PlainYearMonth/prototype/subtract/custom-daysInMonth-irrelevant.js new file mode 100644 index 0000000000..147fa5fa38 --- /dev/null +++ b/test/built-ins/Temporal/PlainYearMonth/prototype/subtract/custom-daysInMonth-irrelevant.js @@ -0,0 +1,23 @@ +// Copyright (C) 2023 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plainyearmonth.prototype.subtract +description: Subtraction of positive duration to a PlainYearMonth is not influenced by the implementation of daysInMonth() +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + daysInMonth(ym, ...args) { + return 15; + } +} + +const customCalendar = new CustomCalendar(); +const instance = new Temporal.PlainYearMonth(2023, 3, customCalendar); + +TemporalHelpers.assertPlainYearMonth(instance.subtract({days: 30}), 2023, 3, 'M03', "Subtracting 30 days from calendar reimplementing daysinMonth()")