From 48d95ac3c4ac56018748c0d243c831f4c0bfb103 Mon Sep 17 00:00:00 2001 From: Ms2ger <Ms2ger@igalia.com> Date: Wed, 1 Aug 2018 17:53:30 +0200 Subject: [PATCH] Intl.RelativeTimeFormat: Add some tests for the 'narrow' style. --- .../prototype/format/en-us-style-short.js | 41 ++++++++ .../formatToParts/en-us-style-short.js | 96 +++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 test/intl402/RelativeTimeFormat/prototype/format/en-us-style-short.js create mode 100644 test/intl402/RelativeTimeFormat/prototype/formatToParts/en-us-style-short.js diff --git a/test/intl402/RelativeTimeFormat/prototype/format/en-us-style-short.js b/test/intl402/RelativeTimeFormat/prototype/format/en-us-style-short.js new file mode 100644 index 0000000000..d62341a937 --- /dev/null +++ b/test/intl402/RelativeTimeFormat/prototype/format/en-us-style-short.js @@ -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`); +} diff --git a/test/intl402/RelativeTimeFormat/prototype/formatToParts/en-us-style-short.js b/test/intl402/RelativeTimeFormat/prototype/formatToParts/en-us-style-short.js new file mode 100644 index 0000000000..b85809b5f5 --- /dev/null +++ b/test/intl402/RelativeTimeFormat/prototype/formatToParts/en-us-style-short.js @@ -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})`); +} -- GitLab