| 1 |
// |reftest| skip-if(!this.hasOwnProperty('Intl')) |
| 2 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
// Tests the format function with a diverse set of locales and options. |
| 7 |
|
| 8 |
var rtf; |
| 9 |
|
| 10 |
{ |
| 11 |
// Numeric format |
| 12 |
rtf = new Intl.RelativeTimeFormat("en-US"); |
| 13 |
assertEq(rtf.format(0, "second"), "in 0 seconds"); |
| 14 |
assertEq(rtf.format(-0, "second"), "0 seconds ago"); |
| 15 |
assertEq(rtf.format(-1, "second"), "1 second ago"); |
| 16 |
assertEq(rtf.format(1, "second"), "in 1 second"); |
| 17 |
|
| 18 |
assertEq(rtf.format(0, "minute"), "in 0 minutes"); |
| 19 |
assertEq(rtf.format(-0, "minute"), "0 minutes ago"); |
| 20 |
assertEq(rtf.format(-1, "minute"), "1 minute ago"); |
| 21 |
assertEq(rtf.format(1, "minute"), "in 1 minute"); |
| 22 |
|
| 23 |
assertEq(rtf.format(0, "hour"), "in 0 hours"); |
| 24 |
assertEq(rtf.format(-0, "hour"), "0 hours ago"); |
| 25 |
assertEq(rtf.format(-1, "hour"), "1 hour ago"); |
| 26 |
assertEq(rtf.format(1, "hour"), "in 1 hour"); |
| 27 |
|
| 28 |
assertEq(rtf.format(0, "day"), "in 0 days"); |
| 29 |
assertEq(rtf.format(-0, "day"), "0 days ago"); |
| 30 |
assertEq(rtf.format(-1, "day"), "1 day ago"); |
| 31 |
assertEq(rtf.format(1, "day"), "in 1 day"); |
| 32 |
|
| 33 |
assertEq(rtf.format(0, "week"), "in 0 weeks"); |
| 34 |
assertEq(rtf.format(-0, "week"), "0 weeks ago"); |
| 35 |
assertEq(rtf.format(-1, "week"), "1 week ago"); |
| 36 |
assertEq(rtf.format(1, "week"), "in 1 week"); |
| 37 |
|
| 38 |
assertEq(rtf.format(0, "month"), "in 0 months"); |
| 39 |
assertEq(rtf.format(-0, "month"), "0 months ago"); |
| 40 |
assertEq(rtf.format(-1, "month"), "1 month ago"); |
| 41 |
assertEq(rtf.format(1, "month"), "in 1 month"); |
| 42 |
|
| 43 |
assertEq(rtf.format(0, "year"), "in 0 years"); |
| 44 |
assertEq(rtf.format(-0, "year"), "0 years ago"); |
| 45 |
assertEq(rtf.format(-1, "year"), "1 year ago"); |
| 46 |
assertEq(rtf.format(1, "year"), "in 1 year"); |
| 47 |
} |
| 48 |
|
| 49 |
{ |
| 50 |
// Text format |
| 51 |
rtf = new Intl.RelativeTimeFormat("en-US", { |
| 52 |
numeric: "auto" |
| 53 |
}); |
| 54 |
assertEq(rtf.format(0, "second"), "now"); |
| 55 |
assertEq(rtf.format(-0, "second"), "now"); |
| 56 |
assertEq(rtf.format(-1, "second"), "1 second ago"); |
| 57 |
assertEq(rtf.format(1, "second"), "in 1 second"); |
| 58 |
|
| 59 |
assertEq(rtf.format(0, "minute"), "this minute"); |
| 60 |
assertEq(rtf.format(-0, "minute"), "this minute"); |
| 61 |
assertEq(rtf.format(-1, "minute"), "1 minute ago"); |
| 62 |
assertEq(rtf.format(1, "minute"), "in 1 minute"); |
| 63 |
|
| 64 |
assertEq(rtf.format(0, "hour"), "this hour"); |
| 65 |
assertEq(rtf.format(-0, "hour"), "this hour"); |
| 66 |
assertEq(rtf.format(-1, "hour"), "1 hour ago"); |
| 67 |
assertEq(rtf.format(1, "hour"), "in 1 hour"); |
| 68 |
|
| 69 |
assertEq(rtf.format(0, "day"), "today"); |
| 70 |
assertEq(rtf.format(-0, "day"), "today"); |
| 71 |
assertEq(rtf.format(-1, "day"), "yesterday"); |
| 72 |
assertEq(rtf.format(1, "day"), "tomorrow"); |
| 73 |
|
| 74 |
assertEq(rtf.format(0, "week"), "this week"); |
| 75 |
assertEq(rtf.format(-0, "week"), "this week"); |
| 76 |
assertEq(rtf.format(-1, "week"), "last week"); |
| 77 |
assertEq(rtf.format(1, "week"), "next week"); |
| 78 |
|
| 79 |
assertEq(rtf.format(0, "month"), "this month"); |
| 80 |
assertEq(rtf.format(-0, "month"), "this month"); |
| 81 |
assertEq(rtf.format(-1, "month"), "last month"); |
| 82 |
assertEq(rtf.format(1, "month"), "next month"); |
| 83 |
|
| 84 |
assertEq(rtf.format(0, "year"), "this year"); |
| 85 |
assertEq(rtf.format(-0, "year"), "this year"); |
| 86 |
assertEq(rtf.format(-1, "year"), "last year"); |
| 87 |
assertEq(rtf.format(1, "year"), "next year"); |
| 88 |
} |
| 89 |
|
| 90 |
{ |
| 91 |
// Plural specifier |
| 92 |
rtf = new Intl.RelativeTimeFormat("en-US"); |
| 93 |
assertEq(rtf.format(1, "seconds"), "in 1 second"); |
| 94 |
assertEq(rtf.format(1, "minutes"), "in 1 minute"); |
| 95 |
assertEq(rtf.format(1, "hours"), "in 1 hour"); |
| 96 |
assertEq(rtf.format(1, "days"), "in 1 day"); |
| 97 |
assertEq(rtf.format(1, "weeks"), "in 1 week"); |
| 98 |
assertEq(rtf.format(1, "months"), "in 1 month"); |
| 99 |
assertEq(rtf.format(1, "years"), "in 1 year"); |
| 100 |
} |
| 101 |
|
| 102 |
rtf = new Intl.RelativeTimeFormat("de", {numeric: "auto"}); |
| 103 |
assertEq(rtf.format(-1, "day"), "gestern"); |
| 104 |
assertEq(rtf.format(1, "day"), "morgen"); |
| 105 |
|
| 106 |
rtf = new Intl.RelativeTimeFormat("ar", {numeric: "auto"}); |
| 107 |
assertEq(rtf.format(-1, "day"), "أمس"); |
| 108 |
assertEq(rtf.format(1, "day"), "غدًا"); |
| 109 |
|
| 110 |
|
| 111 |
rtf = new Intl.RelativeTimeFormat("en-US"); |
| 112 |
|
| 113 |
var weirdValueCases = [ |
| 114 |
Infinity, |
| 115 |
-Infinity, |
| 116 |
NaN, |
| 117 |
"word", |
| 118 |
[0,2], |
| 119 |
{}, |
| 120 |
]; |
| 121 |
|
| 122 |
for (let c of weirdValueCases) { |
| 123 |
assertThrowsInstanceOf(() => rtf.format(c, "year"), RangeError); |
| 124 |
}; |
| 125 |
|
| 126 |
var weirdUnitCases = [ |
| 127 |
"test", |
| 128 |
"SECOND", |
| 129 |
"sEcOnD", |
| 130 |
1, |
| 131 |
NaN, |
| 132 |
undefined, |
| 133 |
null, |
| 134 |
{}, |
| 135 |
]; |
| 136 |
|
| 137 |
for (let u of weirdUnitCases) { |
| 138 |
assertThrowsInstanceOf(function() { |
| 139 |
var rtf = new Intl.RelativeTimeFormat("en-US"); |
| 140 |
rtf.format(1, u); |
| 141 |
}, RangeError); |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
reportCompare(0, 0, 'ok'); |