[intl] Improve test coverage of "best fit" localeMatcher
https: //docs.google.com/document/d/1cPGfiihn76yj2iAomKcspPFyLLcnk3WkCiqceBQPQyk/edit#heading=h.cc9tt7s0iwsd Bug: v8:7051 Change-Id: I8c35e859062c5bdb009334dd1b725751e6df2123 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2965481 Reviewed-by: Shu-yu Guo <syg@chromium.org> Commit-Queue: Frank Tang <ftang@chromium.org> Cr-Commit-Position: refs/heads/master@{#75228}
This commit is contained in:
parent
1fd74664a4
commit
dd8845cd74
39
test/intl/localematcher/bestfit-known-locales.js
Normal file
39
test/intl/localematcher/bestfit-known-locales.js
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright 2021 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Flags: --harmony_intl_best_fit_matcher
|
||||
//
|
||||
// Test the supportedLocales and resolvedOptions handle "zh-TW", "zh-Hant-TW",
|
||||
// etc.
|
||||
|
||||
const intlObjs = [
|
||||
Intl.Collator,
|
||||
Intl.DateTimeFormat,
|
||||
Intl.DisplayNames,
|
||||
Intl.ListFormat,
|
||||
Intl.NumberFormat,
|
||||
Intl.PluralRules,
|
||||
Intl.RelativeTimeFormat,
|
||||
Intl.Segmenter,
|
||||
];
|
||||
|
||||
let bestFitOpt = {localeMatcher: "best fit"};
|
||||
let defaultLocale = (new Intl.NumberFormat()).resolvedOptions().locale;
|
||||
intlObjs.forEach(function(obj) {
|
||||
[{}, bestFitOpt].forEach(function(opt) {
|
||||
// Test consistent between "zh-TW" and "zh-Hant-TW". Either both are
|
||||
// supported or neither are supported.
|
||||
assertEquals((obj.supportedLocalesOf(["zh-TW"], opt)).length,
|
||||
(obj.supportedLocalesOf(["zh-Hant-TW"], opt)).length);
|
||||
assertEquals((obj.supportedLocalesOf(["zh-MO"], opt)).length,
|
||||
(obj.supportedLocalesOf(["zh-Hant-MO"], opt)).length);
|
||||
assertEquals((obj.supportedLocalesOf(["zh-HK"], opt)).length,
|
||||
(obj.supportedLocalesOf(["zh-Hant-HK"], opt)).length);
|
||||
assertEquals((obj.supportedLocalesOf(["zh-CN"], opt)).length,
|
||||
(obj.supportedLocalesOf(["zh-Hans-CN"], opt)).length);
|
||||
assertEquals((obj.supportedLocalesOf(["zh-SG"], opt)).length,
|
||||
(obj.supportedLocalesOf(["zh-Hans-SG"], opt)).length);
|
||||
});
|
||||
|
||||
});
|
56
test/intl/localematcher/bestfit-macrolanguages.js
Normal file
56
test/intl/localematcher/bestfit-macrolanguages.js
Normal file
@ -0,0 +1,56 @@
|
||||
// Copyright 2021 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Flags: --harmony_intl_best_fit_matcher
|
||||
//
|
||||
// Test the supportedLocales and resolvedOptions handle macrolanguages
|
||||
// documented in https://unicode.org/reports/tr35/#Field_Definitions
|
||||
|
||||
const intlObjs = [
|
||||
Intl.Collator,
|
||||
Intl.DateTimeFormat,
|
||||
Intl.DisplayNames,
|
||||
Intl.ListFormat,
|
||||
Intl.NumberFormat,
|
||||
Intl.PluralRules,
|
||||
Intl.RelativeTimeFormat,
|
||||
Intl.Segmenter,
|
||||
];
|
||||
|
||||
// Macrolanguages in
|
||||
// https://unicode.org/reports/tr35/#Field_Definitionsk
|
||||
const macroLanguageMap = {
|
||||
'cmn': 'zh',
|
||||
'arb': 'ar',
|
||||
'zsm': 'ms',
|
||||
'swh': 'sw',
|
||||
'uzn': 'uz',
|
||||
// 'knn': 'kok', // chrome does not ship data for kok locale
|
||||
'kmr': 'ku',
|
||||
};
|
||||
|
||||
let bestFitOpt = {localeMatcher: "best fit"};
|
||||
let defaultLocale = (new Intl.NumberFormat()).resolvedOptions().locale;
|
||||
intlObjs.forEach(function(obj) {
|
||||
for (const [macro, lang] of Object.entries(macroLanguageMap)) {
|
||||
const justMacro = [macro];
|
||||
// Test the macro language will be persist in the supportedLocalesOf
|
||||
assertEquals([lang], obj.supportedLocalesOf(justMacro));
|
||||
assertEquals([lang], obj.supportedLocalesOf(justMacro, bestFitOpt));
|
||||
|
||||
// Test the macro language would be resolved to a locale other than the
|
||||
// default locale.
|
||||
if (obj == Intl.DisplayNames) {
|
||||
assertTrue(defaultLocale != (new obj(macro, {type: "language"}))
|
||||
.resolvedOptions().locale);
|
||||
assertTrue(defaultLocale !=
|
||||
(new obj(macro, {type: "language", localeMatcher: "best fit"}))
|
||||
.resolvedOptions().locale);
|
||||
} else {
|
||||
assertTrue(defaultLocale != (new obj(macro)).resolvedOptions().locale);
|
||||
assertTrue(defaultLocale != (new obj(macro, bestFitOpt))
|
||||
.resolvedOptions().locale);
|
||||
}
|
||||
}
|
||||
});
|
46
test/intl/localematcher/bestfit-supplemental-files.js
Normal file
46
test/intl/localematcher/bestfit-supplemental-files.js
Normal file
@ -0,0 +1,46 @@
|
||||
// Copyright 2021 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Flags: --harmony_intl_best_fit_matcher
|
||||
//
|
||||
// Test the supportedLocales and resolvedOptions based on
|
||||
// https://github.com/unicode-org/cldr/blob/master/common/supplemental/languageInfo.xml
|
||||
|
||||
const intlObjs = [
|
||||
Intl.Collator,
|
||||
Intl.DateTimeFormat,
|
||||
Intl.DisplayNames,
|
||||
Intl.ListFormat,
|
||||
Intl.NumberFormat,
|
||||
Intl.RelativeTimeFormat,
|
||||
Intl.PluralRules,
|
||||
Intl.Segmenter,
|
||||
];
|
||||
|
||||
|
||||
function verifyResolvedLocale(obj, opt, l1, l2) {
|
||||
let options = {}
|
||||
options = Object.assign(options, opt);
|
||||
if (obj == Intl.DisplayNames) {
|
||||
options['type'] = "language";
|
||||
}
|
||||
let resolvedLocale = (new obj(l1, options)).resolvedOptions().locale;
|
||||
assertTrue((l1 == resolvedLocale) ||
|
||||
(l2 == resolvedLocale.substring(0, l2.length)));
|
||||
}
|
||||
|
||||
let bestFitOpt = {localeMatcher: "best fit"};
|
||||
let defaultLocale = (new Intl.NumberFormat()).resolvedOptions().locale;
|
||||
intlObjs.forEach(function(obj) {
|
||||
[{}, bestFitOpt].forEach(function(opt) {
|
||||
print(obj);
|
||||
verifyResolvedLocale(obj, opt, "co", "fr");
|
||||
verifyResolvedLocale(obj, opt, "crs", "fr");
|
||||
verifyResolvedLocale(obj, opt, "lb", "de");
|
||||
verifyResolvedLocale(obj, opt, "gsw", "de");
|
||||
verifyResolvedLocale(obj, opt, "btj", "ms");
|
||||
verifyResolvedLocale(obj, opt, "bjn", "ms");
|
||||
});
|
||||
|
||||
});
|
@ -22,5 +22,17 @@ function verifySupportedLocalesAreSubarray(f, ll) {
|
||||
assertSubarray(f(ll), ll);
|
||||
}
|
||||
|
||||
verifySupportedLocalesAreSubarray(Intl.DateTimeFormat.supportedLocalesOf, ['en', 'ceb']);
|
||||
verifySupportedLocalesAreSubarray(Intl.DateTimeFormat.supportedLocalesOf, ['en', 'ceb', 'fil']);
|
||||
const intlObjs = [
|
||||
Intl.Collator,
|
||||
Intl.DateTimeFormat,
|
||||
Intl.DisplayNames,
|
||||
Intl.ListFormat,
|
||||
Intl.NumberFormat,
|
||||
Intl.PluralRules,
|
||||
Intl.RelativeTimeFormat,
|
||||
Intl.Segmenter,
|
||||
];
|
||||
intlObjs.forEach(function(obj) {
|
||||
verifySupportedLocalesAreSubarray(obj.supportedLocalesOf, ['en', 'ceb']);
|
||||
verifySupportedLocalesAreSubarray(obj.supportedLocalesOf, ['en', 'ceb', 'fil']);
|
||||
});
|
Loading…
Reference in New Issue
Block a user