Debian Codegraph @

tests/test_enum.py

raw
1 import threading
2 import unittest
3
4 from gi.repository import GObject
5 from .helper import capture_glib_warnings
6
7 try:
8 from gi.repository import Gtk
9 except ImportError:
10 Gtk = None
11
12
13 _lock = threading.Lock()
14 counter = 0
15
16
17 def get_id():
18 global counter
19 with _lock:
20 counter += 1
21 return counter
22
23
24 class EnumTests(unittest.TestCase):
25 def test_gtype(self):
26 class MyEnum(GObject.GEnum):
27 ONE = 1
28 TWO = 2
29 THREE = 3
30
31 # The new enum has it's own GType, which subclasses GEnum
32 self.assertIsInstance(MyEnum.__gtype__, GObject.GType)
33 self.assertNotEqual(MyEnum.__gtype__, GObject.GEnum.__gtype__)
34 self.assertEqual(MyEnum.__gtype__.parent, GObject.GEnum.__gtype__)
35 self.assertTrue(MyEnum.__gtype__.is_a(GObject.GEnum.__gtype__))
36 self.assertIn(MyEnum.__gtype__, GObject.GEnum.__gtype__.children)
37
38 # The class can be looked up by name
39 type_name = MyEnum.__gtype__.name
40 self.assertIn("MyEnum", type_name)
41 self.assertEqual(GObject.GType.from_name(type_name), MyEnum.__gtype__)
42
43 # The Python class is registered as the wrapper for the GType
44 self.assertIs(MyEnum.__gtype__.pytype, MyEnum)
45
46 def test_values(self):
47 class MyEnum(GObject.GEnum):
48 ONE = 1
49 FORTY_TWO = 42
50
51 # As this is a stdlib enum, the enum values are subclasses of the enum
52 self.assertIsInstance(MyEnum.ONE, MyEnum)
53 self.assertIsInstance(MyEnum.FORTY_TWO, MyEnum)
54
55 # We can see the registered enum values too:
56 self.assertEqual(MyEnum.ONE.value_name, "ONE")
57 self.assertEqual(MyEnum.ONE.value_nick, "one")
58 self.assertEqual(MyEnum.FORTY_TWO.value_name, "FORTY_TWO")
59 self.assertEqual(MyEnum.FORTY_TWO.value_nick, "forty-two")
60
61 def test_custom_type_name(self):
62 type_name = f"MyEnum{get_id()}"
63
64 class MyEnum(GObject.GEnum):
65 __gtype_name__ = type_name
66 ONE = 1
67
68 self.assertEqual(MyEnum.__gtype__.name, type_name)
69
70 # Trying to register a type with the same name fails:
71 with (
72 self.assertRaises(RuntimeError) as ex,
73 capture_glib_warnings(allow_criticals=True) as w,
74 ):
75
76 class MyEnum2(GObject.GEnum):
77 __gtype_name__ = type_name
78 ONE = 1
79
80 self.assertEqual(str(ex.exception), f"Unable to register enum '{type_name}'")
81 self.assertEqual(len(w), 1)
82 self.assertEqual(
83 str(w[0].message), f"cannot register existing type '{type_name}'"
84 )
85
86 @unittest.skipUnless(Gtk, "Gtk not available")
87 def test_enum_as_string(self):
88 box = Gtk.Box(orientation="vertical")
89
90 assert box.props.orientation == Gtk.Orientation.VERTICAL
91
92
93 class FlagsTests(unittest.TestCase):
94 def test_gtype(self):
95 class MyFlags(GObject.GFlags):
96 ONE = 1
97 TWO = 2
98 FOUR = 4
99
100 # The new enum has it's own GType, which subclasses GFlags
101 self.assertIsInstance(MyFlags.__gtype__, GObject.GType)
102 self.assertNotEqual(MyFlags.__gtype__, GObject.GFlags.__gtype__)
103 self.assertEqual(MyFlags.__gtype__.parent, GObject.GFlags.__gtype__)
104 self.assertTrue(MyFlags.__gtype__.is_a(GObject.GFlags.__gtype__))
105 self.assertIn(MyFlags.__gtype__, GObject.GFlags.__gtype__.children)
106
107 # The class can be looked up by name
108 type_name = MyFlags.__gtype__.name
109 self.assertIn("MyFlags", type_name)
110 self.assertEqual(GObject.GType.from_name(type_name), MyFlags.__gtype__)
111
112 # The Python class is registered as the wrapper for the GType
113 self.assertIs(MyFlags.__gtype__.pytype, MyFlags)
114
115 def test_values(self):
116 class MyFlags(GObject.GFlags):
117 ONE = 1
118 THIRTY_TWO = 32
119
120 # As this is a stdlib enum, the enum values are subclasses of the enum
121 self.assertIsInstance(MyFlags.ONE, MyFlags)
122 self.assertIsInstance(MyFlags.THIRTY_TWO, MyFlags)
123
124 # We can see the registered enum values too:
125 self.assertEqual(MyFlags.ONE.value_names, ["ONE"])
126 self.assertEqual(MyFlags.ONE.value_nicks, ["one"])
127 self.assertEqual(MyFlags.THIRTY_TWO.value_names, ["THIRTY_TWO"])
128 self.assertEqual(MyFlags.THIRTY_TWO.value_nicks, ["thirty-two"])
129
130 # Similar for combinations of flags
131 v = MyFlags.ONE | MyFlags.THIRTY_TWO
132 self.assertIsInstance(v, MyFlags)
133 self.assertEqual(v.value_names, ["ONE", "THIRTY_TWO"])
134 self.assertEqual(v.value_nicks, ["one", "thirty-two"])
135
136 def test_custom_type_name(self):
137 type_name = f"MyFlags{get_id()}"
138
139 class MyFlags(GObject.GFlags):
140 __gtype_name__ = type_name
141 ONE = 1
142
143 self.assertEqual(MyFlags.__gtype__.name, type_name)
144
145 # Trying to register a type with the same name fails:
146 with (
147 self.assertRaises(RuntimeError) as ex,
148 capture_glib_warnings(allow_criticals=True) as w,
149 ):
150
151 class MyFlags2(GObject.GFlags):
152 __gtype_name__ = type_name
153 ONE = 1
154
155 self.assertEqual(str(ex.exception), f"Unable to register flags '{type_name}'")
156 self.assertEqual(len(w), 1)
157 self.assertEqual(
158 str(w[0].message), f"cannot register existing type '{type_name}'"
159 )