1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
# Copyright (C) 2018 and later: Unicode, Inc. and others.
# License & terms of use: http://www.unicode.org/copyright.html
import io as pyio
import json
import os
import unittest
from .. import InFile
from ..comment_stripper import CommentStripper
from ..filtration import Filter
EXAMPLE_FILE_STEMS = [
"af_NA",
"af_VARIANT",
"af_ZA_VARIANT",
"af_ZA",
"af",
"ar",
"ar_SA",
"ars",
"bs_BA",
"bs_Cyrl_BA",
"bs_Cyrl",
"bs_Latn_BA",
"bs_Latn",
"bs",
"en_001",
"en_150",
"en_DE",
"en_GB",
"en_US",
"root",
"sr_BA",
"sr_CS",
"sr_Cyrl_BA",
"sr_Cyrl_CS",
"sr_Cyrl_ME",
"sr_Cyrl",
"sr_Latn_BA",
"sr_Latn_CS",
"sr_Latn_ME_VARIANT",
"sr_Latn_ME",
"sr_Latn",
"sr_ME",
"sr",
"vai_Latn_LR",
"vai_Latn",
"vai_LR",
"vai_Vaii_LR",
"vai_Vaii",
"vai",
"yue",
"zh_CN",
"zh_Hans_CN",
"zh_Hans_HK",
"zh_Hans_MO",
"zh_Hans_SG",
"zh_Hans",
"zh_Hant_HK",
"zh_Hant_MO",
"zh_Hant_TW",
"zh_Hant",
"zh_HK",
"zh_MO",
"zh_SG",
"zh_TW",
"zh"
]
class TestIO(object):
def __init__(self):
pass
def read_locale_deps(self, tree):
if tree not in ("brkitr", "locales", "rbnf"):
return None
with pyio.open(os.path.join(
os.path.dirname(__file__),
"sample_data",
tree,
"LOCALE_DEPS.json"
), "r", encoding="utf-8-sig") as f:
return json.load(CommentStripper(f))
class FiltrationTest(unittest.TestCase):
def test_exclude(self):
self._check_filter(Filter.create_from_json({
"filterType": "exclude"
}, TestIO()), [
])
def test_default_whitelist(self):
self._check_filter(Filter.create_from_json({
"whitelist": [
"ars",
"zh_Hans"
]
}, TestIO()), [
"ars",
"zh_Hans"
])
def test_default_blacklist(self):
expected_matches = set(EXAMPLE_FILE_STEMS)
expected_matches.remove("ars")
expected_matches.remove("zh_Hans")
self._check_filter(Filter.create_from_json({
"blacklist": [
"ars",
"zh_Hans"
]
}, TestIO()), expected_matches)
def test_language_whitelist(self):
self._check_filter(Filter.create_from_json({
"filterType": "language",
"whitelist": [
"af",
"bs"
]
}, TestIO()), [
"root",
"af_NA",
"af_VARIANT",
"af_ZA_VARIANT",
"af_ZA",
"af",
"bs_BA",
"bs_Cyrl_BA",
"bs_Cyrl",
"bs_Latn_BA",
"bs_Latn",
"bs"
])
def test_language_blacklist(self):
expected_matches = set(EXAMPLE_FILE_STEMS)
expected_matches.remove("af_NA")
expected_matches.remove("af_VARIANT")
expected_matches.remove("af_ZA_VARIANT")
expected_matches.remove("af_ZA")
expected_matches.remove("af")
self._check_filter(Filter.create_from_json({
"filterType": "language",
"blacklist": [
"af"
]
}, TestIO()), expected_matches)
def test_regex_whitelist(self):
self._check_filter(Filter.create_from_json({
"filterType": "regex",
"whitelist": [
r"^ar.*$",
r"^zh$"
]
}, TestIO()), [
"ar",
"ar_SA",
"ars",
"zh"
])
def test_regex_blacklist(self):
expected_matches = set(EXAMPLE_FILE_STEMS)
expected_matches.remove("ar")
expected_matches.remove("ar_SA")
expected_matches.remove("ars")
expected_matches.remove("zh")
self._check_filter(Filter.create_from_json({
"filterType": "regex",
"blacklist": [
r"^ar.*$",
r"^zh$"
]
}, TestIO()), expected_matches)
def test_locale_basic(self):
self._check_filter(Filter.create_from_json({
"filterType": "locale",
"whitelist": [
# Default scripts:
# sr => Cyrl
# vai => Vaii
# zh => Hans
"bs_BA", # is an alias to bs_Latn_BA
"en_DE",
"sr", # Language with no script
"vai_Latn", # Language with non-default script
"zh_Hans" # Language with default script
]
}, TestIO()), [
"root",
# bs: should include the full dependency tree of bs_BA
"bs_BA",
"bs_Latn_BA",
"bs_Latn",
"bs",
# en: should include the full dependency tree of en_DE
"en",
"en_DE",
"en_150",
"en_001",
# sr: include Cyrl, the default, but not Latn.
"sr",
"sr_BA",
"sr_CS",
"sr_Cyrl",
"sr_Cyrl_BA",
"sr_Cyrl_CS",
"sr_Cyrl_ME",
# vai: include Latn but NOT Vaii.
"vai_Latn",
"vai_Latn_LR",
# zh: include Hans but NOT Hant.
"zh",
"zh_CN",
"zh_SG",
"zh_Hans",
"zh_Hans_CN",
"zh_Hans_HK",
"zh_Hans_MO",
"zh_Hans_SG"
])
def test_locale_no_children(self):
self._check_filter(Filter.create_from_json({
"filterType": "locale",
"includeChildren": False,
"whitelist": [
# See comments in test_locale_basic.
"bs_BA",
"en_DE",
"sr",
"vai_Latn",
"zh_Hans"
]
}, TestIO()), [
"root",
"bs_BA",
"bs_Latn_BA",
"bs_Latn",
"bs",
"en",
"en_DE",
"en_150",
"en_001",
"sr",
"vai_Latn",
"zh",
"zh_Hans",
])
def test_locale_include_scripts(self):
self._check_filter(Filter.create_from_json({
"filterType": "locale",
"includeScripts": True,
"whitelist": [
# See comments in test_locale_basic.
"bs_BA",
"en_DE",
"sr",
"vai_Latn",
"zh_Hans"
]
}, TestIO()), [
"root",
# bs: includeScripts only works for language-only (without region)
"bs_BA",
"bs_Latn_BA",
"bs_Latn",
"bs",
# en: should include the full dependency tree of en_DE
"en",
"en_DE",
"en_150",
"en_001",
# sr: include Latn, since no particular script was requested.
"sr_BA",
"sr_CS",
"sr_Cyrl_BA",
"sr_Cyrl_CS",
"sr_Cyrl_ME",
"sr_Cyrl",
"sr_Latn_BA",
"sr_Latn_CS",
"sr_Latn_ME_VARIANT",
"sr_Latn_ME",
"sr_Latn",
"sr_ME",
"sr",
# vai: do NOT include Vaii; the script was explicitly requested.
"vai_Latn_LR",
"vai_Latn",
# zh: do NOT include Hant; the script was explicitly requested.
"zh_CN",
"zh_SG",
"zh_Hans_CN",
"zh_Hans_HK",
"zh_Hans_MO",
"zh_Hans_SG",
"zh_Hans",
"zh"
])
def test_locale_no_children_include_scripts(self):
self._check_filter(Filter.create_from_json({
"filterType": "locale",
"includeChildren": False,
"includeScripts": True,
"whitelist": [
# See comments in test_locale_basic.
"bs_BA",
"en_DE",
"sr",
"vai_Latn",
"zh_Hans"
]
}, TestIO()), [
"root",
# bs: includeScripts only works for language-only (without region)
"bs_BA",
"bs_Latn_BA",
"bs_Latn",
"bs",
# en: should include the full dependency tree of en_DE
"en",
"en_DE",
"en_150",
"en_001",
# sr: include Cyrl and Latn but no other children
"sr",
"sr_Cyrl",
"sr_Latn",
# vai: include only the requested script
"vai_Latn",
# zh: include only the requested script
"zh",
"zh_Hans",
])
def test_union(self):
self._check_filter(Filter.create_from_json({
"filterType": "union",
"unionOf": [
{
"whitelist": [
"ars",
"zh_Hans"
]
},
{
"filterType": "regex",
"whitelist": [
r"^bs.*$",
r"^zh$"
]
}
]
}, TestIO()), [
"ars",
"zh_Hans",
"bs_BA",
"bs_Cyrl_BA",
"bs_Cyrl",
"bs_Latn_BA",
"bs_Latn",
"bs",
"zh"
])
def test_hk_deps_normal(self):
self._check_filter(Filter.create_from_json({
"filterType": "locale",
"whitelist": [
"zh_HK"
]
}, TestIO()), [
"root",
"zh_Hant",
"zh_Hant_HK",
"zh_HK",
])
def test_hk_deps_rbnf(self):
self._check_filter(Filter.create_from_json({
"filterType": "locale",
"whitelist": [
"zh_HK"
]
}, TestIO()), [
"root",
"yue",
"zh_Hant_HK",
"zh_HK",
], "rbnf")
def test_no_alias_parent_structure(self):
self._check_filter(Filter.create_from_json({
"filterType": "locale",
"whitelist": [
"zh_HK"
]
}, TestIO()), [
"root",
"zh_HK",
"zh",
], "brkitr")
def _check_filter(self, filter, expected_matches, tree="locales"):
for file_stem in EXAMPLE_FILE_STEMS:
is_match = filter.match(InFile("%s/%s.txt" % (tree, file_stem)))
expected_match = file_stem in expected_matches
self.assertEqual(is_match, expected_match, file_stem)
# Export the test for the runner
suite = unittest.makeSuite(FiltrationTest)
|