6 from gi.repository import GLib, GObject
11 class PyGObject(GObject.GObject):
12 __gtype_name__ = 'PyGObject'
14 'label': (GObject.TYPE_STRING,
16 'the label of the object',
18 GObject.ParamFlags.READABLE | GObject.ParamFlags.WRITABLE),
23 GObject.GObject.__init__(self)
24 self.set_property('label', 'hello')
26 def do_set_property(self, name, value):
27 self._props[name] = value
29 def do_get_property(self, name):
30 return self._props[name]
33 def test_parse_constructor_args():
34 assert testhelper.test_parse_constructor_args("foo") == 1
37 class TestObject(unittest.TestCase):
38 def test_create_ctor(self):
40 self.assertTrue(isinstance(o, GObject.Object))
41 self.assertTrue(isinstance(o, PyGObject))
43 # has expected property
44 self.assertEqual(o.props.label, 'hello')
45 o.props.label = 'goodbye'
46 self.assertEqual(o.props.label, 'goodbye')
47 self.assertRaises(AttributeError, getattr, o.props, 'nosuchprop')
49 def test_pyobject_new_test_type(self):
50 o = testhelper.create_test_type()
51 self.assertTrue(isinstance(o, PyGObject))
53 # has expected property
54 self.assertEqual(o.props.label, 'hello')
55 o.props.label = 'goodbye'
56 self.assertEqual(o.props.label, 'goodbye')
57 self.assertRaises(AttributeError, getattr, o.props, 'nosuchprop')
59 def test_new_refcount(self):
60 # TODO: justify why this should be 2
61 self.assertEqual(testhelper.test_g_object_new(), 2)
64 class TestGValueConversion(unittest.TestCase):
66 self.assertEqual(testhelper.test_value(0), 0)
67 self.assertEqual(testhelper.test_value(5), 5)
68 self.assertEqual(testhelper.test_value(-5), -5)
69 self.assertEqual(testhelper.test_value(GLib.MAXINT32), GLib.MAXINT32)
70 self.assertEqual(testhelper.test_value(GLib.MININT32), GLib.MININT32)
73 self.assertEqual(testhelper.test_value('hello'), 'hello')
75 def test_int_array(self):
76 self.assertEqual(testhelper.test_value_array([]), [])
77 self.assertEqual(testhelper.test_value_array([0]), [0])
79 self.assertEqual(testhelper.test_value_array(ar), ar)
81 def test_str_array(self):
82 self.assertEqual(testhelper.test_value_array([]), [])
83 self.assertEqual(testhelper.test_value_array(['a']), ['a'])
84 ar = ('aa ' * 1000).split()
85 self.assertEqual(testhelper.test_value_array(ar), ar)
88 class TestErrors(unittest.TestCase):
89 def test_gerror(self):
90 callable_ = lambda: GLib.file_get_contents('/nonexisting ')
91 self.assertRaises(GLib.GError, testhelper.test_gerror_exception, callable_)
93 def test_no_gerror(self):
94 callable_ = lambda: GLib.file_get_contents(__file__)
95 self.assertEqual(testhelper.test_gerror_exception(callable_), None)
98 def test_to_unichar_conv():
99 assert testhelper.test_to_unichar_conv(u"A") == 65
100 assert testhelper.test_to_unichar_conv(u"Ä") == 196
102 with pytest.raises(TypeError):
103 assert testhelper.test_to_unichar_conv(b"\x65")
105 with pytest.raises(TypeError):
106 testhelper.test_to_unichar_conv(object())
108 with pytest.raises(TypeError):
109 testhelper.test_to_unichar_conv(u"AA")
112 def test_constant_strip_prefix():
113 assert testhelper.constant_strip_prefix("foo", "bar") == "foo"
114 assert testhelper.constant_strip_prefix("foo", "f") == "oo"
115 assert testhelper.constant_strip_prefix("foo", "f") == "oo"
116 assert testhelper.constant_strip_prefix("ha2foo", "ha") == "a2foo"
117 assert testhelper.constant_strip_prefix("2foo", "ha") == "2foo"
118 assert testhelper.constant_strip_prefix("bla_foo", "bla") == "_foo"
121 def test_state_ensure_release():
122 testhelper.test_state_ensure_release()