From 3ddfa0cd1355198f7f4e8c8f2d44fa94c35c07ae Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Fri, 1 Jul 2022 16:56:00 +0200 Subject: [PATCH] Temporal: Test that Calendar#dateAdd is called with the correct arguments. --- .../prototype/add/calendar-dateadd.js | 40 +++++++++++++++++++ .../prototype/subtract/calendar-dateadd.js | 40 +++++++++++++++++++ .../prototype/add/calendar-dateadd.js | 29 ++++++++++++++ .../prototype/subtract/calendar-dateadd.js | 29 ++++++++++++++ .../prototype/add/calendar-dateadd.js | 29 ++++++++++++++ .../prototype/subtract/calendar-dateadd.js | 29 ++++++++++++++ .../prototype/add/calendar-dateadd.js | 30 ++++++++++++++ .../prototype/subtract/calendar-dateadd.js | 30 ++++++++++++++ 8 files changed, 256 insertions(+) create mode 100644 test/built-ins/Temporal/Duration/prototype/add/calendar-dateadd.js create mode 100644 test/built-ins/Temporal/Duration/prototype/subtract/calendar-dateadd.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/add/calendar-dateadd.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/subtract/calendar-dateadd.js create mode 100644 test/built-ins/Temporal/PlainYearMonth/prototype/add/calendar-dateadd.js create mode 100644 test/built-ins/Temporal/PlainYearMonth/prototype/subtract/calendar-dateadd.js create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/add/calendar-dateadd.js create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/subtract/calendar-dateadd.js diff --git a/test/built-ins/Temporal/Duration/prototype/add/calendar-dateadd.js b/test/built-ins/Temporal/Duration/prototype/add/calendar-dateadd.js new file mode 100644 index 0000000000..a81d1a379b --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/add/calendar-dateadd.js @@ -0,0 +1,40 @@ +// 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.duration.prototype.add +description: Duration.prototype.add should call dateAdd with the appropriate values. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +let calls = 0; +const expected = [ + { + plainDate: [1920, 5, "M05", 3], + duration: [2, 0, 0, 4, 0, 0, 0, 0, 0, 0], + }, + { + plainDate: [1922, 5, "M05", 7], + duration: [0, 10, 0, 0, 0, 0, 0, 0, 0, 0], + }, +]; +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + dateAdd(plainDate, duration, options) { + TemporalHelpers.assertPlainDate(plainDate, ...expected[calls].plainDate, + `plainDate argument ${calls}`); + TemporalHelpers.assertDuration(duration, ...expected[calls].duration, + `duration argument ${calls}`); + assert.sameValue(options, undefined, "options argument"); + ++calls; + return super.dateAdd(plainDate, duration, options); + } +} +const relativeTo = new Temporal.PlainDate(1920, 5, 3, new CustomCalendar()); +const duration = new Temporal.Duration(2, 0, 0, 4, 2); +const result = duration.add({ months: 10, hours: 14 }, { relativeTo }); +TemporalHelpers.assertDuration(result, 2, 10, 0, 4, 16, 0, 0, 0, 0, 0, "result"); +assert.sameValue(calls, 2, "should have called dateAdd"); diff --git a/test/built-ins/Temporal/Duration/prototype/subtract/calendar-dateadd.js b/test/built-ins/Temporal/Duration/prototype/subtract/calendar-dateadd.js new file mode 100644 index 0000000000..d405c26fd2 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/subtract/calendar-dateadd.js @@ -0,0 +1,40 @@ +// 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.duration.prototype.subtract +description: Duration.prototype.subtract should call dateAdd with the appropriate values. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +let calls = 0; +const expected = [ + { + plainDate: [1920, 5, "M05", 3], + duration: [2, 0, 0, 4, 0, 0, 0, 0, 0, 0], + }, + { + plainDate: [1922, 5, "M05", 7], + duration: [0, -10, 0, 0, 0, 0, 0, 0, 0, 0], + }, +]; +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + dateAdd(plainDate, duration, options) { + TemporalHelpers.assertPlainDate(plainDate, ...expected[calls].plainDate, + `plainDate argument ${calls}`); + TemporalHelpers.assertDuration(duration, ...expected[calls].duration, + `duration argument ${calls}`); + assert.sameValue(options, undefined, "options argument"); + ++calls; + return super.dateAdd(plainDate, duration, options); + } +} +const relativeTo = new Temporal.PlainDate(1920, 5, 3, new CustomCalendar()); +const duration = new Temporal.Duration(2, 0, 0, 4, 2); +const result = duration.subtract({ months: 10, hours: 14 }, { relativeTo }); +TemporalHelpers.assertDuration(result, 1, 2, 0, 3, 12, 0, 0, 0, 0, 0, "result"); +assert.sameValue(calls, 2, "should have called dateAdd"); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/add/calendar-dateadd.js b/test/built-ins/Temporal/PlainDateTime/prototype/add/calendar-dateadd.js new file mode 100644 index 0000000000..25f858886f --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/add/calendar-dateadd.js @@ -0,0 +1,29 @@ +// 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.plaindatetime.prototype.add +description: PlainDateTime.prototype.add should call dateAdd with the appropriate values. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +let calls = 0; +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + dateAdd(plainDate, duration, options) { + ++calls; + TemporalHelpers.assertPlainDate(plainDate, 2020, 3, "M03", 14, "plainDate argument"); + TemporalHelpers.assertDuration(duration, 0, 10, 0, 1, 0, 0, 0, 0, 0, 0, "duration argument"); + assert.sameValue(typeof options, "object", "options argument: type"); + assert.sameValue(Object.getPrototypeOf(options), null, "options argument: prototype"); + return super.dateAdd(plainDate, duration, options); + } +} + +const plainDateTime = new Temporal.PlainDateTime(2020, 3, 14, 12, 34, 56, 987, 654, 321, new CustomCalendar()); +const result = plainDateTime.add({ months: 10, hours: 14 }); +TemporalHelpers.assertPlainDateTime(result, 2021, 1, "M01", 15, 2, 34, 56, 987, 654, 321); +assert.sameValue(calls, 1, "should have called dateAdd"); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/subtract/calendar-dateadd.js b/test/built-ins/Temporal/PlainDateTime/prototype/subtract/calendar-dateadd.js new file mode 100644 index 0000000000..8e22137fc1 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/subtract/calendar-dateadd.js @@ -0,0 +1,29 @@ +// 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.plaindatetime.prototype.subtract +description: PlainDateTime.prototype.subtract should call dateAdd with the appropriate values. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +let calls = 0; +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + dateAdd(plainDate, duration, options) { + ++calls; + TemporalHelpers.assertPlainDate(plainDate, 2020, 3, "M03", 14, "plainDate argument"); + TemporalHelpers.assertDuration(duration, 0, -10, 0, -1, 0, 0, 0, 0, 0, 0, "duration argument"); + assert.sameValue(typeof options, "object", "options argument: type"); + assert.sameValue(Object.getPrototypeOf(options), null, "options argument: prototype"); + return super.dateAdd(plainDate, duration, options); + } +} + +const plainDateTime = new Temporal.PlainDateTime(2020, 3, 14, 12, 34, 56, 987, 654, 321, new CustomCalendar()); +const result = plainDateTime.subtract({ months: 10, hours: 14 }); +TemporalHelpers.assertPlainDateTime(result, 2019, 5, "M05", 13, 22, 34, 56, 987, 654, 321); +assert.sameValue(calls, 1, "should have called dateAdd"); diff --git a/test/built-ins/Temporal/PlainYearMonth/prototype/add/calendar-dateadd.js b/test/built-ins/Temporal/PlainYearMonth/prototype/add/calendar-dateadd.js new file mode 100644 index 0000000000..130d2e0f93 --- /dev/null +++ b/test/built-ins/Temporal/PlainYearMonth/prototype/add/calendar-dateadd.js @@ -0,0 +1,29 @@ +// 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.add +description: PlainYearMonth.prototype.add should call dateAdd with the appropriate values. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +let calls = 0; +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + dateAdd(plainDate, duration, options) { + ++calls; + TemporalHelpers.assertPlainDate(plainDate, 2000, 3, "M03", 1, "plainDate argument"); + TemporalHelpers.assertDuration(duration, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, "duration argument"); + assert.sameValue(typeof options, "object", "options argument: type"); + assert.sameValue(Object.getPrototypeOf(options), null, "options argument: prototype"); + return super.dateAdd(plainDate, duration, options); + } +} + +const plainYearMonth = new Temporal.PlainYearMonth(2000, 3, new CustomCalendar()); +const result = plainYearMonth.add({ months: 10 }); +TemporalHelpers.assertPlainYearMonth(result, 2001, 1, "M01"); +assert.sameValue(calls, 1, "should have called dateAdd"); diff --git a/test/built-ins/Temporal/PlainYearMonth/prototype/subtract/calendar-dateadd.js b/test/built-ins/Temporal/PlainYearMonth/prototype/subtract/calendar-dateadd.js new file mode 100644 index 0000000000..5622d87794 --- /dev/null +++ b/test/built-ins/Temporal/PlainYearMonth/prototype/subtract/calendar-dateadd.js @@ -0,0 +1,29 @@ +// 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.subtract +description: PlainYearMonth.prototype.subtract should call dateAdd with the appropriate values. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +let calls = 0; +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + dateAdd(plainDate, duration, options) { + ++calls; + TemporalHelpers.assertPlainDate(plainDate, 2000, 3, "M03", 31, "plainDate argument"); + TemporalHelpers.assertDuration(duration, 0, -10, 0, 0, 0, 0, 0, 0, 0, 0, "duration argument"); + assert.sameValue(typeof options, "object", "options argument: type"); + assert.sameValue(Object.getPrototypeOf(options), null, "options argument: prototype"); + return super.dateAdd(plainDate, duration, options); + } +} + +const plainYearMonth = new Temporal.PlainYearMonth(2000, 3, new CustomCalendar()); +const result = plainYearMonth.subtract({ months: 10 }); +TemporalHelpers.assertPlainYearMonth(result, 1999, 5, "M05"); +assert.sameValue(calls, 1, "should have called dateAdd"); diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/add/calendar-dateadd.js b/test/built-ins/Temporal/ZonedDateTime/prototype/add/calendar-dateadd.js new file mode 100644 index 0000000000..18b7ae50b8 --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/add/calendar-dateadd.js @@ -0,0 +1,30 @@ +// 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.zoneddatetime.prototype.add +description: ZonedDateTime.prototype.add should call dateAdd with the appropriate values. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +let calls = 0; +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + dateAdd(plainDate, duration, options) { + ++calls; + TemporalHelpers.assertPlainDate(plainDate, 2020, 3, "M03", 14, "plainDate argument"); + TemporalHelpers.assertDuration(duration, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, "duration argument"); + assert.sameValue(typeof options, "object", "options argument: type"); + assert.sameValue(Object.getPrototypeOf(options), null, "options argument: prototype"); + return super.dateAdd(plainDate, duration, options); + } +} + +const zonedDateTime = new Temporal.ZonedDateTime(1584189296_987654321n, + new Temporal.TimeZone("UTC"), new CustomCalendar()); +const result = zonedDateTime.add({ months: 10, hours: 14 }); +assert.sameValue(result.epochNanoseconds, 1610678096_987654321n); +assert.sameValue(calls, 1, "should have called dateAdd"); diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/subtract/calendar-dateadd.js b/test/built-ins/Temporal/ZonedDateTime/prototype/subtract/calendar-dateadd.js new file mode 100644 index 0000000000..ca29a9cfcc --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/subtract/calendar-dateadd.js @@ -0,0 +1,30 @@ +// 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.zoneddatetime.prototype.subtract +description: ZonedDateTime.prototype.subtract should call dateAdd with the appropriate values. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +let calls = 0; +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + dateAdd(plainDate, duration, options) { + ++calls; + TemporalHelpers.assertPlainDate(plainDate, 2020, 3, "M03", 14, "plainDate argument"); + TemporalHelpers.assertDuration(duration, 0, -10, 0, 0, 0, 0, 0, 0, 0, 0, "duration argument"); + assert.sameValue(typeof options, "object", "options argument: type"); + assert.sameValue(Object.getPrototypeOf(options), null, "options argument: prototype"); + return super.dateAdd(plainDate, duration, options); + } +} + +const zonedDateTime = new Temporal.ZonedDateTime(1584189296_987654321n, + new Temporal.TimeZone("UTC"), new CustomCalendar()); +const result = zonedDateTime.subtract({ months: 10, hours: 14 }); +assert.sameValue(result.epochNanoseconds, 1557786896_987654321n); +assert.sameValue(calls, 1, "should have called dateAdd");