Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 1 | """ Test script for the Unicode implementation. |
| 2 | |
| 3 | Written by Bill Tutt. |
| 4 | |
| 5 | (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. |
| 6 | |
| 7 | """#" |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 8 | from test_support import verify, verbose |
| 9 | |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 10 | print 'Testing General Unicode Character Name, and case insensitivity...', |
| 11 | |
| 12 | # General and case insensitivity test: |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 13 | try: |
| 14 | # put all \N escapes inside exec'd raw strings, to make sure this |
| 15 | # script runs even if the compiler chokes on \N escapes |
| 16 | exec r""" |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 17 | s = u"\N{LATIN CAPITAL LETTER T}" \ |
| 18 | u"\N{LATIN SMALL LETTER H}" \ |
| 19 | u"\N{LATIN SMALL LETTER E}" \ |
| 20 | u"\N{SPACE}" \ |
| 21 | u"\N{LATIN SMALL LETTER R}" \ |
| 22 | u"\N{LATIN CAPITAL LETTER E}" \ |
| 23 | u"\N{LATIN SMALL LETTER D}" \ |
| 24 | u"\N{SPACE}" \ |
| 25 | u"\N{LATIN SMALL LETTER f}" \ |
| 26 | u"\N{LATIN CAPITAL LeTtEr o}" \ |
| 27 | u"\N{LATIN SMaLl LETTER x}" \ |
| 28 | u"\N{SPACE}" \ |
| 29 | u"\N{LATIN SMALL LETTER A}" \ |
| 30 | u"\N{LATIN SMALL LETTER T}" \ |
| 31 | u"\N{LATIN SMALL LETTER E}" \ |
| 32 | u"\N{SPACE}" \ |
| 33 | u"\N{LATIN SMALL LETTER T}" \ |
| 34 | u"\N{LATIN SMALL LETTER H}" \ |
| 35 | u"\N{LATIN SMALL LETTER E}" \ |
| 36 | u"\N{SpAcE}" \ |
| 37 | u"\N{LATIN SMALL LETTER S}" \ |
| 38 | u"\N{LATIN SMALL LETTER H}" \ |
| 39 | u"\N{LATIN SMALL LETTER E}" \ |
| 40 | u"\N{LATIN SMALL LETTER E}" \ |
| 41 | u"\N{LATIN SMALL LETTER P}" \ |
| 42 | u"\N{FULL STOP}" |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 43 | verify(s == u"The rEd fOx ate the sheep.", s) |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 44 | """ |
| 45 | except UnicodeError, v: |
| 46 | print v |
Fredrik Lundh | 2acb54a | 2001-01-19 11:13:46 +0000 | [diff] [blame] | 47 | print "done." |
Fredrik Lundh | ee865c6 | 2001-01-19 11:00:42 +0000 | [diff] [blame] | 48 | |
| 49 | import ucnhash |
| 50 | |
Fredrik Lundh | 2acb54a | 2001-01-19 11:13:46 +0000 | [diff] [blame] | 51 | print "Testing name to code mapping....", |
Fredrik Lundh | ee865c6 | 2001-01-19 11:00:42 +0000 | [diff] [blame] | 52 | for char in "SPAM": |
| 53 | name = "LATIN SMALL LETTER %s" % char |
| 54 | code = ucnhash.getcode(name) |
| 55 | verify(ucnhash.getname(code) == name) |
Fredrik Lundh | 2acb54a | 2001-01-19 11:13:46 +0000 | [diff] [blame] | 56 | print "done." |
Fredrik Lundh | ee865c6 | 2001-01-19 11:00:42 +0000 | [diff] [blame] | 57 | |
Fredrik Lundh | 2acb54a | 2001-01-19 11:13:46 +0000 | [diff] [blame] | 58 | print "Testing code to name mapping for all characters....", |
| 59 | count = 0 |
Fredrik Lundh | ee865c6 | 2001-01-19 11:00:42 +0000 | [diff] [blame] | 60 | for code in range(65536): |
| 61 | try: |
| 62 | name = ucnhash.getname(code) |
| 63 | verify(ucnhash.getcode(name) == code) |
Fredrik Lundh | 2acb54a | 2001-01-19 11:13:46 +0000 | [diff] [blame] | 64 | count += 1 |
Fredrik Lundh | ee865c6 | 2001-01-19 11:00:42 +0000 | [diff] [blame] | 65 | except ValueError: |
| 66 | pass |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 67 | print "done." |
| 68 | |
Fredrik Lundh | 2acb54a | 2001-01-19 11:13:46 +0000 | [diff] [blame] | 69 | print "Found", count, "characters in the unicode name database" |
| 70 | |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 71 | # misc. symbol testing |
| 72 | print "Testing misc. symbols for unicode character name expansion....", |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 73 | exec r""" |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 74 | verify(u"\N{PILCROW SIGN}" == u"\u00b6") |
| 75 | verify(u"\N{REPLACEMENT CHARACTER}" == u"\uFFFD") |
| 76 | verify(u"\N{HALFWIDTH KATAKANA SEMI-VOICED SOUND MARK}" == u"\uFF9F") |
| 77 | verify(u"\N{FULLWIDTH LATIN SMALL LETTER A}" == u"\uFF41") |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 78 | """ |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 79 | print "done." |
| 80 | |
| 81 | |
| 82 | # strict error testing: |
| 83 | print "Testing unicode character name expansion strict error handling....", |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 84 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 85 | unicode("\N{blah}", 'unicode-escape', 'strict') |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 86 | except UnicodeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 87 | pass |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 88 | else: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 89 | raise AssertionError, "failed to raise an exception when given a bogus character name" |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 90 | |
| 91 | try: |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 92 | unicode("\N{" + "x" * 100000 + "}", 'unicode-escape', 'strict') |
| 93 | except UnicodeError: |
| 94 | pass |
| 95 | else: |
| 96 | raise AssertionError, "failed to raise an exception when given a very " \ |
| 97 | "long bogus character name" |
| 98 | |
| 99 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 100 | unicode("\N{SPACE", 'unicode-escape', 'strict') |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 101 | except UnicodeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 102 | pass |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 103 | else: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 104 | raise AssertionError, "failed to raise an exception for a missing closing brace." |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 105 | |
| 106 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 107 | unicode("\NSPACE", 'unicode-escape', 'strict') |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 108 | except UnicodeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 109 | pass |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 110 | else: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 111 | raise AssertionError, "failed to raise an exception for a missing opening brace." |
Marc-André Lemburg | 6cdec2e | 2000-06-30 09:45:20 +0000 | [diff] [blame] | 112 | print "done." |