Add basic tests for Intl.DurationFormat accepting Temporal.Duration

This commit is contained in:
André Bargull 2025-02-14 16:02:43 +01:00 committed by Philip Chimento
parent 6310295c80
commit e8c771e9df
8 changed files with 389 additions and 0 deletions

View File

@ -0,0 +1,49 @@
// Copyright 2025 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DurationFormat.prototype.format
description: >
Ensure Temporal.Duration.prototype getters aren't called.
features: [Temporal, Intl.DurationFormat]
---*/
var duration = new Temporal.Duration(
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
);
var formatter = new Intl.DurationFormat();
var expected = formatter.format(duration);
// Taint all Temporal.Duration.prototype getters.
for (var prop of [
"years",
"months",
"weeks",
"days",
"hours",
"minutes",
"seconds",
"milliseconds",
"microseconds",
"nanoseconds",
]) {
// Ensure the property is present.
var desc = Object.getOwnPropertyDescriptor(Temporal.Duration.prototype, prop);
assert.notSameValue(
desc,
undefined,
"Descriptor not found: " + prop
);
Object.defineProperty(Temporal.Duration.prototype, prop, {
get() {
throw new Test262Error();
}
});
}
var actual = formatter.format(duration);
assert.sameValue(actual, expected);

View File

@ -0,0 +1,41 @@
// Copyright 2025 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DurationFormat.prototype.format
description: >
Temporal.Duration objects can be passed to format.
features: [Temporal, Intl.DurationFormat]
---*/
var durations = [
{
object: new Temporal.Duration(),
durationLike: {
years: 0,
},
},
{
object: new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
durationLike: {
years: 1,
months: 2,
weeks: 3,
days: 4,
hours: 5,
minutes: 6,
seconds: 7,
milliseconds: 8,
microseconds: 9,
nanoseconds: 10,
},
},
];
var formatter = new Intl.DurationFormat();
for (var {object, durationLike} of durations) {
var expected = formatter.format(durationLike);
var actual = formatter.format(object);
assert.sameValue(actual, expected, `"${object}"`);
}

View File

@ -0,0 +1,41 @@
// Copyright 2025 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DurationFormat.prototype.format
description: >
Temporal duration strings can be passed to format.
features: [Temporal, Intl.DurationFormat]
---*/
var durations = [
{
string: "PT0S",
durationLike: {
years: 0,
},
},
{
string: "P1Y2M3W4DT5H6M7.00800901S",
durationLike: {
years: 1,
months: 2,
weeks: 3,
days: 4,
hours: 5,
minutes: 6,
seconds: 7,
milliseconds: 8,
microseconds: 9,
nanoseconds: 10,
},
},
];
var formatter = new Intl.DurationFormat();
for (var {string, durationLike} of durations) {
var expected = formatter.format(durationLike);
var actual = formatter.format(string);
assert.sameValue(actual, expected, `"${string}"`);
}

View File

@ -0,0 +1,59 @@
// Copyright 2025 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DurationFormat.prototype.formatToParts
description: >
Ensure Temporal.Duration.prototype getters aren't called.
features: [Temporal, Intl.DurationFormat]
---*/
function assertSameParts(actual, expected) {
assert.sameValue(actual.length, expected.length);
for (var i = 0; i < actual.length; ++i) {
assert.sameValue(actual[i].type, expected[i].type);
assert.sameValue(actual[i].value, expected[i].value);
assert.sameValue(actual[i].unit, expected[i].unit);
}
}
var duration = new Temporal.Duration(
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
);
var formatter = new Intl.DurationFormat();
var expected = formatter.formatToParts(duration);
// Taint all Temporal.Duration.prototype getters.
for (var prop of [
"years",
"months",
"weeks",
"days",
"hours",
"minutes",
"seconds",
"milliseconds",
"microseconds",
"nanoseconds",
]) {
// Ensure the property is present.
var desc = Object.getOwnPropertyDescriptor(Temporal.Duration.prototype, prop);
assert.notSameValue(
desc,
undefined,
"Descriptor not found: " + prop
);
Object.defineProperty(Temporal.Duration.prototype, prop, {
get() {
throw new Test262Error();
}
});
}
var actual = formatter.formatToParts(duration);
assertSameParts(actual, expected);

View File

@ -0,0 +1,51 @@
// Copyright 2025 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DurationFormat.prototype.formatToParts
description: >
Temporal.Duration objects can be passed to formatToParts.
features: [Temporal, Intl.DurationFormat]
---*/
function assertSameParts(actual, expected) {
assert.sameValue(actual.length, expected.length);
for (var i = 0; i < actual.length; ++i) {
assert.sameValue(actual[i].type, expected[i].type);
assert.sameValue(actual[i].value, expected[i].value);
assert.sameValue(actual[i].unit, expected[i].unit);
}
}
var durations = [
{
object: new Temporal.Duration(),
durationLike: {
years: 0,
},
},
{
object: new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
durationLike: {
years: 1,
months: 2,
weeks: 3,
days: 4,
hours: 5,
minutes: 6,
seconds: 7,
milliseconds: 8,
microseconds: 9,
nanoseconds: 10,
},
},
];
var formatter = new Intl.DurationFormat();
for (var {object, durationLike} of durations) {
var expected = formatter.format(durationLike);
var actual = formatter.format(object);
assertSameParts(actual, expected);
}

View File

@ -0,0 +1,51 @@
// Copyright 2025 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DurationFormat.prototype.formatToParts
description: >
Temporal duration strings can be passed to formatToParts.
features: [Temporal, Intl.DurationFormat]
---*/
function assertSameParts(actual, expected) {
assert.sameValue(actual.length, expected.length);
for (var i = 0; i < actual.length; ++i) {
assert.sameValue(actual[i].type, expected[i].type);
assert.sameValue(actual[i].value, expected[i].value);
assert.sameValue(actual[i].unit, expected[i].unit);
}
}
var durations = [
{
string: "PT0S",
durationLike: {
years: 0,
},
},
{
string: "P1Y2M3W4DT5H6M7.00800901S",
durationLike: {
years: 1,
months: 2,
weeks: 3,
days: 4,
hours: 5,
minutes: 6,
seconds: 7,
milliseconds: 8,
microseconds: 9,
nanoseconds: 10,
},
},
];
var formatter = new Intl.DurationFormat();
for (var {string, durationLike} of durations) {
var expected = formatter.format(durationLike);
var actual = formatter.format(string);
assertSameParts(actual, expected);
}

View File

@ -0,0 +1,50 @@
// Copyright 2025 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.tolocalestring
description: >
Tests that Temporal.Duration.prototype.toLocaleString produces the same
results as Intl.DurationFormat.
features: [Temporal, Intl.DurationFormat]
---*/
var durationLike = {
years: 1,
months: 2,
weeks: 3,
days: 4,
hours: 5,
minutes: 6,
seconds: 7,
milliseconds: 8,
microseconds: 9,
nanoseconds: 10,
};
var duration = Temporal.Duration.from(durationLike);
var locales = [
undefined,
"en",
"de",
"th-u-nu-thai",
["ar-u-nu-arab"],
];
var options = [
undefined,
{style: "long"},
];
for (var locale of locales) {
for (var opts of options) {
var formatter = new Intl.DurationFormat(locale, opts);
assert.sameValue(
duration.toLocaleString(locale, opts),
formatter.format(durationLike),
`locale="${locale}", options="${JSON.stringify(opts)}", duration="${JSON.stringify(duration)}"`
);
}
}

View File

@ -0,0 +1,47 @@
// Copyright 2025 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.tolocalestring
description: >
Ensure Temporal.Duration.prototype getters aren't called.
features: [Temporal, Intl.DurationFormat]
---*/
var duration = new Temporal.Duration(
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
);
var expected = duration.toLocaleString();
// Taint all Temporal.Duration.prototype getters.
for (var prop of [
"years",
"months",
"weeks",
"days",
"hours",
"minutes",
"seconds",
"milliseconds",
"microseconds",
"nanoseconds",
]) {
// Ensure the property is present.
var desc = Object.getOwnPropertyDescriptor(Temporal.Duration.prototype, prop);
assert.notSameValue(
desc,
undefined,
"Descriptor not found: " + prop
);
Object.defineProperty(Temporal.Duration.prototype, prop, {
get() {
throw new Test262Error();
}
});
}
var actual = duration.toLocaleString();
assert.sameValue(actual, expected);