Skip to content

Commit b75ad5a

Browse files
ptomatojessealama
authored andcommitted
Tests for Temporal formatting the year appropriately as 4 or 6 digits
tc39/proposal-temporal#2090 is a normative change that reached consensus at the March 2022 TC39 plenary meeting. This adds tests that verify the change made to the formatting of years between 0 and 999 inclusive in all toString and toJSON methods of Temporal types that can output an ISO year number in their return value.
1 parent 7fb949e commit b75ad5a

File tree

12 files changed

+608
-0
lines changed

12 files changed

+608
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (C) 2022 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.instant.prototype.tojson
6+
description: Verify that the year is appropriately formatted as 4 or 6 digits
7+
features: [Temporal]
8+
---*/
9+
10+
function epochNsInYear(year) {
11+
// Return an epoch nanoseconds value near the middle of the given year
12+
const avgNsPerYear = 31_556_952_000_000_000n;
13+
return (year - 1970n) * avgNsPerYear + (avgNsPerYear / 2n);
14+
}
15+
16+
let instance = new Temporal.Instant(epochNsInYear(-100000n));
17+
assert.sameValue(instance.toJSON(), "-100000-07-01T21:30:36Z", "large negative year formatted as 6-digit");
18+
19+
instance = new Temporal.Instant(epochNsInYear(-10000n));
20+
assert.sameValue(instance.toJSON(), "-010000-07-01T21:30:36Z", "smallest 5-digit negative year formatted as 6-digit");
21+
22+
instance = new Temporal.Instant(epochNsInYear(-9999n));
23+
assert.sameValue(instance.toJSON(), "-009999-07-02T03:19:48Z", "largest 4-digit negative year formatted as 6-digit");
24+
25+
instance = new Temporal.Instant(epochNsInYear(-1000n));
26+
assert.sameValue(instance.toJSON(), "-001000-07-02T09:30:36Z", "smallest 4-digit negative year formatted as 6-digit");
27+
28+
instance = new Temporal.Instant(epochNsInYear(-999n));
29+
assert.sameValue(instance.toJSON(), "-000999-07-02T15:19:48Z", "largest 3-digit negative year formatted as 6-digit");
30+
31+
instance = new Temporal.Instant(epochNsInYear(-1n));
32+
assert.sameValue(instance.toJSON(), "-000001-07-02T15:41:24Z", "year -1 formatted as 6-digit");
33+
34+
instance = new Temporal.Instant(epochNsInYear(0n));
35+
assert.sameValue(instance.toJSON(), "0000-07-01T21:30:36Z", "year 0 formatted as 4-digit");
36+
37+
instance = new Temporal.Instant(epochNsInYear(1n));
38+
assert.sameValue(instance.toJSON(), "0001-07-02T03:19:48Z", "year 1 formatted as 4-digit");
39+
40+
instance = new Temporal.Instant(epochNsInYear(999n));
41+
assert.sameValue(instance.toJSON(), "0999-07-02T03:41:24Z", "largest 3-digit positive year formatted as 4-digit");
42+
43+
instance = new Temporal.Instant(epochNsInYear(1000n));
44+
assert.sameValue(instance.toJSON(), "1000-07-02T09:30:36Z", "smallest 4-digit positive year formatted as 4-digit");
45+
46+
instance = new Temporal.Instant(epochNsInYear(9999n));
47+
assert.sameValue(instance.toJSON(), "9999-07-02T15:41:24Z", "largest 4-digit positive year formatted as 4-digit");
48+
49+
instance = new Temporal.Instant(epochNsInYear(10000n));
50+
assert.sameValue(instance.toJSON(), "+010000-07-01T21:30:36Z", "smallest 5-digit positive year formatted as 6-digit");
51+
52+
instance = new Temporal.Instant(epochNsInYear(100000n));
53+
assert.sameValue(instance.toJSON(), "+100000-07-01T21:30:36Z", "large positive year formatted as 6-digit");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (C) 2022 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.instant.prototype.tostring
6+
description: Verify that the year is appropriately formatted as 4 or 6 digits
7+
features: [Temporal]
8+
---*/
9+
10+
function epochNsInYear(year) {
11+
// Return an epoch nanoseconds value near the middle of the given year
12+
const avgNsPerYear = 31_556_952_000_000_000n;
13+
return (year - 1970n) * avgNsPerYear + (avgNsPerYear / 2n);
14+
}
15+
16+
let instance = new Temporal.Instant(epochNsInYear(-100000n));
17+
assert.sameValue(instance.toString(), "-100000-07-01T21:30:36Z", "large negative year formatted as 6-digit");
18+
19+
instance = new Temporal.Instant(epochNsInYear(-10000n));
20+
assert.sameValue(instance.toString(), "-010000-07-01T21:30:36Z", "smallest 5-digit negative year formatted as 6-digit");
21+
22+
instance = new Temporal.Instant(epochNsInYear(-9999n));
23+
assert.sameValue(instance.toString(), "-009999-07-02T03:19:48Z", "largest 4-digit negative year formatted as 6-digit");
24+
25+
instance = new Temporal.Instant(epochNsInYear(-1000n));
26+
assert.sameValue(instance.toString(), "-001000-07-02T09:30:36Z", "smallest 4-digit negative year formatted as 6-digit");
27+
28+
instance = new Temporal.Instant(epochNsInYear(-999n));
29+
assert.sameValue(instance.toString(), "-000999-07-02T15:19:48Z", "largest 3-digit negative year formatted as 6-digit");
30+
31+
instance = new Temporal.Instant(epochNsInYear(-1n));
32+
assert.sameValue(instance.toString(), "-000001-07-02T15:41:24Z", "year -1 formatted as 6-digit");
33+
34+
instance = new Temporal.Instant(epochNsInYear(0n));
35+
assert.sameValue(instance.toString(), "0000-07-01T21:30:36Z", "year 0 formatted as 4-digit");
36+
37+
instance = new Temporal.Instant(epochNsInYear(1n));
38+
assert.sameValue(instance.toString(), "0001-07-02T03:19:48Z", "year 1 formatted as 4-digit");
39+
40+
instance = new Temporal.Instant(epochNsInYear(999n));
41+
assert.sameValue(instance.toString(), "0999-07-02T03:41:24Z", "largest 3-digit positive year formatted as 4-digit");
42+
43+
instance = new Temporal.Instant(epochNsInYear(1000n));
44+
assert.sameValue(instance.toString(), "1000-07-02T09:30:36Z", "smallest 4-digit positive year formatted as 4-digit");
45+
46+
instance = new Temporal.Instant(epochNsInYear(9999n));
47+
assert.sameValue(instance.toString(), "9999-07-02T15:41:24Z", "largest 4-digit positive year formatted as 4-digit");
48+
49+
instance = new Temporal.Instant(epochNsInYear(10000n));
50+
assert.sameValue(instance.toString(), "+010000-07-01T21:30:36Z", "smallest 5-digit positive year formatted as 6-digit");
51+
52+
instance = new Temporal.Instant(epochNsInYear(100000n));
53+
assert.sameValue(instance.toString(), "+100000-07-01T21:30:36Z", "large positive year formatted as 6-digit");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (C) 2022 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.tojson
6+
description: Verify that the year is appropriately formatted as 4 or 6 digits
7+
features: [Temporal]
8+
---*/
9+
10+
let instance = new Temporal.PlainDate(-100000, 12, 3);
11+
assert.sameValue(instance.toJSON(), "-100000-12-03", "large negative year formatted as 6-digit");
12+
13+
instance = new Temporal.PlainDate(-10000, 4, 5);
14+
assert.sameValue(instance.toJSON(), "-010000-04-05", "smallest 5-digit negative year formatted as 6-digit");
15+
16+
instance = new Temporal.PlainDate(-9999, 6, 7);
17+
assert.sameValue(instance.toJSON(), "-009999-06-07", "largest 4-digit negative year formatted as 6-digit");
18+
19+
instance = new Temporal.PlainDate(-1000, 8, 9);
20+
assert.sameValue(instance.toJSON(), "-001000-08-09", "smallest 4-digit negative year formatted as 6-digit");
21+
22+
instance = new Temporal.PlainDate(-999, 10, 9);
23+
assert.sameValue(instance.toJSON(), "-000999-10-09", "largest 3-digit negative year formatted as 6-digit");
24+
25+
instance = new Temporal.PlainDate(-1, 8, 7);
26+
assert.sameValue(instance.toJSON(), "-000001-08-07", "year -1 formatted as 6-digit");
27+
28+
instance = new Temporal.PlainDate(0, 6, 5);
29+
assert.sameValue(instance.toJSON(), "0000-06-05", "year 0 formatted as 4-digit");
30+
31+
instance = new Temporal.PlainDate(1, 4, 3);
32+
assert.sameValue(instance.toJSON(), "0001-04-03", "year 1 formatted as 4-digit");
33+
34+
instance = new Temporal.PlainDate(999, 2, 10);
35+
assert.sameValue(instance.toJSON(), "0999-02-10", "largest 3-digit positive year formatted as 4-digit");
36+
37+
instance = new Temporal.PlainDate(1000, 1, 23);
38+
assert.sameValue(instance.toJSON(), "1000-01-23", "smallest 4-digit positive year formatted as 4-digit");
39+
40+
instance = new Temporal.PlainDate(9999, 4, 5);
41+
assert.sameValue(instance.toJSON(), "9999-04-05", "largest 4-digit positive year formatted as 4-digit");
42+
43+
instance = new Temporal.PlainDate(10000, 6, 7);
44+
assert.sameValue(instance.toJSON(), "+010000-06-07", "smallest 5-digit positive year formatted as 6-digit");
45+
46+
instance = new Temporal.PlainDate(100000, 8, 9);
47+
assert.sameValue(instance.toJSON(), "+100000-08-09", "large positive year formatted as 6-digit");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (C) 2022 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.tostring
6+
description: Verify that the year is appropriately formatted as 4 or 6 digits
7+
features: [Temporal]
8+
---*/
9+
10+
let instance = new Temporal.PlainDate(-100000, 12, 3);
11+
assert.sameValue(instance.toString(), "-100000-12-03", "large negative year formatted as 6-digit");
12+
13+
instance = new Temporal.PlainDate(-10000, 4, 5);
14+
assert.sameValue(instance.toString(), "-010000-04-05", "smallest 5-digit negative year formatted as 6-digit");
15+
16+
instance = new Temporal.PlainDate(-9999, 6, 7);
17+
assert.sameValue(instance.toString(), "-009999-06-07", "largest 4-digit negative year formatted as 6-digit");
18+
19+
instance = new Temporal.PlainDate(-1000, 8, 9);
20+
assert.sameValue(instance.toString(), "-001000-08-09", "smallest 4-digit negative year formatted as 6-digit");
21+
22+
instance = new Temporal.PlainDate(-999, 10, 9);
23+
assert.sameValue(instance.toString(), "-000999-10-09", "largest 3-digit negative year formatted as 6-digit");
24+
25+
instance = new Temporal.PlainDate(-1, 8, 7);
26+
assert.sameValue(instance.toString(), "-000001-08-07", "year -1 formatted as 6-digit");
27+
28+
instance = new Temporal.PlainDate(0, 6, 5);
29+
assert.sameValue(instance.toString(), "0000-06-05", "year 0 formatted as 4-digit");
30+
31+
instance = new Temporal.PlainDate(1, 4, 3);
32+
assert.sameValue(instance.toString(), "0001-04-03", "year 1 formatted as 4-digit");
33+
34+
instance = new Temporal.PlainDate(999, 2, 10);
35+
assert.sameValue(instance.toString(), "0999-02-10", "largest 3-digit positive year formatted as 4-digit");
36+
37+
instance = new Temporal.PlainDate(1000, 1, 23);
38+
assert.sameValue(instance.toString(), "1000-01-23", "smallest 4-digit positive year formatted as 4-digit");
39+
40+
instance = new Temporal.PlainDate(9999, 4, 5);
41+
assert.sameValue(instance.toString(), "9999-04-05", "largest 4-digit positive year formatted as 4-digit");
42+
43+
instance = new Temporal.PlainDate(10000, 6, 7);
44+
assert.sameValue(instance.toString(), "+010000-06-07", "smallest 5-digit positive year formatted as 6-digit");
45+
46+
instance = new Temporal.PlainDate(100000, 8, 9);
47+
assert.sameValue(instance.toString(), "+100000-08-09", "large positive year formatted as 6-digit");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (C) 2022 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.tojson
6+
description: Verify that the year is appropriately formatted as 4 or 6 digits
7+
features: [Temporal]
8+
---*/
9+
10+
let instance = new Temporal.PlainDateTime(-100000, 12, 3, 4, 56, 7, 890);
11+
assert.sameValue(instance.toJSON(), "-100000-12-03T04:56:07.89", "large negative year formatted as 6-digit");
12+
13+
instance = new Temporal.PlainDateTime(-10000, 4, 5, 6, 7, 8, 910);
14+
assert.sameValue(instance.toJSON(), "-010000-04-05T06:07:08.91", "smallest 5-digit negative year formatted as 6-digit");
15+
16+
instance = new Temporal.PlainDateTime(-9999, 6, 7, 8, 9, 10, 987);
17+
assert.sameValue(instance.toJSON(), "-009999-06-07T08:09:10.987", "largest 4-digit negative year formatted as 6-digit");
18+
19+
instance = new Temporal.PlainDateTime(-1000, 8, 9, 10, 9, 8, 765);
20+
assert.sameValue(instance.toJSON(), "-001000-08-09T10:09:08.765", "smallest 4-digit negative year formatted as 6-digit");
21+
22+
instance = new Temporal.PlainDateTime(-999, 10, 9, 8, 7, 6, 543);
23+
assert.sameValue(instance.toJSON(), "-000999-10-09T08:07:06.543", "largest 3-digit negative year formatted as 6-digit");
24+
25+
instance = new Temporal.PlainDateTime(-1, 8, 7, 6, 54, 32, 100);
26+
assert.sameValue(instance.toJSON(), "-000001-08-07T06:54:32.1", "year -1 formatted as 6-digit");
27+
28+
instance = new Temporal.PlainDateTime(0, 6, 5, 4, 32, 10, 123);
29+
assert.sameValue(instance.toJSON(), "0000-06-05T04:32:10.123", "year 0 formatted as 4-digit");
30+
31+
instance = new Temporal.PlainDateTime(1, 4, 3, 21, 0, 12, 345);
32+
assert.sameValue(instance.toJSON(), "0001-04-03T21:00:12.345", "year 1 formatted as 4-digit");
33+
34+
instance = new Temporal.PlainDateTime(999, 2, 10, 12, 34, 56, 789);
35+
assert.sameValue(instance.toJSON(), "0999-02-10T12:34:56.789", "largest 3-digit positive year formatted as 4-digit");
36+
37+
instance = new Temporal.PlainDateTime(1000, 1, 23, 4, 56, 7, 890);
38+
assert.sameValue(instance.toJSON(), "1000-01-23T04:56:07.89", "smallest 4-digit positive year formatted as 4-digit");
39+
40+
instance = new Temporal.PlainDateTime(9999, 4, 5, 6, 7, 8, 910);
41+
assert.sameValue(instance.toJSON(), "9999-04-05T06:07:08.91", "largest 4-digit positive year formatted as 4-digit");
42+
43+
instance = new Temporal.PlainDateTime(10000, 6, 7, 8, 9, 10, 987);
44+
assert.sameValue(instance.toJSON(), "+010000-06-07T08:09:10.987", "smallest 5-digit positive year formatted as 6-digit");
45+
46+
instance = new Temporal.PlainDateTime(100000, 8, 9, 10, 9, 8, 765);
47+
assert.sameValue(instance.toJSON(), "+100000-08-09T10:09:08.765", "large positive year formatted as 6-digit");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (C) 2022 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.tostring
6+
description: Verify that the year is appropriately formatted as 4 or 6 digits
7+
features: [Temporal]
8+
---*/
9+
10+
let instance = new Temporal.PlainDateTime(-100000, 12, 3, 4, 56, 7, 890);
11+
assert.sameValue(instance.toString(), "-100000-12-03T04:56:07.89", "large negative year formatted as 6-digit");
12+
13+
instance = new Temporal.PlainDateTime(-10000, 4, 5, 6, 7, 8, 910);
14+
assert.sameValue(instance.toString(), "-010000-04-05T06:07:08.91", "smallest 5-digit negative year formatted as 6-digit");
15+
16+
instance = new Temporal.PlainDateTime(-9999, 6, 7, 8, 9, 10, 987);
17+
assert.sameValue(instance.toString(), "-009999-06-07T08:09:10.987", "largest 4-digit negative year formatted as 6-digit");
18+
19+
instance = new Temporal.PlainDateTime(-1000, 8, 9, 10, 9, 8, 765);
20+
assert.sameValue(instance.toString(), "-001000-08-09T10:09:08.765", "smallest 4-digit negative year formatted as 6-digit");
21+
22+
instance = new Temporal.PlainDateTime(-999, 10, 9, 8, 7, 6, 543);
23+
assert.sameValue(instance.toString(), "-000999-10-09T08:07:06.543", "largest 3-digit negative year formatted as 6-digit");
24+
25+
instance = new Temporal.PlainDateTime(-1, 8, 7, 6, 54, 32, 100);
26+
assert.sameValue(instance.toString(), "-000001-08-07T06:54:32.1", "year -1 formatted as 6-digit");
27+
28+
instance = new Temporal.PlainDateTime(0, 6, 5, 4, 32, 10, 123);
29+
assert.sameValue(instance.toString(), "0000-06-05T04:32:10.123", "year 0 formatted as 4-digit");
30+
31+
instance = new Temporal.PlainDateTime(1, 4, 3, 21, 0, 12, 345);
32+
assert.sameValue(instance.toString(), "0001-04-03T21:00:12.345", "year 1 formatted as 4-digit");
33+
34+
instance = new Temporal.PlainDateTime(999, 2, 10, 12, 34, 56, 789);
35+
assert.sameValue(instance.toString(), "0999-02-10T12:34:56.789", "largest 3-digit positive year formatted as 4-digit");
36+
37+
instance = new Temporal.PlainDateTime(1000, 1, 23, 4, 56, 7, 890);
38+
assert.sameValue(instance.toString(), "1000-01-23T04:56:07.89", "smallest 4-digit positive year formatted as 4-digit");
39+
40+
instance = new Temporal.PlainDateTime(9999, 4, 5, 6, 7, 8, 910);
41+
assert.sameValue(instance.toString(), "9999-04-05T06:07:08.91", "largest 4-digit positive year formatted as 4-digit");
42+
43+
instance = new Temporal.PlainDateTime(10000, 6, 7, 8, 9, 10, 987);
44+
assert.sameValue(instance.toString(), "+010000-06-07T08:09:10.987", "smallest 5-digit positive year formatted as 6-digit");
45+
46+
instance = new Temporal.PlainDateTime(100000, 8, 9, 10, 9, 8, 765);
47+
assert.sameValue(instance.toString(), "+100000-08-09T10:09:08.765", "large positive year formatted as 6-digit");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (C) 2022 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.plainmonthday.prototype.tojson
6+
description: Verify that the year is appropriately formatted as 4 or 6 digits
7+
features: [Temporal]
8+
---*/
9+
10+
// For PlainMonthDay, the ISO reference year is only present in the string if
11+
// the calendar is not ISO 8601
12+
class NotISO extends Temporal.Calendar {
13+
constructor() { super("iso8601"); }
14+
toString() { return "not-iso"; }
15+
}
16+
const calendar = new NotISO();
17+
18+
let instance = new Temporal.PlainMonthDay(12, 3, calendar, -100000);
19+
assert.sameValue(instance.toJSON(), "-100000-12-03[u-ca=not-iso]", "large negative year formatted as 6-digit");
20+
21+
instance = new Temporal.PlainMonthDay(4, 5, calendar, -10000);
22+
assert.sameValue(instance.toJSON(), "-010000-04-05[u-ca=not-iso]", "smallest 5-digit negative year formatted as 6-digit");
23+
24+
instance = new Temporal.PlainMonthDay(6, 7, calendar, -9999);
25+
assert.sameValue(instance.toJSON(), "-009999-06-07[u-ca=not-iso]", "largest 4-digit negative year formatted as 6-digit");
26+
27+
instance = new Temporal.PlainMonthDay(8, 9, calendar, -1000);
28+
assert.sameValue(instance.toJSON(), "-001000-08-09[u-ca=not-iso]", "smallest 4-digit negative year formatted as 6-digit");
29+
30+
instance = new Temporal.PlainMonthDay(10, 9, calendar, -999);
31+
assert.sameValue(instance.toJSON(), "-000999-10-09[u-ca=not-iso]", "largest 3-digit negative year formatted as 6-digit");
32+
33+
instance = new Temporal.PlainMonthDay(8, 7, calendar, -1);
34+
assert.sameValue(instance.toJSON(), "-000001-08-07[u-ca=not-iso]", "year -1 formatted as 6-digit");
35+
36+
instance = new Temporal.PlainMonthDay(6, 5, calendar, 0);
37+
assert.sameValue(instance.toJSON(), "0000-06-05[u-ca=not-iso]", "year 0 formatted as 4-digit");
38+
39+
instance = new Temporal.PlainMonthDay(4, 3, calendar, 1);
40+
assert.sameValue(instance.toJSON(), "0001-04-03[u-ca=not-iso]", "year 1 formatted as 4-digit");
41+
42+
instance = new Temporal.PlainMonthDay(2, 10, calendar, 999);
43+
assert.sameValue(instance.toJSON(), "0999-02-10[u-ca=not-iso]", "largest 3-digit positive year formatted as 4-digit");
44+
45+
instance = new Temporal.PlainMonthDay(1, 23, calendar, 1000);
46+
assert.sameValue(instance.toJSON(), "1000-01-23[u-ca=not-iso]", "smallest 4-digit positive year formatted as 4-digit");
47+
48+
instance = new Temporal.PlainMonthDay(4, 5, calendar, 9999);
49+
assert.sameValue(instance.toJSON(), "9999-04-05[u-ca=not-iso]", "largest 4-digit positive year formatted as 4-digit");
50+
51+
instance = new Temporal.PlainMonthDay(6, 7, calendar, 10000);
52+
assert.sameValue(instance.toJSON(), "+010000-06-07[u-ca=not-iso]", "smallest 5-digit positive year formatted as 6-digit");
53+
54+
instance = new Temporal.PlainMonthDay(8, 9, calendar, 100000);
55+
assert.sameValue(instance.toJSON(), "+100000-08-09[u-ca=not-iso]", "large positive year formatted as 6-digit");

0 commit comments

Comments
 (0)