Bump to 3.46.0
[platform/upstream/pygobject2.git] / tests / test_internal_api.py
1 # -*- Mode: Python -*-
2
3 import unittest
4 import pytest
5
6 from gi.repository import GLib, GObject
7
8 import testhelper
9
10
11 class PyGObject(GObject.GObject):
12     __gtype_name__ = 'PyGObject'
13     __gproperties__ = {
14         'label': (GObject.TYPE_STRING,
15                   'label property',
16                   'the label of the object',
17                   'default',
18                   GObject.ParamFlags.READABLE | GObject.ParamFlags.WRITABLE),
19         }
20
21     def __init__(self):
22         self._props = {}
23         GObject.GObject.__init__(self)
24         self.set_property('label', 'hello')
25
26     def do_set_property(self, name, value):
27         self._props[name] = value
28
29     def do_get_property(self, name):
30         return self._props[name]
31
32
33 def test_parse_constructor_args():
34     assert testhelper.test_parse_constructor_args("foo") == 1
35
36
37 class TestObject(unittest.TestCase):
38     def test_create_ctor(self):
39         o = PyGObject()
40         self.assertTrue(isinstance(o, GObject.Object))
41         self.assertTrue(isinstance(o, PyGObject))
42
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')
48
49     def test_pyobject_new_test_type(self):
50         o = testhelper.create_test_type()
51         self.assertTrue(isinstance(o, PyGObject))
52
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')
58
59     def test_new_refcount(self):
60         # TODO: justify why this should be 2
61         self.assertEqual(testhelper.test_g_object_new(), 2)
62
63
64 class TestGValueConversion(unittest.TestCase):
65     def test_int(self):
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)
71
72     def test_str(self):
73         self.assertEqual(testhelper.test_value('hello'), 'hello')
74
75     def test_int_array(self):
76         self.assertEqual(testhelper.test_value_array([]), [])
77         self.assertEqual(testhelper.test_value_array([0]), [0])
78         ar = list(range(100))
79         self.assertEqual(testhelper.test_value_array(ar), ar)
80
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)
86
87
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_)
92
93     def test_no_gerror(self):
94         callable_ = lambda: GLib.file_get_contents(__file__)
95         self.assertEqual(testhelper.test_gerror_exception(callable_), None)
96
97
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
101
102     with pytest.raises(TypeError):
103         assert testhelper.test_to_unichar_conv(b"\x65")
104
105     with pytest.raises(TypeError):
106         testhelper.test_to_unichar_conv(object())
107
108     with pytest.raises(TypeError):
109         testhelper.test_to_unichar_conv(u"AA")
110
111
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"
119
120
121 def test_state_ensure_release():
122     testhelper.test_state_ensure_release()