release
[pygobject.git] / tests / test_gi.py
blob9be28dbe1b61b41fbf6cb06d821d47e8b9a7a4b8
1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # coding=utf-8
3 # vim: tabstop=4 shiftwidth=4 expandtab
5 import sys
7 import unittest
8 import tempfile
9 import shutil
10 import os
11 import gc
12 import weakref
13 import warnings
14 from io import StringIO, BytesIO
16 import gi
17 import gi.overrides
18 from gi import PyGIWarning
19 from gi import PyGIDeprecationWarning
20 from gi.repository import GObject, GLib, Gio
22 from gi.repository import GIMarshallingTests
24 from compathelper import PY2, PY3
25 from helper import capture_exceptions
28 CONSTANT_UTF8 = "const ♥ utf8"
31 class Number(object):
33 def __init__(self, value):
34 self.value = value
36 def __int__(self):
37 return int(self.value)
39 def __float__(self):
40 return float(self.value)
43 class Sequence(object):
45 def __init__(self, sequence):
46 self.sequence = sequence
48 def __len__(self):
49 return len(self.sequence)
51 def __getitem__(self, key):
52 return self.sequence[key]
55 class TestConstant(unittest.TestCase):
57 def test_constant_utf8(self):
58 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.CONSTANT_UTF8)
60 def test_constant_number(self):
61 self.assertEqual(42, GIMarshallingTests.CONSTANT_NUMBER)
63 def test_min_max_int(self):
64 self.assertEqual(GLib.MAXINT32, 2 ** 31 - 1)
65 self.assertEqual(GLib.MININT32, -2 ** 31)
66 self.assertEqual(GLib.MAXUINT32, 2 ** 32 - 1)
68 self.assertEqual(GLib.MAXINT64, 2 ** 63 - 1)
69 self.assertEqual(GLib.MININT64, -2 ** 63)
70 self.assertEqual(GLib.MAXUINT64, 2 ** 64 - 1)
73 class TestBoolean(unittest.TestCase):
75 def test_boolean_return(self):
76 self.assertEqual(True, GIMarshallingTests.boolean_return_true())
77 self.assertEqual(False, GIMarshallingTests.boolean_return_false())
79 def test_boolean_in(self):
80 GIMarshallingTests.boolean_in_true(True)
81 GIMarshallingTests.boolean_in_false(False)
83 GIMarshallingTests.boolean_in_true(1)
84 GIMarshallingTests.boolean_in_false(0)
86 def test_boolean_out(self):
87 self.assertEqual(True, GIMarshallingTests.boolean_out_true())
88 self.assertEqual(False, GIMarshallingTests.boolean_out_false())
90 def test_boolean_inout(self):
91 self.assertEqual(False, GIMarshallingTests.boolean_inout_true_false(True))
92 self.assertEqual(True, GIMarshallingTests.boolean_inout_false_true(False))
95 class TestInt8(unittest.TestCase):
97 MAX = GLib.MAXINT8
98 MIN = GLib.MININT8
100 def test_int8_return(self):
101 self.assertEqual(self.MAX, GIMarshallingTests.int8_return_max())
102 self.assertEqual(self.MIN, GIMarshallingTests.int8_return_min())
104 def test_int8_in(self):
105 max = Number(self.MAX)
106 min = Number(self.MIN)
108 GIMarshallingTests.int8_in_max(max)
109 GIMarshallingTests.int8_in_min(min)
111 max.value += 1
112 min.value -= 1
114 self.assertRaises(OverflowError, GIMarshallingTests.int8_in_max, max)
115 self.assertRaises(OverflowError, GIMarshallingTests.int8_in_min, min)
117 self.assertRaises(TypeError, GIMarshallingTests.int8_in_max, "self.MAX")
119 def test_int8_out(self):
120 self.assertEqual(self.MAX, GIMarshallingTests.int8_out_max())
121 self.assertEqual(self.MIN, GIMarshallingTests.int8_out_min())
123 def test_int8_inout(self):
124 self.assertEqual(self.MIN, GIMarshallingTests.int8_inout_max_min(Number(self.MAX)))
125 self.assertEqual(self.MAX, GIMarshallingTests.int8_inout_min_max(Number(self.MIN)))
128 class TestUInt8(unittest.TestCase):
130 MAX = GLib.MAXUINT8
132 def test_uint8_return(self):
133 self.assertEqual(self.MAX, GIMarshallingTests.uint8_return())
135 def test_uint8_in(self):
136 number = Number(self.MAX)
138 GIMarshallingTests.uint8_in(number)
139 GIMarshallingTests.uint8_in(b'\xff')
141 number.value += 1
142 self.assertRaises(OverflowError, GIMarshallingTests.uint8_in, number)
143 self.assertRaises(OverflowError, GIMarshallingTests.uint8_in, Number(-1))
145 self.assertRaises(TypeError, GIMarshallingTests.uint8_in, "self.MAX")
147 def test_uint8_out(self):
148 self.assertEqual(self.MAX, GIMarshallingTests.uint8_out())
150 def test_uint8_inout(self):
151 self.assertEqual(0, GIMarshallingTests.uint8_inout(Number(self.MAX)))
154 class TestInt16(unittest.TestCase):
156 MAX = GLib.MAXINT16
157 MIN = GLib.MININT16
159 def test_int16_return(self):
160 self.assertEqual(self.MAX, GIMarshallingTests.int16_return_max())
161 self.assertEqual(self.MIN, GIMarshallingTests.int16_return_min())
163 def test_int16_in(self):
164 max = Number(self.MAX)
165 min = Number(self.MIN)
167 GIMarshallingTests.int16_in_max(max)
168 GIMarshallingTests.int16_in_min(min)
170 max.value += 1
171 min.value -= 1
173 self.assertRaises(OverflowError, GIMarshallingTests.int16_in_max, max)
174 self.assertRaises(OverflowError, GIMarshallingTests.int16_in_min, min)
176 self.assertRaises(TypeError, GIMarshallingTests.int16_in_max, "self.MAX")
178 def test_int16_out(self):
179 self.assertEqual(self.MAX, GIMarshallingTests.int16_out_max())
180 self.assertEqual(self.MIN, GIMarshallingTests.int16_out_min())
182 def test_int16_inout(self):
183 self.assertEqual(self.MIN, GIMarshallingTests.int16_inout_max_min(Number(self.MAX)))
184 self.assertEqual(self.MAX, GIMarshallingTests.int16_inout_min_max(Number(self.MIN)))
187 class TestUInt16(unittest.TestCase):
189 MAX = GLib.MAXUINT16
191 def test_uint16_return(self):
192 self.assertEqual(self.MAX, GIMarshallingTests.uint16_return())
194 def test_uint16_in(self):
195 number = Number(self.MAX)
197 GIMarshallingTests.uint16_in(number)
199 number.value += 1
201 self.assertRaises(OverflowError, GIMarshallingTests.uint16_in, number)
202 self.assertRaises(OverflowError, GIMarshallingTests.uint16_in, Number(-1))
204 self.assertRaises(TypeError, GIMarshallingTests.uint16_in, "self.MAX")
206 def test_uint16_out(self):
207 self.assertEqual(self.MAX, GIMarshallingTests.uint16_out())
209 def test_uint16_inout(self):
210 self.assertEqual(0, GIMarshallingTests.uint16_inout(Number(self.MAX)))
213 class TestInt32(unittest.TestCase):
215 MAX = GLib.MAXINT32
216 MIN = GLib.MININT32
218 def test_int32_return(self):
219 self.assertEqual(self.MAX, GIMarshallingTests.int32_return_max())
220 self.assertEqual(self.MIN, GIMarshallingTests.int32_return_min())
222 def test_int32_in(self):
223 max = Number(self.MAX)
224 min = Number(self.MIN)
226 GIMarshallingTests.int32_in_max(max)
227 GIMarshallingTests.int32_in_min(min)
229 max.value += 1
230 min.value -= 1
232 self.assertRaises(OverflowError, GIMarshallingTests.int32_in_max, max)
233 self.assertRaises(OverflowError, GIMarshallingTests.int32_in_min, min)
235 self.assertRaises(TypeError, GIMarshallingTests.int32_in_max, "self.MAX")
237 def test_int32_out(self):
238 self.assertEqual(self.MAX, GIMarshallingTests.int32_out_max())
239 self.assertEqual(self.MIN, GIMarshallingTests.int32_out_min())
241 def test_int32_inout(self):
242 self.assertEqual(self.MIN, GIMarshallingTests.int32_inout_max_min(Number(self.MAX)))
243 self.assertEqual(self.MAX, GIMarshallingTests.int32_inout_min_max(Number(self.MIN)))
246 class TestUInt32(unittest.TestCase):
248 MAX = GLib.MAXUINT32
250 def test_uint32_return(self):
251 self.assertEqual(self.MAX, GIMarshallingTests.uint32_return())
253 def test_uint32_in(self):
254 number = Number(self.MAX)
256 GIMarshallingTests.uint32_in(number)
258 number.value += 1
260 self.assertRaises(OverflowError, GIMarshallingTests.uint32_in, number)
261 self.assertRaises(OverflowError, GIMarshallingTests.uint32_in, Number(-1))
263 self.assertRaises(TypeError, GIMarshallingTests.uint32_in, "self.MAX")
265 def test_uint32_out(self):
266 self.assertEqual(self.MAX, GIMarshallingTests.uint32_out())
268 def test_uint32_inout(self):
269 self.assertEqual(0, GIMarshallingTests.uint32_inout(Number(self.MAX)))
272 class TestInt64(unittest.TestCase):
274 MAX = 2 ** 63 - 1
275 MIN = - (2 ** 63)
277 def test_int64_return(self):
278 self.assertEqual(self.MAX, GIMarshallingTests.int64_return_max())
279 self.assertEqual(self.MIN, GIMarshallingTests.int64_return_min())
281 def test_int64_in(self):
282 max = Number(self.MAX)
283 min = Number(self.MIN)
285 GIMarshallingTests.int64_in_max(max)
286 GIMarshallingTests.int64_in_min(min)
288 max.value += 1
289 min.value -= 1
291 self.assertRaises(OverflowError, GIMarshallingTests.int64_in_max, max)
292 self.assertRaises(OverflowError, GIMarshallingTests.int64_in_min, min)
294 self.assertRaises(TypeError, GIMarshallingTests.int64_in_max, "self.MAX")
296 def test_int64_out(self):
297 self.assertEqual(self.MAX, GIMarshallingTests.int64_out_max())
298 self.assertEqual(self.MIN, GIMarshallingTests.int64_out_min())
300 def test_int64_inout(self):
301 self.assertEqual(self.MIN, GIMarshallingTests.int64_inout_max_min(Number(self.MAX)))
302 self.assertEqual(self.MAX, GIMarshallingTests.int64_inout_min_max(Number(self.MIN)))
305 class TestUInt64(unittest.TestCase):
307 MAX = 2 ** 64 - 1
309 def test_uint64_return(self):
310 self.assertEqual(self.MAX, GIMarshallingTests.uint64_return())
312 def test_uint64_in(self):
313 number = Number(self.MAX)
315 GIMarshallingTests.uint64_in(number)
317 number.value += 1
319 self.assertRaises(OverflowError, GIMarshallingTests.uint64_in, number)
320 self.assertRaises(OverflowError, GIMarshallingTests.uint64_in, Number(-1))
322 self.assertRaises(TypeError, GIMarshallingTests.uint64_in, "self.MAX")
324 def test_uint64_out(self):
325 self.assertEqual(self.MAX, GIMarshallingTests.uint64_out())
327 def test_uint64_inout(self):
328 self.assertEqual(0, GIMarshallingTests.uint64_inout(Number(self.MAX)))
331 class TestShort(unittest.TestCase):
333 MAX = GLib.MAXSHORT
334 MIN = GLib.MINSHORT
336 def test_short_return(self):
337 self.assertEqual(self.MAX, GIMarshallingTests.short_return_max())
338 self.assertEqual(self.MIN, GIMarshallingTests.short_return_min())
340 def test_short_in(self):
341 max = Number(self.MAX)
342 min = Number(self.MIN)
344 GIMarshallingTests.short_in_max(max)
345 GIMarshallingTests.short_in_min(min)
347 max.value += 1
348 min.value -= 1
350 self.assertRaises(OverflowError, GIMarshallingTests.short_in_max, max)
351 self.assertRaises(OverflowError, GIMarshallingTests.short_in_min, min)
353 self.assertRaises(TypeError, GIMarshallingTests.short_in_max, "self.MAX")
355 def test_short_out(self):
356 self.assertEqual(self.MAX, GIMarshallingTests.short_out_max())
357 self.assertEqual(self.MIN, GIMarshallingTests.short_out_min())
359 def test_short_inout(self):
360 self.assertEqual(self.MIN, GIMarshallingTests.short_inout_max_min(Number(self.MAX)))
361 self.assertEqual(self.MAX, GIMarshallingTests.short_inout_min_max(Number(self.MIN)))
364 class TestUShort(unittest.TestCase):
366 MAX = GLib.MAXUSHORT
368 def test_ushort_return(self):
369 self.assertEqual(self.MAX, GIMarshallingTests.ushort_return())
371 def test_ushort_in(self):
372 number = Number(self.MAX)
374 GIMarshallingTests.ushort_in(number)
376 number.value += 1
378 self.assertRaises(OverflowError, GIMarshallingTests.ushort_in, number)
379 self.assertRaises(OverflowError, GIMarshallingTests.ushort_in, Number(-1))
381 self.assertRaises(TypeError, GIMarshallingTests.ushort_in, "self.MAX")
383 def test_ushort_out(self):
384 self.assertEqual(self.MAX, GIMarshallingTests.ushort_out())
386 def test_ushort_inout(self):
387 self.assertEqual(0, GIMarshallingTests.ushort_inout(Number(self.MAX)))
390 class TestInt(unittest.TestCase):
392 MAX = GLib.MAXINT
393 MIN = GLib.MININT
395 def test_int_return(self):
396 self.assertEqual(self.MAX, GIMarshallingTests.int_return_max())
397 self.assertEqual(self.MIN, GIMarshallingTests.int_return_min())
399 def test_int_in(self):
400 max = Number(self.MAX)
401 min = Number(self.MIN)
403 GIMarshallingTests.int_in_max(max)
404 GIMarshallingTests.int_in_min(min)
406 max.value += 1
407 min.value -= 1
409 self.assertRaises(OverflowError, GIMarshallingTests.int_in_max, max)
410 self.assertRaises(OverflowError, GIMarshallingTests.int_in_min, min)
412 self.assertRaises(TypeError, GIMarshallingTests.int_in_max, "self.MAX")
414 def test_int_out(self):
415 self.assertEqual(self.MAX, GIMarshallingTests.int_out_max())
416 self.assertEqual(self.MIN, GIMarshallingTests.int_out_min())
418 def test_int_inout(self):
419 self.assertEqual(self.MIN, GIMarshallingTests.int_inout_max_min(Number(self.MAX)))
420 self.assertEqual(self.MAX, GIMarshallingTests.int_inout_min_max(Number(self.MIN)))
421 self.assertRaises(TypeError, GIMarshallingTests.int_inout_min_max, Number(self.MIN), 42)
424 class TestUInt(unittest.TestCase):
426 MAX = GLib.MAXUINT
428 def test_uint_return(self):
429 self.assertEqual(self.MAX, GIMarshallingTests.uint_return())
431 def test_uint_in(self):
432 number = Number(self.MAX)
434 GIMarshallingTests.uint_in(number)
436 number.value += 1
438 self.assertRaises(OverflowError, GIMarshallingTests.uint_in, number)
439 self.assertRaises(OverflowError, GIMarshallingTests.uint_in, Number(-1))
441 self.assertRaises(TypeError, GIMarshallingTests.uint_in, "self.MAX")
443 def test_uint_out(self):
444 self.assertEqual(self.MAX, GIMarshallingTests.uint_out())
446 def test_uint_inout(self):
447 self.assertEqual(0, GIMarshallingTests.uint_inout(Number(self.MAX)))
450 class TestLong(unittest.TestCase):
452 MAX = GLib.MAXLONG
453 MIN = GLib.MINLONG
455 def test_long_return(self):
456 self.assertEqual(self.MAX, GIMarshallingTests.long_return_max())
457 self.assertEqual(self.MIN, GIMarshallingTests.long_return_min())
459 def test_long_in(self):
460 max = Number(self.MAX)
461 min = Number(self.MIN)
463 GIMarshallingTests.long_in_max(max)
464 GIMarshallingTests.long_in_min(min)
466 max.value += 1
467 min.value -= 1
469 self.assertRaises(OverflowError, GIMarshallingTests.long_in_max, max)
470 self.assertRaises(OverflowError, GIMarshallingTests.long_in_min, min)
472 self.assertRaises(TypeError, GIMarshallingTests.long_in_max, "self.MAX")
474 def test_long_out(self):
475 self.assertEqual(self.MAX, GIMarshallingTests.long_out_max())
476 self.assertEqual(self.MIN, GIMarshallingTests.long_out_min())
478 def test_long_inout(self):
479 self.assertEqual(self.MIN, GIMarshallingTests.long_inout_max_min(Number(self.MAX)))
480 self.assertEqual(self.MAX, GIMarshallingTests.long_inout_min_max(Number(self.MIN)))
483 class TestULong(unittest.TestCase):
485 MAX = GLib.MAXULONG
487 def test_ulong_return(self):
488 self.assertEqual(self.MAX, GIMarshallingTests.ulong_return())
490 def test_ulong_in(self):
491 number = Number(self.MAX)
493 GIMarshallingTests.ulong_in(number)
495 number.value += 1
497 self.assertRaises(OverflowError, GIMarshallingTests.ulong_in, number)
498 self.assertRaises(OverflowError, GIMarshallingTests.ulong_in, Number(-1))
500 self.assertRaises(TypeError, GIMarshallingTests.ulong_in, "self.MAX")
502 def test_ulong_out(self):
503 self.assertEqual(self.MAX, GIMarshallingTests.ulong_out())
505 def test_ulong_inout(self):
506 self.assertEqual(0, GIMarshallingTests.ulong_inout(Number(self.MAX)))
509 class TestSSize(unittest.TestCase):
511 MAX = GLib.MAXSSIZE
512 MIN = GLib.MINSSIZE
514 def test_ssize_return(self):
515 self.assertEqual(self.MAX, GIMarshallingTests.ssize_return_max())
516 self.assertEqual(self.MIN, GIMarshallingTests.ssize_return_min())
518 def test_ssize_in(self):
519 max = Number(self.MAX)
520 min = Number(self.MIN)
522 GIMarshallingTests.ssize_in_max(max)
523 GIMarshallingTests.ssize_in_min(min)
525 max.value += 1
526 min.value -= 1
528 self.assertRaises(OverflowError, GIMarshallingTests.ssize_in_max, max)
529 self.assertRaises(OverflowError, GIMarshallingTests.ssize_in_min, min)
531 self.assertRaises(TypeError, GIMarshallingTests.ssize_in_max, "self.MAX")
533 def test_ssize_out(self):
534 self.assertEqual(self.MAX, GIMarshallingTests.ssize_out_max())
535 self.assertEqual(self.MIN, GIMarshallingTests.ssize_out_min())
537 def test_ssize_inout(self):
538 self.assertEqual(self.MIN, GIMarshallingTests.ssize_inout_max_min(Number(self.MAX)))
539 self.assertEqual(self.MAX, GIMarshallingTests.ssize_inout_min_max(Number(self.MIN)))
542 class TestSize(unittest.TestCase):
544 MAX = GLib.MAXSIZE
546 def test_size_return(self):
547 self.assertEqual(self.MAX, GIMarshallingTests.size_return())
549 def test_size_in(self):
550 number = Number(self.MAX)
552 GIMarshallingTests.size_in(number)
554 number.value += 1
556 self.assertRaises(OverflowError, GIMarshallingTests.size_in, number)
557 self.assertRaises(OverflowError, GIMarshallingTests.size_in, Number(-1))
559 self.assertRaises(TypeError, GIMarshallingTests.size_in, "self.MAX")
561 def test_size_out(self):
562 self.assertEqual(self.MAX, GIMarshallingTests.size_out())
564 def test_size_inout(self):
565 self.assertEqual(0, GIMarshallingTests.size_inout(Number(self.MAX)))
568 class TestTimet(unittest.TestCase):
570 def test_time_t_return(self):
571 self.assertEqual(1234567890, GIMarshallingTests.time_t_return())
573 def test_time_t_in(self):
574 GIMarshallingTests.time_t_in(1234567890)
575 self.assertRaises(TypeError, GIMarshallingTests.time_t_in, "hello")
577 def test_time_t_out(self):
578 self.assertEqual(1234567890, GIMarshallingTests.time_t_out())
580 def test_time_t_inout(self):
581 self.assertEqual(0, GIMarshallingTests.time_t_inout(1234567890))
584 class TestFloat(unittest.TestCase):
586 MAX = GLib.MAXFLOAT
587 MIN = GLib.MINFLOAT
589 def test_float_return(self):
590 self.assertAlmostEqual(self.MAX, GIMarshallingTests.float_return())
592 def test_float_in(self):
593 GIMarshallingTests.float_in(Number(self.MAX))
595 self.assertRaises(TypeError, GIMarshallingTests.float_in, "self.MAX")
597 def test_float_out(self):
598 self.assertAlmostEqual(self.MAX, GIMarshallingTests.float_out())
600 def test_float_inout(self):
601 self.assertAlmostEqual(self.MIN, GIMarshallingTests.float_inout(Number(self.MAX)))
604 class TestDouble(unittest.TestCase):
606 MAX = GLib.MAXDOUBLE
607 MIN = GLib.MINDOUBLE
609 def test_double_return(self):
610 self.assertAlmostEqual(self.MAX, GIMarshallingTests.double_return())
612 def test_double_in(self):
613 GIMarshallingTests.double_in(Number(self.MAX))
615 self.assertRaises(TypeError, GIMarshallingTests.double_in, "self.MAX")
617 def test_double_out(self):
618 self.assertAlmostEqual(self.MAX, GIMarshallingTests.double_out())
620 def test_double_inout(self):
621 self.assertAlmostEqual(self.MIN, GIMarshallingTests.double_inout(Number(self.MAX)))
624 class TestGType(unittest.TestCase):
626 def test_gtype_name(self):
627 self.assertEqual("void", GObject.TYPE_NONE.name)
628 self.assertEqual("gchararray", GObject.TYPE_STRING.name)
630 def check_readonly(gtype):
631 gtype.name = "foo"
633 self.assertRaises(AttributeError, check_readonly, GObject.TYPE_NONE)
634 self.assertRaises(AttributeError, check_readonly, GObject.TYPE_STRING)
636 def test_gtype_return(self):
637 self.assertEqual(GObject.TYPE_NONE, GIMarshallingTests.gtype_return())
638 self.assertEqual(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_return())
640 def test_gtype_in(self):
641 GIMarshallingTests.gtype_in(GObject.TYPE_NONE)
642 GIMarshallingTests.gtype_string_in(GObject.TYPE_STRING)
643 self.assertRaises(TypeError, GIMarshallingTests.gtype_in, "foo")
644 self.assertRaises(TypeError, GIMarshallingTests.gtype_string_in, "foo")
646 def test_gtype_out(self):
647 self.assertEqual(GObject.TYPE_NONE, GIMarshallingTests.gtype_out())
648 self.assertEqual(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_out())
650 def test_gtype_inout(self):
651 self.assertEqual(GObject.TYPE_INT, GIMarshallingTests.gtype_inout(GObject.TYPE_NONE))
654 class TestUtf8(unittest.TestCase):
656 def test_utf8_none_return(self):
657 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_none_return())
659 def test_utf8_full_return(self):
660 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_full_return())
662 def test_utf8_none_in(self):
663 GIMarshallingTests.utf8_none_in(CONSTANT_UTF8)
664 if sys.version_info < (3, 0):
665 GIMarshallingTests.utf8_none_in(CONSTANT_UTF8.decode("utf-8"))
667 self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, 42)
668 self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, None)
670 def test_utf8_none_out(self):
671 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_none_out())
673 def test_utf8_full_out(self):
674 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_full_out())
676 def test_utf8_dangling_out(self):
677 GIMarshallingTests.utf8_dangling_out()
679 def test_utf8_none_inout(self):
680 self.assertEqual("", GIMarshallingTests.utf8_none_inout(CONSTANT_UTF8))
682 def test_utf8_full_inout(self):
683 self.assertEqual("", GIMarshallingTests.utf8_full_inout(CONSTANT_UTF8))
686 class TestFilename(unittest.TestCase):
687 def setUp(self):
688 self.workdir = tempfile.mkdtemp()
690 def tearDown(self):
691 shutil.rmtree(self.workdir)
693 @unittest.skipIf(os.name == "nt", "fixme")
694 def test_filename_in(self):
695 fname = os.path.join(self.workdir, u'testäø.txt')
697 try:
698 os.path.exists(fname)
699 except ValueError:
700 # non-unicode fs encoding
701 return
703 self.assertRaises(GLib.GError, GLib.file_get_contents, fname)
705 with open(fname.encode('UTF-8'), 'wb') as f:
706 f.write(b'hello world!\n\x01\x02')
708 (result, contents) = GLib.file_get_contents(fname)
709 self.assertEqual(result, True)
710 self.assertEqual(contents, b'hello world!\n\x01\x02')
712 def test_filename_in_nullable(self):
713 self.assertTrue(GIMarshallingTests.filename_copy(None) is None)
714 self.assertRaises(TypeError, GIMarshallingTests.filename_exists, None)
716 @unittest.skipIf(os.name == "nt", "fixme")
717 def test_filename_out(self):
718 self.assertRaises(GLib.GError, GLib.Dir.make_tmp, 'test')
719 name = 'testäø.XXXXXX'
721 try:
722 os.path.exists(name)
723 except ValueError:
724 # non-unicode fs encoding
725 return
727 dirname = GLib.Dir.make_tmp(name)
728 self.assertTrue(os.path.sep + 'testäø.' in dirname, dirname)
729 self.assertTrue(os.path.isdir(dirname))
730 os.rmdir(dirname)
732 def test_wrong_types(self):
733 self.assertRaises(TypeError, GIMarshallingTests.filename_copy, 23)
734 self.assertRaises(TypeError, GIMarshallingTests.filename_copy, [])
736 def test_null(self):
737 self.assertTrue(GIMarshallingTests.filename_copy(None) is None)
738 self.assertRaises(TypeError, GIMarshallingTests.filename_exists, None)
740 def test_round_trip(self):
741 self.assertEqual(GIMarshallingTests.filename_copy(u"foo"), "foo")
742 self.assertEqual(GIMarshallingTests.filename_copy(b"foo"), "foo")
744 def test_contains_null(self):
745 self.assertRaises(
746 (ValueError, TypeError),
747 GIMarshallingTests.filename_copy, b"foo\x00")
748 self.assertRaises(
749 (ValueError, TypeError),
750 GIMarshallingTests.filename_copy, u"foo\x00")
752 def test_as_is_py2(self):
753 if not PY2:
754 return
756 values = [
757 b"foo",
758 b"\xff\xff",
759 b"\xc3\xb6\xc3\xa4\xc3\xbc",
760 b"\xed\xa0\xbd",
761 b"\xf0\x90\x80\x81",
764 for v in values:
765 self.assertEqual(GIMarshallingTests.filename_copy(v), v)
766 self.assertEqual(GIMarshallingTests.filename_to_glib_repr(v), v)
768 def test_win32_surrogates(self):
769 if os.name != "nt":
770 return
772 copy = GIMarshallingTests.filename_copy
773 glib_repr = GIMarshallingTests.filename_to_glib_repr
775 if PY3:
776 self.assertEqual(copy(u"\ud83d"), u"\ud83d")
777 self.assertEqual(copy(u"\x61\uDC00"), u"\x61\uDC00")
778 self.assertEqual(copy(u"\uD800\uDC01"), u"\U00010001")
779 self.assertEqual(copy(u"\uD83D\x20\uDCA9"), u"\uD83D\x20\uDCA9")
780 else:
781 self.assertEqual(copy(u"\ud83d"), u"\ud83d".encode("utf-8"))
782 self.assertEqual(copy(u"\uD800\uDC01").decode("utf-8"),
783 u"\U00010001")
785 self.assertEqual(glib_repr(u"\ud83d"), b"\xed\xa0\xbd")
786 self.assertEqual(glib_repr(u"\uD800\uDC01"), b"\xf0\x90\x80\x81")
788 self.assertEqual(
789 glib_repr(u"\uD800\uDBFF"), b"\xED\xA0\x80\xED\xAF\xBF")
790 self.assertEqual(
791 glib_repr(u"\uD800\uE000"), b"\xED\xA0\x80\xEE\x80\x80")
792 self.assertEqual(
793 glib_repr(u"\uD7FF\uDC00"), b"\xED\x9F\xBF\xED\xB0\x80")
794 self.assertEqual(glib_repr(u"\x61\uDC00"), b"\x61\xED\xB0\x80")
795 self.assertEqual(glib_repr(u"\uDC00"), b"\xED\xB0\x80")
797 def test_win32_bytes_py3(self):
798 if not (os.name == "nt" and PY3):
799 return
801 values = [
802 b"foo",
803 b"\xff\xff",
804 b"\xc3\xb6\xc3\xa4\xc3\xbc",
805 b"\xed\xa0\xbd",
806 b"\xf0\x90\x80\x81",
809 for v in values:
810 try:
811 uni = v.decode(sys.getfilesystemencoding(), "surrogatepass")
812 except UnicodeDecodeError:
813 continue
814 self.assertEqual(GIMarshallingTests.filename_copy(v), uni)
816 def test_unix_various(self):
817 if os.name == "nt":
818 return
820 copy = GIMarshallingTests.filename_copy
821 glib_repr = GIMarshallingTests.filename_to_glib_repr
823 if PY3:
824 try:
825 os.fsdecode(b"\xff\xfe")
826 except UnicodeDecodeError:
827 self.assertRaises(UnicodeDecodeError, copy, b"\xff\xfe")
828 else:
829 str_path = copy(b"\xff\xfe")
830 self.assertTrue(isinstance(str_path, str))
831 self.assertEqual(str_path, os.fsdecode(b"\xff\xfe"))
832 self.assertEqual(copy(str_path), str_path)
833 self.assertEqual(glib_repr(b"\xff\xfe"), b"\xff\xfe")
834 self.assertEqual(glib_repr(str_path), b"\xff\xfe")
836 # if getfilesystemencoding is ASCII, then we should fail like
837 # os.fsencode
838 try:
839 byte_path = os.fsencode(u"ä")
840 except UnicodeEncodeError:
841 self.assertRaises(UnicodeEncodeError, copy, u"ä")
842 else:
843 self.assertEqual(copy(u"ä"), u"ä")
844 self.assertEqual(glib_repr(u"ä"), byte_path)
845 else:
846 self.assertTrue(isinstance(copy(b"\xff\xfe"), bytes))
847 self.assertEqual(copy(u"foo"), b"foo")
848 self.assertTrue(isinstance(copy(u"foo"), bytes))
849 try:
850 byte_path = u"ä".encode(sys.getfilesystemencoding())
851 except UnicodeEncodeError:
852 self.assertRaises(UnicodeEncodeError, copy, u"ä")
853 else:
854 self.assertEqual(copy(u"ä"), byte_path)
855 self.assertEqual(glib_repr(u"ä"), byte_path)
857 @unittest.skip("glib can't handle non-unicode paths")
858 def test_win32_surrogates_exists(self):
859 if os.name != "nt":
860 return
862 path = os.path.join(self.workdir, u"\ud83d")
863 with open(path, "wb"):
864 self.assertTrue(os.path.exists(path))
865 self.assertTrue(GIMarshallingTests.filename_exists(path))
866 os.unlink(path)
868 def test_path_exists_various_types(self):
869 wd = self.workdir
870 wdb = os.fsencode(wd) if PY3 else wd
872 paths = [(wdb, b"foo-1"), (wd, u"foo-2"), (wd, u"öäü-3")]
873 if PY3:
874 try:
875 paths.append((wd, os.fsdecode(b"\xff\xfe-4")))
876 except UnicodeDecodeError:
877 # depends on the code page
878 pass
880 if os.name != "nt":
881 paths.append((wdb, b"\xff\xfe-5"))
883 def valid_path(p):
884 try:
885 os.path.exists(p)
886 except ValueError:
887 return False
888 return True
890 for (d, path) in paths:
891 if not valid_path(path):
892 continue
893 path = os.path.join(d, path)
894 with open(path, "wb"):
895 self.assertTrue(GIMarshallingTests.filename_exists(path))
898 class TestArray(unittest.TestCase):
900 def test_array_fixed_int_return(self):
901 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_int_return())
903 def test_array_fixed_short_return(self):
904 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_short_return())
906 def test_array_fixed_int_in(self):
907 GIMarshallingTests.array_fixed_int_in(Sequence([-1, 0, 1, 2]))
909 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, Sequence([-1, '0', 1, 2]))
911 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, 42)
912 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, None)
914 def test_array_fixed_short_in(self):
915 GIMarshallingTests.array_fixed_short_in(Sequence([-1, 0, 1, 2]))
917 def test_array_fixed_out(self):
918 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_out())
920 def test_array_fixed_inout(self):
921 self.assertEqual([2, 1, 0, -1], GIMarshallingTests.array_fixed_inout([-1, 0, 1, 2]))
923 def test_array_return(self):
924 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_return())
926 def test_array_return_etc(self):
927 self.assertEqual(([5, 0, 1, 9], 14), GIMarshallingTests.array_return_etc(5, 9))
929 def test_array_in(self):
930 GIMarshallingTests.array_in(Sequence([-1, 0, 1, 2]))
931 GIMarshallingTests.array_in_guint64_len(Sequence([-1, 0, 1, 2]))
932 GIMarshallingTests.array_in_guint8_len(Sequence([-1, 0, 1, 2]))
934 def test_array_in_len_before(self):
935 GIMarshallingTests.array_in_len_before(Sequence([-1, 0, 1, 2]))
937 def test_array_in_len_zero_terminated(self):
938 GIMarshallingTests.array_in_len_zero_terminated(Sequence([-1, 0, 1, 2]))
940 def test_array_uint8_in(self):
941 GIMarshallingTests.array_uint8_in(Sequence([97, 98, 99, 100]))
942 GIMarshallingTests.array_uint8_in(b"abcd")
944 def test_array_string_in(self):
945 GIMarshallingTests.array_string_in(['foo', 'bar'])
947 def test_array_out(self):
948 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_out())
950 def test_array_out_etc(self):
951 self.assertEqual(([-5, 0, 1, 9], 4), GIMarshallingTests.array_out_etc(-5, 9))
953 def test_array_inout(self):
954 self.assertEqual([-2, -1, 0, 1, 2], GIMarshallingTests.array_inout(Sequence([-1, 0, 1, 2])))
956 def test_array_inout_etc(self):
957 self.assertEqual(([-5, -1, 0, 1, 9], 4),
958 GIMarshallingTests.array_inout_etc(-5, Sequence([-1, 0, 1, 2]), 9))
960 def test_method_array_in(self):
961 object_ = GIMarshallingTests.Object()
962 object_.method_array_in(Sequence([-1, 0, 1, 2]))
964 def test_method_array_out(self):
965 object_ = GIMarshallingTests.Object()
966 self.assertEqual([-1, 0, 1, 2], object_.method_array_out())
968 def test_method_array_inout(self):
969 object_ = GIMarshallingTests.Object()
970 self.assertEqual([-2, -1, 0, 1, 2], object_.method_array_inout(Sequence([-1, 0, 1, 2])))
972 def test_method_array_return(self):
973 object_ = GIMarshallingTests.Object()
974 self.assertEqual([-1, 0, 1, 2], object_.method_array_return())
976 def test_array_enum_in(self):
977 GIMarshallingTests.array_enum_in([GIMarshallingTests.Enum.VALUE1,
978 GIMarshallingTests.Enum.VALUE2,
979 GIMarshallingTests.Enum.VALUE3])
981 def test_array_boxed_struct_in(self):
982 struct1 = GIMarshallingTests.BoxedStruct()
983 struct1.long_ = 1
984 struct2 = GIMarshallingTests.BoxedStruct()
985 struct2.long_ = 2
986 struct3 = GIMarshallingTests.BoxedStruct()
987 struct3.long_ = 3
989 GIMarshallingTests.array_struct_in([struct1, struct2, struct3])
991 def test_array_boxed_struct_in_item_marshal_failure(self):
992 struct1 = GIMarshallingTests.BoxedStruct()
993 struct1.long_ = 1
994 struct2 = GIMarshallingTests.BoxedStruct()
995 struct2.long_ = 2
997 self.assertRaises(TypeError, GIMarshallingTests.array_struct_in,
998 [struct1, struct2, 'not_a_struct'])
1000 def test_array_boxed_struct_value_in(self):
1001 struct1 = GIMarshallingTests.BoxedStruct()
1002 struct1.long_ = 1
1003 struct2 = GIMarshallingTests.BoxedStruct()
1004 struct2.long_ = 2
1005 struct3 = GIMarshallingTests.BoxedStruct()
1006 struct3.long_ = 3
1008 GIMarshallingTests.array_struct_value_in([struct1, struct2, struct3])
1010 def test_array_boxed_struct_value_in_item_marshal_failure(self):
1011 struct1 = GIMarshallingTests.BoxedStruct()
1012 struct1.long_ = 1
1013 struct2 = GIMarshallingTests.BoxedStruct()
1014 struct2.long_ = 2
1016 self.assertRaises(TypeError, GIMarshallingTests.array_struct_value_in,
1017 [struct1, struct2, 'not_a_struct'])
1019 def test_array_boxed_struct_take_in(self):
1020 struct1 = GIMarshallingTests.BoxedStruct()
1021 struct1.long_ = 1
1022 struct2 = GIMarshallingTests.BoxedStruct()
1023 struct2.long_ = 2
1024 struct3 = GIMarshallingTests.BoxedStruct()
1025 struct3.long_ = 3
1027 GIMarshallingTests.array_struct_take_in([struct1, struct2, struct3])
1029 self.assertEqual(1, struct1.long_)
1031 def test_array_boxed_struct_return(self):
1032 (struct1, struct2, struct3) = GIMarshallingTests.array_zero_terminated_return_struct()
1033 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct1))
1034 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct2))
1035 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct3))
1036 self.assertEqual(42, struct1.long_)
1037 self.assertEqual(43, struct2.long_)
1038 self.assertEqual(44, struct3.long_)
1040 def test_array_simple_struct_in(self):
1041 struct1 = GIMarshallingTests.SimpleStruct()
1042 struct1.long_ = 1
1043 struct2 = GIMarshallingTests.SimpleStruct()
1044 struct2.long_ = 2
1045 struct3 = GIMarshallingTests.SimpleStruct()
1046 struct3.long_ = 3
1048 GIMarshallingTests.array_simple_struct_in([struct1, struct2, struct3])
1050 def test_array_simple_struct_in_item_marshal_failure(self):
1051 struct1 = GIMarshallingTests.SimpleStruct()
1052 struct1.long_ = 1
1053 struct2 = GIMarshallingTests.SimpleStruct()
1054 struct2.long_ = 2
1056 self.assertRaises(TypeError, GIMarshallingTests.array_simple_struct_in,
1057 [struct1, struct2, 'not_a_struct'])
1059 def test_array_multi_array_key_value_in(self):
1060 GIMarshallingTests.multi_array_key_value_in(["one", "two", "three"],
1061 [1, 2, 3])
1063 def test_array_in_nonzero_nonlen(self):
1064 GIMarshallingTests.array_in_nonzero_nonlen(1, b'abcd')
1066 def test_array_fixed_out_struct(self):
1067 struct1, struct2 = GIMarshallingTests.array_fixed_out_struct()
1069 self.assertEqual(7, struct1.long_)
1070 self.assertEqual(6, struct1.int8)
1071 self.assertEqual(6, struct2.long_)
1072 self.assertEqual(7, struct2.int8)
1074 def test_array_zero_terminated_return(self):
1075 self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_return())
1077 def test_array_zero_terminated_return_null(self):
1078 self.assertEqual([], GIMarshallingTests.array_zero_terminated_return_null())
1080 def test_array_zero_terminated_in(self):
1081 GIMarshallingTests.array_zero_terminated_in(Sequence(['0', '1', '2']))
1083 def test_array_zero_terminated_out(self):
1084 self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
1086 def test_array_zero_terminated_inout(self):
1087 self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.array_zero_terminated_inout(['0', '1', '2']))
1089 def test_init_function(self):
1090 self.assertEqual((True, []), GIMarshallingTests.init_function([]))
1091 self.assertEqual((True, []), GIMarshallingTests.init_function(['hello']))
1092 self.assertEqual((True, ['hello']),
1093 GIMarshallingTests.init_function(['hello', 'world']))
1095 def test_enum_array_return_type(self):
1096 self.assertEqual(GIMarshallingTests.enum_array_return_type(),
1097 [GIMarshallingTests.ExtraEnum.VALUE1,
1098 GIMarshallingTests.ExtraEnum.VALUE2,
1099 GIMarshallingTests.ExtraEnum.VALUE3])
1102 class TestGStrv(unittest.TestCase):
1104 def test_gstrv_return(self):
1105 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_return())
1107 def test_gstrv_in(self):
1108 GIMarshallingTests.gstrv_in(Sequence(['0', '1', '2']))
1110 def test_gstrv_out(self):
1111 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_out())
1113 def test_gstrv_inout(self):
1114 self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.gstrv_inout(['0', '1', '2']))
1117 class TestArrayGVariant(unittest.TestCase):
1119 def test_array_gvariant_none_in(self):
1120 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
1121 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_none_in(v)]
1122 self.assertEqual([27, "Hello"], returned)
1124 def test_array_gvariant_container_in(self):
1125 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
1126 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_container_in(v)]
1127 self.assertEqual([27, "Hello"], returned)
1129 def test_array_gvariant_full_in(self):
1130 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
1131 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_full_in(v)]
1132 self.assertEqual([27, "Hello"], returned)
1134 def test_bytearray_gvariant(self):
1135 v = GLib.Variant.new_bytestring(b"foo")
1136 self.assertEqual(v.get_bytestring(), b"foo")
1139 class TestGArray(unittest.TestCase):
1141 def test_garray_int_none_return(self):
1142 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.garray_int_none_return())
1144 def test_garray_uint64_none_return(self):
1145 self.assertEqual([0, GLib.MAXUINT64], GIMarshallingTests.garray_uint64_none_return())
1147 def test_garray_utf8_none_return(self):
1148 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_return())
1150 def test_garray_utf8_container_return(self):
1151 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_return())
1153 def test_garray_utf8_full_return(self):
1154 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_return())
1156 def test_garray_int_none_in(self):
1157 GIMarshallingTests.garray_int_none_in(Sequence([-1, 0, 1, 2]))
1159 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, Sequence([-1, '0', 1, 2]))
1161 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, 42)
1162 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, None)
1164 def test_garray_uint64_none_in(self):
1165 GIMarshallingTests.garray_uint64_none_in(Sequence([0, GLib.MAXUINT64]))
1167 def test_garray_utf8_none_in(self):
1168 GIMarshallingTests.garray_utf8_none_in(Sequence(['0', '1', '2']))
1170 def test_garray_utf8_none_out(self):
1171 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_out())
1173 def test_garray_utf8_container_out(self):
1174 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_out())
1176 def test_garray_utf8_full_out(self):
1177 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out())
1179 def test_garray_utf8_full_out_caller_allocated(self):
1180 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out_caller_allocated())
1182 def test_garray_utf8_none_inout(self):
1183 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_none_inout(Sequence(('0', '1', '2'))))
1185 def test_garray_utf8_container_inout(self):
1186 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_container_inout(['0', '1', '2']))
1188 def test_garray_utf8_full_inout(self):
1189 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_full_inout(['0', '1', '2']))
1192 class TestGPtrArray(unittest.TestCase):
1194 def test_gptrarray_utf8_none_return(self):
1195 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_return())
1197 def test_gptrarray_utf8_container_return(self):
1198 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_return())
1200 def test_gptrarray_utf8_full_return(self):
1201 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_return())
1203 def test_gptrarray_utf8_none_in(self):
1204 GIMarshallingTests.gptrarray_utf8_none_in(Sequence(['0', '1', '2']))
1206 def test_gptrarray_utf8_none_out(self):
1207 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_out())
1209 def test_gptrarray_utf8_container_out(self):
1210 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_out())
1212 def test_gptrarray_utf8_full_out(self):
1213 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_out())
1215 def test_gptrarray_utf8_none_inout(self):
1216 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_none_inout(Sequence(('0', '1', '2'))))
1218 def test_gptrarray_utf8_container_inout(self):
1219 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_container_inout(['0', '1', '2']))
1221 def test_gptrarray_utf8_full_inout(self):
1222 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_full_inout(['0', '1', '2']))
1225 class TestGBytes(unittest.TestCase):
1226 def test_gbytes_create(self):
1227 b = GLib.Bytes.new(b'\x00\x01\xFF')
1228 self.assertEqual(3, b.get_size())
1229 self.assertEqual(b'\x00\x01\xFF', b.get_data())
1231 def test_gbytes_create_take(self):
1232 b = GLib.Bytes.new_take(b'\x00\x01\xFF')
1233 self.assertEqual(3, b.get_size())
1234 self.assertEqual(b'\x00\x01\xFF', b.get_data())
1236 def test_gbytes_full_return(self):
1237 b = GIMarshallingTests.gbytes_full_return()
1238 self.assertEqual(4, b.get_size())
1239 self.assertEqual(b'\x00\x31\xFF\x33', b.get_data())
1241 def test_gbytes_none_in(self):
1242 b = GIMarshallingTests.gbytes_full_return()
1243 GIMarshallingTests.gbytes_none_in(b)
1245 def test_compare(self):
1246 a1 = GLib.Bytes.new(b'\x00\x01\xFF')
1247 a2 = GLib.Bytes.new(b'\x00\x01\xFF')
1248 b = GLib.Bytes.new(b'\x00\x01\xFE')
1250 self.assertTrue(a1.equal(a2))
1251 self.assertTrue(a2.equal(a1))
1252 self.assertFalse(a1.equal(b))
1253 self.assertFalse(b.equal(a2))
1255 self.assertEqual(0, a1.compare(a2))
1256 self.assertLess(0, a1.compare(b))
1257 self.assertGreater(0, b.compare(a1))
1260 class TestGByteArray(unittest.TestCase):
1261 def test_new(self):
1262 ba = GLib.ByteArray.new()
1263 self.assertEqual(b'', ba)
1265 ba = GLib.ByteArray.new_take(b'\x01\x02\xFF')
1266 self.assertEqual(b'\x01\x02\xFF', ba)
1268 def test_bytearray_full_return(self):
1269 self.assertEqual(b'\x001\xFF3', GIMarshallingTests.bytearray_full_return())
1271 def test_bytearray_none_in(self):
1272 b = b'\x00\x31\xFF\x33'
1273 ba = GLib.ByteArray.new_take(b)
1275 # b should always have the same value even
1276 # though the generated GByteArray is being modified
1277 GIMarshallingTests.bytearray_none_in(b)
1278 GIMarshallingTests.bytearray_none_in(b)
1280 # The GByteArray is just a bytes
1281 # thus it will not reflect any changes
1282 GIMarshallingTests.bytearray_none_in(ba)
1283 GIMarshallingTests.bytearray_none_in(ba)
1286 class TestGList(unittest.TestCase):
1288 def test_glist_int_none_return(self):
1289 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.glist_int_none_return())
1291 def test_glist_uint32_none_return(self):
1292 self.assertEqual([0, GLib.MAXUINT32], GIMarshallingTests.glist_uint32_none_return())
1294 def test_glist_utf8_none_return(self):
1295 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_return())
1297 def test_glist_utf8_container_return(self):
1298 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_return())
1300 def test_glist_utf8_full_return(self):
1301 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_return())
1303 def test_glist_int_none_in(self):
1304 GIMarshallingTests.glist_int_none_in(Sequence((-1, 0, 1, 2)))
1306 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, Sequence((-1, '0', 1, 2)))
1308 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, 42)
1309 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, None)
1311 def test_glist_int_none_in_error_getitem(self):
1313 class FailingSequence(Sequence):
1314 def __getitem__(self, key):
1315 raise Exception
1317 self.assertRaises(Exception, GIMarshallingTests.glist_int_none_in, FailingSequence((-1, 0, 1, 2)))
1319 def test_glist_uint32_none_in(self):
1320 GIMarshallingTests.glist_uint32_none_in(Sequence((0, GLib.MAXUINT32)))
1322 def test_glist_utf8_none_in(self):
1323 GIMarshallingTests.glist_utf8_none_in(Sequence(('0', '1', '2')))
1325 def test_glist_utf8_none_out(self):
1326 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_out())
1328 def test_glist_utf8_container_out(self):
1329 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_out())
1331 def test_glist_utf8_full_out(self):
1332 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_out())
1334 def test_glist_utf8_none_inout(self):
1335 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_none_inout(Sequence(('0', '1', '2'))))
1337 def test_glist_utf8_container_inout(self):
1338 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_container_inout(('0', '1', '2')))
1340 def test_glist_utf8_full_inout(self):
1341 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_full_inout(('0', '1', '2')))
1344 class TestGSList(unittest.TestCase):
1346 def test_gslist_int_none_return(self):
1347 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.gslist_int_none_return())
1349 def test_gslist_utf8_none_return(self):
1350 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_return())
1352 def test_gslist_utf8_container_return(self):
1353 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_return())
1355 def test_gslist_utf8_full_return(self):
1356 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_return())
1358 def test_gslist_int_none_in(self):
1359 GIMarshallingTests.gslist_int_none_in(Sequence((-1, 0, 1, 2)))
1361 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, Sequence((-1, '0', 1, 2)))
1363 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, 42)
1364 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, None)
1366 def test_gslist_int_none_in_error_getitem(self):
1368 class FailingSequence(Sequence):
1369 def __getitem__(self, key):
1370 raise Exception
1372 self.assertRaises(Exception, GIMarshallingTests.gslist_int_none_in, FailingSequence((-1, 0, 1, 2)))
1374 def test_gslist_utf8_none_in(self):
1375 GIMarshallingTests.gslist_utf8_none_in(Sequence(('0', '1', '2')))
1377 def test_gslist_utf8_none_out(self):
1378 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_out())
1380 def test_gslist_utf8_container_out(self):
1381 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_out())
1383 def test_gslist_utf8_full_out(self):
1384 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_out())
1386 def test_gslist_utf8_none_inout(self):
1387 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_none_inout(Sequence(('0', '1', '2'))))
1389 def test_gslist_utf8_container_inout(self):
1390 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_container_inout(('0', '1', '2')))
1392 def test_gslist_utf8_full_inout(self):
1393 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_full_inout(('0', '1', '2')))
1396 class TestGHashTable(unittest.TestCase):
1398 def test_ghashtable_int_none_return(self):
1399 self.assertEqual({-1: 1, 0: 0, 1: -1, 2: -2}, GIMarshallingTests.ghashtable_int_none_return())
1401 def test_ghashtable_int_none_return2(self):
1402 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_return())
1404 def test_ghashtable_int_container_return(self):
1405 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_return())
1407 def test_ghashtable_int_full_return(self):
1408 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_return())
1410 def test_ghashtable_int_none_in(self):
1411 GIMarshallingTests.ghashtable_int_none_in({-1: 1, 0: 0, 1: -1, 2: -2})
1413 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, '0': 0, 1: -1, 2: -2})
1414 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, 0: '0', 1: -1, 2: -2})
1416 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, '{-1: 1, 0: 0, 1: -1, 2: -2}')
1417 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, None)
1419 def test_ghashtable_utf8_none_in(self):
1420 GIMarshallingTests.ghashtable_utf8_none_in({'-1': '1', '0': '0', '1': '-1', '2': '-2'})
1422 def test_ghashtable_utf8_none_out(self):
1423 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_out())
1425 def test_ghashtable_utf8_container_out(self):
1426 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_out())
1428 def test_ghashtable_utf8_full_out(self):
1429 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_out())
1431 def test_ghashtable_utf8_none_inout(self):
1432 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1433 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1434 GIMarshallingTests.ghashtable_utf8_none_inout(i))
1436 def test_ghashtable_utf8_container_inout(self):
1437 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1438 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1439 GIMarshallingTests.ghashtable_utf8_container_inout(i))
1441 def test_ghashtable_utf8_full_inout(self):
1442 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1443 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1444 GIMarshallingTests.ghashtable_utf8_full_inout(i))
1446 def test_ghashtable_enum_none_in(self):
1447 GIMarshallingTests.ghashtable_enum_none_in({1: GIMarshallingTests.ExtraEnum.VALUE1,
1448 2: GIMarshallingTests.ExtraEnum.VALUE2,
1449 3: GIMarshallingTests.ExtraEnum.VALUE3})
1451 def test_ghashtable_enum_none_return(self):
1452 self.assertEqual({1: GIMarshallingTests.ExtraEnum.VALUE1,
1453 2: GIMarshallingTests.ExtraEnum.VALUE2,
1454 3: GIMarshallingTests.ExtraEnum.VALUE3},
1455 GIMarshallingTests.ghashtable_enum_none_return())
1458 class TestGValue(unittest.TestCase):
1460 def test_gvalue_return(self):
1461 self.assertEqual(42, GIMarshallingTests.gvalue_return())
1463 def test_gvalue_in(self):
1464 GIMarshallingTests.gvalue_in(42)
1465 value = GObject.Value(GObject.TYPE_INT, 42)
1466 GIMarshallingTests.gvalue_in(value)
1468 def test_gvalue_in_with_modification(self):
1469 value = GObject.Value(GObject.TYPE_INT, 42)
1470 GIMarshallingTests.gvalue_in_with_modification(value)
1471 self.assertEqual(value.get_int(), 24)
1473 def test_gvalue_int64_in(self):
1474 value = GObject.Value(GObject.TYPE_INT64, GLib.MAXINT64)
1475 GIMarshallingTests.gvalue_int64_in(value)
1477 def test_gvalue_in_with_type(self):
1478 value = GObject.Value(GObject.TYPE_STRING, 'foo')
1479 GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_STRING)
1481 value = GObject.Value(GIMarshallingTests.Flags.__gtype__,
1482 GIMarshallingTests.Flags.VALUE1)
1483 GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_FLAGS)
1485 def test_gvalue_in_enum(self):
1486 value = GObject.Value(GIMarshallingTests.Enum.__gtype__,
1487 GIMarshallingTests.Enum.VALUE3)
1488 GIMarshallingTests.gvalue_in_enum(value)
1490 def test_gvalue_out(self):
1491 self.assertEqual(42, GIMarshallingTests.gvalue_out())
1493 def test_gvalue_int64_out(self):
1494 self.assertEqual(GLib.MAXINT64, GIMarshallingTests.gvalue_int64_out())
1496 def test_gvalue_out_caller_allocates(self):
1497 self.assertEqual(42, GIMarshallingTests.gvalue_out_caller_allocates())
1499 def test_gvalue_inout(self):
1500 self.assertEqual('42', GIMarshallingTests.gvalue_inout(42))
1501 value = GObject.Value(int, 42)
1502 self.assertEqual('42', GIMarshallingTests.gvalue_inout(value))
1504 def test_gvalue_flat_array_in(self):
1505 # the function already asserts the correct values
1506 GIMarshallingTests.gvalue_flat_array([42, "42", True])
1508 def test_gvalue_flat_array_in_item_marshal_failure(self):
1509 # Tests the failure to marshal 2^256 to a GValue mid-way through the array marshaling.
1510 self.assertRaises(OverflowError, GIMarshallingTests.gvalue_flat_array,
1511 [42, 2 ** 256, True])
1513 self.assertRaises(OverflowError, GIMarshallingTests.gvalue_flat_array,
1514 [GLib.MAXINT + 1, "42", True])
1515 self.assertRaises(OverflowError, GIMarshallingTests.gvalue_flat_array,
1516 [GLib.MININT - 1, "42", True])
1518 def test_gvalue_flat_array_out(self):
1519 values = GIMarshallingTests.return_gvalue_flat_array()
1520 self.assertEqual(values, [42, '42', True])
1522 def test_gvalue_gobject_ref_counts(self):
1523 # Tests a GObject held by a GValue
1524 obj = GObject.Object()
1525 ref = weakref.ref(obj)
1526 grefcount = obj.__grefcount__
1528 value = GObject.Value()
1529 value.init(GObject.TYPE_OBJECT)
1531 # TYPE_OBJECT will inc ref count as it should
1532 value.set_object(obj)
1533 self.assertEqual(obj.__grefcount__, grefcount + 1)
1535 # multiple set_object should not inc ref count
1536 value.set_object(obj)
1537 self.assertEqual(obj.__grefcount__, grefcount + 1)
1539 # get_object will re-use the same wrapper as obj
1540 res = value.get_object()
1541 self.assertEqual(obj, res)
1542 self.assertEqual(obj.__grefcount__, grefcount + 1)
1544 # multiple get_object should not inc ref count
1545 res = value.get_object()
1546 self.assertEqual(obj.__grefcount__, grefcount + 1)
1548 # deletion of the result and value holder should bring the
1549 # refcount back to where we started
1550 del res
1551 del value
1552 gc.collect()
1553 self.assertEqual(obj.__grefcount__, grefcount)
1555 del obj
1556 gc.collect()
1557 self.assertEqual(ref(), None)
1559 def test_gvalue_boxed_ref_counts(self):
1560 # Tests a boxed type wrapping a python object pointer (TYPE_PYOBJECT)
1561 # held by a GValue
1562 class Obj(object):
1563 pass
1565 obj = Obj()
1566 ref = weakref.ref(obj)
1567 refcount = sys.getrefcount(obj)
1569 value = GObject.Value()
1570 value.init(GObject.TYPE_PYOBJECT)
1572 # boxed TYPE_PYOBJECT will inc ref count as it should
1573 value.set_boxed(obj)
1574 self.assertEqual(sys.getrefcount(obj), refcount + 1)
1576 # multiple set_boxed should not inc ref count
1577 value.set_boxed(obj)
1578 self.assertEqual(sys.getrefcount(obj), refcount + 1)
1580 res = value.get_boxed()
1581 self.assertEqual(obj, res)
1582 self.assertEqual(sys.getrefcount(obj), refcount + 2)
1584 # multiple get_boxed should not inc ref count
1585 res = value.get_boxed()
1586 self.assertEqual(sys.getrefcount(obj), refcount + 2)
1588 # deletion of the result and value holder should bring the
1589 # refcount back to where we started
1590 del res
1591 del value
1592 gc.collect()
1593 self.assertEqual(sys.getrefcount(obj), refcount)
1595 del obj
1596 gc.collect()
1597 self.assertEqual(ref(), None)
1599 # FIXME: crashes
1600 def disabled_test_gvalue_flat_array_round_trip(self):
1601 self.assertEqual([42, '42', True],
1602 GIMarshallingTests.gvalue_flat_array_round_trip(42, '42', True))
1605 class TestGClosure(unittest.TestCase):
1607 def test_in(self):
1608 GIMarshallingTests.gclosure_in(lambda: 42)
1610 def test_pass(self):
1611 # test passing a closure between two C calls
1612 closure = GIMarshallingTests.gclosure_return()
1613 GIMarshallingTests.gclosure_in(closure)
1615 def test_type_error(self):
1616 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, 42)
1617 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, None)
1620 class TestCallbacks(unittest.TestCase):
1621 def test_return_value_only(self):
1622 def cb():
1623 return 5
1624 self.assertEqual(GIMarshallingTests.callback_return_value_only(cb), 5)
1626 def test_one_out_arg(self):
1627 def cb():
1628 return 5.5
1629 self.assertAlmostEqual(GIMarshallingTests.callback_one_out_parameter(cb), 5.5)
1631 def test_multiple_out_args(self):
1632 def cb():
1633 return (5.5, 42.0)
1634 res = GIMarshallingTests.callback_multiple_out_parameters(cb)
1635 self.assertAlmostEqual(res[0], 5.5)
1636 self.assertAlmostEqual(res[1], 42.0)
1638 def test_return_and_one_out_arg(self):
1639 def cb():
1640 return (5, 42.0)
1641 res = GIMarshallingTests.callback_return_value_and_one_out_parameter(cb)
1642 self.assertEqual(res[0], 5)
1643 self.assertAlmostEqual(res[1], 42.0)
1645 def test_return_and_multiple_out_arg(self):
1646 def cb():
1647 return (5, 42, -1000)
1648 self.assertEqual(GIMarshallingTests.callback_return_value_and_multiple_out_parameters(cb),
1649 (5, 42, -1000))
1652 class TestPointer(unittest.TestCase):
1653 def test_pointer_in_return(self):
1654 self.assertEqual(GIMarshallingTests.pointer_in_return(42), 42)
1657 class TestEnum(unittest.TestCase):
1659 def test_enum(self):
1660 self.assertTrue(issubclass(GIMarshallingTests.Enum, int))
1661 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE1, GIMarshallingTests.Enum))
1662 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE2, GIMarshallingTests.Enum))
1663 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE3, GIMarshallingTests.Enum))
1664 self.assertEqual(42, GIMarshallingTests.Enum.VALUE3)
1666 def test_value_nick_and_name(self):
1667 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_nick, 'value1')
1668 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_nick, 'value2')
1669 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_nick, 'value3')
1671 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE1')
1672 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE2')
1673 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE3')
1675 def test_enum_in(self):
1676 GIMarshallingTests.enum_in(GIMarshallingTests.Enum.VALUE3)
1677 GIMarshallingTests.enum_in(42)
1679 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 43)
1680 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 'GIMarshallingTests.Enum.VALUE3')
1682 def test_enum_return(self):
1683 enum = GIMarshallingTests.enum_returnv()
1684 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1685 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1687 def test_enum_out(self):
1688 enum = GIMarshallingTests.enum_out()
1689 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1690 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1692 def test_enum_inout(self):
1693 enum = GIMarshallingTests.enum_inout(GIMarshallingTests.Enum.VALUE3)
1694 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1695 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE1)
1697 def test_enum_second(self):
1698 # check for the bug where different non-gtype enums share the same class
1699 self.assertNotEqual(GIMarshallingTests.Enum, GIMarshallingTests.SecondEnum)
1701 # check that values are not being shared between different enums
1702 self.assertTrue(hasattr(GIMarshallingTests.SecondEnum, "SECONDVALUE1"))
1703 self.assertRaises(AttributeError, getattr, GIMarshallingTests.Enum, "SECONDVALUE1")
1704 self.assertTrue(hasattr(GIMarshallingTests.Enum, "VALUE1"))
1705 self.assertRaises(AttributeError, getattr, GIMarshallingTests.SecondEnum, "VALUE1")
1707 def test_enum_gtype_name_is_namespaced(self):
1708 self.assertEqual(GIMarshallingTests.Enum.__gtype__.name,
1709 'PyGIMarshallingTestsEnum')
1711 def test_enum_add_type_error(self):
1712 self.assertRaises(TypeError,
1713 gi._gi.enum_add,
1714 GIMarshallingTests.NoTypeFlags.__gtype__)
1716 def test_type_module_name(self):
1717 self.assertEqual(GIMarshallingTests.Enum.__name__, "Enum")
1718 self.assertEqual(GIMarshallingTests.Enum.__module__,
1719 "gi.repository.GIMarshallingTests")
1721 def test_repr(self):
1722 self.assertEqual(repr(GIMarshallingTests.Enum.VALUE3),
1723 "<enum GI_MARSHALLING_TESTS_ENUM_VALUE3 of type "
1724 "GIMarshallingTests.Enum>")
1727 class TestEnumVFuncResults(unittest.TestCase):
1728 class EnumTester(GIMarshallingTests.Object):
1729 def do_vfunc_return_enum(self):
1730 return GIMarshallingTests.Enum.VALUE2
1732 def do_vfunc_out_enum(self):
1733 return GIMarshallingTests.Enum.VALUE3
1735 def test_vfunc_return_enum(self):
1736 tester = self.EnumTester()
1737 self.assertEqual(tester.vfunc_return_enum(), GIMarshallingTests.Enum.VALUE2)
1739 def test_vfunc_out_enum(self):
1740 tester = self.EnumTester()
1741 self.assertEqual(tester.vfunc_out_enum(), GIMarshallingTests.Enum.VALUE3)
1744 class TestGEnum(unittest.TestCase):
1746 def test_genum(self):
1747 self.assertTrue(issubclass(GIMarshallingTests.GEnum, GObject.GEnum))
1748 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE1, GIMarshallingTests.GEnum))
1749 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE2, GIMarshallingTests.GEnum))
1750 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE3, GIMarshallingTests.GEnum))
1751 self.assertEqual(42, GIMarshallingTests.GEnum.VALUE3)
1753 def test_value_nick_and_name(self):
1754 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_nick, 'value1')
1755 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_nick, 'value2')
1756 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_nick, 'value3')
1758 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE1')
1759 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE2')
1760 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE3')
1762 def test_genum_in(self):
1763 GIMarshallingTests.genum_in(GIMarshallingTests.GEnum.VALUE3)
1764 GIMarshallingTests.genum_in(42)
1765 GIMarshallingTests.GEnum.in_(42)
1767 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 43)
1768 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 'GIMarshallingTests.GEnum.VALUE3')
1770 def test_genum_return(self):
1771 genum = GIMarshallingTests.genum_returnv()
1772 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1773 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1775 def test_genum_out(self):
1776 genum = GIMarshallingTests.genum_out()
1777 genum = GIMarshallingTests.GEnum.out()
1778 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1779 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1781 def test_genum_inout(self):
1782 genum = GIMarshallingTests.genum_inout(GIMarshallingTests.GEnum.VALUE3)
1783 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1784 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE1)
1786 def test_type_module_name(self):
1787 self.assertEqual(GIMarshallingTests.GEnum.__name__, "GEnum")
1788 self.assertEqual(GIMarshallingTests.GEnum.__module__,
1789 "gi.repository.GIMarshallingTests")
1791 def test_repr(self):
1792 self.assertEqual(repr(GIMarshallingTests.GEnum.VALUE3),
1793 "<enum GI_MARSHALLING_TESTS_GENUM_VALUE3 of type "
1794 "GIMarshallingTests.GEnum>")
1797 class TestGFlags(unittest.TestCase):
1799 def test_flags(self):
1800 self.assertTrue(issubclass(GIMarshallingTests.Flags, GObject.GFlags))
1801 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1, GIMarshallingTests.Flags))
1802 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE2, GIMarshallingTests.Flags))
1803 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE3, GIMarshallingTests.Flags))
1804 # __or__() operation should still return an instance, not an int.
1805 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1 | GIMarshallingTests.Flags.VALUE2,
1806 GIMarshallingTests.Flags))
1807 self.assertEqual(1 << 1, GIMarshallingTests.Flags.VALUE2)
1809 def test_value_nick_and_name(self):
1810 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_nick, 'value1')
1811 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_nick, 'value2')
1812 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_nick, 'value3')
1814 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE1')
1815 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE2')
1816 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE3')
1818 def test_flags_in(self):
1819 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2)
1820 GIMarshallingTests.Flags.in_(GIMarshallingTests.Flags.VALUE2)
1821 # result of __or__() operation should still be valid instance, not an int.
1822 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE2)
1823 GIMarshallingTests.flags_in_zero(Number(0))
1824 GIMarshallingTests.Flags.in_zero(Number(0))
1826 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 1 << 1)
1827 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 'GIMarshallingTests.Flags.VALUE2')
1829 def test_flags_return(self):
1830 flags = GIMarshallingTests.flags_returnv()
1831 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1832 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1834 def test_flags_return_method(self):
1835 flags = GIMarshallingTests.Flags.returnv()
1836 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1837 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1839 def test_flags_out(self):
1840 flags = GIMarshallingTests.flags_out()
1841 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1842 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1844 def test_flags_inout(self):
1845 flags = GIMarshallingTests.flags_inout(GIMarshallingTests.Flags.VALUE2)
1846 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1847 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE1)
1849 def test_type_module_name(self):
1850 self.assertEqual(GIMarshallingTests.Flags.__name__, "Flags")
1851 self.assertEqual(GIMarshallingTests.Flags.__module__,
1852 "gi.repository.GIMarshallingTests")
1854 def test_repr(self):
1855 self.assertEqual(repr(GIMarshallingTests.Flags.VALUE2),
1856 "<flags GI_MARSHALLING_TESTS_FLAGS_VALUE2 of type "
1857 "GIMarshallingTests.Flags>")
1859 def test_flags_large_in(self):
1860 GIMarshallingTests.extra_flags_large_in(
1861 GIMarshallingTests.ExtraFlags.VALUE2)
1864 class TestNoTypeFlags(unittest.TestCase):
1866 def test_flags(self):
1867 self.assertTrue(issubclass(GIMarshallingTests.NoTypeFlags, GObject.GFlags))
1868 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1, GIMarshallingTests.NoTypeFlags))
1869 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE2, GIMarshallingTests.NoTypeFlags))
1870 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE3, GIMarshallingTests.NoTypeFlags))
1871 # __or__() operation should still return an instance, not an int.
1872 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1 | GIMarshallingTests.NoTypeFlags.VALUE2,
1873 GIMarshallingTests.NoTypeFlags))
1874 self.assertEqual(1 << 1, GIMarshallingTests.NoTypeFlags.VALUE2)
1876 def test_value_nick_and_name(self):
1877 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_nick, 'value1')
1878 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_nick, 'value2')
1879 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_nick, 'value3')
1881 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE1')
1882 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2')
1883 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE3')
1885 def test_flags_in(self):
1886 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2)
1887 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2 | GIMarshallingTests.NoTypeFlags.VALUE2)
1888 GIMarshallingTests.no_type_flags_in_zero(Number(0))
1890 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 1 << 1)
1891 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 'GIMarshallingTests.NoTypeFlags.VALUE2')
1893 def test_flags_return(self):
1894 flags = GIMarshallingTests.no_type_flags_returnv()
1895 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1896 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1898 def test_flags_out(self):
1899 flags = GIMarshallingTests.no_type_flags_out()
1900 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1901 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1903 def test_flags_inout(self):
1904 flags = GIMarshallingTests.no_type_flags_inout(GIMarshallingTests.NoTypeFlags.VALUE2)
1905 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1906 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE1)
1908 def test_flags_gtype_name_is_namespaced(self):
1909 self.assertEqual(GIMarshallingTests.NoTypeFlags.__gtype__.name,
1910 'PyGIMarshallingTestsNoTypeFlags')
1912 def test_type_module_name(self):
1913 self.assertEqual(GIMarshallingTests.NoTypeFlags.__name__,
1914 "NoTypeFlags")
1915 self.assertEqual(GIMarshallingTests.NoTypeFlags.__module__,
1916 "gi.repository.GIMarshallingTests")
1918 def test_repr(self):
1919 self.assertEqual(repr(GIMarshallingTests.NoTypeFlags.VALUE2),
1920 "<flags GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2 of "
1921 "type GIMarshallingTests.NoTypeFlags>")
1924 class TestStructure(unittest.TestCase):
1926 def test_simple_struct(self):
1927 self.assertTrue(issubclass(GIMarshallingTests.SimpleStruct, GObject.GPointer))
1929 struct = GIMarshallingTests.SimpleStruct()
1930 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1932 self.assertEqual(0, struct.long_)
1933 self.assertEqual(0, struct.int8)
1935 struct.long_ = 6
1936 struct.int8 = 7
1938 self.assertEqual(6, struct.long_)
1939 self.assertEqual(7, struct.int8)
1941 del struct
1943 def test_nested_struct(self):
1944 struct = GIMarshallingTests.NestedStruct()
1946 self.assertTrue(isinstance(struct.simple_struct, GIMarshallingTests.SimpleStruct))
1948 struct.simple_struct.long_ = 42
1949 self.assertEqual(42, struct.simple_struct.long_)
1951 del struct
1953 def test_not_simple_struct(self):
1954 struct = GIMarshallingTests.NotSimpleStruct()
1955 self.assertEqual(None, struct.pointer)
1957 def test_simple_struct_return(self):
1958 struct = GIMarshallingTests.simple_struct_returnv()
1960 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1961 self.assertEqual(6, struct.long_)
1962 self.assertEqual(7, struct.int8)
1964 del struct
1966 def test_simple_struct_in(self):
1967 struct = GIMarshallingTests.SimpleStruct()
1968 struct.long_ = 6
1969 struct.int8 = 7
1971 GIMarshallingTests.SimpleStruct.inv(struct)
1973 del struct
1975 struct = GIMarshallingTests.NestedStruct()
1977 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, struct)
1979 del struct
1981 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, None)
1983 def test_simple_struct_method(self):
1984 struct = GIMarshallingTests.SimpleStruct()
1985 struct.long_ = 6
1986 struct.int8 = 7
1988 struct.method()
1990 del struct
1992 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.method)
1994 def test_pointer_struct(self):
1995 self.assertTrue(issubclass(GIMarshallingTests.PointerStruct, GObject.GPointer))
1997 struct = GIMarshallingTests.PointerStruct()
1998 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
2000 del struct
2002 def test_pointer_struct_return(self):
2003 struct = GIMarshallingTests.pointer_struct_returnv()
2005 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
2006 self.assertEqual(42, struct.long_)
2008 del struct
2010 def test_pointer_struct_in(self):
2011 struct = GIMarshallingTests.PointerStruct()
2012 struct.long_ = 42
2014 struct.inv()
2016 del struct
2018 def test_boxed_struct(self):
2019 self.assertTrue(issubclass(GIMarshallingTests.BoxedStruct, GObject.GBoxed))
2021 struct = GIMarshallingTests.BoxedStruct()
2022 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
2024 self.assertEqual(0, struct.long_)
2025 self.assertEqual(None, struct.string_)
2026 self.assertEqual([], struct.g_strv)
2028 del struct
2030 def test_boxed_struct_new(self):
2031 struct = GIMarshallingTests.BoxedStruct.new()
2032 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
2033 self.assertEqual(struct.long_, 0)
2034 self.assertEqual(struct.string_, None)
2036 del struct
2038 def test_boxed_struct_copy(self):
2039 struct = GIMarshallingTests.BoxedStruct()
2040 struct.long_ = 42
2041 struct.string_ = 'hello'
2043 new_struct = struct.copy()
2044 self.assertTrue(isinstance(new_struct, GIMarshallingTests.BoxedStruct))
2045 self.assertEqual(new_struct.long_, 42)
2046 self.assertEqual(new_struct.string_, 'hello')
2048 del new_struct
2049 del struct
2051 def test_boxed_struct_return(self):
2052 struct = GIMarshallingTests.boxed_struct_returnv()
2054 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
2055 self.assertEqual(42, struct.long_)
2056 self.assertEqual('hello', struct.string_)
2057 self.assertEqual(['0', '1', '2'], struct.g_strv)
2059 del struct
2061 def test_boxed_struct_in(self):
2062 struct = GIMarshallingTests.BoxedStruct()
2063 struct.long_ = 42
2065 struct.inv()
2067 del struct
2069 def test_boxed_struct_out(self):
2070 struct = GIMarshallingTests.boxed_struct_out()
2072 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
2073 self.assertEqual(42, struct.long_)
2075 del struct
2077 def test_boxed_struct_inout(self):
2078 in_struct = GIMarshallingTests.BoxedStruct()
2079 in_struct.long_ = 42
2081 out_struct = GIMarshallingTests.boxed_struct_inout(in_struct)
2083 self.assertTrue(isinstance(out_struct, GIMarshallingTests.BoxedStruct))
2084 self.assertEqual(0, out_struct.long_)
2086 del in_struct
2087 del out_struct
2089 def test_struct_field_assignment(self):
2090 struct = GIMarshallingTests.BoxedStruct()
2092 struct.long_ = 42
2093 struct.string_ = 'hello'
2094 self.assertEqual(struct.long_, 42)
2095 self.assertEqual(struct.string_, 'hello')
2097 def test_union_init(self):
2098 with warnings.catch_warnings(record=True) as warn:
2099 warnings.simplefilter('always')
2100 GIMarshallingTests.Union(42)
2102 self.assertTrue(issubclass(warn[0].category, TypeError))
2104 with warnings.catch_warnings(record=True) as warn:
2105 warnings.simplefilter('always')
2106 GIMarshallingTests.Union(f=42)
2108 self.assertTrue(issubclass(warn[0].category, TypeError))
2110 def test_union(self):
2111 union = GIMarshallingTests.Union()
2113 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
2115 new_union = union.copy()
2116 self.assertTrue(isinstance(new_union, GIMarshallingTests.Union))
2118 del union
2119 del new_union
2121 def test_union_return(self):
2122 union = GIMarshallingTests.union_returnv()
2124 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
2125 self.assertEqual(42, union.long_)
2127 del union
2129 def test_union_in(self):
2130 union = GIMarshallingTests.Union()
2131 union.long_ = 42
2133 union.inv()
2135 del union
2137 def test_union_method(self):
2138 union = GIMarshallingTests.Union()
2139 union.long_ = 42
2141 union.method()
2143 del union
2145 self.assertRaises(TypeError, GIMarshallingTests.Union.method)
2147 def test_repr(self):
2148 self.assertRegexpMatches(
2149 repr(GIMarshallingTests.PointerStruct()),
2150 r"<GIMarshallingTests.PointerStruct object at 0x[^\s]+ "
2151 r"\(void at 0x[^\s]+\)>")
2153 self.assertRegexpMatches(
2154 repr(GIMarshallingTests.SimpleStruct()),
2155 r"<GIMarshallingTests.SimpleStruct object at 0x[^\s]+ "
2156 r"\(void at 0x[^\s]+\)>")
2158 self.assertRegexpMatches(
2159 repr(GIMarshallingTests.Union()),
2160 r"<GIMarshallingTests.Union object at 0x[^\s]+ "
2161 r"\(GIMarshallingTestsUnion at 0x[^\s]+\)>")
2163 self.assertRegexpMatches(
2164 repr(GIMarshallingTests.BoxedStruct()),
2165 r"<GIMarshallingTests.BoxedStruct object at 0x[^\s]+ "
2166 r"\(GIMarshallingTestsBoxedStruct at 0x[^\s]+\)>")
2169 class TestGObject(unittest.TestCase):
2171 def test_object(self):
2172 self.assertTrue(issubclass(GIMarshallingTests.Object, GObject.GObject))
2174 object_ = GIMarshallingTests.Object()
2175 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
2176 self.assertEqual(object_.__grefcount__, 1)
2178 def test_object_new(self):
2179 object_ = GIMarshallingTests.Object.new(42)
2180 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
2181 self.assertEqual(object_.__grefcount__, 1)
2183 def test_object_int(self):
2184 object_ = GIMarshallingTests.Object(int=42)
2185 self.assertEqual(object_.int_, 42)
2186 # FIXME: Don't work yet.
2187 # object_.int_ = 0
2188 # self.assertEqual(object_.int_, 0)
2190 def test_object_static_method(self):
2191 GIMarshallingTests.Object.static_method()
2193 def test_object_method(self):
2194 GIMarshallingTests.Object(int=42).method()
2195 self.assertRaises(TypeError, GIMarshallingTests.Object.method, GObject.GObject())
2196 self.assertRaises(TypeError, GIMarshallingTests.Object.method)
2198 def test_sub_object(self):
2199 self.assertTrue(issubclass(GIMarshallingTests.SubObject, GIMarshallingTests.Object))
2201 object_ = GIMarshallingTests.SubObject()
2202 self.assertTrue(isinstance(object_, GIMarshallingTests.SubObject))
2204 def test_sub_object_new(self):
2205 self.assertRaises(TypeError, GIMarshallingTests.SubObject.new, 42)
2207 def test_sub_object_static_method(self):
2208 object_ = GIMarshallingTests.SubObject()
2209 object_.static_method()
2211 def test_sub_object_method(self):
2212 object_ = GIMarshallingTests.SubObject(int=42)
2213 object_.method()
2215 def test_sub_object_sub_method(self):
2216 object_ = GIMarshallingTests.SubObject()
2217 object_.sub_method()
2219 def test_sub_object_overwritten_method(self):
2220 object_ = GIMarshallingTests.SubObject()
2221 object_.overwritten_method()
2223 self.assertRaises(TypeError, GIMarshallingTests.SubObject.overwritten_method, GIMarshallingTests.Object())
2225 def test_sub_object_int(self):
2226 object_ = GIMarshallingTests.SubObject()
2227 self.assertEqual(object_.int_, 0)
2228 # FIXME: Don't work yet.
2229 # object_.int_ = 42
2230 # self.assertEqual(object_.int_, 42)
2232 def test_object_none_return(self):
2233 object_ = GIMarshallingTests.Object.none_return()
2234 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
2235 self.assertEqual(object_.__grefcount__, 2)
2237 def test_object_full_return(self):
2238 object_ = GIMarshallingTests.Object.full_return()
2239 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
2240 self.assertEqual(object_.__grefcount__, 1)
2242 def test_object_none_in(self):
2243 object_ = GIMarshallingTests.Object(int=42)
2244 GIMarshallingTests.Object.none_in(object_)
2245 self.assertEqual(object_.__grefcount__, 1)
2247 object_ = GIMarshallingTests.SubObject(int=42)
2248 GIMarshallingTests.Object.none_in(object_)
2250 object_ = GObject.GObject()
2251 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, object_)
2253 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, None)
2255 def test_object_none_out(self):
2256 object_ = GIMarshallingTests.Object.none_out()
2257 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
2258 self.assertEqual(object_.__grefcount__, 2)
2260 new_object = GIMarshallingTests.Object.none_out()
2261 self.assertTrue(new_object is object_)
2263 def test_object_full_out(self):
2264 object_ = GIMarshallingTests.Object.full_out()
2265 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
2266 self.assertEqual(object_.__grefcount__, 1)
2268 def test_object_none_inout(self):
2269 object_ = GIMarshallingTests.Object(int=42)
2270 new_object = GIMarshallingTests.Object.none_inout(object_)
2272 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
2274 self.assertFalse(object_ is new_object)
2276 self.assertEqual(object_.__grefcount__, 1)
2277 self.assertEqual(new_object.__grefcount__, 2)
2279 new_new_object = GIMarshallingTests.Object.none_inout(object_)
2280 self.assertTrue(new_new_object is new_object)
2282 GIMarshallingTests.Object.none_inout(GIMarshallingTests.SubObject(int=42))
2284 def test_object_full_inout(self):
2285 # Using gimarshallingtests.c from GI versions > 1.38.0 will show this
2286 # test as an "unexpected success" due to reference leak fixes in that file.
2287 # TODO: remove the expectedFailure once PyGI relies on GI > 1.38.0.
2288 object_ = GIMarshallingTests.Object(int=42)
2289 new_object = GIMarshallingTests.Object.full_inout(object_)
2291 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
2293 self.assertFalse(object_ is new_object)
2295 self.assertEqual(object_.__grefcount__, 1)
2296 self.assertEqual(new_object.__grefcount__, 1)
2298 def test_repr(self):
2299 self.assertRegexpMatches(
2300 repr(GIMarshallingTests.Object(int=42)),
2301 r"<GIMarshallingTests.Object object at 0x[^\s]+ "
2302 r"\(GIMarshallingTestsObject at 0x[^\s]+\)>")
2304 def test_nongir_repr(self):
2305 self.assertRegexpMatches(
2306 repr(Gio.File.new_for_path("")),
2307 r"<__gi__.GLocalFile object at 0x[^\s]+ "
2308 r"\(GLocalFile at 0x[^\s]+\)>")
2310 # FIXME: Doesn't actually return the same object.
2311 # def test_object_inout_same(self):
2312 # object_ = GIMarshallingTests.Object()
2313 # new_object = GIMarshallingTests.object_full_inout(object_)
2314 # self.assertTrue(object_ is new_object)
2315 # self.assertEqual(object_.__grefcount__, 1)
2318 class TestPythonGObject(unittest.TestCase):
2320 class Object(GIMarshallingTests.Object):
2321 return_for_caller_allocated_out_parameter = 'test caller alloc return'
2323 def __init__(self, int):
2324 GIMarshallingTests.Object.__init__(self)
2325 self.val = None
2327 def method(self):
2328 # Don't call super, which asserts that self.int == 42.
2329 pass
2331 def do_method_int8_in(self, int8):
2332 self.val = int8
2334 def do_method_int8_out(self):
2335 return 42
2337 def do_method_int8_arg_and_out_caller(self, arg):
2338 return arg + 1
2340 def do_method_int8_arg_and_out_callee(self, arg):
2341 return arg + 1
2343 def do_method_str_arg_out_ret(self, arg):
2344 return (arg.upper(), len(arg))
2346 def do_method_with_default_implementation(self, int8):
2347 GIMarshallingTests.Object.do_method_with_default_implementation(self, int8)
2348 self.props.int += int8
2350 def do_vfunc_return_value_only(self):
2351 return 4242
2353 def do_vfunc_one_out_parameter(self):
2354 return 42.42
2356 def do_vfunc_multiple_out_parameters(self):
2357 return (42.42, 3.14)
2359 def do_vfunc_return_value_and_one_out_parameter(self):
2360 return (5, 42)
2362 def do_vfunc_return_value_and_multiple_out_parameters(self):
2363 return (5, 42, 99)
2365 def do_vfunc_caller_allocated_out_parameter(self):
2366 return self.return_for_caller_allocated_out_parameter
2368 class SubObject(GIMarshallingTests.SubObject):
2369 def __init__(self, int):
2370 GIMarshallingTests.SubObject.__init__(self)
2371 self.val = None
2373 def do_method_with_default_implementation(self, int8):
2374 self.val = int8
2376 def do_vfunc_return_value_only(self):
2377 return 2121
2379 class Interface3Impl(GObject.Object, GIMarshallingTests.Interface3):
2380 def __init__(self):
2381 GObject.Object.__init__(self)
2382 self.variants = None
2383 self.n_variants = None
2385 def do_test_variant_array_in(self, variants, n_variants):
2386 self.variants = variants
2387 self.n_variants = n_variants
2389 class ErrorObject(GIMarshallingTests.Object):
2390 def do_vfunc_return_value_only(self):
2391 raise ValueError('Return value should be 0')
2393 def test_object(self):
2394 self.assertTrue(issubclass(self.Object, GIMarshallingTests.Object))
2396 object_ = self.Object(int=42)
2397 self.assertTrue(isinstance(object_, self.Object))
2399 @unittest.skipUnless(hasattr(GIMarshallingTests.Object, 'new_fail'),
2400 'Requires newer version of GI')
2401 def test_object_fail(self):
2402 with self.assertRaises(GLib.Error):
2403 GIMarshallingTests.Object.new_fail(int_=42)
2405 def test_object_method(self):
2406 self.Object(int=0).method()
2408 def test_object_vfuncs(self):
2409 object_ = self.Object(int=42)
2410 object_.method_int8_in(84)
2411 self.assertEqual(object_.val, 84)
2412 self.assertEqual(object_.method_int8_out(), 42)
2414 # can be dropped when bumping g-i dependencies to >= 1.35.2
2415 if hasattr(object_, 'method_int8_arg_and_out_caller'):
2416 self.assertEqual(object_.method_int8_arg_and_out_caller(42), 43)
2417 self.assertEqual(object_.method_int8_arg_and_out_callee(42), 43)
2418 self.assertEqual(object_.method_str_arg_out_ret('hello'), ('HELLO', 5))
2420 object_.method_with_default_implementation(42)
2421 self.assertEqual(object_.props.int, 84)
2423 self.assertEqual(object_.vfunc_return_value_only(), 4242)
2424 self.assertAlmostEqual(object_.vfunc_one_out_parameter(), 42.42, places=5)
2426 (a, b) = object_.vfunc_multiple_out_parameters()
2427 self.assertAlmostEqual(a, 42.42, places=5)
2428 self.assertAlmostEqual(b, 3.14, places=5)
2430 self.assertEqual(object_.vfunc_return_value_and_one_out_parameter(), (5, 42))
2431 self.assertEqual(object_.vfunc_return_value_and_multiple_out_parameters(), (5, 42, 99))
2433 self.assertEqual(object_.vfunc_caller_allocated_out_parameter(),
2434 object_.return_for_caller_allocated_out_parameter)
2436 class ObjectWithoutVFunc(GIMarshallingTests.Object):
2437 def __init__(self, int):
2438 GIMarshallingTests.Object.__init__(self)
2440 object_ = ObjectWithoutVFunc(int=42)
2441 object_.method_with_default_implementation(84)
2442 self.assertEqual(object_.props.int, 84)
2444 def test_vfunc_return_ref_count(self):
2445 obj = self.Object(int=42)
2446 ref_count = sys.getrefcount(obj.return_for_caller_allocated_out_parameter)
2447 ret = obj.vfunc_caller_allocated_out_parameter()
2448 gc.collect()
2450 # Make sure the return and what the vfunc returned
2451 # are equal but not the same object.
2452 self.assertEqual(ret, obj.return_for_caller_allocated_out_parameter)
2453 self.assertFalse(ret is obj.return_for_caller_allocated_out_parameter)
2454 self.assertEqual(sys.getrefcount(obj.return_for_caller_allocated_out_parameter),
2455 ref_count)
2457 def test_subobject_parent_vfunc(self):
2458 object_ = self.SubObject(int=81)
2459 object_.method_with_default_implementation(87)
2460 self.assertEqual(object_.val, 87)
2462 def test_subobject_child_vfunc(self):
2463 object_ = self.SubObject(int=1)
2464 self.assertEqual(object_.vfunc_return_value_only(), 2121)
2466 def test_subobject_non_vfunc_do_method(self):
2467 class PythonObjectWithNonVFuncDoMethod(object):
2468 def do_not_a_vfunc(self):
2469 return 5
2471 class ObjectOverrideNonVFuncDoMethod(GIMarshallingTests.Object, PythonObjectWithNonVFuncDoMethod):
2472 def do_not_a_vfunc(self):
2473 value = super(ObjectOverrideNonVFuncDoMethod, self).do_not_a_vfunc()
2474 return 13 + value
2476 object_ = ObjectOverrideNonVFuncDoMethod()
2477 self.assertEqual(18, object_.do_not_a_vfunc())
2479 def test_native_function_not_set_in_subclass_dict(self):
2480 # Previously, GI was setting virtual functions on the class as well
2481 # as any *native* class that subclasses it. Here we check that it is only
2482 # set on the class that the method is originally from.
2483 self.assertTrue('do_method_with_default_implementation' in GIMarshallingTests.Object.__dict__)
2484 self.assertTrue('do_method_with_default_implementation' not in GIMarshallingTests.SubObject.__dict__)
2486 def test_subobject_with_interface_and_non_vfunc_do_method(self):
2487 # There was a bug for searching for vfuncs in interfaces. It was
2488 # triggered by having a do_* method that wasn't overriding
2489 # a native vfunc, as well as inheriting from an interface.
2490 class GObjectSubclassWithInterface(GObject.GObject, GIMarshallingTests.Interface):
2491 def do_method_not_a_vfunc(self):
2492 pass
2494 def test_subsubobject(self):
2495 class SubSubSubObject(GIMarshallingTests.SubSubObject):
2496 def do_method_deep_hierarchy(self, num):
2497 self.props.int = num * 2
2499 sub_sub_sub_object = SubSubSubObject()
2500 GIMarshallingTests.SubSubObject.do_method_deep_hierarchy(sub_sub_sub_object, 5)
2501 self.assertEqual(sub_sub_sub_object.props.int, 5)
2503 def test_interface3impl(self):
2504 iface3 = self.Interface3Impl()
2505 variants = [GLib.Variant('i', 27), GLib.Variant('s', 'Hello')]
2506 iface3.test_variant_array_in(variants)
2507 self.assertEqual(iface3.n_variants, 2)
2508 self.assertEqual(iface3.variants[0].unpack(), 27)
2509 self.assertEqual(iface3.variants[1].unpack(), 'Hello')
2511 def test_python_subsubobject_vfunc(self):
2512 class PySubObject(GIMarshallingTests.Object):
2513 def __init__(self):
2514 GIMarshallingTests.Object.__init__(self)
2515 self.sub_method_int8_called = 0
2517 def do_method_int8_in(self, int8):
2518 self.sub_method_int8_called += 1
2520 class PySubSubObject(PySubObject):
2521 def __init__(self):
2522 PySubObject.__init__(self)
2523 self.subsub_method_int8_called = 0
2525 def do_method_int8_in(self, int8):
2526 self.subsub_method_int8_called += 1
2528 so = PySubObject()
2529 so.method_int8_in(1)
2530 self.assertEqual(so.sub_method_int8_called, 1)
2532 # it should call the method on the SubSub object only
2533 sso = PySubSubObject()
2534 sso.method_int8_in(1)
2535 self.assertEqual(sso.subsub_method_int8_called, 1)
2536 self.assertEqual(sso.sub_method_int8_called, 0)
2538 def test_callback_in_vfunc(self):
2539 class SubObject(GIMarshallingTests.Object):
2540 def __init__(self):
2541 GObject.GObject.__init__(self)
2542 self.worked = False
2544 def do_vfunc_with_callback(self, callback):
2545 self.worked = callback(42) == 42
2547 _object = SubObject()
2548 _object.call_vfunc_with_callback()
2549 self.assertTrue(_object.worked)
2550 _object.worked = False
2551 _object.call_vfunc_with_callback()
2552 self.assertTrue(_object.worked)
2554 def test_exception_in_vfunc_return_value(self):
2555 obj = self.ErrorObject()
2556 with capture_exceptions() as exc:
2557 self.assertEqual(obj.vfunc_return_value_only(), 0)
2558 self.assertEqual(len(exc), 1)
2559 self.assertEqual(exc[0].type, ValueError)
2561 @unittest.skipUnless(hasattr(GIMarshallingTests, 'callback_owned_boxed'),
2562 'requires newer version of GI')
2563 def test_callback_owned_box(self):
2564 def callback(box, data):
2565 self.box = box
2567 def nop_callback(box, data):
2568 pass
2570 GIMarshallingTests.callback_owned_boxed(callback, None)
2571 GIMarshallingTests.callback_owned_boxed(nop_callback, None)
2572 self.assertEqual(self.box.long_, 1)
2575 class TestMultiOutputArgs(unittest.TestCase):
2577 def test_int_out_out(self):
2578 self.assertEqual((6, 7), GIMarshallingTests.int_out_out())
2580 def test_int_return_out(self):
2581 self.assertEqual((6, 7), GIMarshallingTests.int_return_out())
2584 # Interface
2586 class TestInterfaces(unittest.TestCase):
2588 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2589 def __init__(self):
2590 GObject.GObject.__init__(self)
2591 self.val = None
2593 def do_test_int8_in(self, int8):
2594 self.val = int8
2596 def setUp(self):
2597 self.instance = self.TestInterfaceImpl()
2599 def test_wrapper(self):
2600 self.assertTrue(issubclass(GIMarshallingTests.Interface, GObject.GInterface))
2601 self.assertEqual(GIMarshallingTests.Interface.__gtype__.name, 'GIMarshallingTestsInterface')
2602 self.assertRaises(NotImplementedError, GIMarshallingTests.Interface)
2604 def test_implementation(self):
2605 self.assertTrue(issubclass(self.TestInterfaceImpl, GIMarshallingTests.Interface))
2606 self.assertTrue(isinstance(self.instance, GIMarshallingTests.Interface))
2608 def test_int8_int(self):
2609 GIMarshallingTests.test_interface_test_int8_in(self.instance, 42)
2610 self.assertEqual(self.instance.val, 42)
2612 def test_subclass(self):
2613 class TestInterfaceImplA(self.TestInterfaceImpl):
2614 pass
2616 class TestInterfaceImplB(TestInterfaceImplA):
2617 pass
2619 instance = TestInterfaceImplA()
2620 GIMarshallingTests.test_interface_test_int8_in(instance, 42)
2621 self.assertEqual(instance.val, 42)
2623 def test_subclass_override(self):
2624 class TestInterfaceImplD(TestInterfaces.TestInterfaceImpl):
2625 val2 = None
2627 def do_test_int8_in(self, int8):
2628 self.val2 = int8
2630 instance = TestInterfaceImplD()
2631 self.assertEqual(instance.val, None)
2632 self.assertEqual(instance.val2, None)
2634 GIMarshallingTests.test_interface_test_int8_in(instance, 42)
2635 self.assertEqual(instance.val, None)
2636 self.assertEqual(instance.val2, 42)
2638 def test_type_mismatch(self):
2639 obj = GIMarshallingTests.Object()
2641 # wrong type for first argument: interface
2642 enum = Gio.File.new_for_path('.').enumerate_children(
2643 '', Gio.FileQueryInfoFlags.NONE, None)
2644 try:
2645 enum.next_file(obj)
2646 self.fail('call with wrong type argument unexpectedly succeeded')
2647 except TypeError as e:
2648 # should have argument name
2649 self.assertTrue('cancellable' in str(e), e)
2650 # should have expected type
2651 self.assertTrue('xpected Gio.Cancellable' in str(e), e)
2652 # should have actual type
2653 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2655 # wrong type for self argument: interface
2656 try:
2657 Gio.FileEnumerator.next_file(obj, None)
2658 self.fail('call with wrong type argument unexpectedly succeeded')
2659 except TypeError as e:
2660 # should have argument name
2661 self.assertTrue('self' in str(e), e)
2662 # should have expected type
2663 self.assertTrue('xpected Gio.FileEnumerator' in str(e), e)
2664 # should have actual type
2665 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2667 # wrong type for first argument: GObject
2668 var = GLib.Variant('s', 'mystring')
2669 action = Gio.SimpleAction.new('foo', var.get_type())
2670 try:
2671 action.activate(obj)
2672 self.fail('call with wrong type argument unexpectedly succeeded')
2673 except TypeError as e:
2674 # should have argument name
2675 self.assertTrue('parameter' in str(e), e)
2676 # should have expected type
2677 self.assertTrue('xpected GLib.Variant' in str(e), e)
2678 # should have actual type
2679 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2681 # wrong type for self argument: GObject
2682 try:
2683 Gio.SimpleAction.activate(obj, obj)
2684 self.fail('call with wrong type argument unexpectedly succeeded')
2685 except TypeError as e:
2686 # should have argument name
2687 self.assertTrue('self' in str(e), e)
2688 # should have expected type
2689 self.assertTrue('xpected Gio.Action' in str(e), e)
2690 # should have actual type
2691 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2694 class TestMRO(unittest.TestCase):
2695 def test_mro(self):
2696 # check that our own MRO calculation matches what we would expect
2697 # from Python's own C3 calculations
2698 class A(object):
2699 pass
2701 class B(A):
2702 pass
2704 class C(A):
2705 pass
2707 class D(B, C):
2708 pass
2710 class E(D, GIMarshallingTests.Object):
2711 pass
2713 expected = (E, D, B, C, A, GIMarshallingTests.Object,
2714 GObject.Object, GObject.Object.__base__, gi._gi.GObject,
2715 object)
2716 self.assertEqual(expected, E.__mro__)
2718 def test_interface_collision(self):
2719 # there was a problem with Python bailing out because of
2720 # http://en.wikipedia.org/wiki/Diamond_problem with interfaces,
2721 # which shouldn't really be a problem.
2723 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2724 pass
2726 class TestInterfaceImpl2(GIMarshallingTests.Interface,
2727 TestInterfaceImpl):
2728 pass
2730 class TestInterfaceImpl3(TestInterfaceImpl,
2731 GIMarshallingTests.Interface2):
2732 pass
2734 def test_old_style_mixin(self):
2735 # Note: Old style classes don't exist in Python 3
2736 class Mixin:
2737 pass
2739 with warnings.catch_warnings(record=True) as warn:
2740 warnings.simplefilter('always')
2742 # Dynamically create a new gi based class with an old
2743 # style mixin.
2744 type('GIWithOldStyleMixin', (GIMarshallingTests.Object, Mixin), {})
2746 if sys.version_info < (3, 0):
2747 self.assertTrue(issubclass(warn[0].category, RuntimeWarning))
2748 else:
2749 self.assertEqual(len(warn), 0)
2752 class TestInterfaceClash(unittest.TestCase):
2754 def test_clash(self):
2755 def create_clash():
2756 class TestClash(GObject.GObject, GIMarshallingTests.Interface, GIMarshallingTests.Interface2):
2757 def do_test_int8_in(self, int8):
2758 pass
2759 TestClash()
2761 self.assertRaises(TypeError, create_clash)
2764 class TestOverrides(unittest.TestCase):
2766 def test_constant(self):
2767 self.assertEqual(GIMarshallingTests.OVERRIDES_CONSTANT, 7)
2769 def test_struct(self):
2770 # Test that the constructor has been overridden.
2771 struct = GIMarshallingTests.OverridesStruct(42)
2773 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2775 # Test that the method has been overridden.
2776 self.assertEqual(6, struct.method())
2778 del struct
2780 # Test that the overrides wrapper has been registered.
2781 struct = GIMarshallingTests.overrides_struct_returnv()
2783 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2785 del struct
2787 def test_object(self):
2788 # Test that the constructor has been overridden.
2789 object_ = GIMarshallingTests.OverridesObject(42)
2791 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2793 # Test that the alternate constructor has been overridden.
2794 object_ = GIMarshallingTests.OverridesObject.new(42)
2796 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2798 # Test that the method has been overridden.
2799 self.assertEqual(6, object_.method())
2801 # Test that the overrides wrapper has been registered.
2802 object_ = GIMarshallingTests.OverridesObject.returnv()
2804 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2806 def test_module_name(self):
2807 # overridden types
2808 self.assertEqual(GIMarshallingTests.OverridesStruct.__module__, 'gi.overrides.GIMarshallingTests')
2809 self.assertEqual(GIMarshallingTests.OverridesObject.__module__, 'gi.overrides.GIMarshallingTests')
2810 self.assertEqual(GObject.Object.__module__, 'gi.overrides.GObject')
2812 # not overridden
2813 self.assertEqual(GIMarshallingTests.SubObject.__module__, 'gi.repository.GIMarshallingTests')
2814 self.assertEqual(GObject.InitiallyUnowned.__module__, 'gi.repository.GObject')
2817 class TestDir(unittest.TestCase):
2818 def test_members_list(self):
2819 list = dir(GIMarshallingTests)
2820 self.assertTrue('OverridesStruct' in list)
2821 self.assertTrue('BoxedStruct' in list)
2822 self.assertTrue('OVERRIDES_CONSTANT' in list)
2823 self.assertTrue('GEnum' in list)
2824 self.assertTrue('int32_return_max' in list)
2826 def test_modules_list(self):
2827 import gi.repository
2828 list = dir(gi.repository)
2829 self.assertTrue('GIMarshallingTests' in list)
2831 # FIXME: test to see if a module which was not imported is in the list
2832 # we should be listing every typelib we find, not just the ones
2833 # which are imported
2835 # to test this I recommend we compile a fake module which
2836 # our tests would never import and check to see if it is
2837 # in the list:
2839 # self.assertTrue('DoNotImportDummyTests' in list)
2842 class TestParamSpec(unittest.TestCase):
2843 # https://bugzilla.gnome.org/show_bug.cgi?id=682355
2844 @unittest.expectedFailure
2845 def test_param_spec_in_bool(self):
2846 ps = GObject.param_spec_boolean('mybool', 'test-bool', 'boolblurb',
2847 True, GObject.ParamFlags.READABLE)
2848 GIMarshallingTests.param_spec_in_bool(ps)
2850 def test_param_spec_return(self):
2851 obj = GIMarshallingTests.param_spec_return()
2852 self.assertEqual(obj.name, 'test-param')
2853 self.assertEqual(obj.nick, 'test')
2854 self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2856 def test_param_spec_out(self):
2857 obj = GIMarshallingTests.param_spec_out()
2858 self.assertEqual(obj.name, 'test-param')
2859 self.assertEqual(obj.nick, 'test')
2860 self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2863 class TestKeywordArgs(unittest.TestCase):
2865 def test_calling(self):
2866 kw_func = GIMarshallingTests.int_three_in_three_out
2868 self.assertEqual(kw_func(1, 2, 3), (1, 2, 3))
2869 self.assertEqual(kw_func(**{'a': 4, 'b': 5, 'c': 6}), (4, 5, 6))
2870 self.assertEqual(kw_func(1, **{'b': 7, 'c': 8}), (1, 7, 8))
2871 self.assertEqual(kw_func(1, 7, **{'c': 8}), (1, 7, 8))
2872 self.assertEqual(kw_func(1, c=8, **{'b': 7}), (1, 7, 8))
2873 self.assertEqual(kw_func(2, c=4, b=3), (2, 3, 4))
2874 self.assertEqual(kw_func(a=2, c=4, b=3), (2, 3, 4))
2876 def assertRaisesMessage(self, exception, message, func, *args, **kwargs):
2877 try:
2878 func(*args, **kwargs)
2879 except exception:
2880 (e_type, e) = sys.exc_info()[:2]
2881 if message is not None:
2882 self.assertEqual(str(e), message)
2883 except:
2884 raise
2885 else:
2886 msg = "%s() did not raise %s" % (func.__name__, exception.__name__)
2887 raise AssertionError(msg)
2889 def test_type_errors(self):
2890 # test too few args
2891 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() takes exactly 3 arguments (0 given)",
2892 GIMarshallingTests.int_three_in_three_out)
2893 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() takes exactly 3 arguments (1 given)",
2894 GIMarshallingTests.int_three_in_three_out, 1)
2895 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() takes exactly 3 arguments (0 given)",
2896 GIMarshallingTests.int_three_in_three_out, *())
2897 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() takes exactly 3 arguments (0 given)",
2898 GIMarshallingTests.int_three_in_three_out, *(), **{})
2899 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() takes exactly 3 non-keyword arguments (0 given)",
2900 GIMarshallingTests.int_three_in_three_out, *(), **{'c': 4})
2902 # test too many args
2903 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() takes exactly 3 arguments (4 given)",
2904 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4))
2905 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() takes exactly 3 non-keyword arguments (4 given)",
2906 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4), c=6)
2908 # test too many keyword args
2909 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() got multiple values for keyword argument 'a'",
2910 GIMarshallingTests.int_three_in_three_out, 1, 2, 3, **{'a': 4, 'b': 5})
2911 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() got an unexpected keyword argument 'd'",
2912 GIMarshallingTests.int_three_in_three_out, d=4)
2913 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() got an unexpected keyword argument 'e'",
2914 GIMarshallingTests.int_three_in_three_out, **{'e': 2})
2916 def test_kwargs_are_not_modified(self):
2917 d = {'b': 2}
2918 d2 = d.copy()
2919 GIMarshallingTests.int_three_in_three_out(1, c=4, **d)
2920 self.assertEqual(d, d2)
2922 @unittest.skipUnless(hasattr(GIMarshallingTests, 'int_one_in_utf8_two_in_one_allows_none'),
2923 'Requires newer GIMarshallingTests')
2924 def test_allow_none_as_default(self):
2925 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2, '3', '4')
2926 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2, '3')
2927 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2)
2928 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2, d='4')
2930 GIMarshallingTests.array_in_utf8_two_in_out_of_order('1', [-1, 0, 1, 2])
2931 GIMarshallingTests.array_in_utf8_two_in_out_of_order('1', [-1, 0, 1, 2], '2')
2932 self.assertRaises(TypeError,
2933 GIMarshallingTests.array_in_utf8_two_in_out_of_order,
2934 [-1, 0, 1, 2], a='1')
2935 self.assertRaises(TypeError,
2936 GIMarshallingTests.array_in_utf8_two_in_out_of_order,
2937 [-1, 0, 1, 2])
2939 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2], '1', '2')
2940 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2], '1')
2941 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2])
2942 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2], b='2')
2944 GIMarshallingTests.int_one_in_utf8_two_in_one_allows_none(1, '2', '3')
2945 self.assertRaises(TypeError,
2946 GIMarshallingTests.int_one_in_utf8_two_in_one_allows_none,
2947 1, '3')
2948 self.assertRaises(TypeError,
2949 GIMarshallingTests.int_one_in_utf8_two_in_one_allows_none,
2950 1, c='3')
2953 class TestKeywords(unittest.TestCase):
2954 def test_method(self):
2955 # g_variant_print()
2956 v = GLib.Variant('i', 1)
2957 self.assertEqual(v.print_(False), '1')
2959 def test_function(self):
2960 # g_thread_yield()
2961 self.assertEqual(GLib.Thread.yield_(), None)
2963 def test_struct_method(self):
2964 # g_timer_continue()
2965 # we cannot currently instantiate GLib.Timer objects, so just ensure
2966 # the method exists
2967 self.assertTrue(callable(GLib.Timer.continue_))
2969 def test_uppercase(self):
2970 self.assertEqual(GLib.IOCondition.IN.value_nicks, ['in'])
2973 class TestModule(unittest.TestCase):
2974 def test_path(self):
2975 self.assertTrue(GIMarshallingTests.__path__.endswith('GIMarshallingTests-1.0.typelib'),
2976 GIMarshallingTests.__path__)
2978 def test_str(self):
2979 self.assertTrue("'GIMarshallingTests' from '" in str(GIMarshallingTests),
2980 str(GIMarshallingTests))
2982 def test_dir(self):
2983 _dir = dir(GIMarshallingTests)
2984 self.assertGreater(len(_dir), 10)
2986 self.assertTrue('SimpleStruct' in _dir)
2987 self.assertTrue('Interface2' in _dir)
2988 self.assertTrue('CONSTANT_GERROR_CODE' in _dir)
2989 self.assertTrue('array_zero_terminated_inout' in _dir)
2991 # assert that dir() does not contain garbage
2992 for item_name in _dir:
2993 item = getattr(GIMarshallingTests, item_name)
2994 self.assertTrue(hasattr(item, '__class__'))
2996 def test_help(self):
2997 orig_stdout = sys.stdout
2998 try:
2999 if sys.version_info < (3, 0):
3000 sys.stdout = BytesIO()
3001 else:
3002 sys.stdout = StringIO()
3003 help(GIMarshallingTests)
3004 output = sys.stdout.getvalue()
3005 finally:
3006 sys.stdout = orig_stdout
3008 self.assertTrue('SimpleStruct' in output, output)
3009 self.assertTrue('Interface2' in output, output)
3010 self.assertTrue('method_array_inout' in output, output)
3013 class TestProjectVersion(unittest.TestCase):
3014 def test_version_str(self):
3015 self.assertGreater(gi.__version__, "3.")
3017 def test_version_info(self):
3018 self.assertEqual(len(gi.version_info), 3)
3019 self.assertGreaterEqual(gi.version_info, (3, 3, 5))
3021 def test_check_version(self):
3022 self.assertRaises(ValueError, gi.check_version, (99, 0, 0))
3023 self.assertRaises(ValueError, gi.check_version, "99.0.0")
3024 gi.check_version((3, 3, 5))
3025 gi.check_version("3.3.5")
3028 class TestGIWarning(unittest.TestCase):
3030 def test_warning(self):
3031 ignored_by_default = (DeprecationWarning, PendingDeprecationWarning,
3032 ImportWarning)
3034 with warnings.catch_warnings(record=True) as warn:
3035 warnings.simplefilter('always')
3036 warnings.warn("test", PyGIWarning)
3037 self.assertTrue(issubclass(warn[0].category, Warning))
3038 # We don't want PyGIWarning get ignored by default
3039 self.assertFalse(issubclass(warn[0].category, ignored_by_default))
3042 class TestDeprecation(unittest.TestCase):
3043 def test_method(self):
3044 d = GLib.Date.new()
3045 with warnings.catch_warnings(record=True) as warn:
3046 warnings.simplefilter('always')
3047 d.set_time(1)
3048 self.assertTrue(issubclass(warn[0].category, DeprecationWarning))
3049 self.assertEqual(str(warn[0].message), "GLib.Date.set_time is deprecated")
3051 def test_function(self):
3052 with warnings.catch_warnings(record=True) as warn:
3053 warnings.simplefilter('always')
3054 GLib.strcasecmp("foo", "bar")
3055 self.assertTrue(issubclass(warn[0].category, DeprecationWarning))
3056 self.assertEqual(str(warn[0].message), "GLib.strcasecmp is deprecated")
3058 def test_deprecated_attribute_compat(self):
3059 # test if the deprecation descriptor behaves like an instance attribute
3061 # save the descriptor
3062 desc = type(GLib).__dict__["IO_STATUS_ERROR"]
3064 # the descriptor raises AttributeError for itself
3065 self.assertFalse(hasattr(type(GLib), "IO_STATUS_ERROR"))
3067 with warnings.catch_warnings():
3068 warnings.simplefilter('ignore', PyGIDeprecationWarning)
3069 self.assertTrue(hasattr(GLib, "IO_STATUS_ERROR"))
3071 try:
3072 # check if replacing works
3073 GLib.IO_STATUS_ERROR = "foo"
3074 self.assertEqual(GLib.IO_STATUS_ERROR, "foo")
3075 finally:
3076 # restore descriptor
3077 try:
3078 del GLib.IO_STATUS_ERROR
3079 except AttributeError:
3080 pass
3081 setattr(type(GLib), "IO_STATUS_ERROR", desc)
3083 try:
3084 # check if deleting works
3085 del GLib.IO_STATUS_ERROR
3086 self.assertFalse(hasattr(GLib, "IO_STATUS_ERROR"))
3087 finally:
3088 # restore descriptor
3089 try:
3090 del GLib.IO_STATUS_ERROR
3091 except AttributeError:
3092 pass
3093 setattr(type(GLib), "IO_STATUS_ERROR", desc)
3095 def test_deprecated_attribute_warning(self):
3096 with warnings.catch_warnings(record=True) as warn:
3097 warnings.simplefilter('always')
3098 self.assertEqual(GLib.IO_STATUS_ERROR, GLib.IOStatus.ERROR)
3099 GLib.IO_STATUS_ERROR
3100 GLib.IO_STATUS_ERROR
3101 self.assertEqual(len(warn), 3)
3102 self.assertTrue(
3103 issubclass(warn[0].category, PyGIDeprecationWarning))
3104 self.assertRegexpMatches(
3105 str(warn[0].message),
3106 ".*GLib.IO_STATUS_ERROR.*GLib.IOStatus.ERROR.*")
3108 def test_deprecated_attribute_warning_coverage(self):
3109 with warnings.catch_warnings(record=True) as warn:
3110 warnings.simplefilter('always')
3111 GObject.markup_escape_text
3112 GObject.PRIORITY_DEFAULT
3113 GObject.GError
3114 GObject.PARAM_CONSTRUCT
3115 GObject.SIGNAL_ACTION
3116 GObject.property
3117 GObject.IO_STATUS_ERROR
3118 GObject.G_MAXUINT64
3119 GLib.IO_STATUS_ERROR
3120 GLib.SPAWN_SEARCH_PATH
3121 GLib.OPTION_FLAG_HIDDEN
3122 GLib.IO_FLAG_IS_WRITEABLE
3123 GLib.IO_FLAG_NONBLOCK
3124 GLib.USER_DIRECTORY_DESKTOP
3125 GLib.OPTION_ERROR_BAD_VALUE
3126 GLib.glib_version
3127 GLib.pyglib_version
3128 self.assertEqual(len(warn), 17)
3130 def test_deprecated_init_no_keywords(self):
3131 def init(self, **kwargs):
3132 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
3134 fn = gi.overrides.deprecated_init(init, arg_names=('a', 'b', 'c'))
3135 with warnings.catch_warnings(record=True) as warn:
3136 warnings.simplefilter('always')
3137 fn(self, 1, 2, 3)
3138 self.assertEqual(len(warn), 1)
3139 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3140 self.assertRegexpMatches(str(warn[0].message),
3141 '.*keyword.*a, b, c.*')
3143 def test_deprecated_init_no_keywords_out_of_order(self):
3144 def init(self, **kwargs):
3145 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
3147 fn = gi.overrides.deprecated_init(init, arg_names=('b', 'a', 'c'))
3148 with warnings.catch_warnings(record=True) as warn:
3149 warnings.simplefilter('always')
3150 fn(self, 2, 1, 3)
3151 self.assertEqual(len(warn), 1)
3152 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3153 self.assertRegexpMatches(str(warn[0].message),
3154 '.*keyword.*b, a, c.*')
3156 def test_deprecated_init_ignored_keyword(self):
3157 def init(self, **kwargs):
3158 self.assertDictEqual(kwargs, {'a': 1, 'c': 3})
3160 fn = gi.overrides.deprecated_init(init,
3161 arg_names=('a', 'b', 'c'),
3162 ignore=('b',))
3163 with warnings.catch_warnings(record=True) as warn:
3164 warnings.simplefilter('always')
3165 fn(self, 1, 2, 3)
3166 self.assertEqual(len(warn), 1)
3167 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3168 self.assertRegexpMatches(str(warn[0].message),
3169 '.*keyword.*a, b, c.*')
3171 def test_deprecated_init_with_aliases(self):
3172 def init(self, **kwargs):
3173 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
3175 fn = gi.overrides.deprecated_init(init,
3176 arg_names=('a', 'b', 'c'),
3177 deprecated_aliases={'b': 'bb', 'c': 'cc'})
3178 with warnings.catch_warnings(record=True) as warn:
3179 warnings.simplefilter('always')
3181 fn(self, a=1, bb=2, cc=3)
3182 self.assertEqual(len(warn), 1)
3183 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3184 self.assertRegexpMatches(str(warn[0].message),
3185 '.*keyword.*"bb, cc".*deprecated.*"b, c" respectively')
3187 def test_deprecated_init_with_defaults(self):
3188 def init(self, **kwargs):
3189 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
3191 fn = gi.overrides.deprecated_init(init,
3192 arg_names=('a', 'b', 'c'),
3193 deprecated_defaults={'b': 2, 'c': 3})
3194 with warnings.catch_warnings(record=True) as warn:
3195 warnings.simplefilter('always')
3196 fn(self, a=1)
3197 self.assertEqual(len(warn), 1)
3198 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3199 self.assertRegexpMatches(str(warn[0].message),
3200 '.*relying on deprecated non-standard defaults.*'
3201 'explicitly use: b=2, c=3')