Skip to content

Commit 5676bcf

Browse files
committed
Add basic tests for Intl.DurationFormat accepting Temporal.Duration
1 parent 6310295 commit 5676bcf

8 files changed

+389
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2025 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-Intl.DurationFormat.prototype.format
6+
description: >
7+
Ensure Temporal.Duration.prototype getters aren't called.
8+
features: [Temporal, Intl.DurationFormat]
9+
---*/
10+
11+
var duration = new Temporal.Duration(
12+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
13+
);
14+
15+
var formatter = new Intl.DurationFormat();
16+
17+
var expected = formatter.format(duration);
18+
19+
// Taint all Temporal.Duration.prototype getters.
20+
for (var prop of [
21+
"years",
22+
"months",
23+
"weeks",
24+
"days",
25+
"hours",
26+
"minutes",
27+
"seconds",
28+
"milliseconds",
29+
"microseconds",
30+
"nanoseconds",
31+
]) {
32+
// Ensure the property is present.
33+
var desc = Object.getOwnPropertyDescriptor(Temporal.Duration.prototype, prop);
34+
assert.notSameValue(
35+
desc,
36+
undefined,
37+
"Descriptor not found: " + prop
38+
);
39+
40+
Object.defineProperty(Temporal.Duration.prototype, prop, {
41+
get() {
42+
throw new Test262Error();
43+
}
44+
});
45+
}
46+
47+
var actual = formatter.format(duration);
48+
49+
assert.sameValue(actual, expected);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2025 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-Intl.DurationFormat.prototype.format
6+
description: >
7+
Temporal.Duration objects can be passed to format.
8+
features: [Temporal, Intl.DurationFormat]
9+
---*/
10+
11+
var durations = [
12+
{
13+
object: new Temporal.Duration(),
14+
durationLike: {
15+
years: 0,
16+
},
17+
},
18+
{
19+
object: new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
20+
durationLike: {
21+
years: 1,
22+
months: 2,
23+
weeks: 3,
24+
days: 4,
25+
hours: 5,
26+
minutes: 6,
27+
seconds: 7,
28+
milliseconds: 8,
29+
microseconds: 9,
30+
nanoseconds: 10,
31+
},
32+
},
33+
];
34+
35+
var formatter = new Intl.DurationFormat();
36+
37+
for (var {object, durationLike} of durations) {
38+
var expected = formatter.format(durationLike);
39+
var actual = formatter.format(object);
40+
assert.sameValue(actual, expected, `"${object}"`);
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2025 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-Intl.DurationFormat.prototype.format
6+
description: >
7+
Temporal duration strings can be passed to format.
8+
features: [Temporal, Intl.DurationFormat]
9+
---*/
10+
11+
var durations = [
12+
{
13+
string: "PT0S",
14+
durationLike: {
15+
years: 0,
16+
},
17+
},
18+
{
19+
string: "P1Y2M3W4DT5H6M7.00800901S",
20+
durationLike: {
21+
years: 1,
22+
months: 2,
23+
weeks: 3,
24+
days: 4,
25+
hours: 5,
26+
minutes: 6,
27+
seconds: 7,
28+
milliseconds: 8,
29+
microseconds: 9,
30+
nanoseconds: 10,
31+
},
32+
},
33+
];
34+
35+
var formatter = new Intl.DurationFormat();
36+
37+
for (var {string, durationLike} of durations) {
38+
var expected = formatter.format(durationLike);
39+
var actual = formatter.format(string);
40+
assert.sameValue(actual, expected, `"${string}"`);
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2025 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-Intl.DurationFormat.prototype.formatToParts
6+
description: >
7+
Ensure Temporal.Duration.prototype getters aren't called.
8+
features: [Temporal, Intl.DurationFormat]
9+
---*/
10+
11+
function assertSameParts(actual, expected) {
12+
assert.sameValue(actual.length, expected.length);
13+
14+
for (var i = 0; i < actual.length; ++i) {
15+
assert.sameValue(actual[i].type, expected[i].type);
16+
assert.sameValue(actual[i].value, expected[i].value);
17+
assert.sameValue(actual[i].unit, expected[i].unit);
18+
}
19+
}
20+
21+
var duration = new Temporal.Duration(
22+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
23+
);
24+
25+
var formatter = new Intl.DurationFormat();
26+
27+
var expected = formatter.formatToParts(duration);
28+
29+
// Taint all Temporal.Duration.prototype getters.
30+
for (var prop of [
31+
"years",
32+
"months",
33+
"weeks",
34+
"days",
35+
"hours",
36+
"minutes",
37+
"seconds",
38+
"milliseconds",
39+
"microseconds",
40+
"nanoseconds",
41+
]) {
42+
// Ensure the property is present.
43+
var desc = Object.getOwnPropertyDescriptor(Temporal.Duration.prototype, prop);
44+
assert.notSameValue(
45+
desc,
46+
undefined,
47+
"Descriptor not found: " + prop
48+
);
49+
50+
Object.defineProperty(Temporal.Duration.prototype, prop, {
51+
get() {
52+
throw new Test262Error();
53+
}
54+
});
55+
}
56+
57+
var actual = formatter.formatToParts(duration);
58+
59+
assertSameParts(actual, expected);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2025 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-Intl.DurationFormat.prototype.formatToParts
6+
description: >
7+
Temporal.Duration objects can be passed to formatToParts.
8+
features: [Temporal, Intl.DurationFormat]
9+
---*/
10+
11+
function assertSameParts(actual, expected) {
12+
assert.sameValue(actual.length, expected.length);
13+
14+
for (var i = 0; i < actual.length; ++i) {
15+
assert.sameValue(actual[i].type, expected[i].type);
16+
assert.sameValue(actual[i].value, expected[i].value);
17+
assert.sameValue(actual[i].unit, expected[i].unit);
18+
}
19+
}
20+
21+
var durations = [
22+
{
23+
object: new Temporal.Duration(),
24+
durationLike: {
25+
years: 0,
26+
},
27+
},
28+
{
29+
object: new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
30+
durationLike: {
31+
years: 1,
32+
months: 2,
33+
weeks: 3,
34+
days: 4,
35+
hours: 5,
36+
minutes: 6,
37+
seconds: 7,
38+
milliseconds: 8,
39+
microseconds: 9,
40+
nanoseconds: 10,
41+
},
42+
},
43+
];
44+
45+
var formatter = new Intl.DurationFormat();
46+
47+
for (var {object, durationLike} of durations) {
48+
var expected = formatter.format(durationLike);
49+
var actual = formatter.format(object);
50+
assertSameParts(actual, expected);
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2025 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-Intl.DurationFormat.prototype.formatToParts
6+
description: >
7+
Temporal duration strings can be passed to formatToParts.
8+
features: [Temporal, Intl.DurationFormat]
9+
---*/
10+
11+
function assertSameParts(actual, expected) {
12+
assert.sameValue(actual.length, expected.length);
13+
14+
for (var i = 0; i < actual.length; ++i) {
15+
assert.sameValue(actual[i].type, expected[i].type);
16+
assert.sameValue(actual[i].value, expected[i].value);
17+
assert.sameValue(actual[i].unit, expected[i].unit);
18+
}
19+
}
20+
21+
var durations = [
22+
{
23+
string: "PT0S",
24+
durationLike: {
25+
years: 0,
26+
},
27+
},
28+
{
29+
string: "P1Y2M3W4DT5H6M7.00800901S",
30+
durationLike: {
31+
years: 1,
32+
months: 2,
33+
weeks: 3,
34+
days: 4,
35+
hours: 5,
36+
minutes: 6,
37+
seconds: 7,
38+
milliseconds: 8,
39+
microseconds: 9,
40+
nanoseconds: 10,
41+
},
42+
},
43+
];
44+
45+
var formatter = new Intl.DurationFormat();
46+
47+
for (var {string, durationLike} of durations) {
48+
var expected = formatter.format(durationLike);
49+
var actual = formatter.format(string);
50+
assertSameParts(actual, expected);
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2025 André Bargull. 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.tolocalestring
6+
description: >
7+
Tests that Temporal.Duration.prototype.toLocaleString produces the same
8+
results as Intl.DurationFormat.
9+
features: [Temporal, Intl.DurationFormat]
10+
---*/
11+
12+
var durationLike = {
13+
years: 1,
14+
months: 2,
15+
weeks: 3,
16+
days: 4,
17+
hours: 5,
18+
minutes: 6,
19+
seconds: 7,
20+
milliseconds: 8,
21+
microseconds: 9,
22+
nanoseconds: 10,
23+
};
24+
25+
var duration = Temporal.Duration.from(durationLike);
26+
27+
var locales = [
28+
undefined,
29+
"en",
30+
"de",
31+
"th-u-nu-thai",
32+
["ar-u-nu-arab"],
33+
];
34+
35+
var options = [
36+
undefined,
37+
{style: "long"},
38+
];
39+
40+
for (var locale of locales) {
41+
for (var opts of options) {
42+
var formatter = new Intl.DurationFormat(locale, opts);
43+
44+
assert.sameValue(
45+
duration.toLocaleString(locale, opts),
46+
formatter.format(durationLike),
47+
`locale="${locale}", options="${JSON.stringify(opts)}", duration="${JSON.stringify(duration)}"`
48+
);
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2025 André Bargull. 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.tolocalestring
6+
description: >
7+
Ensure Temporal.Duration.prototype getters aren't called.
8+
features: [Temporal, Intl.DurationFormat]
9+
---*/
10+
11+
var duration = new Temporal.Duration(
12+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
13+
);
14+
15+
var expected = duration.toLocaleString();
16+
17+
// Taint all Temporal.Duration.prototype getters.
18+
for (var prop of [
19+
"years",
20+
"months",
21+
"weeks",
22+
"days",
23+
"hours",
24+
"minutes",
25+
"seconds",
26+
"milliseconds",
27+
"microseconds",
28+
"nanoseconds",
29+
]) {
30+
// Ensure the property is present.
31+
var desc = Object.getOwnPropertyDescriptor(Temporal.Duration.prototype, prop);
32+
assert.notSameValue(
33+
desc,
34+
undefined,
35+
"Descriptor not found: " + prop
36+
);
37+
38+
Object.defineProperty(Temporal.Duration.prototype, prop, {
39+
get() {
40+
throw new Test262Error();
41+
}
42+
});
43+
}
44+
45+
var actual = duration.toLocaleString();
46+
47+
assert.sameValue(actual, expected);

0 commit comments

Comments
 (0)