functions: revert the function init order to make pylint happy again. See #217
[pygobject.git] / tests / test_internal_api.py
blob1ed822c8638a34b4d7589705df30dfd146370a64
1 # -*- coding: utf-8 -*-
2 # -*- Mode: Python -*-
4 from __future__ import absolute_import
6 import unittest
7 import pytest
9 from gi.repository import GLib, GObject
10 from gi._compat import PY3
12 import testhelper
15 class PyGObject(GObject.GObject):
16 __gtype_name__ = 'PyGObject'
17 __gproperties__ = {
18 'label': (GObject.TYPE_STRING,
19 'label property',
20 'the label of the object',
21 'default',
22 GObject.ParamFlags.READABLE | GObject.ParamFlags.WRITABLE),
25 def __init__(self):
26 self._props = {}
27 GObject.GObject.__init__(self)
28 self.set_property('label', 'hello')
30 def do_set_property(self, name, value):
31 self._props[name] = value
33 def do_get_property(self, name):
34 return self._props[name]
37 def test_parse_constructor_args():
38 assert testhelper.test_parse_constructor_args("foo") == 1
41 class TestObject(unittest.TestCase):
42 def test_create_ctor(self):
43 o = PyGObject()
44 self.assertTrue(isinstance(o, GObject.Object))
45 self.assertTrue(isinstance(o, PyGObject))
47 # has expected property
48 self.assertEqual(o.props.label, 'hello')
49 o.props.label = 'goodbye'
50 self.assertEqual(o.props.label, 'goodbye')
51 self.assertRaises(AttributeError, getattr, o.props, 'nosuchprop')
53 def test_pyobject_new_test_type(self):
54 o = testhelper.create_test_type()
55 self.assertTrue(isinstance(o, PyGObject))
57 # has expected property
58 self.assertEqual(o.props.label, 'hello')
59 o.props.label = 'goodbye'
60 self.assertEqual(o.props.label, 'goodbye')
61 self.assertRaises(AttributeError, getattr, o.props, 'nosuchprop')
63 def test_new_refcount(self):
64 # TODO: justify why this should be 2
65 self.assertEqual(testhelper.test_g_object_new(), 2)
68 class TestGValueConversion(unittest.TestCase):
69 def test_int(self):
70 self.assertEqual(testhelper.test_value(0), 0)
71 self.assertEqual(testhelper.test_value(5), 5)
72 self.assertEqual(testhelper.test_value(-5), -5)
73 self.assertEqual(testhelper.test_value(GLib.MAXINT32), GLib.MAXINT32)
74 self.assertEqual(testhelper.test_value(GLib.MININT32), GLib.MININT32)
76 def test_str(self):
77 self.assertEqual(testhelper.test_value('hello'), 'hello')
79 def test_int_array(self):
80 self.assertEqual(testhelper.test_value_array([]), [])
81 self.assertEqual(testhelper.test_value_array([0]), [0])
82 ar = list(range(100))
83 self.assertEqual(testhelper.test_value_array(ar), ar)
85 def test_str_array(self):
86 self.assertEqual(testhelper.test_value_array([]), [])
87 self.assertEqual(testhelper.test_value_array(['a']), ['a'])
88 ar = ('aa ' * 1000).split()
89 self.assertEqual(testhelper.test_value_array(ar), ar)
92 class TestErrors(unittest.TestCase):
93 def test_gerror(self):
94 callable_ = lambda: GLib.file_get_contents('/nonexisting ')
95 self.assertRaises(GLib.GError, testhelper.test_gerror_exception, callable_)
97 def test_no_gerror(self):
98 callable_ = lambda: GLib.file_get_contents(__file__)
99 self.assertEqual(testhelper.test_gerror_exception(callable_), None)
102 def test_to_unichar_conv():
103 assert testhelper.test_to_unichar_conv(u"A") == 65
104 assert testhelper.test_to_unichar_conv(u"Ä") == 196
106 if PY3:
107 with pytest.raises(TypeError):
108 assert testhelper.test_to_unichar_conv(b"\x65")
109 else:
110 assert testhelper.test_to_unichar_conv(b"\x65") == 0x65
111 with pytest.raises(ValueError):
112 assert testhelper.test_to_unichar_conv(b"\xff")
114 with pytest.raises(TypeError):
115 testhelper.test_to_unichar_conv(object())
117 with pytest.raises(TypeError):
118 testhelper.test_to_unichar_conv(u"AA")
121 def test_constant_strip_prefix():
122 assert testhelper.constant_strip_prefix("foo", "bar") == "foo"
123 assert testhelper.constant_strip_prefix("foo", "f") == "oo"
124 assert testhelper.constant_strip_prefix("foo", "f") == "oo"
125 assert testhelper.constant_strip_prefix("ha2foo", "ha") == "a2foo"
126 assert testhelper.constant_strip_prefix("2foo", "ha") == "2foo"
127 assert testhelper.constant_strip_prefix("bla_foo", "bla") == "_foo"
130 def test_state_ensure_release():
131 testhelper.test_state_ensure_release()