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
|
# ====================================================================
# Copyright (c) 2007-2013 Open Source Applications Foundation.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# ====================================================================
#
# This is a python translation of ICU's LocaleDataTest.java
import sys, os
from unittest import TestCase, main
from icu import *
NOT_FOUND_ERR = 'The requested resource cannot be found, error code: 2'
def print_output(string):
pass
class ExemplarGroup:
def __init__(_self, s, scriptCodes):
_self.set = s
_self.scs = scriptCodes
def __hash__(_self):
hash = 0
for i in range(len(_self.scs) if len(_self.scs) < 4 else 4):
hash = (hash << 8) + _self.scs[i]
return hash
def __eq__(_self, other):
return (_self.set == other.set) and (_self.scs == other.scs)
class TestLocaleData(TestCase):
def setUp(self):
self.availableLocales = Locale.getAvailableLocales()
def testPapersize(self):
for locale in self.availableLocales.keys():
papersize = LocaleData(locale).getPaperSize()
#language = Locale(locale).getLanguage()
country = Locale(locale).getCountry()
if (['BZ','CA','CL','CO','CR','GT','MX','NI','PA','PH','PR','SV','US','VE'].count(country) > 0):
self.assertTrue(papersize == (279, 216))
elif country:
self.assertTrue(papersize == (297, 210))
def testMeasurementSystem(self):
for locale in self.availableLocales.keys():
measurementSystem = LocaleData(locale).getMeasurementSystem()
#language = Locale(locale).getLanguage()
country = Locale(locale).getCountry()
# 0 means SI, 1 means US
if (['LR', 'MM', 'US'].count(country) > 0):
self.assertTrue(measurementSystem == 1)
elif country:
self.assertTrue(measurementSystem == 0)
def testExemplarSet(self):
testedExemplars = set()
equalCount = 0
for locale in self.availableLocales:
scriptCodes = Script.getCode(locale)
exemplarSets = []
for k in range(2):
# it seems USET_CASE_INSENSITIVE = 2
option = 0 if k == 0 else 2
# standard = 0, auxiliary = 1, index = 2, count = 3
exemplarSet = LocaleData(locale).getExemplarSet(option, 0)
exemplarSets.append(exemplarSet)
exGrp = ExemplarGroup(exemplarSet, scriptCodes)
if (not exGrp in testedExemplars):
testedExemplars.add(exGrp)
sets = []
for j in range(len(scriptCodes)):
sets.append(UnicodeSet('[:' + Script(scriptCodes[j]).getShortName() + ':]'))
existsInScript = False
iter = UnicodeSetIterator(exemplarSet)
while (not existsInScript and iter.nextRange()):
if (iter.getCodepoint != iter.isString()):
for j in range(len(sets)):
if sets[j].contains(iter.getCodepoint(), iter.getCodepointEnd()):
existsInScript = True
break
else:
for j in range(len(sets)):
if sets[j].contains(iter.getString):
existsInScript = True
break
if existsInScript == False:
print_output("ExemplarSet containment failed for locale : "+ locale)
print_output(locale + " exemplar " + repr(unicode(exemplarSets[0])))
print_output(locale + " exemplar(case-folded) " + repr(unicode(exemplarSets[1])))
self.assertTrue(locale + " case-folded is a superset", exemplarSets[1].containsAll(exemplarSets[0]))
if (exemplarSets[1] == exemplarSets[0]):
++equalCount
self.assertTrue("case-folded is sometimes a strict superset, and sometimes equal",\
equalCount > 0 and equalCount < len(self.availableLocales))
def testExemplarSet2(self):
testedExemplars = set()
equalCount = 0
for locale in self.availableLocales:
ld = LocaleData(locale)
scriptCodes = Script.getCode(locale)
exemplarSets = []
for k in range(2): # for casing option in (normal, uncased)
# it seems USET_CASE_INSENSITIVE = 2
option = 0 if k == 0 else 2
for h in range(2):
esType = 0 if h == 0 else 1
# standard = 0, auxiliary = 1, index = 2, count = 3
exemplarSet = LocaleData(locale).getExemplarSet(option, esType)
exemplarSets.append(exemplarSet)
exGrp = ExemplarGroup(exemplarSet, scriptCodes)
if (not exGrp in testedExemplars):
testedExemplars.add(exGrp)
sets = []
for j in range(len(scriptCodes)):
sets.append(UnicodeSet('[:' + Script(scriptCodes[j]).getShortName() + ':]'))
existsInScript = False
iter = UnicodeSetIterator(exemplarSet)
while (not existsInScript and iter.nextRange()):
if (iter.getCodepoint != iter.isString()):
for j in range(len(sets)):
if sets[j].contains(iter.getCodepoint(), iter.getCodepointEnd()):
existsInScript = True
break
else:
for j in range(len(sets)):
if sets[j].contains(iter.getString):
existsInScript = True
break
if existsInScript == False and h == 0:
print_output("ExemplarSet containment failed for locale,option,type : " \
+ locale + "," + str(option) + "," + str(esType))
print_output(locale + " exemplar(ES_STANDARD)" + repr(unicode(exemplarSets[0])))
print_output(locale + " exemplar(ES_AUXILIARY)" + repr(unicode(exemplarSets[1])))
print_output(locale + " exemplar(case-folded,ES_STANDARD)" + repr(unicode(exemplarSets[2])))
print_output(locale + " exemplar(case-folded,ES_AUXILIARY)" + repr(unicode(exemplarSets[3])))
self.assertTrue(locale + " case-folded is a superset", exemplarSets[2].containsAll(exemplarSets[0]))
self.assertTrue(locale + " case-folder is a superset", exemplarSets[3].containsAll(exemplarSets[1]))
if (exemplarSets[2] == exemplarSets[0]):
++equalCount
if (exemplarSets[3] == exemplarSets[1]):
++equalCount
self.assertTrue("case-folded is sometimes a strict superset, and sometimes equal",\
equalCount > 0 and equalCount < len(self.availableLocales) * 2)
def testCoverage(self):
ld = LocaleData(Locale().getName())
t = ld.getNoSubstitute()
ld.setNoSubstitute(t)
self.assertEqual(t, ld.getNoSubstitute())
for i in range(4):
print_output(repr(ld.getDelimiter(i)))
def testLocaleDisplayPattern(self):
ld = LocaleData(Locale().getName())
print_output("Default locale LocaleDisplayPattern:" + ld.getLocaleDisplayPattern());
print_output("Default locale LocaleSeparator:" + ld.getLocaleSeparator());
for locale in self.availableLocales:
ld = LocaleData(locale)
try:
print_output(locale + " LocaleDisplayPattern:" + repr(ld.getLocaleDisplayPattern()))
getLocaleDisplayPattern = True
except ICUError, e: # resource not found
getLocaleDisplayPattern = str(e) == NOT_FOUND_ERR
except:
getLocaleDisplayPattern = False
self.assertTrue(getLocaleDisplayPattern)
try:
print_output(locale + " LocaleSeparator:" + repr(ld.getLocaleSeparator()))
getLocaleSeparator = True
except ICUError, e: # resource not found
getLocaleSeparator = str(e) == NOT_FOUND_ERR
except:
getLocaleSeparator = False
self.assertTrue(getLocaleSeparator)
if __name__ == "__main__":
main()
|