Bug 1613139 - Enable ESLint for intl/locale and intl/strres (automatic changes). r=zbraniecki

# ignore-this-changeset

Differential Revision: https://phabricator.services.mozilla.com/D61580

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Mark Banner 2020-02-05 09:04:33 +00:00
parent 0de91e6382
commit c4d713de2b
14 changed files with 6549 additions and 926 deletions

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var EXPORTED_SYMBOLS = [ "PluralForm" ];
var EXPORTED_SYMBOLS = ["PluralForm"];
/**
* This module provides the PluralForm object which contains a method to figure
@ -38,45 +38,151 @@ const kIntlProperties = "chrome://global/locale/intl.properties";
// of plural forms and the second is the function to figure out the index.
var gFunctions = [
// 0: Chinese
[1, (n) => 0],
[1, n => 0],
// 1: English
[2, (n) => n!=1?1:0],
[2, n => (n != 1 ? 1 : 0)],
// 2: French
[2, (n) => n>1?1:0],
[2, n => (n > 1 ? 1 : 0)],
// 3: Latvian
[3, (n) => n%10==1&&n%100!=11?1:n%10==0?0:2],
[3, n => (n % 10 == 1 && n % 100 != 11 ? 1 : n % 10 == 0 ? 0 : 2)],
// 4: Scottish Gaelic
[4, (n) => n==1||n==11?0:n==2||n==12?1:n>0&&n<20?2:3],
[
4,
n =>
n == 1 || n == 11 ? 0 : n == 2 || n == 12 ? 1 : n > 0 && n < 20 ? 2 : 3,
],
// 5: Romanian
[3, (n) => n==1?0:n==0||n%100>0&&n%100<20?1:2],
[3, n => (n == 1 ? 0 : n == 0 || (n % 100 > 0 && n % 100 < 20) ? 1 : 2)],
// 6: Lithuanian
[3, (n) => n%10==1&&n%100!=11?0:n%10>=2&&(n%100<10||n%100>=20)?2:1],
[
3,
n =>
n % 10 == 1 && n % 100 != 11
? 0
: n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20)
? 2
: 1,
],
// 7: Russian
[3, (n) => n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
[
3,
n =>
n % 10 == 1 && n % 100 != 11
? 0
: n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
? 1
: 2,
],
// 8: Slovak
[3, (n) => n==1?0:n>=2&&n<=4?1:2],
[3, n => (n == 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2)],
// 9: Polish
[3, (n) => n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
[
3,
n =>
n == 1
? 0
: n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
? 1
: 2,
],
// 10: Slovenian
[4, (n) => n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3],
[
4,
n =>
n % 100 == 1
? 0
: n % 100 == 2
? 1
: n % 100 == 3 || n % 100 == 4
? 2
: 3,
],
// 11: Irish Gaeilge
[5, (n) => n==1?0:n==2?1:n>=3&&n<=6?2:n>=7&&n<=10?3:4],
[
5,
n =>
n == 1
? 0
: n == 2
? 1
: n >= 3 && n <= 6
? 2
: n >= 7 && n <= 10
? 3
: 4,
],
// 12: Arabic
[6, (n) => n==0?5:n==1?0:n==2?1:n%100>=3&&n%100<=10?2:n%100>=11&&n%100<=99?3:4],
[
6,
n =>
n == 0
? 5
: n == 1
? 0
: n == 2
? 1
: n % 100 >= 3 && n % 100 <= 10
? 2
: n % 100 >= 11 && n % 100 <= 99
? 3
: 4,
],
// 13: Maltese
[4, (n) => n==1?0:n==0||n%100>0&&n%100<=10?1:n%100>10&&n%100<20?2:3],
[
4,
n =>
n == 1
? 0
: n == 0 || (n % 100 > 0 && n % 100 <= 10)
? 1
: n % 100 > 10 && n % 100 < 20
? 2
: 3,
],
// 14: Unused
[3, (n) => n%10==1?0:n%10==2?1:2],
[3, n => (n % 10 == 1 ? 0 : n % 10 == 2 ? 1 : 2)],
// 15: Icelandic, Macedonian
[2, (n) => n%10==1&&n%100!=11?0:1],
[2, n => (n % 10 == 1 && n % 100 != 11 ? 0 : 1)],
// 16: Breton
[5, (n) => n%10==1&&n%100!=11&&n%100!=71&&n%100!=91?0:n%10==2&&n%100!=12&&n%100!=72&&n%100!=92?1:(n%10==3||n%10==4||n%10==9)&&n%100!=13&&n%100!=14&&n%100!=19&&n%100!=73&&n%100!=74&&n%100!=79&&n%100!=93&&n%100!=94&&n%100!=99?2:n%1000000==0&&n!=0?3:4],
[
5,
n =>
n % 10 == 1 && n % 100 != 11 && n % 100 != 71 && n % 100 != 91
? 0
: n % 10 == 2 && n % 100 != 12 && n % 100 != 72 && n % 100 != 92
? 1
: (n % 10 == 3 || n % 10 == 4 || n % 10 == 9) &&
n % 100 != 13 &&
n % 100 != 14 &&
n % 100 != 19 &&
n % 100 != 73 &&
n % 100 != 74 &&
n % 100 != 79 &&
n % 100 != 93 &&
n % 100 != 94 &&
n % 100 != 99
? 2
: n % 1000000 == 0 && n != 0
? 3
: 4,
],
// 17: Shuar
[2, (n) => n!=0?1:0],
[2, n => (n != 0 ? 1 : 0)],
// 18: Welsh
[6, (n) => n==0?0:n==1?1:n==2?2:n==3?3:n==6?4:5],
[
6,
n => (n == 0 ? 0 : n == 1 ? 1 : n == 2 ? 2 : n == 3 ? 3 : n == 6 ? 4 : 5),
],
// 19: Slavic languages (bs, hr, sr). Same as rule 7, but resulting in different CLDR categories
[3, (n) => n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
[
3,
n =>
n % 10 == 1 && n % 100 != 11
? 0
: n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
? 1
: 2,
],
];
var PluralForm = {
@ -89,8 +195,7 @@ var PluralForm = {
* A semi-colon (;) separated string of words to pick the plural form
* @return The appropriate plural form of the word
*/
get get()
{
get get() {
// This method will lazily load to avoid perf when it is first needed and
// creates getPluralForm function. The function it creates is based on the
// value of pluralRule specified in the intl stringbundle.
@ -101,7 +206,9 @@ var PluralForm = {
delete PluralForm.get;
// Make the plural form get function and set it as the default get
[PluralForm.get, PluralForm.numForms] = PluralForm.makeGetter(PluralForm.ruleNum);
[PluralForm.get, PluralForm.numForms] = PluralForm.makeGetter(
PluralForm.ruleNum
);
return PluralForm.get;
},
@ -113,8 +220,7 @@ var PluralForm = {
* @return A pair: [function that gets the right plural form,
* function that returns the number of plural forms]
*/
makeGetter: function(aRuleNum)
{
makeGetter(aRuleNum) {
// Default to "all plural" if the value is out of bounds or invalid
if (aRuleNum < 0 || aRuleNum >= gFunctions.length || isNaN(aRuleNum)) {
log(["Invalid rule number: ", aRuleNum, " -- defaulting to 0"]);
@ -126,29 +232,44 @@ var PluralForm = {
// Return functions that give 1) the number of forms and 2) gets the right
// plural form
return [function(aNum, aWords) {
// Figure out which index to use for the semi-colon separated words
let index = pluralFunc(aNum ? Number(aNum) : 0);
let words = aWords ? aWords.split(/;/) : [""];
return [
function(aNum, aWords) {
// Figure out which index to use for the semi-colon separated words
let index = pluralFunc(aNum ? Number(aNum) : 0);
let words = aWords ? aWords.split(/;/) : [""];
// Explicitly check bounds to avoid strict warnings
let ret = index < words.length ? words[index] : undefined;
// Explicitly check bounds to avoid strict warnings
let ret = index < words.length ? words[index] : undefined;
// Check for array out of bounds or empty strings
if ((ret == undefined) || (ret == "")) {
// Report the caller to help figure out who is causing badness
let caller = Components.stack.caller ? Components.stack.caller.name : "top";
// Check for array out of bounds or empty strings
if (ret == undefined || ret == "") {
// Report the caller to help figure out who is causing badness
let caller = Components.stack.caller
? Components.stack.caller.name
: "top";
// Display a message in the error console
log(["Index #", index, " of '", aWords, "' for value ", aNum,
" is invalid -- plural rule #", aRuleNum, "; called by ", caller]);
// Display a message in the error console
log([
"Index #",
index,
" of '",
aWords,
"' for value ",
aNum,
" is invalid -- plural rule #",
aRuleNum,
"; called by ",
caller,
]);
// Default to the first entry (which might be empty, but not undefined)
ret = words[0];
}
// Default to the first entry (which might be empty, but not undefined)
ret = words[0];
}
return ret;
}, () => numForms];
return ret;
},
() => numForms,
];
},
/**
@ -156,8 +277,7 @@ var PluralForm = {
*
* @return The number of forms
*/
get numForms()
{
get numForms() {
// We lazily load numForms, so trigger the init logic with get()
PluralForm.get();
return PluralForm.numForms;
@ -168,12 +288,14 @@ var PluralForm = {
*
* @return The plural rule number
*/
get ruleNum()
{
return Number(Cc["@mozilla.org/intl/stringbundle;1"].
getService(Ci.nsIStringBundleService).createBundle(kIntlProperties).
GetStringFromName("pluralRule"));
}
get ruleNum() {
return Number(
Cc["@mozilla.org/intl/stringbundle;1"]
.getService(Ci.nsIStringBundleService)
.createBundle(kIntlProperties)
.GetStringFromName("pluralRule")
);
},
};
/**
@ -182,10 +304,10 @@ var PluralForm = {
* @param aMsg
* Error message to log or an array of strings to concat
*/
function log(aMsg)
{
function log(aMsg) {
let msg = "PluralForm.jsm: " + (aMsg.join ? aMsg.join("") : aMsg);
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).
logStringMessage(msg);
Cc["@mozilla.org/consoleservice;1"]
.getService(Ci.nsIConsoleService)
.logStringMessage(msg);
dump(msg + "\n");
}

View file

@ -1,4 +1,4 @@
self.onmessage = function (data) {
self.onmessage = function(data) {
let myLocale = Intl.NumberFormat().resolvedOptions().locale;
self.postMessage(myLocale);
};

View file

@ -6,7 +6,9 @@
* This unit test makes sure that PluralForm.get can be called from strict mode
*/
const {PluralForm} = ChromeUtils.import("resource://gre/modules/PluralForm.jsm");
const { PluralForm } = ChromeUtils.import(
"resource://gre/modules/PluralForm.jsm"
);
delete PluralForm.numForms;
delete PluralForm.get;
@ -16,5 +18,5 @@ function run_test() {
"use strict";
Assert.equal(3, PluralForm.numForms());
Assert.equal("one", PluralForm.get(5, 'one;many'));
Assert.equal("one", PluralForm.get(5, "one;many"));
}

View file

@ -1,51 +1,65 @@
String.prototype.has = function(s) { return this.includes(s); }
String.prototype.has = function(s) {
return this.includes(s);
};
function dt(locale) {
var date = new Date("2008-06-30T13:56:34");
const dtOptions = { year: 'numeric', month: 'long', day: 'numeric',
hour: 'numeric', minute: 'numeric', second: 'numeric' };
const dtOptions = {
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
};
return date.toLocaleString(locale, dtOptions);
}
var all_passed = true;
const tests =
[
[dt("en-US").has("June"), "month name in en-US"],
[dt("en-US").has("2008"), "year in en-US"],
[dt("da").has("jun"), "month name in da"],
[dt("da-DK") == dt("da"), "da same as da-DK"],
[dt("en-GB").has("30") && dt("en-GB").has("June") &&
dt("en-GB").indexOf("30") < dt("en-GB").indexOf("June"),
"day before month in en-GB"],
[dt("en-US").has("30") && dt("en-US").has("June") &&
dt("en-US").indexOf("30") > dt("en-US").indexOf("June"),
"month before day in en-US"],
[dt("ja-JP").has("\u5E746\u670830\u65E5"), "year month and day in ja-JP"],
// The Firefox locale code ja-JP-mac will be resolved to a BCP47-compliant
// tag ja-JP-x-lvariant-mac by uloc_toLanguageTag
[dt("ja-JP") == dt("ja-JP-x-lvariant-mac"), "ja-JP-x-lvariant-mac same as ja-JP"],
[dt("nn-NO").has("juni"), "month name in nn-NO"],
[dt("nb-NO").has("juni"), "month name in nb-NO"],
// Bug 1261775 - failures on win10
//[dt("no-NO").has("30. juni"), "month name in no-NO"],
[dt("sv-SE").has("30 jun"), "month name in sv-SE"],
[dt("kok").has("\u091C\u0942\u0928"), "month name in kok"],
[dt("ta-IN").has("\u0B9C\u0BC2\u0BA9\u0BCD"), "month name in ta-IN"],
[dt("ab-CD").length > 0, "fallback for ab-CD"]
const tests = [
[dt("en-US").has("June"), "month name in en-US"],
[dt("en-US").has("2008"), "year in en-US"],
[dt("da").has("jun"), "month name in da"],
[dt("da-DK") == dt("da"), "da same as da-DK"],
[
dt("en-GB").has("30") &&
dt("en-GB").has("June") &&
dt("en-GB").indexOf("30") < dt("en-GB").indexOf("June"),
"day before month in en-GB",
],
[
dt("en-US").has("30") &&
dt("en-US").has("June") &&
dt("en-US").indexOf("30") > dt("en-US").indexOf("June"),
"month before day in en-US",
],
[dt("ja-JP").has("\u5E746\u670830\u65E5"), "year month and day in ja-JP"],
// The Firefox locale code ja-JP-mac will be resolved to a BCP47-compliant
// tag ja-JP-x-lvariant-mac by uloc_toLanguageTag
[
dt("ja-JP") == dt("ja-JP-x-lvariant-mac"),
"ja-JP-x-lvariant-mac same as ja-JP",
],
[dt("nn-NO").has("juni"), "month name in nn-NO"],
[dt("nb-NO").has("juni"), "month name in nb-NO"],
// Bug 1261775 - failures on win10
//[dt("no-NO").has("30. juni"), "month name in no-NO"],
[dt("sv-SE").has("30 jun"), "month name in sv-SE"],
[dt("kok").has("\u091C\u0942\u0928"), "month name in kok"],
[dt("ta-IN").has("\u0B9C\u0BC2\u0BA9\u0BCD"), "month name in ta-IN"],
[!!dt("ab-CD").length, "fallback for ab-CD"],
];
function one_test(testcase, msg)
{
if (!testcase) {
all_passed = false;
dump("Unexpected date format: " + msg + "\n");
}
function one_test(testcase, msg) {
if (!testcase) {
all_passed = false;
dump("Unexpected date format: " + msg + "\n");
}
}
function run_test()
{
for (var i = 0; i < tests.length; ++i) {
one_test(tests[i][0], tests[i][1]);
}
Assert.ok(all_passed);
function run_test() {
for (var i = 0; i < tests.length; ++i) {
one_test(tests[i][0], tests[i][1]);
}
Assert.ok(all_passed);
}

View file

@ -1,7 +1,6 @@
ChromeUtils.import("resource://gre/modules/Services.jsm");
function run_test()
{
function run_test() {
var input = [
"Argentina",
"Oerlikon",
@ -18,11 +17,11 @@ function run_test()
];
function test(locale, expected) {
var collator = Cc["@mozilla.org/intl/collation-factory;1"].
createInstance(Ci.nsICollationFactory).
CreateCollationForLocale(locale);
var collator = Cc["@mozilla.org/intl/collation-factory;1"]
.createInstance(Ci.nsICollationFactory)
.CreateCollationForLocale(locale);
var strength = Ci.nsICollation.kCollationStrengthDefault;
var actual = input.sort((x, y) => collator.compareString(strength, x,y));
var actual = input.sort((x, y) => collator.compareString(strength, x, y));
deepEqual(actual, expected, locale);
}

View file

@ -8,11 +8,17 @@ function run_test() {
}
let mainThreadLocale = Intl.NumberFormat().resolvedOptions().locale;
let testWorker = new Worker("chrome://locale/content/intl_on_workers_worker.js");
testWorker.onmessage = function (e) {
let testWorker = new Worker(
"chrome://locale/content/intl_on_workers_worker.js"
);
testWorker.onmessage = function(e) {
try {
let workerLocale = e.data;
equal(mainThreadLocale, workerLocale, "Worker should inherit Intl locale from main thread.");
equal(
mainThreadLocale,
workerLocale,
"Worker should inherit Intl locale from main thread."
);
} finally {
do_test_finished();
}

View file

@ -3,20 +3,17 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const osPrefs = Cc["@mozilla.org/intl/ospreferences;1"].
getService(Ci.mozIOSPreferences);
const osPrefs = Cc["@mozilla.org/intl/ospreferences;1"].getService(
Ci.mozIOSPreferences
);
const localeService =
Cc["@mozilla.org/intl/localeservice;1"]
.getService(Ci.mozILocaleService);
const localeService = Cc["@mozilla.org/intl/localeservice;1"].getService(
Ci.mozILocaleService
);
/**
* Make sure the locale service can be instantiated.
*/
function run_test()
{
run_next_test();
}
add_test(function test_defaultLocale() {
const defaultLocale = localeService.defaultLocale;
@ -37,7 +34,10 @@ add_test(function test_appLocalesAsLangTags() {
const appLocales = localeService.appLocalesAsLangTags;
Assert.ok(Array.isArray(appLocales), "appLocales returns an array");
Assert.ok(appLocale == appLocales[0], "appLocale matches first entry in appLocales");
Assert.ok(
appLocale == appLocales[0],
"appLocale matches first entry in appLocales"
);
const enUSLocales = appLocales.filter(loc => loc === "en-US");
Assert.ok(enUSLocales.length == 1, "en-US is present exactly one time");
@ -50,7 +50,10 @@ const REQ_LOC_CHANGE_EVENT = "intl:requested-locales-changed";
add_test(function test_requestedLocales() {
const requestedLocales = localeService.requestedLocales;
Assert.ok(Array.isArray(requestedLocales), "requestedLocales returns an array");
Assert.ok(
Array.isArray(requestedLocales),
"requestedLocales returns an array"
);
run_next_test();
});
@ -69,7 +72,7 @@ add_test(function test_requestedLocales_matchOS() {
Services.prefs.setCharPref(PREF_REQUESTED_LOCALES, "ar-IR");
const observer = {
observe: function (aSubject, aTopic, aData) {
observe(aSubject, aTopic, aData) {
switch (aTopic) {
case REQ_LOC_CHANGE_EVENT:
const reqLocs = localeService.requestedLocales;
@ -77,7 +80,7 @@ add_test(function test_requestedLocales_matchOS() {
Services.obs.removeObserver(observer, REQ_LOC_CHANGE_EVENT);
do_test_finished();
}
}
},
};
Services.obs.addObserver(observer, REQ_LOC_CHANGE_EVENT);
@ -97,7 +100,7 @@ add_test(function test_requestedLocales_onChange() {
Services.prefs.setCharPref(PREF_REQUESTED_LOCALES, "ar-IR");
const observer = {
observe: function (aSubject, aTopic, aData) {
observe(aSubject, aTopic, aData) {
switch (aTopic) {
case REQ_LOC_CHANGE_EVENT:
const reqLocs = localeService.requestedLocales;
@ -105,7 +108,7 @@ add_test(function test_requestedLocales_onChange() {
Services.obs.removeObserver(observer, REQ_LOC_CHANGE_EVENT);
do_test_finished();
}
}
},
};
Services.obs.addObserver(observer, REQ_LOC_CHANGE_EVENT);
@ -118,7 +121,10 @@ add_test(function test_requestedLocale() {
Services.prefs.setCharPref(PREF_REQUESTED_LOCALES, "tlh");
let requestedLocale = localeService.requestedLocale;
Assert.ok(requestedLocale === "tlh", "requestedLocale returns the right value");
Assert.ok(
requestedLocale === "tlh",
"requestedLocale returns the right value"
);
Services.prefs.clearUserPref(PREF_REQUESTED_LOCALES);
@ -126,18 +132,18 @@ add_test(function test_requestedLocale() {
});
add_test(function test_requestedLocales() {
localeService.requestedLocales = ['de-AT', 'de-DE', 'de-CH'];
localeService.requestedLocales = ["de-AT", "de-DE", "de-CH"];
let locales = localeService.requestedLocales;
Assert.ok(locales[0] === 'de-AT');
Assert.ok(locales[1] === 'de-DE');
Assert.ok(locales[2] === 'de-CH');
Assert.ok(locales[0] === "de-AT");
Assert.ok(locales[1] === "de-DE");
Assert.ok(locales[2] === "de-CH");
run_next_test();
});
add_test(function test_isAppLocaleRTL() {
Assert.ok(typeof localeService.isAppLocaleRTL === 'boolean');
Assert.ok(typeof localeService.isAppLocaleRTL === "boolean");
run_next_test();
});
@ -179,8 +185,8 @@ add_test(function test_availableLocales() {
let locales = localeService.availableLocales;
Assert.ok(locales.length == 2);
Assert.ok(locales[0] === 'und');
Assert.ok(locales[1] === 'ar-IR');
Assert.ok(locales[0] === "und");
Assert.ok(locales[1] === "ar-IR");
localeService.availableLocales = avLocales;
@ -191,7 +197,10 @@ add_test(function test_availableLocales() {
* This test verifies that all values coming from the pref are sanitized.
*/
add_test(function test_requestedLocales_sanitize() {
Services.prefs.setStringPref(PREF_REQUESTED_LOCALES, "de,2,#$@#,pl,ąó,!a2,DE-at,,;");
Services.prefs.setStringPref(
PREF_REQUESTED_LOCALES,
"de,2,#$@#,pl,ąó,!a2,DE-at,,;"
);
let locales = localeService.requestedLocales;
Assert.equal(locales[0], "de");
@ -210,19 +219,19 @@ add_test(function test_handle_ja_JP_mac() {
localeService.availableLocales = ["ja-JP-mac", "en-US"];
localeService.requestedLocales = ['ja-JP-mac'];
localeService.requestedLocales = ["ja-JP-mac"];
let reqLocales = localeService.requestedLocales;
Assert.equal(reqLocales[0], 'ja-JP-macos');
Assert.equal(reqLocales[0], "ja-JP-macos");
let avLocales = localeService.availableLocales;
Assert.equal(avLocales[0], 'ja-JP-macos');
Assert.equal(avLocales[0], "ja-JP-macos");
let appLocales = localeService.appLocalesAsBCP47;
Assert.equal(appLocales[0], 'ja-JP-macos');
Assert.equal(appLocales[0], "ja-JP-macos");
let appLocalesAsLT = localeService.appLocalesAsLangTags;
Assert.equal(appLocalesAsLT[0], 'ja-JP-mac');
Assert.equal(appLocalesAsLT[0], "ja-JP-mac");
Assert.equal(localeService.appLocaleAsLangTag, "ja-JP-mac");
@ -231,7 +240,6 @@ add_test(function test_handle_ja_JP_mac() {
run_next_test();
});
registerCleanupFunction(() => {
Services.prefs.clearUserPref(PREF_REQUESTED_LOCALES);
});

View file

@ -2,12 +2,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const localeService =
Cc["@mozilla.org/intl/localeservice;1"]
.getService(Ci.mozILocaleService);
const localeService = Cc["@mozilla.org/intl/localeservice;1"].getService(
Ci.mozILocaleService
);
const data = {
"filtering": {
filtering: {
"exact match": [
[["en"], ["en"], ["en"]],
[["en-US"], ["en-US"], ["en-US"]],
@ -26,7 +26,11 @@ const data = {
],
"should match on likely subtag": [
[["en"], ["en-GB", "de", "en-US"], ["en-US", "en-GB"]],
[["en"], ["en-Latn-GB", "de", "en-Latn-US"], ["en-Latn-US", "en-Latn-GB"]],
[
["en"],
["en-Latn-GB", "de", "en-Latn-US"],
["en-Latn-US", "en-Latn-GB"],
],
[["fr"], ["fr-CA", "fr-FR"], ["fr-FR", "fr-CA"]],
[["az-IR"], ["az-Latn", "az-Arab"], ["az-Arab"]],
[["sr-RU"], ["sr-Cyrl", "sr-Latn"], ["sr-Latn"]],
@ -48,26 +52,47 @@ const data = {
],
"should prioritize properly": [
// exact match first
[["en-US"], ["en-US-windows", "en", "en-US"], ["en-US", "en", "en-US-windows"]],
[
["en-US"],
["en-US-windows", "en", "en-US"],
["en-US", "en", "en-US-windows"],
],
// available as range second
[["en-Latn-US"], ["en-GB", "en-US"], ["en-US", "en-GB"]],
// likely subtags third
[["en"], ["en-Cyrl-US", "en-Latn-US"], ["en-Latn-US"]],
// variant range fourth
[["en-US-macos"], ["en-US-windows", "en-GB-macos"], ["en-US-windows", "en-GB-macos"]],
[
["en-US-macos"],
["en-US-windows", "en-GB-macos"],
["en-US-windows", "en-GB-macos"],
],
// regional range fifth
[["en-US-macos"], ["en-GB-windows"], ["en-GB-windows"]],
[["en-US"], ["en-GB", "en"], ["en", "en-GB"]],
[["fr-CA-macos", "de-DE"], ["de-DE", "fr-FR-windows"], ["fr-FR-windows", "de-DE"]],
[
["fr-CA-macos", "de-DE"],
["de-DE", "fr-FR-windows"],
["fr-FR-windows", "de-DE"],
],
],
"should handle default locale properly": [
[["fr"], ["de", "it"], []],
[["fr"], ["de", "it"], "en-US", ["en-US"]],
[["fr"], ["de", "en-US"], "en-US", ["en-US"]],
[["fr", "de-DE"], ["de-DE", "fr-CA"], "en-US", ["fr-CA", "de-DE", "en-US"]],
[
["fr", "de-DE"],
["de-DE", "fr-CA"],
"en-US",
["fr-CA", "de-DE", "en-US"],
],
],
"should handle all matches on the 1st higher than any on the 2nd": [
[["fr-CA-macos", "de-DE"], ["de-DE", "fr-FR-windows"], ["fr-FR-windows", "de-DE"]],
[
["fr-CA-macos", "de-DE"],
["de-DE", "fr-FR-windows"],
["fr-FR-windows", "de-DE"],
],
],
"should handle cases and underscores, returning the form given in the 'available' list": [
[["fr_FR"], ["fr-FR"], ["fr-FR"]],
@ -86,33 +111,33 @@ const data = {
[["2"], ["ąóżł"], []],
[[[""]], ["fr-FR"], []],
],
"should not match on invalid input": [
[["en"], ["ťŮ"], []],
],
"should not match on invalid input": [[["en"], ["ťŮ"], []]],
},
"matching": {
matching: {
"should match only one per requested": [
[
["fr", "en"],
["en-US", "fr-FR", "en", "fr"], null,
localeService.langNegStrategyMatching, ["fr", "en"]
["en-US", "fr-FR", "en", "fr"],
null,
localeService.langNegStrategyMatching,
["fr", "en"],
],
]
],
},
"lookup": {
lookup: {
"should match only one": [
[
["fr-FR", "en"],
["en-US", "fr-FR", "en", "fr"], 'en-US',
localeService.langNegStrategyLookup, ["fr-FR"]
["en-US", "fr-FR", "en", "fr"],
"en-US",
localeService.langNegStrategyLookup,
["fr-FR"],
],
]
}
],
},
};
function run_test()
{
function run_test() {
const nl = localeService.negotiateLanguages;
const json = JSON.stringify;
@ -127,8 +152,13 @@ function run_test()
const supported = test[test.length - 1];
const result = nl(test[0], test[1], defaultLocale, strategy);
deepEqual(result, supported,
`\nExpected ${json(requested)} * ${json(available)} = ${json(supported)}.\n`);
deepEqual(
result,
supported,
`\nExpected ${json(requested)} * ${json(available)} = ${json(
supported
)}.\n`
);
}
}
}
@ -136,13 +166,13 @@ function run_test()
// Verify that we error out when requested or available is not an array.
for (const [req, avail] in [
[null, ["fr-FR"]],
[undefined, ["fr-FR"]],
[2, ["fr-FR"]],
["fr-FR", ["fr-FR"]],
[["fr-FR"], null],
[["fr-FR"], undefined],
[["fr-FR"], 2],
[["fr-FR"], "fr-FR"],
[undefined, ["fr-FR"]],
[2, ["fr-FR"]],
["fr-FR", ["fr-FR"]],
[["fr-FR"], null],
[["fr-FR"], undefined],
[["fr-FR"], 2],
[["fr-FR"], "fr-FR"],
[[null], []],
[[undefined], []],
[[undefined], [null]],
@ -150,8 +180,11 @@ function run_test()
[[null], [null]],
[[[]], [[2]]],
]) {
Assert.throws(() => {
nl(req, avail);
}, err => err.result == Cr.NS_ERROR_XPC_CANT_CONVERT_PRIMITIVE_TO_ARRAY);
Assert.throws(
() => {
nl(req, avail);
},
err => err.result == Cr.NS_ERROR_XPC_CANT_CONVERT_PRIMITIVE_TO_ARRAY
);
}
}

View file

@ -2,11 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
function run_test()
{
const osprefs =
Cc["@mozilla.org/intl/ospreferences;1"]
.getService(Ci.mozIOSPreferences);
function run_test() {
const osprefs = Cc["@mozilla.org/intl/ospreferences;1"].getService(
Ci.mozIOSPreferences
);
const systemLocale = osprefs.systemLocale;
Assert.ok(systemLocale != "", "systemLocale is non-empty");
@ -14,8 +13,10 @@ function run_test()
const systemLocales = osprefs.systemLocales;
Assert.ok(Array.isArray(systemLocales), "systemLocales returns an array");
Assert.ok(systemLocale == systemLocales[0],
"systemLocale matches first entry in systemLocales");
Assert.ok(
systemLocale == systemLocales[0],
"systemLocale matches first entry in systemLocales"
);
const rgLocales = osprefs.regionalPrefsLocales;
Assert.ok(Array.isArray(rgLocales), "regionalPrefsLocales returns an array");
@ -31,8 +32,10 @@ function run_test()
const test = getDateTimePatternTests[i];
const pattern = osprefs.getDateTimePattern(...test);
if (test[0] !== osprefs.dateTimeFormatStyleNone &&
test[1] !== osprefs.dateTImeFormatStyleNone) {
if (
test[0] !== osprefs.dateTimeFormatStyleNone &&
test[1] !== osprefs.dateTImeFormatStyleNone
) {
Assert.greater(pattern.length, 0, "pattern is not empty.");
}
}

File diff suppressed because it is too large Load diff

View file

@ -7,16 +7,21 @@
* development), English, is working for the PluralForm javascript module.
*/
const {PluralForm} = ChromeUtils.import("resource://gre/modules/PluralForm.jsm");
const { PluralForm } = ChromeUtils.import(
"resource://gre/modules/PluralForm.jsm"
);
function run_test()
{
function run_test() {
// English has 2 plural forms
Assert.equal(2, PluralForm.numForms());
// Make sure for good inputs, things work as expected
for (var num = 0; num <= 200; num++)
Assert.equal(num == 1 ? "word" : "words", PluralForm.get(num, "word;words"));
for (var num = 0; num <= 200; num++) {
Assert.equal(
num == 1 ? "word" : "words",
PluralForm.get(num, "word;words")
);
}
// Not having enough plural forms defaults to the first form
Assert.equal("word", PluralForm.get(2, "word"));

View file

@ -8,10 +8,11 @@
* development), English.
*/
const {PluralForm} = ChromeUtils.import("resource://gre/modules/PluralForm.jsm");
const { PluralForm } = ChromeUtils.import(
"resource://gre/modules/PluralForm.jsm"
);
function run_test()
{
function run_test() {
// Irish is plural rule #11
let [get, numForms] = PluralForm.makeGetter(11);
@ -22,8 +23,9 @@ function run_test()
let words = "is 1;is 2;is 3-6;is 7-10;everything else";
let test = function(text, low, high) {
for (let num = low; num <= high; num++)
for (let num = low; num <= high; num++) {
Assert.equal(text, get(num, words));
}
};
// Make sure for good inputs, things work as expected

View file

@ -26,39 +26,36 @@ const name_hello = "hello";
const var_hello = "World";
const value_hello = "Hello World"; // tests formatStringFromName with parameter
function run_test() {
var StringBundle =
Cc["@mozilla.org/intl/stringbundle;1"]
.getService(Ci.nsIStringBundleService);
var ios = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService);
var bundleURI = ios.newFileURI(do_get_file("strres.properties"));
var StringBundle = Cc["@mozilla.org/intl/stringbundle;1"].getService(
Ci.nsIStringBundleService
);
var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
var bundleURI = ios.newFileURI(do_get_file("strres.properties"));
var bundle = StringBundle.createBundle(bundleURI.spec);
var bundle = StringBundle.createBundle(bundleURI.spec);
var bundle_file = bundle.GetStringFromName(name_file);
Assert.equal(bundle_file, value_file);
var bundle_file = bundle.GetStringFromName(name_file);
Assert.equal(bundle_file, value_file);
var bundle_loyal = bundle.GetStringFromName(name_loyal);
Assert.equal(bundle_loyal, value_loyal);
var bundle_loyal = bundle.GetStringFromName(name_loyal);
Assert.equal(bundle_loyal, value_loyal);
var bundle_trout = bundle.GetStringFromName(name_trout);
Assert.equal(bundle_trout, value_trout);
var bundle_trout = bundle.GetStringFromName(name_trout);
Assert.equal(bundle_trout, value_trout);
var bundle_edit = bundle.GetStringFromName(name_edit);
Assert.equal(bundle_edit, value_edit);
var bundle_edit = bundle.GetStringFromName(name_edit);
Assert.equal(bundle_edit, value_edit);
var bundle_view = bundle.GetStringFromName(name_view);
Assert.equal(bundle_view, value_view);
var bundle_view = bundle.GetStringFromName(name_view);
Assert.equal(bundle_view, value_view);
var bundle_go = bundle.GetStringFromName(name_go);
Assert.equal(bundle_go, value_go);
var bundle_go = bundle.GetStringFromName(name_go);
Assert.equal(bundle_go, value_go);
var bundle_message = bundle.GetStringFromName(name_message);
Assert.equal(bundle_message, value_message);
var bundle_message = bundle.GetStringFromName(name_message);
Assert.equal(bundle_message, value_message);
var bundle_hello = bundle.formatStringFromName(name_hello, [var_hello]);
Assert.equal(bundle_hello, value_hello);
var bundle_hello = bundle.formatStringFromName(name_hello, [var_hello]);
Assert.equal(bundle_hello, value_hello);
}

View file

@ -13,31 +13,30 @@ const value_utf8 = "";
const name_latin1 = "latin1";
const value_latin1 = "";
function run_test() {
var StringBundle =
Cc["@mozilla.org/intl/stringbundle;1"]
.getService(Ci.nsIStringBundleService);
var ios = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService);
var bundleURI = ios.newFileURI(do_get_file("397093.properties"));
var StringBundle = Cc["@mozilla.org/intl/stringbundle;1"].getService(
Ci.nsIStringBundleService
);
var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
var bundleURI = ios.newFileURI(do_get_file("397093.properties"));
var bundle = StringBundle.createBundle(bundleURI.spec);
var bundle_ascii="", bundle_utf8="", bundle_latin1="";
try {
bundle_ascii = bundle.GetStringFromName(name_ascii);
} catch(e) {}
Assert.equal(bundle_ascii, value_ascii);
var bundle = StringBundle.createBundle(bundleURI.spec);
try {
bundle_utf8 = bundle.GetStringFromName(name_utf8);
} catch(e) {}
Assert.equal(bundle_utf8, value_utf8);
var bundle_ascii = "",
bundle_utf8 = "",
bundle_latin1 = "";
try {
bundle_ascii = bundle.GetStringFromName(name_ascii);
} catch (e) {}
Assert.equal(bundle_ascii, value_ascii);
try {
bundle_latin1 = bundle.GetStringFromName(name_latin1);
} catch(e) {}
Assert.equal(bundle_latin1, value_latin1);
try {
bundle_utf8 = bundle.GetStringFromName(name_utf8);
} catch (e) {}
Assert.equal(bundle_utf8, value_utf8);
try {
bundle_latin1 = bundle.GetStringFromName(name_latin1);
} catch (e) {}
Assert.equal(bundle_latin1, value_latin1);
}