Skip to content

Commit

Permalink
Temporal: Tests for PlainDateTime exposed in DisambiguatePossibleInst…
Browse files Browse the repository at this point in the history
…ants

In the AO DisambiguatePossibleInstants, a PlainDateTime instance is passed
to user code. This instance should have the built-in ISO 8601 calendar.
Here are some tests that ensure it does.

See tc39/proposal-temporal#2671.
  • Loading branch information
ptomato committed Oct 12, 2023
1 parent 95d9f02 commit 8f1e7cd
Show file tree
Hide file tree
Showing 20 changed files with 1,114 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.duration.prototype.compare
description: >
Time zone's getPossibleInstantsFor is called with a PlainDateTime with the
built-in ISO 8601 calendar
features: [Temporal]
info: |
DisambiguatePossibleInstants:
2. Let _n_ be _possibleInstants_'s length.
...
5. Assert: _n_ = 0.
...
19. If _disambiguation_ is *"earlier"*, then
...
c. Let _earlierDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
d. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _earlierDateTime_).
...
20. Assert: _disambiguation_ is *"compatible"* or *"later"*.
...
23. Let _laterDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
24. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _laterDateTime_).
---*/

class SkippedDateTime extends Temporal.TimeZone {
constructor() {
super("UTC");
this.calls = 0;
}

getPossibleInstantsFor(dateTime) {
// Calls occur in pairs. For the first one return no possible instants so
// that DisambiguatePossibleInstants will call it again
if (this.calls++ % 2 == 0) {
return [];
}

assert.sameValue(
dateTime.getISOFields().calendar,
"iso8601",
"getPossibleInstantsFor called with dateTime with built-in ISO 8601 calendar"
);
return super.getPossibleInstantsFor(dateTime);
}
}

const nonBuiltinISOCalendar = new Temporal.Calendar("iso8601");
const timeZone = new SkippedDateTime();
const relativeTo = { year: 2000, month: 5, day: 2, timeZone, calendar: nonBuiltinISOCalendar };

Temporal.Duration.compare(new Temporal.Duration(1), new Temporal.Duration(2), { relativeTo });

assert.sameValue(timeZone.calls, 6, "getPossibleInstantsFor should have been called 6 times");
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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.duration.prototype.add
description: >
Time zone's getPossibleInstantsFor is called with a PlainDateTime with the
built-in ISO 8601 calendar
features: [Temporal]
info: |
DisambiguatePossibleInstants:
2. Let _n_ be _possibleInstants_'s length.
...
5. Assert: _n_ = 0.
...
19. If _disambiguation_ is *"earlier"*, then
...
c. Let _earlierDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
d. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _earlierDateTime_).
...
20. Assert: _disambiguation_ is *"compatible"* or *"later"*.
...
23. Let _laterDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
24. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _laterDateTime_).
---*/

class SkippedDateTime extends Temporal.TimeZone {
constructor() {
super("UTC");
this.calls = 0;
}

getPossibleInstantsFor(dateTime) {
// Calls occur in pairs. For the first one return no possible instants so
// that DisambiguatePossibleInstants will call it again
if (this.calls++ % 2 == 0) {
return [];
}

assert.sameValue(
dateTime.getISOFields().calendar,
"iso8601",
"getPossibleInstantsFor called with dateTime with built-in ISO 8601 calendar"
);
return super.getPossibleInstantsFor(dateTime);
}
}

const nonBuiltinISOCalendar = new Temporal.Calendar("iso8601");
const timeZone = new SkippedDateTime();
const relativeTo = { year: 2000, month: 5, day: 2, timeZone, calendar: nonBuiltinISOCalendar };

const instance = new Temporal.Duration(1, 0, 0, 1);
instance.add(new Temporal.Duration(0, 0, 0, 0, -24), { relativeTo });

assert.sameValue(timeZone.calls, 6, "getPossibleInstantsFor should have been called 6 times");
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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.duration.prototype.round
description: >
Time zone's getPossibleInstantsFor is called with a PlainDateTime with the
built-in ISO 8601 calendar
features: [Temporal]
info: |
DisambiguatePossibleInstants:
2. Let _n_ be _possibleInstants_'s length.
...
5. Assert: _n_ = 0.
...
19. If _disambiguation_ is *"earlier"*, then
...
c. Let _earlierDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
d. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _earlierDateTime_).
...
20. Assert: _disambiguation_ is *"compatible"* or *"later"*.
...
23. Let _laterDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
24. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _laterDateTime_).
---*/

class SkippedDateTime extends Temporal.TimeZone {
constructor() {
super("UTC");
this.calls = 0;
}

getPossibleInstantsFor(dateTime) {
// Calls occur in pairs. For the first one return no possible instants so
// that DisambiguatePossibleInstants will call it again
if (this.calls++ % 2 == 0) {
return [];
}

assert.sameValue(
dateTime.getISOFields().calendar,
"iso8601",
"getPossibleInstantsFor called with dateTime with built-in ISO 8601 calendar"
);
return super.getPossibleInstantsFor(dateTime);
}
}

const nonBuiltinISOCalendar = new Temporal.Calendar("iso8601");
const timeZone = new SkippedDateTime();
const relativeTo = { year: 2000, month: 5, day: 2, timeZone, calendar: nonBuiltinISOCalendar };

const instance = new Temporal.Duration(1, 0, 0, 0, 24);
instance.round({ largestUnit: "years", relativeTo });

assert.sameValue(timeZone.calls, 6, "getPossibleInstantsFor should have been called 6 times");
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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.duration.prototype.subtract
description: >
Time zone's getPossibleInstantsFor is called with a PlainDateTime with the
built-in ISO 8601 calendar
features: [Temporal]
info: |
DisambiguatePossibleInstants:
2. Let _n_ be _possibleInstants_'s length.
...
5. Assert: _n_ = 0.
...
19. If _disambiguation_ is *"earlier"*, then
...
c. Let _earlierDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
d. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _earlierDateTime_).
...
20. Assert: _disambiguation_ is *"compatible"* or *"later"*.
...
23. Let _laterDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
24. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _laterDateTime_).
---*/

class SkippedDateTime extends Temporal.TimeZone {
constructor() {
super("UTC");
this.calls = 0;
}

getPossibleInstantsFor(dateTime) {
// Calls occur in pairs. For the first one return no possible instants so
// that DisambiguatePossibleInstants will call it again
if (this.calls++ % 2 == 0) {
return [];
}

assert.sameValue(
dateTime.getISOFields().calendar,
"iso8601",
"getPossibleInstantsFor called with dateTime with built-in ISO 8601 calendar"
);
return super.getPossibleInstantsFor(dateTime);
}
}

const nonBuiltinISOCalendar = new Temporal.Calendar("iso8601");
const timeZone = new SkippedDateTime();
const relativeTo = { year: 2000, month: 5, day: 2, timeZone, calendar: nonBuiltinISOCalendar };

const instance = new Temporal.Duration(1, 0, 0, 1);
instance.subtract(new Temporal.Duration(0, 0, 0, 0, 24), { relativeTo });

assert.sameValue(timeZone.calls, 6, "getPossibleInstantsFor should have been called 6 times");
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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.duration.prototype.total
description: >
Time zone's getPossibleInstantsFor is called with a PlainDateTime with the
built-in ISO 8601 calendar
features: [Temporal]
info: |
DisambiguatePossibleInstants:
2. Let _n_ be _possibleInstants_'s length.
...
5. Assert: _n_ = 0.
...
19. If _disambiguation_ is *"earlier"*, then
...
c. Let _earlierDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
d. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _earlierDateTime_).
...
20. Assert: _disambiguation_ is *"compatible"* or *"later"*.
...
23. Let _laterDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
24. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _laterDateTime_).
---*/

class SkippedDateTime extends Temporal.TimeZone {
constructor() {
super("UTC");
this.calls = 0;
}

getPossibleInstantsFor(dateTime) {
// Calls occur in pairs. For the first one return no possible instants so
// that DisambiguatePossibleInstants will call it again
if (this.calls++ % 2 == 0) {
return [];
}

assert.sameValue(
dateTime.getISOFields().calendar,
"iso8601",
"getPossibleInstantsFor called with dateTime with built-in ISO 8601 calendar"
);
return super.getPossibleInstantsFor(dateTime);
}
}

const nonBuiltinISOCalendar = new Temporal.Calendar("iso8601");
const timeZone = new SkippedDateTime();
const relativeTo = { year: 2000, month: 5, day: 2, timeZone, calendar: nonBuiltinISOCalendar };

const instance = new Temporal.Duration(1, 0, 0, 0, 24);
instance.total({ unit: "days", relativeTo });

assert.sameValue(timeZone.calls, 10, "getPossibleInstantsFor should have been called 10 times");
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.plaindate.prototype.tozoneddatetime
description: >
Time zone's getPossibleInstantsFor is called with a PlainDateTime with the
built-in ISO 8601 calendar
features: [Temporal]
info: |
DisambiguatePossibleInstants:
2. Let _n_ be _possibleInstants_'s length.
...
5. Assert: _n_ = 0.
...
19. If _disambiguation_ is *"earlier"*, then
...
c. Let _earlierDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
d. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _earlierDateTime_).
...
20. Assert: _disambiguation_ is *"compatible"* or *"later"*.
...
23. Let _laterDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
24. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _laterDateTime_).
---*/

class SkippedDateTime extends Temporal.TimeZone {
constructor() {
super("UTC");
this.calls = 0;
}

getPossibleInstantsFor(dateTime) {
// Calls occur in pairs. For the first one return no possible instants so
// that DisambiguatePossibleInstants will call it again
if (this.calls++ % 2 == 0) {
return [];
}

assert.sameValue(
dateTime.getISOFields().calendar,
"iso8601",
"getPossibleInstantsFor called with dateTime with built-in ISO 8601 calendar"
);
return super.getPossibleInstantsFor(dateTime);
}
}

const nonBuiltinISOCalendar = new Temporal.Calendar("iso8601");
const timeZone = new SkippedDateTime();

const instance = new Temporal.PlainDate(2000, 5, 2, nonBuiltinISOCalendar);
instance.toZonedDateTime(timeZone);

assert.sameValue(timeZone.calls, 2, "getPossibleInstantsFor should have been called 2 times");
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.plaindatetime.prototype.tozoneddatetime
description: >
Time zone's getPossibleInstantsFor is called with a PlainDateTime with the
built-in ISO 8601 calendar
features: [Temporal]
info: |
DisambiguatePossibleInstants:
2. Let _n_ be _possibleInstants_'s length.
...
5. Assert: _n_ = 0.
...
19. If _disambiguation_ is *"earlier"*, then
...
c. Let _earlierDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
d. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _earlierDateTime_).
...
20. Assert: _disambiguation_ is *"compatible"* or *"later"*.
...
23. Let _laterDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
24. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _laterDateTime_).
---*/

class SkippedDateTime extends Temporal.TimeZone {
constructor() {
super("UTC");
this.calls = 0;
}

getPossibleInstantsFor(dateTime) {
// Calls occur in pairs. For the first one return no possible instants so
// that DisambiguatePossibleInstants will call it again
if (this.calls++ % 2 == 0) {
return [];
}

assert.sameValue(
dateTime.getISOFields().calendar,
"iso8601",
"getPossibleInstantsFor called with dateTime with built-in ISO 8601 calendar"
);
return super.getPossibleInstantsFor(dateTime);
}
}

const nonBuiltinISOCalendar = new Temporal.Calendar("iso8601");
const timeZone = new SkippedDateTime();

const instance = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 0, 0, 0, nonBuiltinISOCalendar);
instance.toZonedDateTime(timeZone);

assert.sameValue(timeZone.calls, 2, "getPossibleInstantsFor should have been called 2 times");
Loading

0 comments on commit 8f1e7cd

Please sign in to comment.