Test that non-undefined, non-zoned relativeTo parameters are converted to PlainDate

Tests for the normative changes made to Temporal in
https://github.com/tc39/proposal-temporal/pull/1873

This adds a new Temporal helper calendar that asserts that its dateAdd()
method is always called with a PlainDate instance. This allows testing
that relativeTo parameters are always converted to PlainDate if they are
not ZonedDateTime and not undefined. Prior to the normative PR, they
would be converted to PlainDateTime instead.

Additionally and optionally, the helper calendar can also assert that its
dateAdd() method is called with a specific PlainDate instance. This allows
testing that the instance is the same PlainDate passed as the relativeTo
parameter (in the case of Duration methods) or is the receiver (in the
case of PlainDate methods). For the PlainDateTime and PlainYearMonth
methods the PlainDate instance is synthesized internally so there is no
need to assert that dateAdd() is called with a specific instance.
This commit is contained in:
Philip Chimento 2021-11-24 10:50:26 -08:00 committed by Rick Waldron
parent 0184842b09
commit 8d025ef1d6
12 changed files with 217 additions and 0 deletions

View File

@ -951,6 +951,36 @@ var TemporalHelpers = {
return new CalendarDateAddUndefinedOptions();
},
/*
* A custom calendar that asserts its dateAdd() method is called with a
* PlainDate instance. Optionally, it also asserts that the PlainDate instance
* is the specific object `this.specificPlainDate`, if it is set by the
* calling code.
*/
calendarDateAddPlainDateInstance() {
class CalendarDateAddPlainDateInstance extends Temporal.Calendar {
constructor() {
super("iso8601");
this.dateAddCallCount = 0;
this.specificPlainDate = undefined;
}
toString() {
return "dateadd-plain-date-instance";
}
dateAdd(date, duration, options) {
this.dateAddCallCount++;
assert(date instanceof Temporal.PlainDate, "dateAdd() should be called with a PlainDate instance");
if (this.dateAddCallCount === 1 && this.specificPlainDate) {
assert.sameValue(date, this.specificPlainDate, `dateAdd() should be called first with the specific PlainDate instance ${this.specificPlainDate}`);
}
return super.dateAdd(date, duration, options);
}
}
return new CalendarDateAddPlainDateInstance();
},
/*
* A custom calendar that returns @returnValue from its dateUntil() method,
* recording the call in @calls.

View File

@ -0,0 +1,17 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.compare
description: >
relativeTo parameters that are not ZonedDateTime or undefined, are always
converted to PlainDate for observable calendar calls
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const calendar = TemporalHelpers.calendarDateAddPlainDateInstance();
const relativeTo = new Temporal.PlainDate(2000, 1, 1, calendar);
calendar.specificPlainDate = relativeTo;
Temporal.Duration.compare(new Temporal.Duration(1, 1, 1, 1), new Temporal.Duration(1, 1, 1), { relativeTo });
assert(calendar.dateAddCallCount > 0, "assertions in calendar.dateAdd() should have been tested");

View File

@ -0,0 +1,18 @@
// Copyright (C) 2021 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: >
relativeTo parameters that are not ZonedDateTime or undefined, are always
converted to PlainDate for observable calendar calls
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const calendar = TemporalHelpers.calendarDateAddPlainDateInstance();
const instance = new Temporal.Duration(1, 1, 1, 1);
const relativeTo = new Temporal.PlainDate(2000, 1, 1, calendar);
calendar.specificPlainDate = relativeTo;
instance.add(instance, { relativeTo });
assert(calendar.dateAddCallCount > 0, "assertions in calendar.dateAdd() should have been tested");

View File

@ -0,0 +1,18 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.round
description: >
relativeTo parameters that are not ZonedDateTime or undefined, are always
converted to PlainDate for observable calendar calls
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const calendar = TemporalHelpers.calendarDateAddPlainDateInstance();
const instance = new Temporal.Duration(1, 1, 1, 1);
const relativeTo = new Temporal.PlainDate(2000, 1, 1, calendar);
calendar.specificPlainDate = relativeTo;
instance.round({ largestUnit: "days", relativeTo });
assert(calendar.dateAddCallCount > 0, "assertions in calendar.dateAdd() should have been tested");

View File

@ -0,0 +1,18 @@
// Copyright (C) 2021 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: >
relativeTo parameters that are not ZonedDateTime or undefined, are always
converted to PlainDate for observable calendar calls
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const calendar = TemporalHelpers.calendarDateAddPlainDateInstance();
const instance = new Temporal.Duration(1, 1, 1, 1);
const relativeTo = new Temporal.PlainDate(2000, 1, 1, calendar);
calendar.specificPlainDate = relativeTo;
instance.subtract(new Temporal.Duration(-1, -1, -1, -1), { relativeTo });
assert(calendar.dateAddCallCount > 0, "assertions in calendar.dateAdd() should have been tested");

View File

@ -0,0 +1,18 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.total
description: >
relativeTo parameters that are not ZonedDateTime or undefined, are always
converted to PlainDate for observable calendar calls
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const calendar = TemporalHelpers.calendarDateAddPlainDateInstance();
const instance = new Temporal.Duration(1, 1, 1, 1);
const relativeTo = new Temporal.PlainDate(2000, 1, 1, calendar);
calendar.specificPlainDate = relativeTo;
instance.total({ unit: "days", relativeTo });
assert(calendar.dateAddCallCount > 0, "assertions in calendar.dateAdd() should have been tested");

View File

@ -0,0 +1,17 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindate.prototype.since
description: >
relativeTo parameters that are not ZonedDateTime or undefined, are always
converted to PlainDate for observable calendar calls
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const calendar = TemporalHelpers.calendarDateAddPlainDateInstance();
const instance = new Temporal.PlainDate(1970, 1, 1, calendar);
calendar.specificPlainDate = instance;
instance.since(new Temporal.PlainDate(2000, 5, 2, calendar), { smallestUnit: "month" });
assert(calendar.dateAddCallCount > 0, "assertions in calendar.dateAdd() should have been tested");

View File

@ -0,0 +1,17 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindate.prototype.until
description: >
relativeTo parameters that are not ZonedDateTime or undefined, are always
converted to PlainDate for observable calendar calls
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const calendar = TemporalHelpers.calendarDateAddPlainDateInstance();
const instance = new Temporal.PlainDate(1970, 1, 1, calendar);
calendar.specificPlainDate = instance;
instance.until(new Temporal.PlainDate(2000, 5, 2, calendar), { smallestUnit: "month" });
assert(calendar.dateAddCallCount > 0, "assertions in calendar.dateAdd() should have been tested");

View File

@ -0,0 +1,16 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.prototype.since
description: >
relativeTo parameters that are not ZonedDateTime or undefined, are always
converted to PlainDate for observable calendar calls
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const calendar = TemporalHelpers.calendarDateAddPlainDateInstance();
const instance = new Temporal.PlainDateTime(1970, 1, 1, 0, 0, 0, 0, 0, 0, calendar);
instance.since(new Temporal.PlainDateTime(2000, 5, 2, 0, 0, 0, 0, 0, 0, calendar), { smallestUnit: "month" });
assert(calendar.dateAddCallCount > 0, "assertions in calendar.dateAdd() should have been tested");

View File

@ -0,0 +1,16 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.prototype.until
description: >
relativeTo parameters that are not ZonedDateTime or undefined, are always
converted to PlainDate for observable calendar calls
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const calendar = TemporalHelpers.calendarDateAddPlainDateInstance();
const instance = new Temporal.PlainDateTime(1970, 1, 1, 0, 0, 0, 0, 0, 0, calendar);
instance.until(new Temporal.PlainDateTime(2000, 5, 2, 0, 0, 0, 0, 0, 0, calendar), { smallestUnit: "month" });
assert(calendar.dateAddCallCount > 0, "assertions in calendar.dateAdd() should have been tested");

View File

@ -0,0 +1,16 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainyearmonth.prototype.since
description: >
relativeTo parameters that are not ZonedDateTime or undefined, are always
converted to PlainDate for observable calendar calls
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const calendar = TemporalHelpers.calendarDateAddPlainDateInstance();
const instance = new Temporal.PlainYearMonth(1970, 1, calendar);
instance.since(new Temporal.PlainYearMonth(2000, 5, calendar), { smallestUnit: "year" });
assert(calendar.dateAddCallCount > 0, "assertions in calendar.dateAdd() should have been tested");

View File

@ -0,0 +1,16 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainyearmonth.prototype.until
description: >
relativeTo parameters that are not ZonedDateTime or undefined, are always
converted to PlainDate for observable calendar calls
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const calendar = TemporalHelpers.calendarDateAddPlainDateInstance();
const instance = new Temporal.PlainYearMonth(1970, 1, calendar);
instance.until(new Temporal.PlainYearMonth(2000, 5, calendar), { smallestUnit: "year" });
assert(calendar.dateAddCallCount > 0, "assertions in calendar.dateAdd() should have been tested");