test/test-standalone.py: Test API guarantee that integer types subclass int or long
[dbus-python-phuang.git] / test / test-standalone.py
blobe447dde6997f662b73f8e104afc78ea1d796af9e
1 #!/usr/bin/env python
3 # Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
5 # Licensed under the Academic Free License version 2.1
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 import sys
24 import os
25 import unittest
26 import time
28 builddir = os.environ["DBUS_TOP_BUILDDIR"]
29 pydir = builddir
31 sys.path.insert(0, pydir)
32 sys.path.insert(0, pydir + 'dbus')
34 import _dbus_bindings
35 import dbus
36 import dbus.types as types
38 pkg = dbus.__file__
39 if not pkg.startswith(pydir):
40 raise Exception("DBus modules (%s) are not being picked up from the package"%pkg)
42 if not _dbus_bindings.__file__.startswith(pydir):
43 raise Exception("DBus modules (%s) are not being picked up from the package"%_dbus_bindings.__file__)
45 class TestTypes(unittest.TestCase):
47 def test_integers(self):
48 # This is an API guarantee. Note that exactly which of these types
49 # are ints and which of them are longs is *not* guaranteed.
50 for cls in (types.Int16, types.UInt16, types.Int32, types.UInt32,
51 types.Int64, types.UInt64):
52 assert issubclass(cls, (int, long))
53 assert isinstance(cls(0), (int, long))
55 def test_Variant(self):
56 Variant = types.Variant
57 a = self.assert_
58 a(Variant(1, 'i') == Variant(1, 'i'))
59 a(not (Variant(1, 'i') == Variant(1, 'u')))
60 a(not (Variant(1, 'i') == Variant(2, 'i')))
61 a(not (Variant(1, 'i') == Variant(2, 'u')))
62 a(not (Variant(1, 'i') != Variant(1, 'i')))
63 a(Variant(1, 'i') != Variant(1, 'u'))
64 a(Variant(1, 'i') != Variant(2, 'i'))
65 a(Variant(1, 'i') != Variant(2, 'u'))
67 def test_Signature(self):
68 self.assertRaises(Exception, types.Signature, 'a')
69 self.assertEquals(types.Signature('ab'), 'ab')
70 self.assert_(isinstance(types.Signature('ab'), str))
71 self.assertEquals(tuple(types.Signature('ab(xt)a{sv}')),
72 ('ab', '(xt)', 'a{sv}'))
73 self.assert_(isinstance(tuple(types.Signature('ab'))[0],
74 types.Signature))
77 class TestMessageMarshalling(unittest.TestCase):
79 def test_append(self):
80 aeq = self.assertEquals
81 from _dbus_bindings import SignalMessage
82 s = SignalMessage('/', 'foo.bar', 'baz')
83 s.append([types.Byte(1)], signature='ay')
84 aeq(s.get_signature(), 'ay')
85 aeq(s.get_args_list(), [[types.Byte(1)]])
87 s = SignalMessage('/', 'foo.bar', 'baz')
88 s.append([], signature='ay')
89 aeq(s.get_args_list(), [[]])
91 def test_append_ByteArray(self):
92 aeq = self.assertEquals
93 from _dbus_bindings import SignalMessage
94 s = SignalMessage('/', 'foo.bar', 'baz')
95 s.append(types.ByteArray('ab'), signature='ay')
96 aeq(s.get_args_list(), [[types.Byte('a'), types.Byte('b')]])
97 s = SignalMessage('/', 'foo.bar', 'baz')
98 s.append(types.ByteArray('ab'), signature='av')
99 aeq(s.get_args_list(), [[types.Variant(types.Byte('a')),
100 types.Variant(types.Byte('b'))]])
102 def test_append_Variant(self):
103 a = self.assert_
104 aeq = self.assertEquals
105 from _dbus_bindings import SignalMessage
106 s = SignalMessage('/', 'foo.bar', 'baz')
107 s.append(types.Variant(1, signature='i'),
108 types.Variant('a', signature='s'),
109 types.Variant([(types.Variant('a', signature='y'), 'b'),
110 (types.Variant(123, signature='u'), 1)],
111 signature='a(vy)'))
112 aeq(s.get_signature(), 'vvv')
113 args = s.get_args_list()
114 aeq(args[0].__class__, types.Variant)
115 aeq(args[0].signature, 'i')
116 aeq(args[0].object.__class__, types.Int32)
117 aeq(args[0].object, 1)
118 aeq(args[1].__class__, types.Variant)
119 aeq(args[1].signature, 's')
120 a(isinstance(args[1].object, unicode))
121 aeq(args[2].__class__, types.Variant)
122 aeq(args[1].object, 'a')
123 aeq(args[2].signature, 'a(vy)')
124 avy = args[2].object
125 aeq(avy.__class__, types.Array)
126 aeq(len(avy), 2)
127 aeq(avy[0].__class__, tuple)
128 aeq(len(avy[0]), 2)
129 aeq(avy[0][0].__class__, types.Variant)
130 aeq(avy[0][0].signature, 'y')
131 aeq(avy[0][0].object.__class__, types.Byte)
132 aeq(avy[0][0].object, types.Byte('a'))
133 aeq(avy[0][1].__class__, types.Byte)
134 aeq(avy[0][1], types.Byte('b'))
135 aeq(avy[1].__class__, tuple)
136 aeq(len(avy[1]), 2)
137 aeq(avy[1][0].__class__, types.Variant)
138 aeq(avy[1][0].signature, 'u')
139 aeq(avy[1][0].object.__class__, types.UInt32)
140 aeq(avy[1][0].object, 123)
141 aeq(avy[1][1].__class__, types.Byte)
142 aeq(avy[1][1], types.Byte(1))
144 def test_guess_signature(self):
145 aeq = self.assertEquals
146 from _dbus_bindings import Message
147 aeq(Message.guess_signature(('a','b')), '(ss)')
148 aeq(Message.guess_signature('a','b'), 'ss')
149 aeq(Message.guess_signature(['a','b']), 'as')
150 aeq(Message.guess_signature(('a',)), '(s)')
151 aeq(Message.guess_signature('abc'), 's')
152 aeq(Message.guess_signature(types.Int32(123)), 'i')
153 aeq(Message.guess_signature(('a',)), '(s)')
154 aeq(Message.guess_signature(['a']), 'as')
155 aeq(Message.guess_signature({'a':'b'}), 'a{ss}')
157 def test_get_args_options(self):
158 aeq = self.assertEquals
159 s = _dbus_bindings.SignalMessage('/', 'foo.bar', 'baz')
160 s.append('b', 'bytes', -1, 1, 'str', 'var', signature='yayiusv')
161 aeq(s.get_args_list(), ['b', ['b','y','t','e','s'], -1, 1,
162 u'str', types.Variant(u'var', signature='s')])
163 byte, bytes, int32, uint32, string, variant = s.get_args_list()
164 aeq(byte.__class__, types.Byte)
165 aeq(bytes.__class__, types.Array)
166 aeq(bytes[0].__class__, types.Byte)
167 aeq(int32.__class__, types.Int32)
168 aeq(uint32.__class__, types.UInt32)
169 aeq(string.__class__, unicode)
170 aeq(variant.__class__, types.Variant)
171 aeq(variant.signature, 's')
173 byte, bytes, int32, uint32, string, variant = s.get_args_list(
174 integer_bytes=True)
175 aeq(byte.__class__, int)
176 aeq(byte, ord('b'))
177 aeq(bytes.__class__, types.Array)
178 aeq(bytes[0].__class__, int)
179 aeq(bytes, [ord('b'), ord('y'), ord('t'), ord('e'), ord('s')])
180 aeq(int32.__class__, types.Int32)
181 aeq(uint32.__class__, types.UInt32)
182 aeq(string.__class__, unicode)
183 aeq(variant.__class__, types.Variant)
184 aeq(variant.signature, 's')
186 byte, bytes, int32, uint32, string, variant = s.get_args_list(
187 byte_arrays=True)
188 aeq(byte.__class__, types.Byte)
189 aeq(bytes.__class__, types.ByteArray)
190 aeq(bytes, 'bytes')
191 aeq(bytes[0].__class__, types.Byte)
192 aeq(int32.__class__, types.Int32)
193 aeq(uint32.__class__, types.UInt32)
194 aeq(string.__class__, unicode)
195 aeq(variant.__class__, types.Variant)
196 aeq(variant.signature, 's')
198 byte, bytes, int32, uint32, string, variant = s.get_args_list(
199 utf8_strings=True)
200 aeq(byte.__class__, types.Byte)
201 aeq(bytes.__class__, types.Array)
202 aeq(bytes[0].__class__, types.Byte)
203 aeq(int32.__class__, types.Int32)
204 aeq(uint32.__class__, types.UInt32)
205 aeq(string.__class__, str)
206 aeq(string, 'str')
207 aeq(variant.__class__, types.Variant)
208 aeq(variant.signature, 's')
209 aeq(variant.object.__class__, str)
210 aeq(variant.object, 'var')
212 byte, bytes, int32, uint32, string, variant = s.get_args_list(
213 variant_unpack_level=1)
214 aeq(byte.__class__, types.Byte)
215 aeq(bytes.__class__, types.Array)
216 aeq(bytes[0].__class__, types.Byte)
217 aeq(int32.__class__, types.Int32)
218 aeq(uint32.__class__, types.UInt32)
219 aeq(string.__class__, unicode)
220 aeq(variant.__class__, unicode)
221 aeq(variant, 'var')
224 if __name__ == '__main__':
225 unittest.main()