Skip to content

Commit 1f0fc4e

Browse files
committed
Temporal: Tests for PlainDateTime exposed in DisambiguatePossibleInstants
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.
1 parent 70c36b6 commit 1f0fc4e

File tree

20 files changed

+1114
-0
lines changed

20 files changed

+1114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.duration.prototype.compare
6+
description: >
7+
Time zone's getPossibleInstantsFor is called with a PlainDateTime with the
8+
built-in ISO 8601 calendar
9+
features: [Temporal]
10+
info: |
11+
DisambiguatePossibleInstants:
12+
2. Let _n_ be _possibleInstants_'s length.
13+
...
14+
5. Assert: _n_ = 0.
15+
...
16+
19. If _disambiguation_ is *"earlier"*, then
17+
...
18+
c. Let _earlierDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
19+
d. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _earlierDateTime_).
20+
...
21+
20. Assert: _disambiguation_ is *"compatible"* or *"later"*.
22+
...
23+
23. Let _laterDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
24+
24. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _laterDateTime_).
25+
---*/
26+
27+
class SkippedDateTime extends Temporal.TimeZone {
28+
constructor() {
29+
super("UTC");
30+
this.calls = 0;
31+
}
32+
33+
getPossibleInstantsFor(dateTime) {
34+
// Calls occur in pairs. For the first one return no possible instants so
35+
// that DisambiguatePossibleInstants will call it again
36+
if (this.calls++ % 2 == 0) {
37+
return [];
38+
}
39+
40+
assert.sameValue(
41+
dateTime.getISOFields().calendar,
42+
"iso8601",
43+
"getPossibleInstantsFor called with dateTime with built-in ISO 8601 calendar"
44+
);
45+
return super.getPossibleInstantsFor(dateTime);
46+
}
47+
}
48+
49+
const nonBuiltinISOCalendar = new Temporal.Calendar("iso8601");
50+
const timeZone = new SkippedDateTime();
51+
const relativeTo = { year: 2000, month: 5, day: 2, timeZone, calendar: nonBuiltinISOCalendar };
52+
53+
Temporal.Duration.compare(new Temporal.Duration(1), new Temporal.Duration(2), { relativeTo });
54+
55+
assert.sameValue(timeZone.calls, 6, "getPossibleInstantsFor should have been called 6 times");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.duration.prototype.add
6+
description: >
7+
Time zone's getPossibleInstantsFor is called with a PlainDateTime with the
8+
built-in ISO 8601 calendar
9+
features: [Temporal]
10+
info: |
11+
DisambiguatePossibleInstants:
12+
2. Let _n_ be _possibleInstants_'s length.
13+
...
14+
5. Assert: _n_ = 0.
15+
...
16+
19. If _disambiguation_ is *"earlier"*, then
17+
...
18+
c. Let _earlierDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
19+
d. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _earlierDateTime_).
20+
...
21+
20. Assert: _disambiguation_ is *"compatible"* or *"later"*.
22+
...
23+
23. Let _laterDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
24+
24. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _laterDateTime_).
25+
---*/
26+
27+
class SkippedDateTime extends Temporal.TimeZone {
28+
constructor() {
29+
super("UTC");
30+
this.calls = 0;
31+
}
32+
33+
getPossibleInstantsFor(dateTime) {
34+
// Calls occur in pairs. For the first one return no possible instants so
35+
// that DisambiguatePossibleInstants will call it again
36+
if (this.calls++ % 2 == 0) {
37+
return [];
38+
}
39+
40+
assert.sameValue(
41+
dateTime.getISOFields().calendar,
42+
"iso8601",
43+
"getPossibleInstantsFor called with dateTime with built-in ISO 8601 calendar"
44+
);
45+
return super.getPossibleInstantsFor(dateTime);
46+
}
47+
}
48+
49+
const nonBuiltinISOCalendar = new Temporal.Calendar("iso8601");
50+
const timeZone = new SkippedDateTime();
51+
const relativeTo = { year: 2000, month: 5, day: 2, timeZone, calendar: nonBuiltinISOCalendar };
52+
53+
const instance = new Temporal.Duration(1, 0, 0, 1);
54+
instance.add(new Temporal.Duration(0, 0, 0, 0, -24), { relativeTo });
55+
56+
assert.sameValue(timeZone.calls, 6, "getPossibleInstantsFor should have been called 6 times");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.duration.prototype.round
6+
description: >
7+
Time zone's getPossibleInstantsFor is called with a PlainDateTime with the
8+
built-in ISO 8601 calendar
9+
features: [Temporal]
10+
info: |
11+
DisambiguatePossibleInstants:
12+
2. Let _n_ be _possibleInstants_'s length.
13+
...
14+
5. Assert: _n_ = 0.
15+
...
16+
19. If _disambiguation_ is *"earlier"*, then
17+
...
18+
c. Let _earlierDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
19+
d. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _earlierDateTime_).
20+
...
21+
20. Assert: _disambiguation_ is *"compatible"* or *"later"*.
22+
...
23+
23. Let _laterDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
24+
24. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _laterDateTime_).
25+
---*/
26+
27+
class SkippedDateTime extends Temporal.TimeZone {
28+
constructor() {
29+
super("UTC");
30+
this.calls = 0;
31+
}
32+
33+
getPossibleInstantsFor(dateTime) {
34+
// Calls occur in pairs. For the first one return no possible instants so
35+
// that DisambiguatePossibleInstants will call it again
36+
if (this.calls++ % 2 == 0) {
37+
return [];
38+
}
39+
40+
assert.sameValue(
41+
dateTime.getISOFields().calendar,
42+
"iso8601",
43+
"getPossibleInstantsFor called with dateTime with built-in ISO 8601 calendar"
44+
);
45+
return super.getPossibleInstantsFor(dateTime);
46+
}
47+
}
48+
49+
const nonBuiltinISOCalendar = new Temporal.Calendar("iso8601");
50+
const timeZone = new SkippedDateTime();
51+
const relativeTo = { year: 2000, month: 5, day: 2, timeZone, calendar: nonBuiltinISOCalendar };
52+
53+
const instance = new Temporal.Duration(1, 0, 0, 0, 24);
54+
instance.round({ largestUnit: "years", relativeTo });
55+
56+
assert.sameValue(timeZone.calls, 6, "getPossibleInstantsFor should have been called 6 times");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.duration.prototype.subtract
6+
description: >
7+
Time zone's getPossibleInstantsFor is called with a PlainDateTime with the
8+
built-in ISO 8601 calendar
9+
features: [Temporal]
10+
info: |
11+
DisambiguatePossibleInstants:
12+
2. Let _n_ be _possibleInstants_'s length.
13+
...
14+
5. Assert: _n_ = 0.
15+
...
16+
19. If _disambiguation_ is *"earlier"*, then
17+
...
18+
c. Let _earlierDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
19+
d. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _earlierDateTime_).
20+
...
21+
20. Assert: _disambiguation_ is *"compatible"* or *"later"*.
22+
...
23+
23. Let _laterDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
24+
24. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _laterDateTime_).
25+
---*/
26+
27+
class SkippedDateTime extends Temporal.TimeZone {
28+
constructor() {
29+
super("UTC");
30+
this.calls = 0;
31+
}
32+
33+
getPossibleInstantsFor(dateTime) {
34+
// Calls occur in pairs. For the first one return no possible instants so
35+
// that DisambiguatePossibleInstants will call it again
36+
if (this.calls++ % 2 == 0) {
37+
return [];
38+
}
39+
40+
assert.sameValue(
41+
dateTime.getISOFields().calendar,
42+
"iso8601",
43+
"getPossibleInstantsFor called with dateTime with built-in ISO 8601 calendar"
44+
);
45+
return super.getPossibleInstantsFor(dateTime);
46+
}
47+
}
48+
49+
const nonBuiltinISOCalendar = new Temporal.Calendar("iso8601");
50+
const timeZone = new SkippedDateTime();
51+
const relativeTo = { year: 2000, month: 5, day: 2, timeZone, calendar: nonBuiltinISOCalendar };
52+
53+
const instance = new Temporal.Duration(1, 0, 0, 1);
54+
instance.subtract(new Temporal.Duration(0, 0, 0, 0, 24), { relativeTo });
55+
56+
assert.sameValue(timeZone.calls, 6, "getPossibleInstantsFor should have been called 6 times");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.duration.prototype.total
6+
description: >
7+
Time zone's getPossibleInstantsFor is called with a PlainDateTime with the
8+
built-in ISO 8601 calendar
9+
features: [Temporal]
10+
info: |
11+
DisambiguatePossibleInstants:
12+
2. Let _n_ be _possibleInstants_'s length.
13+
...
14+
5. Assert: _n_ = 0.
15+
...
16+
19. If _disambiguation_ is *"earlier"*, then
17+
...
18+
c. Let _earlierDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
19+
d. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _earlierDateTime_).
20+
...
21+
20. Assert: _disambiguation_ is *"compatible"* or *"later"*.
22+
...
23+
23. Let _laterDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
24+
24. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _laterDateTime_).
25+
---*/
26+
27+
class SkippedDateTime extends Temporal.TimeZone {
28+
constructor() {
29+
super("UTC");
30+
this.calls = 0;
31+
}
32+
33+
getPossibleInstantsFor(dateTime) {
34+
// Calls occur in pairs. For the first one return no possible instants so
35+
// that DisambiguatePossibleInstants will call it again
36+
if (this.calls++ % 2 == 0) {
37+
return [];
38+
}
39+
40+
assert.sameValue(
41+
dateTime.getISOFields().calendar,
42+
"iso8601",
43+
"getPossibleInstantsFor called with dateTime with built-in ISO 8601 calendar"
44+
);
45+
return super.getPossibleInstantsFor(dateTime);
46+
}
47+
}
48+
49+
const nonBuiltinISOCalendar = new Temporal.Calendar("iso8601");
50+
const timeZone = new SkippedDateTime();
51+
const relativeTo = { year: 2000, month: 5, day: 2, timeZone, calendar: nonBuiltinISOCalendar };
52+
53+
const instance = new Temporal.Duration(1, 0, 0, 0, 24);
54+
instance.total({ unit: "days", relativeTo });
55+
56+
assert.sameValue(timeZone.calls, 10, "getPossibleInstantsFor should have been called 10 times");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.plaindate.prototype.tozoneddatetime
6+
description: >
7+
Time zone's getPossibleInstantsFor is called with a PlainDateTime with the
8+
built-in ISO 8601 calendar
9+
features: [Temporal]
10+
info: |
11+
DisambiguatePossibleInstants:
12+
2. Let _n_ be _possibleInstants_'s length.
13+
...
14+
5. Assert: _n_ = 0.
15+
...
16+
19. If _disambiguation_ is *"earlier"*, then
17+
...
18+
c. Let _earlierDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
19+
d. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _earlierDateTime_).
20+
...
21+
20. Assert: _disambiguation_ is *"compatible"* or *"later"*.
22+
...
23+
23. Let _laterDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
24+
24. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _laterDateTime_).
25+
---*/
26+
27+
class SkippedDateTime extends Temporal.TimeZone {
28+
constructor() {
29+
super("UTC");
30+
this.calls = 0;
31+
}
32+
33+
getPossibleInstantsFor(dateTime) {
34+
// Calls occur in pairs. For the first one return no possible instants so
35+
// that DisambiguatePossibleInstants will call it again
36+
if (this.calls++ % 2 == 0) {
37+
return [];
38+
}
39+
40+
assert.sameValue(
41+
dateTime.getISOFields().calendar,
42+
"iso8601",
43+
"getPossibleInstantsFor called with dateTime with built-in ISO 8601 calendar"
44+
);
45+
return super.getPossibleInstantsFor(dateTime);
46+
}
47+
}
48+
49+
const nonBuiltinISOCalendar = new Temporal.Calendar("iso8601");
50+
const timeZone = new SkippedDateTime();
51+
52+
const instance = new Temporal.PlainDate(2000, 5, 2, nonBuiltinISOCalendar);
53+
instance.toZonedDateTime(timeZone);
54+
55+
assert.sameValue(timeZone.calls, 2, "getPossibleInstantsFor should have been called 2 times");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.plaindatetime.prototype.tozoneddatetime
6+
description: >
7+
Time zone's getPossibleInstantsFor is called with a PlainDateTime with the
8+
built-in ISO 8601 calendar
9+
features: [Temporal]
10+
info: |
11+
DisambiguatePossibleInstants:
12+
2. Let _n_ be _possibleInstants_'s length.
13+
...
14+
5. Assert: _n_ = 0.
15+
...
16+
19. If _disambiguation_ is *"earlier"*, then
17+
...
18+
c. Let _earlierDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
19+
d. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _earlierDateTime_).
20+
...
21+
20. Assert: _disambiguation_ is *"compatible"* or *"later"*.
22+
...
23+
23. Let _laterDateTime_ be ! CreateTemporalDateTime(..., *"iso8601"*).
24+
24. Set _possibleInstants_ to ? GetPossibleInstantsFor(_timeZone_, _laterDateTime_).
25+
---*/
26+
27+
class SkippedDateTime extends Temporal.TimeZone {
28+
constructor() {
29+
super("UTC");
30+
this.calls = 0;
31+
}
32+
33+
getPossibleInstantsFor(dateTime) {
34+
// Calls occur in pairs. For the first one return no possible instants so
35+
// that DisambiguatePossibleInstants will call it again
36+
if (this.calls++ % 2 == 0) {
37+
return [];
38+
}
39+
40+
assert.sameValue(
41+
dateTime.getISOFields().calendar,
42+
"iso8601",
43+
"getPossibleInstantsFor called with dateTime with built-in ISO 8601 calendar"
44+
);
45+
return super.getPossibleInstantsFor(dateTime);
46+
}
47+
}
48+
49+
const nonBuiltinISOCalendar = new Temporal.Calendar("iso8601");
50+
const timeZone = new SkippedDateTime();
51+
52+
const instance = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 0, 0, 0, nonBuiltinISOCalendar);
53+
instance.toZonedDateTime(timeZone);
54+
55+
assert.sameValue(timeZone.calls, 2, "getPossibleInstantsFor should have been called 2 times");

0 commit comments

Comments
 (0)