2000-06-30 06:45:20 -03:00
|
|
|
""" Test script for the Unicode implementation.
|
|
|
|
|
|
|
|
Written by Bill Tutt.
|
2001-01-24 03:59:11 -04:00
|
|
|
Modified for Python 2.0 by Fredrik Lundh (fredrik@pythonware.com)
|
2000-06-30 06:45:20 -03:00
|
|
|
|
|
|
|
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
|
|
|
|
|
|
|
|
"""#"
|
2003-02-26 10:49:41 -04:00
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
2008-05-20 18:35:26 -03:00
|
|
|
from test import support
|
2003-02-26 10:49:41 -04:00
|
|
|
|
|
|
|
class UnicodeNamesTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def checkletter(self, name, code):
|
|
|
|
# Helper that put all \N escapes inside eval'd raw strings,
|
2003-03-07 13:30:48 -04:00
|
|
|
# to make sure this script runs even if the compiler
|
2003-02-26 10:49:41 -04:00
|
|
|
# chokes on \N escapes
|
2007-07-20 21:15:34 -03:00
|
|
|
res = eval(r'"\N{%s}"' % name)
|
2003-02-26 10:49:41 -04:00
|
|
|
self.assertEqual(res, code)
|
|
|
|
return res
|
|
|
|
|
|
|
|
def test_general(self):
|
|
|
|
# General and case insensitivity test:
|
|
|
|
chars = [
|
|
|
|
"LATIN CAPITAL LETTER T",
|
|
|
|
"LATIN SMALL LETTER H",
|
|
|
|
"LATIN SMALL LETTER E",
|
|
|
|
"SPACE",
|
|
|
|
"LATIN SMALL LETTER R",
|
|
|
|
"LATIN CAPITAL LETTER E",
|
|
|
|
"LATIN SMALL LETTER D",
|
|
|
|
"SPACE",
|
|
|
|
"LATIN SMALL LETTER f",
|
|
|
|
"LATIN CAPITAL LeTtEr o",
|
|
|
|
"LATIN SMaLl LETTER x",
|
|
|
|
"SPACE",
|
|
|
|
"LATIN SMALL LETTER A",
|
|
|
|
"LATIN SMALL LETTER T",
|
|
|
|
"LATIN SMALL LETTER E",
|
|
|
|
"SPACE",
|
|
|
|
"LATIN SMALL LETTER T",
|
|
|
|
"LATIN SMALL LETTER H",
|
|
|
|
"LATIN SMALL LETTER E",
|
|
|
|
"SpAcE",
|
|
|
|
"LATIN SMALL LETTER S",
|
|
|
|
"LATIN SMALL LETTER H",
|
|
|
|
"LATIN small LETTER e",
|
|
|
|
"LATIN small LETTER e",
|
|
|
|
"LATIN SMALL LETTER P",
|
|
|
|
"FULL STOP"
|
|
|
|
]
|
2007-05-02 16:09:54 -03:00
|
|
|
string = "The rEd fOx ate the sheep."
|
2003-02-26 10:49:41 -04:00
|
|
|
|
|
|
|
self.assertEqual(
|
2007-05-02 16:09:54 -03:00
|
|
|
"".join([self.checkletter(*args) for args in zip(chars, string)]),
|
2003-02-26 10:49:41 -04:00
|
|
|
string
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ascii_letters(self):
|
|
|
|
import unicodedata
|
|
|
|
|
2007-05-07 19:24:25 -03:00
|
|
|
for char in "".join(map(chr, range(ord("a"), ord("z")))):
|
2003-02-26 10:49:41 -04:00
|
|
|
name = "LATIN SMALL LETTER %s" % char.upper()
|
|
|
|
code = unicodedata.lookup(name)
|
|
|
|
self.assertEqual(unicodedata.name(code), name)
|
|
|
|
|
|
|
|
def test_hangul_syllables(self):
|
2007-05-02 16:09:54 -03:00
|
|
|
self.checkletter("HANGUL SYLLABLE GA", "\uac00")
|
|
|
|
self.checkletter("HANGUL SYLLABLE GGWEOSS", "\uafe8")
|
|
|
|
self.checkletter("HANGUL SYLLABLE DOLS", "\ub3d0")
|
|
|
|
self.checkletter("HANGUL SYLLABLE RYAN", "\ub7b8")
|
|
|
|
self.checkletter("HANGUL SYLLABLE MWIK", "\ubba0")
|
|
|
|
self.checkletter("HANGUL SYLLABLE BBWAEM", "\ubf88")
|
|
|
|
self.checkletter("HANGUL SYLLABLE SSEOL", "\uc370")
|
|
|
|
self.checkletter("HANGUL SYLLABLE YI", "\uc758")
|
|
|
|
self.checkletter("HANGUL SYLLABLE JJYOSS", "\ucb40")
|
|
|
|
self.checkletter("HANGUL SYLLABLE KYEOLS", "\ucf28")
|
|
|
|
self.checkletter("HANGUL SYLLABLE PAN", "\ud310")
|
|
|
|
self.checkletter("HANGUL SYLLABLE HWEOK", "\ud6f8")
|
|
|
|
self.checkletter("HANGUL SYLLABLE HIH", "\ud7a3")
|
2003-02-26 10:49:41 -04:00
|
|
|
|
|
|
|
import unicodedata
|
2007-05-02 16:09:54 -03:00
|
|
|
self.assertRaises(ValueError, unicodedata.name, "\ud7a4")
|
2003-02-26 10:49:41 -04:00
|
|
|
|
|
|
|
def test_cjk_unified_ideographs(self):
|
2007-05-02 16:09:54 -03:00
|
|
|
self.checkletter("CJK UNIFIED IDEOGRAPH-3400", "\u3400")
|
|
|
|
self.checkletter("CJK UNIFIED IDEOGRAPH-4DB5", "\u4db5")
|
|
|
|
self.checkletter("CJK UNIFIED IDEOGRAPH-4E00", "\u4e00")
|
|
|
|
self.checkletter("CJK UNIFIED IDEOGRAPH-9FA5", "\u9fa5")
|
|
|
|
self.checkletter("CJK UNIFIED IDEOGRAPH-20000", "\U00020000")
|
|
|
|
self.checkletter("CJK UNIFIED IDEOGRAPH-2A6D6", "\U0002a6d6")
|
2003-02-26 10:49:41 -04:00
|
|
|
|
|
|
|
def test_bmp_characters(self):
|
|
|
|
import unicodedata
|
|
|
|
count = 0
|
2007-05-07 19:24:25 -03:00
|
|
|
for code in range(0x10000):
|
2007-05-03 14:18:26 -03:00
|
|
|
char = chr(code)
|
2003-02-26 10:49:41 -04:00
|
|
|
name = unicodedata.name(char, None)
|
|
|
|
if name is not None:
|
|
|
|
self.assertEqual(unicodedata.lookup(name), char)
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
def test_misc_symbols(self):
|
2007-05-02 16:09:54 -03:00
|
|
|
self.checkletter("PILCROW SIGN", "\u00b6")
|
|
|
|
self.checkletter("REPLACEMENT CHARACTER", "\uFFFD")
|
|
|
|
self.checkletter("HALFWIDTH KATAKANA SEMI-VOICED SOUND MARK", "\uFF9F")
|
|
|
|
self.checkletter("FULLWIDTH LATIN SMALL LETTER A", "\uFF41")
|
2003-02-26 10:49:41 -04:00
|
|
|
|
|
|
|
def test_errors(self):
|
|
|
|
import unicodedata
|
|
|
|
self.assertRaises(TypeError, unicodedata.name)
|
2007-05-02 16:09:54 -03:00
|
|
|
self.assertRaises(TypeError, unicodedata.name, 'xx')
|
2003-02-26 10:49:41 -04:00
|
|
|
self.assertRaises(TypeError, unicodedata.lookup)
|
2007-05-02 16:09:54 -03:00
|
|
|
self.assertRaises(KeyError, unicodedata.lookup, 'unknown')
|
2003-02-26 10:49:41 -04:00
|
|
|
|
2007-07-20 21:15:34 -03:00
|
|
|
def test_strict_error_handling(self):
|
2003-02-26 10:49:41 -04:00
|
|
|
# bogus character name
|
|
|
|
self.assertRaises(
|
|
|
|
UnicodeError,
|
2007-07-20 21:15:34 -03:00
|
|
|
str, b"\\N{blah}", 'unicode-escape', 'strict'
|
2003-02-26 10:49:41 -04:00
|
|
|
)
|
|
|
|
# long bogus character name
|
|
|
|
self.assertRaises(
|
|
|
|
UnicodeError,
|
2007-08-27 15:31:48 -03:00
|
|
|
str, bytes("\\N{%s}" % ("x" * 100000), "ascii"), 'unicode-escape', 'strict'
|
2003-02-26 10:49:41 -04:00
|
|
|
)
|
|
|
|
# missing closing brace
|
|
|
|
self.assertRaises(
|
|
|
|
UnicodeError,
|
2007-07-20 21:15:34 -03:00
|
|
|
str, b"\\N{SPACE", 'unicode-escape', 'strict'
|
2003-02-26 10:49:41 -04:00
|
|
|
)
|
|
|
|
# missing opening brace
|
|
|
|
self.assertRaises(
|
|
|
|
UnicodeError,
|
2007-07-20 21:15:34 -03:00
|
|
|
str, b"\\NSPACE", 'unicode-escape', 'strict'
|
2003-02-26 10:49:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_main():
|
2008-05-20 18:35:26 -03:00
|
|
|
support.run_unittest(UnicodeNamesTest)
|
2003-02-26 10:49:41 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|