3ece684e42430f62b7810d6fd896623415b98b75
[platform/upstream/pygobject2.git] / tests / test_typeclass.py
1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # vim: tabstop=4 shiftwidth=4 expandtab
3 #
4 # test_typeclass.py: Tests for GTypeClass related methods and marshalling.
5 #
6 # Copyright (C) 2014 Simon Feltman <sfeltman@gnome.org>
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
21 # USA
22
23 import unittest
24
25 from gi.repository import GObject
26 from gi.repository import GIMarshallingTests
27
28
29 class TestCoercion(unittest.TestCase):
30     def test_coerce_from_class(self):
31         prop = GObject.ObjectClass.find_property(GIMarshallingTests.PropertiesObject,
32                                                  'some-int')
33
34         self.assertIsInstance(prop, GObject.GParamSpec)
35         self.assertEqual(prop.name, 'some-int')
36         self.assertEqual(prop.value_type, GObject.TYPE_INT)
37         self.assertEqual(prop.owner_type,
38                          GIMarshallingTests.PropertiesObject.__gtype__)
39
40     def test_coerce_from_gtype(self):
41         gtype = GIMarshallingTests.PropertiesObject.__gtype__
42         prop = GObject.ObjectClass.find_property(gtype, 'some-int')
43
44         self.assertIsInstance(prop, GObject.GParamSpec)
45         self.assertEqual(prop.name, 'some-int')
46         self.assertEqual(prop.value_type, GObject.TYPE_INT)
47         self.assertEqual(prop.owner_type, gtype)
48
49     def test_coerce_from_instance(self):
50         obj = GIMarshallingTests.PropertiesObject()
51         prop = GObject.ObjectClass.find_property(obj, 'some-int')
52
53         self.assertIsInstance(prop, GObject.GParamSpec)
54         self.assertEqual(prop.name, 'some-int')
55         self.assertEqual(prop.value_type, GObject.TYPE_INT)
56         self.assertEqual(prop.owner_type, obj.__gtype__)
57
58     def test_marshalling_error(self):
59         with self.assertRaises(TypeError):
60             GObject.ObjectClass.find_property(object, 'some-int')
61
62         with self.assertRaises(TypeError):
63             GObject.ObjectClass.find_property(42, 'some-int')
64
65
66 class TestTypeClassMethodsMovedToClass(unittest.TestCase):
67     def test_list_child_properties(self):
68         pspecs = GIMarshallingTests.PropertiesObject.list_properties()
69         pnames = [pspec.name for pspec in pspecs]
70         self.assertTrue('some-int' in pnames)
71         self.assertTrue('some-float' in pnames)
72         self.assertTrue('some-char' in pnames)
73
74     def test_find_child_property(self):
75         pspec = GIMarshallingTests.PropertiesObject.find_property('some-int')
76         self.assertEqual(pspec.name, 'some-int')
77
78
79 if __name__ == '__main__':
80     unittest.main()