diff --git a/test/intl402/RelativeTimeFormat/prototype/format/en-us-numeric-always.js b/test/intl402/RelativeTimeFormat/prototype/format/en-us-numeric-always.js new file mode 100644 index 0000000000000000000000000000000000000000..58c3909d0814347ba02e244304bda76c8a191cdc --- /dev/null +++ b/test/intl402/RelativeTimeFormat/prototype/format/en-us-numeric-always.js @@ -0,0 +1,36 @@ +// 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", + "minute", + "hour", + "day", + "week", + "month", + "quarter", + "year", +]; + +const rtf = new Intl.RelativeTimeFormat("en-US"); + +assert.sameValue(typeof rtf.format, "function", "format should be supported"); + +for (const unit of units) { + // Note https://github.com/tc39/proposal-intl-relative-time/issues/80 + assert.sameValue(rtf.format(10, unit), `in 10 ${unit}s`); + assert.sameValue(rtf.format(2, unit), `in 2 ${unit}s`); + assert.sameValue(rtf.format(1, unit), `in 1 ${unit}`); + assert.sameValue(rtf.format(0, unit), `in 0 ${unit}s`); + assert.sameValue(rtf.format(-0, unit), `0 ${unit}s ago`); + assert.sameValue(rtf.format(-1, unit), `1 ${unit} ago`); + assert.sameValue(rtf.format(-2, unit), `2 ${unit}s ago`); + assert.sameValue(rtf.format(-10, unit), `10 ${unit}s ago`); +} diff --git a/test/intl402/RelativeTimeFormat/prototype/format/en-us-numeric-auto.js b/test/intl402/RelativeTimeFormat/prototype/format/en-us-numeric-auto.js new file mode 100644 index 0000000000000000000000000000000000000000..f4f8a1185b8ffee6a3e51ee10778840606ac0759 --- /dev/null +++ b/test/intl402/RelativeTimeFormat/prototype/format/en-us-numeric-auto.js @@ -0,0 +1,68 @@ +// 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", + "minute", + "hour", + "day", + "week", + "month", + "quarter", + "year", +]; + +const rtf = new Intl.RelativeTimeFormat("en-US", { "numeric": "auto" }); + +assert.sameValue(typeof rtf.format, "function", "format should be supported"); + +// https://www.unicode.org/cldr/charts/33/summary/en.html#1530 +const exceptions = { + "year": { + "-1": "last year", + "0": "this year", + "1": "next year", + }, + "month": { + "-1": "last month", + "0": "this month", + "1": "next month", + }, + "week": { + "-1": "last week", + "0": "this week", + "1": "next week", + }, + "day": { + "-1": "yesterday", + "0": "today", + "1": "tomorrow", + }, + "second": { + "-1": "1 second ago", + "0": "now", + "1": "in 1 second", + }, +}; + +for (const unit of units) { + const expected = unit in exceptions + ? [exceptions[unit]["1"], exceptions[unit]["0"], exceptions[unit]["0"], exceptions[unit]["-1"]] + : [`in 1 ${unit}`, `in 0 ${unit}s`, `0 ${unit}s ago`, `1 ${unit} ago`]; + // Note https://github.com/tc39/proposal-intl-relative-time/issues/80 + assert.sameValue(rtf.format(10, unit), `in 10 ${unit}s`); + assert.sameValue(rtf.format(2, unit), `in 2 ${unit}s`); + assert.sameValue(rtf.format(1, unit), expected[0]); + assert.sameValue(rtf.format(0, unit), expected[1]); + assert.sameValue(rtf.format(-0, unit), expected[2]); + assert.sameValue(rtf.format(-1, unit), expected[3]); + assert.sameValue(rtf.format(-2, unit), `2 ${unit}s ago`); + assert.sameValue(rtf.format(-10, unit), `10 ${unit}s ago`); +} diff --git a/test/intl402/RelativeTimeFormat/prototype/formatToParts/en-us-numeric-always.js b/test/intl402/RelativeTimeFormat/prototype/formatToParts/en-us-numeric-always.js new file mode 100644 index 0000000000000000000000000000000000000000..ffa49871d15a79289a18f8f0e7297782a5751020 --- /dev/null +++ b/test/intl402/RelativeTimeFormat/prototype/formatToParts/en-us-numeric-always.js @@ -0,0 +1,81 @@ +// 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", + "minute", + "hour", + "day", + "week", + "month", + "quarter", + "year", +]; + +const rtf = new Intl.RelativeTimeFormat("en-US"); + +assert.sameValue(typeof rtf.formatToParts, "function", "formatToParts should be supported"); + +for (const unit of units) { + // Note https://github.com/tc39/proposal-intl-relative-time/issues/80 + verifyFormatParts(rtf.formatToParts(10, unit), [ + { "type": "literal", "value": "in " }, + { "type": "integer", "value": "10", "unit": unit }, + { "type": "literal", "value": ` ${unit}s` }, + ], `formatToParts(10, ${unit})`); + + verifyFormatParts(rtf.formatToParts(2, unit), [ + { "type": "literal", "value": "in " }, + { "type": "integer", "value": "2", "unit": unit }, + { "type": "literal", "value": ` ${unit}s` }, + ], `formatToParts(2, ${unit})`); + + verifyFormatParts(rtf.formatToParts(1, unit), [ + { "type": "literal", "value": "in " }, + { "type": "integer", "value": "1", "unit": unit }, + { "type": "literal", "value": ` ${unit}` }, + ], `formatToParts(1, ${unit})`); + + verifyFormatParts(rtf.formatToParts(0, unit), [ + { "type": "literal", "value": "in " }, + { "type": "integer", "value": "0", "unit": unit }, + { "type": "literal", "value": ` ${unit}s` }, + ], `formatToParts(0, ${unit})`); + + verifyFormatParts(rtf.formatToParts(-0, unit), [ + { "type": "integer", "value": "0", "unit": unit }, + { "type": "literal", "value": ` ${unit}s ago` }, + ], `formatToParts(-0, ${unit})`); + + verifyFormatParts(rtf.formatToParts(-1, unit), [ + { "type": "integer", "value": "1", "unit": unit }, + { "type": "literal", "value": ` ${unit} ago` }, + ], `formatToParts(-1, ${unit})`); + + verifyFormatParts(rtf.formatToParts(-2, unit), [ + { "type": "integer", "value": "2", "unit": unit }, + { "type": "literal", "value": ` ${unit}s ago` }, + ], `formatToParts(-2, ${unit})`); + + verifyFormatParts(rtf.formatToParts(-10, unit), [ + { "type": "integer", "value": "10", "unit": unit }, + { "type": "literal", "value": ` ${unit}s ago` }, + ], `formatToParts(-10, ${unit})`); +} diff --git a/test/intl402/RelativeTimeFormat/prototype/formatToParts/en-us-numeric-auto.js b/test/intl402/RelativeTimeFormat/prototype/formatToParts/en-us-numeric-auto.js new file mode 100644 index 0000000000000000000000000000000000000000..16e286a31291a9d52410afc522a4d1ddf2c1c0ef --- /dev/null +++ b/test/intl402/RelativeTimeFormat/prototype/formatToParts/en-us-numeric-auto.js @@ -0,0 +1,129 @@ +// 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`); + } +} + +function expected(key, unit, default_) { + // https://www.unicode.org/cldr/charts/33/summary/en.html#1530 + const exceptions = { + "year": { + "-1": "last year", + "0": "this year", + "1": "next year", + }, + "month": { + "-1": "last month", + "0": "this month", + "1": "next month", + }, + "week": { + "-1": "last week", + "0": "this week", + "1": "next week", + }, + "day": { + "-1": "yesterday", + "0": "today", + "1": "tomorrow", + }, + "second": { + "0": "now", + }, + }; + + const exception = exceptions[unit] || {}; + if (key in exception) { + return [ + { "type": "literal", "value": exception[key] }, + ] + } + + return default_; +} + +const units = [ + "second", + "minute", + "hour", + "day", + "week", + "month", + "quarter", + "year", +]; + +const rtf = new Intl.RelativeTimeFormat("en-US", { "numeric": "auto" }); + +assert.sameValue(typeof rtf.formatToParts, "function", "formatToParts should be supported"); + +for (const unit of units) { + // Note https://github.com/tc39/proposal-intl-relative-time/issues/80 + /* + assert.sameValue(rtf.formatToParts(10, unit), `in 10 ${unit}s`); + assert.sameValue(rtf.formatToParts(2, unit), `in 2 ${unit}s`); + assert.sameValue(rtf.formatToParts(1, unit), expected[0]); + assert.sameValue(rtf.formatToParts(0, unit), expected[1]); + assert.sameValue(rtf.formatToParts(-0, unit), expected[2]); + assert.sameValue(rtf.formatToParts(-1, unit), expected[3]); + assert.sameValue(rtf.formatToParts(-2, unit), `2 ${unit}s ago`); + assert.sameValue(rtf.formatToParts(-10, unit), `10 ${unit}s ago`); +*/ + verifyFormatParts(rtf.formatToParts(10, unit), [ + { "type": "literal", "value": "in " }, + { "type": "integer", "value": "10", "unit": unit }, + { "type": "literal", "value": ` ${unit}s` }, + ], `formatToParts(10, ${unit})`); + + verifyFormatParts(rtf.formatToParts(2, unit), [ + { "type": "literal", "value": "in " }, + { "type": "integer", "value": "2", "unit": unit }, + { "type": "literal", "value": ` ${unit}s` }, + ], `formatToParts(2, ${unit})`); + + verifyFormatParts(rtf.formatToParts(1, unit), expected("1", unit, [ + { "type": "literal", "value": "in " }, + { "type": "integer", "value": "1", "unit": unit }, + { "type": "literal", "value": ` ${unit}` }, + ]), `formatToParts(1, ${unit})`); + + verifyFormatParts(rtf.formatToParts(0, unit), expected("0", unit, [ + { "type": "literal", "value": "in " }, + { "type": "integer", "value": "0", "unit": unit }, + { "type": "literal", "value": ` ${unit}s` }, + ]), `formatToParts(0, ${unit})`); + + verifyFormatParts(rtf.formatToParts(-0, unit), expected("0", unit, [ + { "type": "integer", "value": "0", "unit": unit }, + { "type": "literal", "value": ` ${unit}s ago` }, + ]), `formatToParts(-0, ${unit})`); + + verifyFormatParts(rtf.formatToParts(-1, unit), expected("-1", unit, [ + { "type": "integer", "value": "1", "unit": unit }, + { "type": "literal", "value": ` ${unit} ago` }, + ]), `formatToParts(-1, ${unit})`); + + verifyFormatParts(rtf.formatToParts(-2, unit), [ + { "type": "integer", "value": "2", "unit": unit }, + { "type": "literal", "value": ` ${unit}s ago` }, + ], `formatToParts(-2, ${unit})`); + + verifyFormatParts(rtf.formatToParts(-10, unit), [ + { "type": "integer", "value": "10", "unit": unit }, + { "type": "literal", "value": ` ${unit}s ago` }, + ], `formatToParts(-10, ${unit})`); +}