blob: f7d3ce43a9116c00411e6934ae789eaab27e4a9f [file] [log] [blame]
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +00001""" Test script for the Unicode implementation.
2
3Written by Bill Tutt.
Fredrik Lundh06d12682001-01-24 07:59:11 +00004Modified for Python 2.0 by Fredrik Lundh (fredrik@pythonware.com)
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +00005
6(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
7
8"""#"
Marc-André Lemburg36619082001-01-17 19:11:13 +00009from test_support import verify, verbose
10
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +000011print 'Testing General Unicode Character Name, and case insensitivity...',
12
13# General and case insensitivity test:
Fredrik Lundhf6056062001-01-20 11:15:25 +000014try:
15 # put all \N escapes inside exec'd raw strings, to make sure this
16 # script runs even if the compiler chokes on \N escapes
17 exec r"""
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +000018s = u"\N{LATIN CAPITAL LETTER T}" \
19 u"\N{LATIN SMALL LETTER H}" \
20 u"\N{LATIN SMALL LETTER E}" \
21 u"\N{SPACE}" \
22 u"\N{LATIN SMALL LETTER R}" \
23 u"\N{LATIN CAPITAL LETTER E}" \
24 u"\N{LATIN SMALL LETTER D}" \
25 u"\N{SPACE}" \
26 u"\N{LATIN SMALL LETTER f}" \
27 u"\N{LATIN CAPITAL LeTtEr o}" \
28 u"\N{LATIN SMaLl LETTER x}" \
29 u"\N{SPACE}" \
30 u"\N{LATIN SMALL LETTER A}" \
31 u"\N{LATIN SMALL LETTER T}" \
32 u"\N{LATIN SMALL LETTER E}" \
33 u"\N{SPACE}" \
34 u"\N{LATIN SMALL LETTER T}" \
35 u"\N{LATIN SMALL LETTER H}" \
36 u"\N{LATIN SMALL LETTER E}" \
37 u"\N{SpAcE}" \
38 u"\N{LATIN SMALL LETTER S}" \
39 u"\N{LATIN SMALL LETTER H}" \
40 u"\N{LATIN SMALL LETTER E}" \
41 u"\N{LATIN SMALL LETTER E}" \
42 u"\N{LATIN SMALL LETTER P}" \
43 u"\N{FULL STOP}"
Marc-André Lemburg36619082001-01-17 19:11:13 +000044verify(s == u"The rEd fOx ate the sheep.", s)
Fredrik Lundhf6056062001-01-20 11:15:25 +000045"""
46except UnicodeError, v:
47 print v
Fredrik Lundh2acb54a2001-01-19 11:13:46 +000048print "done."
Fredrik Lundhee865c62001-01-19 11:00:42 +000049
Fredrik Lundh06d12682001-01-24 07:59:11 +000050import unicodedata
Fredrik Lundhee865c62001-01-19 11:00:42 +000051
Fredrik Lundh2acb54a2001-01-19 11:13:46 +000052print "Testing name to code mapping....",
Fredrik Lundhee865c62001-01-19 11:00:42 +000053for char in "SPAM":
54 name = "LATIN SMALL LETTER %s" % char
Fredrik Lundh06d12682001-01-24 07:59:11 +000055 code = unicodedata.lookup(name)
56 verify(unicodedata.name(code) == name)
Fredrik Lundh2acb54a2001-01-19 11:13:46 +000057print "done."
Fredrik Lundhee865c62001-01-19 11:00:42 +000058
Fredrik Lundh2acb54a2001-01-19 11:13:46 +000059print "Testing code to name mapping for all characters....",
60count = 0
Fredrik Lundhee865c62001-01-19 11:00:42 +000061for code in range(65536):
62 try:
Fredrik Lundh06d12682001-01-24 07:59:11 +000063 char = unichr(code)
64 name = unicodedata.name(char)
65 verify(unicodedata.lookup(name) == char)
Fredrik Lundh2acb54a2001-01-19 11:13:46 +000066 count += 1
Fredrik Lundh06d12682001-01-24 07:59:11 +000067 except (KeyError, ValueError):
Fredrik Lundhee865c62001-01-19 11:00:42 +000068 pass
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +000069print "done."
70
Fredrik Lundh2acb54a2001-01-19 11:13:46 +000071print "Found", count, "characters in the unicode name database"
72
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +000073# misc. symbol testing
74print "Testing misc. symbols for unicode character name expansion....",
Fredrik Lundhf6056062001-01-20 11:15:25 +000075exec r"""
Marc-André Lemburg36619082001-01-17 19:11:13 +000076verify(u"\N{PILCROW SIGN}" == u"\u00b6")
77verify(u"\N{REPLACEMENT CHARACTER}" == u"\uFFFD")
78verify(u"\N{HALFWIDTH KATAKANA SEMI-VOICED SOUND MARK}" == u"\uFF9F")
79verify(u"\N{FULLWIDTH LATIN SMALL LETTER A}" == u"\uFF41")
Fredrik Lundhf6056062001-01-20 11:15:25 +000080"""
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +000081print "done."
82
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +000083# strict error testing:
84print "Testing unicode character name expansion strict error handling....",
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +000085try:
Fred Drake004d5e62000-10-23 17:22:08 +000086 unicode("\N{blah}", 'unicode-escape', 'strict')
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +000087except UnicodeError:
Fred Drake004d5e62000-10-23 17:22:08 +000088 pass
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +000089else:
Fred Drake004d5e62000-10-23 17:22:08 +000090 raise AssertionError, "failed to raise an exception when given a bogus character name"
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +000091
92try:
Fredrik Lundh0fdb90c2001-01-19 09:45:02 +000093 unicode("\N{" + "x" * 100000 + "}", 'unicode-escape', 'strict')
94except UnicodeError:
95 pass
96else:
97 raise AssertionError, "failed to raise an exception when given a very " \
98 "long bogus character name"
99
100try:
Fred Drake004d5e62000-10-23 17:22:08 +0000101 unicode("\N{SPACE", 'unicode-escape', 'strict')
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +0000102except UnicodeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000103 pass
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +0000104else:
Fred Drake004d5e62000-10-23 17:22:08 +0000105 raise AssertionError, "failed to raise an exception for a missing closing brace."
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +0000106
107try:
Fred Drake004d5e62000-10-23 17:22:08 +0000108 unicode("\NSPACE", 'unicode-escape', 'strict')
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +0000109except UnicodeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000110 pass
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +0000111else:
Fred Drake004d5e62000-10-23 17:22:08 +0000112 raise AssertionError, "failed to raise an exception for a missing opening brace."
Marc-André Lemburg6cdec2e2000-06-30 09:45:20 +0000113print "done."