Fix formatToParts expectation @ RelativeTimeFormat

Fix wrong expectations of
Intl.RelativeTimeFormat.prototype.formatToParts
Add tests to cover "group", "decimal" and "fraction".
This commit is contained in:
Frank Tang 2018-10-29 15:10:29 -07:00 committed by Rick Waldron
parent 412c8b5dce
commit d38e98d1f3
6 changed files with 110 additions and 12 deletions

View File

@ -36,7 +36,9 @@ assert.sameValue(typeof rtf.formatToParts, "function", "formatToParts should be
for (const unit of units) { for (const unit of units) {
verifyFormatParts(rtf.formatToParts(1000, unit), [ verifyFormatParts(rtf.formatToParts(1000, unit), [
{ "type": "literal", "value": "in " }, { "type": "literal", "value": "in " },
{ "type": "integer", "value": "1,000", "unit": unit }, { "type": "integer", "value": "1", "unit": unit },
{ "type": "group", "value": ",", "unit": unit },
{ "type": "integer", "value": "000", "unit": unit },
{ "type": "literal", "value": ` ${unit}s` }, { "type": "literal", "value": ` ${unit}s` },
], `formatToParts(1000, ${unit})`); ], `formatToParts(1000, ${unit})`);
@ -85,7 +87,19 @@ for (const unit of units) {
], `formatToParts(-10, ${unit})`); ], `formatToParts(-10, ${unit})`);
verifyFormatParts(rtf.formatToParts(-1000, unit), [ verifyFormatParts(rtf.formatToParts(-1000, unit), [
{ "type": "integer", "value": "1,000", "unit": unit }, { "type": "integer", "value": "1", "unit": unit },
{ "type": "group", "value": ",", "unit": unit },
{ "type": "integer", "value": "000", "unit": unit },
{ "type": "literal", "value": ` ${unit}s ago` }, { "type": "literal", "value": ` ${unit}s ago` },
], `formatToParts(-1000, ${unit})`); ], `formatToParts(-1000, ${unit})`);
verifyFormatParts(rtf.formatToParts(123456.78, unit), [
{ "type": "literal", "value": "in " },
{ "type": "integer", "value": "123", "unit": unit },
{ "type": "group", "value": ",", "unit": unit },
{ "type": "integer", "value": "456", "unit": unit },
{ "type": "decimal", "value": ".", "unit": unit },
{ "type": "fraction", "value": "78", "unit": unit },
{ "type": "literal", "value": ` ${unit}s` },
], `formatToParts(123456.78, ${unit})`);
} }

View File

@ -79,7 +79,9 @@ assert.sameValue(typeof rtf.formatToParts, "function", "formatToParts should be
for (const unit of units) { for (const unit of units) {
verifyFormatParts(rtf.formatToParts(1000, unit), [ verifyFormatParts(rtf.formatToParts(1000, unit), [
{ "type": "literal", "value": "in " }, { "type": "literal", "value": "in " },
{ "type": "integer", "value": "1,000", "unit": unit }, { "type": "integer", "value": "1", "unit": unit },
{ "type": "group", "value": ",", "unit": unit },
{ "type": "integer", "value": "000", "unit": unit },
{ "type": "literal", "value": ` ${unit}s` }, { "type": "literal", "value": ` ${unit}s` },
], `formatToParts(1000, ${unit})`); ], `formatToParts(1000, ${unit})`);
@ -128,7 +130,19 @@ for (const unit of units) {
], `formatToParts(-10, ${unit})`); ], `formatToParts(-10, ${unit})`);
verifyFormatParts(rtf.formatToParts(-1000, unit), [ verifyFormatParts(rtf.formatToParts(-1000, unit), [
{ "type": "integer", "value": "1,000", "unit": unit }, { "type": "integer", "value": "1", "unit": unit },
{ "type": "group", "value": ",", "unit": unit },
{ "type": "integer", "value": "000", "unit": unit },
{ "type": "literal", "value": ` ${unit}s ago` }, { "type": "literal", "value": ` ${unit}s ago` },
], `formatToParts(-1000, ${unit})`); ], `formatToParts(-1000, ${unit})`);
verifyFormatParts(rtf.formatToParts(123456.78, unit), [
{ "type": "literal", "value": "in " },
{ "type": "integer", "value": "123", "unit": unit },
{ "type": "group", "value": ",", "unit": unit },
{ "type": "integer", "value": "456", "unit": unit },
{ "type": "decimal", "value": ".", "unit": unit },
{ "type": "fraction", "value": "78", "unit": unit },
{ "type": "literal", "value": ` ${unit}s` },
], `formatToParts(123456.78, ${unit})`);
} }

View File

@ -40,7 +40,9 @@ for (const [unitArgument, unitStrings] of Object.entries(units)) {
verifyFormatParts(rtf.formatToParts(1000, unitArgument), [ verifyFormatParts(rtf.formatToParts(1000, unitArgument), [
{ "type": "literal", "value": "in " }, { "type": "literal", "value": "in " },
{ "type": "integer", "value": "1,000", "unit": unitArgument }, { "type": "integer", "value": "1", "unit": unitArgument },
{ "type": "group", "value": ",", "unit": unitArgument },
{ "type": "integer", "value": "000", "unit": unitArgument },
{ "type": "literal", "value": ` ${plural}` }, { "type": "literal", "value": ` ${plural}` },
], `formatToParts(1000, ${unitArgument})`); ], `formatToParts(1000, ${unitArgument})`);
@ -89,7 +91,19 @@ for (const [unitArgument, unitStrings] of Object.entries(units)) {
], `formatToParts(-10, ${unitArgument})`); ], `formatToParts(-10, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(-1000, unitArgument), [ verifyFormatParts(rtf.formatToParts(-1000, unitArgument), [
{ "type": "integer", "value": "1,000", "unit": unitArgument }, { "type": "integer", "value": "1", "unit": unitArgument },
{ "type": "group", "value": ",", "unit": unitArgument },
{ "type": "integer", "value": "000", "unit": unitArgument },
{ "type": "literal", "value": ` ${plural} ago` }, { "type": "literal", "value": ` ${plural} ago` },
], `formatToParts(-1000, ${unitArgument})`); ], `formatToParts(-1000, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(123456.78, unitArgument), [
{ "type": "literal", "value": "in " },
{ "type": "integer", "value": "123", "unit": unitArgument },
{ "type": "group", "value": ",", "unit": unitArgument },
{ "type": "integer", "value": "456", "unit": unitArgument },
{ "type": "decimal", "value": ".", "unit": unitArgument },
{ "type": "fraction", "value": "78", "unit": unitArgument },
{ "type": "literal", "value": ` ${plural}` },
], `formatToParts(123456.78, ${unitArgument})`);
} }

View File

@ -23,6 +23,7 @@ function regular(s) {
"many": s, "many": s,
"few": s + "y", "few": s + "y",
"one": s + "ę", "one": s + "ę",
"other": s + "y",
} }
} }
@ -35,27 +36,32 @@ const units = {
"many": "dni", "many": "dni",
"few": "dni", "few": "dni",
"one": "dzień", "one": "dzień",
"other": "dnia",
}, },
"week": { "week": {
"many": "tygodni", "many": "tygodni",
"few": "tygodnie", "few": "tygodnie",
"one": "tydzień", "one": "tydzień",
"other": "tygodnia",
}, },
"month": { "month": {
1000: "miesięcy", 1000: "miesięcy",
"many": "miesięcy", "many": "miesięcy",
"few": "miesiące", "few": "miesiące",
"one": "miesiąc", "one": "miesiąc",
"other": "miesiąca",
}, },
"quarter": { "quarter": {
"many": "kwartałów", "many": "kwartałów",
"few": "kwartały", "few": "kwartały",
"one": "kwartał", "one": "kwartał",
"other": "kwartału",
}, },
"year": { "year": {
"many": "lat", "many": "lat",
"few": "lata", "few": "lata",
"one": "rok", "one": "rok",
"other": "roku",
}, },
}; };
@ -68,7 +74,9 @@ assert.sameValue(typeof rtf.formatToParts, "function", "formatToParts should be
for (const [unitArgument, expected] of Object.entries(units)) { for (const [unitArgument, expected] of Object.entries(units)) {
verifyFormatParts(rtf.formatToParts(1000, unitArgument), [ verifyFormatParts(rtf.formatToParts(1000, unitArgument), [
{ "type": "literal", "value": "za " }, { "type": "literal", "value": "za " },
{ "type": "integer", "value": "1\u00a0000", "unit": unitArgument }, { "type": "integer", "value": "1", "unit": unitArgument },
{ "type": "group", "value": "\u00a0", "unit": unitArgument },
{ "type": "integer", "value": "000", "unit": unitArgument },
{ "type": "literal", "value": ` ${expected.many}` }, { "type": "literal", "value": ` ${expected.many}` },
], `formatToParts(1000, ${unitArgument})`); ], `formatToParts(1000, ${unitArgument})`);
@ -117,7 +125,19 @@ for (const [unitArgument, expected] of Object.entries(units)) {
], `formatToParts(-10, ${unitArgument})`); ], `formatToParts(-10, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(-1000, unitArgument), [ verifyFormatParts(rtf.formatToParts(-1000, unitArgument), [
{ "type": "integer", "value": "1\u00a0000", "unit": unitArgument }, { "type": "integer", "value": "1", "unit": unitArgument },
{ "type": "group", "value": "\u00a0", "unit": unitArgument },
{ "type": "integer", "value": "000", "unit": unitArgument },
{ "type": "literal", "value": ` ${expected.many} temu` }, { "type": "literal", "value": ` ${expected.many} temu` },
], `formatToParts(-1000, ${unitArgument})`); ], `formatToParts(-1000, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(123456.78, unitArgument), [
{ "type": "literal", "value": "za " },
{ "type": "integer", "value": "123", "unit": unitArgument },
{ "type": "group", "value": "\u00a0", "unit": unitArgument },
{ "type": "integer", "value": "456", "unit": unitArgument },
{ "type": "decimal", "value": ",", "unit": unitArgument },
{ "type": "fraction", "value": "78", "unit": unitArgument },
{ "type": "literal", "value": ` ${expected.other}` },
], `formatToParts(123456.78, ${unitArgument})`);
} }

View File

@ -23,6 +23,7 @@ function always(s) {
"many": s, "many": s,
"few": s, "few": s,
"one": s, "one": s,
"other": s,
} }
} }
@ -35,11 +36,13 @@ const units = {
"many": "dni", "many": "dni",
"few": "dni", "few": "dni",
"one": "dzień", "one": "dzień",
"other": "dnia",
}, },
"week": { "week": {
"many": "tyg.", "many": "tyg.",
"few": "tyg.", "few": "tyg.",
"one": "tydz.", "one": "tydz.",
"other": "tyg.",
}, },
"month": always("mies."), "month": always("mies."),
"quarter": always("kw."), "quarter": always("kw."),
@ -47,6 +50,7 @@ const units = {
"many": "lat", "many": "lat",
"few": "lata", "few": "lata",
"one": "rok", "one": "rok",
"other": "roku",
}, },
}; };
@ -59,7 +63,9 @@ assert.sameValue(typeof rtf.formatToParts, "function", "formatToParts should be
for (const [unitArgument, expected] of Object.entries(units)) { for (const [unitArgument, expected] of Object.entries(units)) {
verifyFormatParts(rtf.formatToParts(1000, unitArgument), [ verifyFormatParts(rtf.formatToParts(1000, unitArgument), [
{ "type": "literal", "value": "za " }, { "type": "literal", "value": "za " },
{ "type": "integer", "value": "1\u00a0000", "unit": unitArgument }, { "type": "integer", "value": "1", "unit": unitArgument },
{ "type": "group", "value": "\u00a0", "unit": unitArgument },
{ "type": "integer", "value": "000", "unit": unitArgument },
{ "type": "literal", "value": ` ${expected.many}` }, { "type": "literal", "value": ` ${expected.many}` },
], `formatToParts(1000, ${unitArgument})`); ], `formatToParts(1000, ${unitArgument})`);
@ -108,7 +114,19 @@ for (const [unitArgument, expected] of Object.entries(units)) {
], `formatToParts(-10, ${unitArgument})`); ], `formatToParts(-10, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(-1000, unitArgument), [ verifyFormatParts(rtf.formatToParts(-1000, unitArgument), [
{ "type": "integer", "value": "1\u00a0000", "unit": unitArgument }, { "type": "integer", "value": "1", "unit": unitArgument },
{ "type": "group", "value": "\u00a0", "unit": unitArgument },
{ "type": "integer", "value": "000", "unit": unitArgument },
{ "type": "literal", "value": ` ${expected.many} temu` }, { "type": "literal", "value": ` ${expected.many} temu` },
], `formatToParts(-1000, ${unitArgument})`); ], `formatToParts(-1000, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(123456.78, unitArgument), [
{ "type": "literal", "value": "za " },
{ "type": "integer", "value": "123", "unit": unitArgument },
{ "type": "group", "value": "\u00a0", "unit": unitArgument },
{ "type": "integer", "value": "456", "unit": unitArgument },
{ "type": "decimal", "value": ",", "unit": unitArgument },
{ "type": "fraction", "value": "78", "unit": unitArgument },
{ "type": "literal", "value": ` ${expected.other}` },
], `formatToParts(123456.78, ${unitArgument})`);
} }

View File

@ -23,6 +23,7 @@ function always(s) {
"many": s, "many": s,
"few": s, "few": s,
"one": s, "one": s,
"other": s,
} }
} }
@ -35,11 +36,13 @@ const units = {
"many": "dni", "many": "dni",
"few": "dni", "few": "dni",
"one": "dzień", "one": "dzień",
"other": "dnia",
}, },
"week": { "week": {
"many": "tyg.", "many": "tyg.",
"few": "tyg.", "few": "tyg.",
"one": "tydz.", "one": "tydz.",
"other": "tyg.",
}, },
"month": always("mies."), "month": always("mies."),
"quarter": always("kw."), "quarter": always("kw."),
@ -47,6 +50,7 @@ const units = {
"many": "lat", "many": "lat",
"few": "lata", "few": "lata",
"one": "rok", "one": "rok",
"other": "roku",
}, },
}; };
@ -59,7 +63,9 @@ assert.sameValue(typeof rtf.formatToParts, "function", "formatToParts should be
for (const [unitArgument, expected] of Object.entries(units)) { for (const [unitArgument, expected] of Object.entries(units)) {
verifyFormatParts(rtf.formatToParts(1000, unitArgument), [ verifyFormatParts(rtf.formatToParts(1000, unitArgument), [
{ "type": "literal", "value": "za " }, { "type": "literal", "value": "za " },
{ "type": "integer", "value": "1\u00a0000", "unit": unitArgument }, { "type": "integer", "value": "1", "unit": unitArgument },
{ "type": "group", "value": "\u00a0", "unit": unitArgument },
{ "type": "integer", "value": "000", "unit": unitArgument },
{ "type": "literal", "value": ` ${expected.many}` }, { "type": "literal", "value": ` ${expected.many}` },
], `formatToParts(1000, ${unitArgument})`); ], `formatToParts(1000, ${unitArgument})`);
@ -108,7 +114,19 @@ for (const [unitArgument, expected] of Object.entries(units)) {
], `formatToParts(-10, ${unitArgument})`); ], `formatToParts(-10, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(-1000, unitArgument), [ verifyFormatParts(rtf.formatToParts(-1000, unitArgument), [
{ "type": "integer", "value": "1\u00a0000", "unit": unitArgument }, { "type": "integer", "value": "1", "unit": unitArgument },
{ "type": "group", "value": "\u00a0", "unit": unitArgument },
{ "type": "integer", "value": "000", "unit": unitArgument },
{ "type": "literal", "value": ` ${expected.many} temu` }, { "type": "literal", "value": ` ${expected.many} temu` },
], `formatToParts(-1000, ${unitArgument})`); ], `formatToParts(-1000, ${unitArgument})`);
verifyFormatParts(rtf.formatToParts(123456.78, unitArgument), [
{ "type": "literal", "value": "za " },
{ "type": "integer", "value": "123", "unit": unitArgument },
{ "type": "group", "value": "\u00a0", "unit": unitArgument },
{ "type": "integer", "value": "456", "unit": unitArgument },
{ "type": "decimal", "value": ",", "unit": unitArgument },
{ "type": "fraction", "value": "78", "unit": unitArgument },
{ "type": "literal", "value": ` ${expected.other}` },
], `formatToParts(123456.78, ${unitArgument})`);
} }