aboutsummaryrefslogtreecommitdiffstats
path: root/tests/qtcore/qstring_test.py
blob: 9bb68a3ed2875f2935bd4c3d0d3f3445486ce1a9 (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
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
185
186
187
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''Test cases for QString'''

import unittest
import ctypes
import sys

from PySide.QtCore import QString, QByteArray, QObject

class QStringToNumber(unittest.TestCase):
    def testReturnValueTypes(self):
        obj = QString('37')
        val, ok = obj.toInt()
        self.assertEqual(type(val), int)
        self.assertEqual(type(ok), bool)

    def testToNumberInt(self):
        obj = QString('37')
        self.assertEqual((37, True), obj.toInt())

    def testToNumberLong(self):
        obj = QString('3700000')
        self.assertEqual((3700000, True), obj.toInt())

    def testToNumberShort(self):
        obj = QString('33')
        self.assertEqual((ctypes.c_short(33).value, True), obj.toShort())

    def testToNumberShortNegative(self):
        obj = QString('-4')
        self.assertEqual((ctypes.c_short(-4).value, True), obj.toShort())

    def testToNumberShortOverflow(self):
        obj = QString('1000000')
        self.assertEqual(False, obj.toShort()[1])

    def testToNumberUInt(self):
        obj = QString('33')
        self.assertEqual((ctypes.c_uint(33).value, True), obj.toUInt())

    def testToNumberUIntNegative(self):
        obj = QString('-4')
        self.assertEqual(False, obj.toUInt()[1])

    def testToNumberUIntOverflow(self):
        obj = QString('10000000000000')
        self.assertEqual(False, obj.toUInt()[1])

    def testToNumberULong(self):
        obj = QString('33')
        self.assertEqual((ctypes.c_ulong(33).value, True), obj.toULong())

    def testToNumberULongNegative(self):
        obj = QString('-4')
        self.assertEqual(False, obj.toULong()[1])

    def testToNumberUShort(self):
        obj = QString('33')
        self.assertEqual((ctypes.c_ushort(33).value, True), obj.toUShort())

    def testToNumberUShortLarge(self):
        obj = QString('128')
        self.assertEqual((ctypes.c_ushort(128).value, True), obj.toUShort())

    def testToNumberUShortOverflow(self):
        obj = QString('205000')
        self.assertEqual(False, obj.toUShort()[1])

    def testToNumberUShortNegative(self):
        obj = QString('-4')
        self.assertEqual(False, obj.toUShort()[1])

    def testToNumberIntUsingHex(self):
        obj = QString('2A')
        self.assertEquals((0, False), obj.toInt())
        self.assertEqual((int(str(obj), 16), True), obj.toInt(16))

    def testToNumberIntUsingHex(self):
        obj = QString('101010')
        self.assertEqual((int(str(obj), 2), True), obj.toInt(2))

    def testToNumberFloat(self):
        obj = QString('37.109')
        self.assertEqual(ctypes.c_float(37.109).value,
                         obj.toFloat()[0])

    def testToNumberDouble(self):
        obj = QString('37.109')
        self.assertEqual(ctypes.c_double(37.109).value,
                         obj.toDouble()[0])

    def testToULongLong(self):
        obj = QString('37109')
        self.assertEqual(ctypes.c_ulong(37109).value,
                        obj.toULongLong()[0])

class QStringConstructor(unittest.TestCase):
    '''Test case for QString constructors'''

    def testQStringDefault(self):
        #QString()
        obj1 = QString()
        obj2 = QString()

        self.assertEqual(obj1, obj2)

    def testNullQString(self):
        s = QString(None)
        self.assertTrue(s.isNull())

    def testQStringFromPy(self):
        #QString(const char*)
        sample = 'a new string'
        obj1 = QString(sample)
        obj2 = QString(sample)
        self.assertEqual(obj1, obj2)

    def testQStringFromUnicode(self):
        sample = u'áâãà'
        obj1 = QString(sample)
        obj2 = QString(sample)
        self.assertEqual(obj1, obj2)
        self.assertEqual(obj1, sample)
        self.assertEqual(obj2, sample)

    def testQStringFromByteArray(self):
        # QByteArray(const char *) must be working
        sample = QByteArray('foo')
        obj1 = QString(sample)
        obj2 = QString(sample)
        self.assertEqual(obj1, obj2)

    def testQStringArg(self):
        a = QString("%1 %2 %3").arg(1).arg("two").arg(3.14)
        self.assertEquals("1 two 3.14", str(a))

    def testQStringArgNegative(self):
        a = QString("%1").arg(-20)
        self.assertEquals("-20", str(a))


class QStringComparison(unittest.TestCase):
    '''Test case for comparison to python strings'''

    def testComparePyString(self):
        #Compare QStrings and Python strings.
        py = ''
        qstr = QString()
        self.assertEqual(py, qstr)

        py = 'The quick brown fox jumps over the lazy dog'
        qstr = QString(py)
        self.assertEqual(py, qstr)

class QStringRange(unittest.TestCase):
    '''Test case for ranges in python strings'''

    def testSimpleRange(self):
        #Test open start and open end intervals
        py = 'The quick brown fox jumps over the lazy dog'
        qstr = QString(py)
        self.assertEqual(py[5:], qstr[5:])
        self.assertEqual(py[:7], qstr[:7])

class QStringIndexOf(unittest.TestCase):
    def testEmpty(self):
        string = QString()
        self.assertEqual(string.indexOf(QString("aaa")), -1)
        self.assertEqual(string.indexOf(QString()), 0)

    def testString(self):
        string = QString("the quick brown fox")
        self.assertEqual(string.indexOf("quick", 0), 4)


class QStringImplicitConvertion(unittest.TestCase):
    '''Implicit conversions for QString'''

    def testQByteArray(self):
        '''QString implicitly conversion: QByteArray'''
        obj = QObject()
        obj.setObjectName(QByteArray('foobar'))
        self.assertEqual(obj.objectName(), QString('foobar'))

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