Intl.RelativeTimeFormat: Add some tests for the 'narrow' style.

This commit is contained in:
Ms2ger 2018-08-01 17:53:30 +02:00 committed by Rick Waldron
parent 1e8d69f0fa
commit 48d95ac3c4
2 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,41 @@
// Copyright 2018 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.RelativeTimeFormat.prototype.format
description: Checks the behavior of Intl.RelativeTimeFormat.prototype.format() in English.
features: [Intl.RelativeTimeFormat]
locale: [en-US]
---*/
const units = {
"second": "sec.",
"minute": "min.",
"hour": "hr.",
"day": undefined,
"week": "wk.",
"month": "mo.",
"quarter": "qtr.",
"year": "yr.",
};
const rtf = new Intl.RelativeTimeFormat("en-US", {
"style": "short",
});
assert.sameValue(typeof rtf.format, "function", "format should be supported");
for (const [unitArgument, unitString] of Object.entries(units)) {
const singular = unitString || `${unitArgument}`;
const plural = unitString || `${unitArgument}s`;
assert.sameValue(rtf.format(1000, unitArgument), `in 1,000 ${plural}`);
assert.sameValue(rtf.format(10, unitArgument), `in 10 ${plural}`);
assert.sameValue(rtf.format(2, unitArgument), `in 2 ${plural}`);
assert.sameValue(rtf.format(1, unitArgument), `in 1 ${singular}`);
assert.sameValue(rtf.format(0, unitArgument), `in 0 ${plural}`);
assert.sameValue(rtf.format(-0, unitArgument), `0 ${plural} ago`);
assert.sameValue(rtf.format(-1, unitArgument), `1 ${singular} ago`);
assert.sameValue(rtf.format(-2, unitArgument), `2 ${plural} ago`);
assert.sameValue(rtf.format(-10, unitArgument), `10 ${plural} ago`);
assert.sameValue(rtf.format(-1000, unitArgument), `1,000 ${plural} ago`);
}

View File

@ -0,0 +1,96 @@
// Copyright 2018 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.RelativeTimeFormat.prototype.formatToParts
description: Checks the behavior of Intl.RelativeTimeFormat.prototype.formatToParts() in English.
features: [Intl.RelativeTimeFormat]
locale: [en-US]
---*/
function verifyFormatParts(actual, expected, message) {
assert.sameValue(actual.length, expected.length, `${message}: length`);
for (let i = 0; i < actual.length; ++i) {
assert.sameValue(actual[i].type, expected[i].type, `${message}: parts[${i}].type`);
assert.sameValue(actual[i].value, expected[i].value, `${message}: parts[${i}].value`);
assert.sameValue(actual[i].unit, expected[i].unit, `${message}: parts[${i}].unit`);
}
}
const units = {
"second": "sec.",
"minute": "min.",
"hour": "hr.",
"day": undefined,
"week": "wk.",
"month": "mo.",
"quarter": "qtr.",
"year": "yr.",
};
const rtf = new Intl.RelativeTimeFormat("en-US", {
"style": "short",
});
assert.sameValue(typeof rtf.formatToParts, "function", "formatToParts should be supported");
for (const [unitArgument, unitString] of Object.entries(units)) {
const singular = unitString || `${unitArgument}`;
const plural = unitString || `${unitArgument}s`;
verifyFormatParts(rtf.formatToParts(1000, unitArgument), [
{ "type": "literal", "value": "in " },
{ "type": "integer", "value": "1,000", "unit": unitArgument },
{ "type": "literal", "value": ` ${plural}` },
], `formatToParts(1000, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(10, unitArgument), [
{ "type": "literal", "value": "in " },
{ "type": "integer", "value": "10", "unit": unitArgument },
{ "type": "literal", "value": ` ${plural}` },
], `formatToParts(10, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(2, unitArgument), [
{ "type": "literal", "value": "in " },
{ "type": "integer", "value": "2", "unit": unitArgument },
{ "type": "literal", "value": ` ${plural}` },
], `formatToParts(2, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(1, unitArgument), [
{ "type": "literal", "value": "in " },
{ "type": "integer", "value": "1", "unit": unitArgument },
{ "type": "literal", "value": ` ${singular}` },
], `formatToParts(1, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(0, unitArgument), [
{ "type": "literal", "value": "in " },
{ "type": "integer", "value": "0", "unit": unitArgument },
{ "type": "literal", "value": ` ${plural}` },
], `formatToParts(0, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(-0, unitArgument), [
{ "type": "integer", "value": "0", "unit": unitArgument },
{ "type": "literal", "value": ` ${plural} ago` },
], `formatToParts(-0, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(-1, unitArgument), [
{ "type": "integer", "value": "1", "unit": unitArgument },
{ "type": "literal", "value": ` ${singular} ago` },
], `formatToParts(-1, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(-2, unitArgument), [
{ "type": "integer", "value": "2", "unit": unitArgument },
{ "type": "literal", "value": ` ${plural} ago` },
], `formatToParts(-2, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(-10, unitArgument), [
{ "type": "integer", "value": "10", "unit": unitArgument },
{ "type": "literal", "value": ` ${plural} ago` },
], `formatToParts(-10, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(-1000, unitArgument), [
{ "type": "integer", "value": "1,000", "unit": unitArgument },
{ "type": "literal", "value": ` ${plural} ago` },
], `formatToParts(-1000, ${unitArgument})`);
}