summaryrefslogtreecommitdiff
path: root/test_gtype.py
blob: cd1b140d9b36fa16720acfe6dd699a24f50c9799 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import unittest

from gobject import GType
from common import gobject, testhelper

def raiseGError():
    err = gobject.GError
    err.message = "Test conversion between exception and GError"
    err.code = 1
    err.domain = ""
    raise err

def test_raiseGError():
    testhelper.test_gerror_exception(raiseGError)

class GTypeTest(unittest.TestCase):
    def checkType(self, expected, *objects):
        # First, double check so we get back what we sent
        str = GType(expected).name # pyg_type_from_object
        val = GType.from_name(str) # pyg_type_wrapper_new
        self.assertEqual(val, expected,
                         'got %r while %r was expected' % (val, expected))

        # Then test the objects
        for object in objects:
            val = GType.from_name(GType(expected).name)
            self.assertEqual(val, expected,
                             'got %r while %r was expected' %
                             (val, expected))

    def assertPyType(self, val, pytype):
        val.pytype = pytype
        self.assertEqual(val.pytype, pytype,
                         'got %r while %r was expected' % (val.pytype, pytype))

    def setInvalidPyType(self):
        val = GType(gobject.TYPE_INT)
        val.pytype = 1

    def testBool(self):
        self.checkType(gobject.TYPE_BOOLEAN, 'gboolean', bool)

    def testInt(self):
        self.checkType(gobject.TYPE_INT, 'gint', int)
    #    model = gtk.ListStore(str, int)
    #    iter = model.append()
    #    model.set(iter, 1, 100000000)

    def testInt64(self):
        self.checkType(gobject.TYPE_INT64, 'gint64')

    def testUint(self):
        self.checkType(gobject.TYPE_UINT, 'guint')

    def testUint64(self):
        self.checkType(gobject.TYPE_UINT64, 'guint64')

    def testLong(self):
        self.checkType(gobject.TYPE_LONG, 'glong', long)

    def testUlong(self):
        self.checkType(gobject.TYPE_ULONG, 'gulong')

    def testDouble(self):
        self.checkType(gobject.TYPE_DOUBLE, 'gdouble', float)

    def testFloat(self):
        self.checkType(gobject.TYPE_FLOAT, 'gfloat')

    def testPyObject(self):
        self.checkType(gobject.TYPE_PYOBJECT, 'GObject', object)

    def testObject(self):
        self.checkType(gobject.TYPE_OBJECT, 'PyObject')

    def testPyType(self):
        val = GType(gobject.TYPE_INT)
        self.assertEqual(val.pytype, None,
                         'got %r while %r was expected' % (val.pytype, None))
        for t in [int, None, float]:
            self.assertPyType(val, t)

    def testInvalidPyType(self):
        self.assertRaises(TypeError, self.setInvalidPyType)

    def testValue(self):
        array = [1, "foo", True]
        for i in array:
            self.assertEqual(i, testhelper.test_value(i))
            
    def testValueArray(self):
        array = [1, 2, 3, "foo", True]
        self.assertEqual(array, testhelper.test_value_array(array))

    def testValueArrayNone(self):
        array = [1, 2, 3, "foo", True, None]
        self.assertEqual(array, testhelper.test_value_array(array))

    def testGError(self):
        self.assertRaises(gobject.GError, test_raiseGError)

    # XXX: Flags, Enums

class MyObject(gobject.GObject):
    __gtype_name__ = 'MyObject'

class TypeNameTest(unittest.TestCase):
    def testTypeName(self):
        self.assertEqual(GType(MyObject).name, 'MyObject')

if __name__ == '__main__':
    unittest.main()