1- # Copyright (c) 2018, 2021 , Oracle and/or its affiliates. All rights reserved.
1+ # Copyright (c) 2018, 2025 , Oracle and/or its affiliates. All rights reserved.
22# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33#
44# The Universal Permissive License (UPL), Version 1.0
3737# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838# SOFTWARE.
3939
40+ import unicodedata
41+ import unittest
4042
41- def assert_raises (err , fn , * args , ** kwargs ):
42- raised = False
43- try :
44- fn (* args , ** kwargs )
45- except err :
46- raised = True
47- assert raised
43+ class TestUnicodedata (unittest .TestCase ):
4844
45+ def test_args_validation (self ):
46+ self .assertRaises (TypeError , unicodedata .category , None )
47+ self .assertRaises (TypeError , unicodedata .bidirectional , None )
48+ self .assertRaises (TypeError , unicodedata .name , None )
4949
50- def test_args_validation ():
51- import unicodedata
52- assert_raises (TypeError , unicodedata .category , None )
53- assert_raises (TypeError , unicodedata .bidirectional , None )
54- assert_raises (TypeError , unicodedata .name , None )
5550
51+ def test_normalize (self ):
52+ self .assertRaises (TypeError , unicodedata .normalize )
53+ self .assertRaises (ValueError , unicodedata .normalize , 'unknown' , 'xx' )
54+ assert unicodedata .normalize ('NFKC' , '' ) == ''
5655
57- def test_normalize ():
58- import unicodedata
59- assert_raises (TypeError , unicodedata .normalize )
60- assert_raises (ValueError , unicodedata .normalize , 'unknown' , 'xx' )
61- assert unicodedata .normalize ('NFKC' , '' ) == ''
6256
57+ def test_category (self ):
58+ assert unicodedata .category ('\uFFFE ' ) == 'Cn'
59+ assert unicodedata .category ('a' ) == 'Ll'
60+ assert unicodedata .category ('A' ) == 'Lu'
61+ self .assertRaises (TypeError , unicodedata .category )
62+ self .assertRaises (TypeError , unicodedata .category , 'xx' )
6363
64- def test_category ():
65- import unicodedata
66- assert unicodedata .category ('\uFFFE ' ) == 'Cn'
67- assert unicodedata .category ('a' ) == 'Ll'
68- assert unicodedata .category ('A' ) == 'Lu'
69- assert_raises (TypeError , unicodedata .category )
70- assert_raises (TypeError , unicodedata .category , 'xx' )
64+
65+ def test_lookup (self ):
66+ unicode_name = "ARABIC SMALL HIGH LIGATURE ALEF WITH LAM WITH YEH"
67+ self .assertEqual (unicodedata .lookup (unicode_name ), "\u0616 " )
68+
69+ unicode_name_alias = "ARABIC SMALL HIGH LIGATURE ALEF WITH YEH BARREE"
70+ self .assertEqual (unicodedata .lookup (unicode_name_alias ), "\u0616 " )
71+
72+ with self .assertRaisesRegex (KeyError , "undefined character name 'wrong-name'" ):
73+ unicodedata .lookup ("wrong-name" )
74+
75+ with self .assertRaisesRegex (KeyError , "name too long" ):
76+ unicodedata .lookup ("a" * 257 )
77+
78+
79+ def test_east_asian_width (self ):
80+ list = [1 , 2 , 3 ]
81+ with self .assertRaisesRegex (TypeError , r"east_asian_width\(\) argument must be a unicode character, not list" ):
82+ unicodedata .east_asian_width (list )
83+
84+ multi_character_string = "abc"
85+ with self .assertRaisesRegex (TypeError , r"east_asian_width\(\) argument must be a unicode character, not str" ):
86+ unicodedata .east_asian_width (multi_character_string )
87+
88+ empty_string = ""
89+ with self .assertRaisesRegex (TypeError , r"east_asian_width\(\) argument must be a unicode character, not str" ):
90+ unicodedata .east_asian_width (empty_string )
91+
92+
93+ def test_combining (self ):
94+ list = [1 , 2 , 3 ]
95+ with self .assertRaisesRegex (TypeError , r"combining\(\) argument must be a unicode character, not list" ):
96+ unicodedata .combining (list )
97+
98+ multi_character_string = "abc"
99+ with self .assertRaisesRegex (TypeError , r"combining\(\) argument must be a unicode character, not str" ):
100+ unicodedata .combining (multi_character_string )
101+
102+ empty_string = ""
103+ with self .assertRaisesRegex (TypeError , r"combining\(\) argument must be a unicode character, not str" ):
104+ unicodedata .combining (empty_string )
0 commit comments