File: typeconverters_test.py

package info (click to toggle)
shiboken 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,248 kB
  • ctags: 8,532
  • sloc: cpp: 50,923; python: 5,950; xml: 2,354; makefile: 144; ansic: 136
file content (184 lines) | stat: -rw-r--r-- 7,247 bytes parent folder | download | duplicates (3)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of the Shiboken Python Bindings Generator project.
#
# Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
#
# Contact: PySide team <contact@pyside.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# version 2.1 as published by the Free Software Foundation. Please
# review the following information to ensure the GNU Lesser General
# Public License version 2.1 requirements will be met:
# http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
# #
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA

'''Tests various usages of the type converters.'''

import unittest
import sample

class GetPythonTypeByNameTest(unittest.TestCase):

    '''Uses an added function with inject code that uses the libshiboken
    function "Shiboken::Conversions::getPythonTypeObject(typeName)".'''

    def testGetObjectType(self):
        pyType1 = sample.getPythonType('ObjectType')
        self.assertEqual(pyType1, sample.ObjectType)
        pyType2 = sample.getPythonType('ObjectType*')
        self.assertEqual(pyType2, sample.ObjectType)
        self.assertEqual(pyType1, pyType2)

    def testGetValueType(self):
        pyType1 = sample.getPythonType('Point')
        self.assertEqual(pyType1, sample.Point)
        pyType2 = sample.getPythonType('Point*')
        self.assertEqual(pyType2, sample.Point)
        self.assertEqual(pyType1, pyType2)

    def testGetUsersPrimitiveType(self):
        pyType = sample.getPythonType('OddBool')
        self.assertEqual(pyType, bool)

    def testGetUsersPrimitiveTypeWithoutTargetLangApiName(self):
        '''If the primitive type attribute "target-lang-api-name" is not set
        there'll be no Python type associated with the C++ type.'''
        pyType = sample.getPythonType('PStr')
        self.assertEqual(pyType, None)

    def testPrimitiveTypeAndTypedef(self):
        pyType = sample.getPythonType('double')
        self.assertEqual(pyType, float)
        pyTypedef = sample.getPythonType('real')
        self.assertEqual(pyType, pyTypedef)

    def testPairContainerType(self):
        pyType = sample.getPythonType('std::pair<Complex,int>')
        self.assertEqual(pyType, list)

    def testListContainerType(self):
        pyType = sample.getPythonType('std::list<int>')
        self.assertEqual(pyType, list)

    def testMapContainerType(self):
        pyType = sample.getPythonType('std::map<std::string,int>')
        self.assertEqual(pyType, dict)

    def testGlobalEnumType(self):
        pyType = sample.getPythonType('GlobalEnum')
        self.assertEqual(pyType, sample.GlobalEnum)

    def testScopedEnumType(self):
        pyType = sample.getPythonType('Abstract::Type')
        self.assertEqual(pyType, sample.Abstract.Type)


class CheckValueAndObjectTypeByNameTest(unittest.TestCase):

    '''Uses an added function with inject code that uses the libshiboken
    functions that check if a type is Object or Value, based on its converter.'''

    def testErrors(self):
        '''not existent type'''
        self.assertRaises(ValueError, sample.cppTypeIsValueType, 'NotExistentType')
        self.assertRaises(ValueError, sample.cppTypeIsObjectType, 'NotExistentType')

    def testObjectType1(self):
        self.assertTrue(sample.cppTypeIsObjectType('ObjectType'))
        self.assertFalse(sample.cppTypeIsValueType('ObjectType'))

    def testObjectType2(self):
        self.assertTrue(sample.cppTypeIsObjectType('ObjectType*'))
        self.assertFalse(sample.cppTypeIsValueType('ObjectType*'))

    def testValueType1(self):
        self.assertTrue(sample.cppTypeIsValueType('Point'))
        self.assertFalse(sample.cppTypeIsObjectType('Point'))

    def testValueType2(self):
        self.assertTrue(sample.cppTypeIsValueType('Point*'))
        self.assertFalse(sample.cppTypeIsObjectType('Point*'))

    def testUsersPrimitiveType(self):
        self.assertFalse(sample.cppTypeIsValueType('Complex'))
        self.assertFalse(sample.cppTypeIsObjectType('Complex'))

    def testContainerType(self):
        self.assertFalse(sample.cppTypeIsValueType('std::list<int>'))
        self.assertFalse(sample.cppTypeIsObjectType('std::list<int>'))


class SpecificConverterTest(unittest.TestCase):

    '''Uses an added function with inject code that uses the libshiboken
    adapter class "Shiboken::Conversions::SpecificConverter".'''

    def testNotExistentType(self):
        conversion = sample.getConversionTypeString('NotExistentType')
        self.assertEqual(conversion, 'Invalid conversion')

    def testObjectType(self):
        conversion = sample.getConversionTypeString('ObjectType')
        self.assertEqual(conversion, 'Pointer conversion')
        conversion = sample.getConversionTypeString('ObjectType*')
        self.assertEqual(conversion, 'Pointer conversion')
        conversion = sample.getConversionTypeString('ObjectType&')
        self.assertEqual(conversion, 'Reference conversion')

    def testValueType(self):
        conversion = sample.getConversionTypeString('Point')
        self.assertEqual(conversion, 'Copy conversion')
        conversion = sample.getConversionTypeString('Point*')
        self.assertEqual(conversion, 'Pointer conversion')
        conversion = sample.getConversionTypeString('Point&')
        self.assertEqual(conversion, 'Reference conversion')


class StringBasedConversionTest(unittest.TestCase):

    def testValueType(self):
        pts = (sample.Point(1, 1), sample.Point(2, 2), sample.Point(3, 3))
        result = sample.convertValueTypeToCppAndThenToPython(pts[0], pts[1], pts[2])
        for orig, new in zip(pts, result):
            self.assertEqual(orig, new)
        self.assertFalse(pts[0] is result[0])
        self.assertTrue(pts[1] is result[1])
        self.assertTrue(pts[2] is result[2])

    def testObjectType(self):
        objs = (sample.ObjectType(), sample.ObjectType())
        objs[0].setObjectName('obj0')
        objs[1].setObjectName('obj1')
        result = sample.convertObjectTypeToCppAndThenToPython(objs[0], objs[1])
        for orig, new in zip(objs, result):
            self.assertEqual(orig, new)
            self.assertEqual(orig.objectName(), new.objectName())
            self.assertTrue(orig is new)

    def testContainerType(self):
        lst = range(4)
        result = sample.convertListOfIntegersToCppAndThenToPython(lst)
        self.assertTrue(len(result), 1)
        self.assertTrue(lst, result[0])

    def testCppPrimitiveType(self):
        integers = (12, 34)
        result = sample.convertIntegersToCppAndThenToPython(integers[0], integers[1])
        for orig, new in zip(integers, result):
            self.assertEqual(orig, new)


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