1 # -*- Mode: Python; py-indent-offset: 4 -*-
3 # vim: tabstop=4 shiftwidth=4 expandtab
16 from io import StringIO, BytesIO
20 from gi import PyGIDeprecationWarning
21 from gi.repository import GObject, GLib, Gio
23 from gi.repository import GIMarshallingTests
25 from compathelper import _bytes, _unicode
27 if sys.version_info < (3, 0):
28 CONSTANT_UTF8 = "const \xe2\x99\xa5 utf8"
29 PY2_UNICODE_UTF8 = unicode(CONSTANT_UTF8, 'UTF-8')
32 CONSTANT_UTF8 = "const ♥ utf8"
33 CHAR_255 = bytes([255])
40 def __init__(self, value):
44 return int(self.value)
47 return float(self.value)
50 class Sequence(object):
52 def __init__(self, sequence):
53 self.sequence = sequence
56 return len(self.sequence)
58 def __getitem__(self, key):
59 return self.sequence[key]
62 class TestConstant(unittest.TestCase):
64 # Blocked by https://bugzilla.gnome.org/show_bug.cgi?id=595773
65 # def test_constant_utf8(self):
66 # self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.CONSTANT_UTF8)
68 def test_constant_number(self):
69 self.assertEqual(CONSTANT_NUMBER, GIMarshallingTests.CONSTANT_NUMBER)
71 def test_min_max_int(self):
72 self.assertEqual(GLib.MAXINT32, 2 ** 31 - 1)
73 self.assertEqual(GLib.MININT32, -2 ** 31)
74 self.assertEqual(GLib.MAXUINT32, 2 ** 32 - 1)
76 self.assertEqual(GLib.MAXINT64, 2 ** 63 - 1)
77 self.assertEqual(GLib.MININT64, -2 ** 63)
78 self.assertEqual(GLib.MAXUINT64, 2 ** 64 - 1)
81 class TestBoolean(unittest.TestCase):
83 def test_boolean_return(self):
84 self.assertEqual(True, GIMarshallingTests.boolean_return_true())
85 self.assertEqual(False, GIMarshallingTests.boolean_return_false())
87 def test_boolean_in(self):
88 GIMarshallingTests.boolean_in_true(True)
89 GIMarshallingTests.boolean_in_false(False)
91 GIMarshallingTests.boolean_in_true(1)
92 GIMarshallingTests.boolean_in_false(0)
94 def test_boolean_out(self):
95 self.assertEqual(True, GIMarshallingTests.boolean_out_true())
96 self.assertEqual(False, GIMarshallingTests.boolean_out_false())
98 def test_boolean_inout(self):
99 self.assertEqual(False, GIMarshallingTests.boolean_inout_true_false(True))
100 self.assertEqual(True, GIMarshallingTests.boolean_inout_false_true(False))
103 class TestInt8(unittest.TestCase):
105 MAX = GObject.G_MAXINT8
106 MIN = GObject.G_MININT8
108 def test_int8_return(self):
109 self.assertEqual(self.MAX, GIMarshallingTests.int8_return_max())
110 self.assertEqual(self.MIN, GIMarshallingTests.int8_return_min())
112 def test_int8_in(self):
113 max = Number(self.MAX)
114 min = Number(self.MIN)
116 GIMarshallingTests.int8_in_max(max)
117 GIMarshallingTests.int8_in_min(min)
122 self.assertRaises(OverflowError, GIMarshallingTests.int8_in_max, max)
123 self.assertRaises(OverflowError, GIMarshallingTests.int8_in_min, min)
125 self.assertRaises(TypeError, GIMarshallingTests.int8_in_max, "self.MAX")
127 def test_int8_out(self):
128 self.assertEqual(self.MAX, GIMarshallingTests.int8_out_max())
129 self.assertEqual(self.MIN, GIMarshallingTests.int8_out_min())
131 def test_int8_inout(self):
132 self.assertEqual(self.MIN, GIMarshallingTests.int8_inout_max_min(Number(self.MAX)))
133 self.assertEqual(self.MAX, GIMarshallingTests.int8_inout_min_max(Number(self.MIN)))
136 class TestUInt8(unittest.TestCase):
138 MAX = GObject.G_MAXUINT8
140 def test_uint8_return(self):
141 self.assertEqual(self.MAX, GIMarshallingTests.uint8_return())
143 def test_uint8_in(self):
144 number = Number(self.MAX)
146 GIMarshallingTests.uint8_in(number)
147 GIMarshallingTests.uint8_in(CHAR_255)
150 self.assertRaises(OverflowError, GIMarshallingTests.uint8_in, number)
151 self.assertRaises(OverflowError, GIMarshallingTests.uint8_in, Number(-1))
153 self.assertRaises(TypeError, GIMarshallingTests.uint8_in, "self.MAX")
155 def test_uint8_out(self):
156 self.assertEqual(self.MAX, GIMarshallingTests.uint8_out())
158 def test_uint8_inout(self):
159 self.assertEqual(0, GIMarshallingTests.uint8_inout(Number(self.MAX)))
162 class TestInt16(unittest.TestCase):
164 MAX = GObject.G_MAXINT16
165 MIN = GObject.G_MININT16
167 def test_int16_return(self):
168 self.assertEqual(self.MAX, GIMarshallingTests.int16_return_max())
169 self.assertEqual(self.MIN, GIMarshallingTests.int16_return_min())
171 def test_int16_in(self):
172 max = Number(self.MAX)
173 min = Number(self.MIN)
175 GIMarshallingTests.int16_in_max(max)
176 GIMarshallingTests.int16_in_min(min)
181 self.assertRaises(OverflowError, GIMarshallingTests.int16_in_max, max)
182 self.assertRaises(OverflowError, GIMarshallingTests.int16_in_min, min)
184 self.assertRaises(TypeError, GIMarshallingTests.int16_in_max, "self.MAX")
186 def test_int16_out(self):
187 self.assertEqual(self.MAX, GIMarshallingTests.int16_out_max())
188 self.assertEqual(self.MIN, GIMarshallingTests.int16_out_min())
190 def test_int16_inout(self):
191 self.assertEqual(self.MIN, GIMarshallingTests.int16_inout_max_min(Number(self.MAX)))
192 self.assertEqual(self.MAX, GIMarshallingTests.int16_inout_min_max(Number(self.MIN)))
195 class TestUInt16(unittest.TestCase):
197 MAX = GObject.G_MAXUINT16
199 def test_uint16_return(self):
200 self.assertEqual(self.MAX, GIMarshallingTests.uint16_return())
202 def test_uint16_in(self):
203 number = Number(self.MAX)
205 GIMarshallingTests.uint16_in(number)
209 self.assertRaises(OverflowError, GIMarshallingTests.uint16_in, number)
210 self.assertRaises(OverflowError, GIMarshallingTests.uint16_in, Number(-1))
212 self.assertRaises(TypeError, GIMarshallingTests.uint16_in, "self.MAX")
214 def test_uint16_out(self):
215 self.assertEqual(self.MAX, GIMarshallingTests.uint16_out())
217 def test_uint16_inout(self):
218 self.assertEqual(0, GIMarshallingTests.uint16_inout(Number(self.MAX)))
221 class TestInt32(unittest.TestCase):
223 MAX = GObject.G_MAXINT32
224 MIN = GObject.G_MININT32
226 def test_int32_return(self):
227 self.assertEqual(self.MAX, GIMarshallingTests.int32_return_max())
228 self.assertEqual(self.MIN, GIMarshallingTests.int32_return_min())
230 def test_int32_in(self):
231 max = Number(self.MAX)
232 min = Number(self.MIN)
234 GIMarshallingTests.int32_in_max(max)
235 GIMarshallingTests.int32_in_min(min)
240 self.assertRaises(OverflowError, GIMarshallingTests.int32_in_max, max)
241 self.assertRaises(OverflowError, GIMarshallingTests.int32_in_min, min)
243 self.assertRaises(TypeError, GIMarshallingTests.int32_in_max, "self.MAX")
245 def test_int32_out(self):
246 self.assertEqual(self.MAX, GIMarshallingTests.int32_out_max())
247 self.assertEqual(self.MIN, GIMarshallingTests.int32_out_min())
249 def test_int32_inout(self):
250 self.assertEqual(self.MIN, GIMarshallingTests.int32_inout_max_min(Number(self.MAX)))
251 self.assertEqual(self.MAX, GIMarshallingTests.int32_inout_min_max(Number(self.MIN)))
254 class TestUInt32(unittest.TestCase):
256 MAX = GObject.G_MAXUINT32
258 def test_uint32_return(self):
259 self.assertEqual(self.MAX, GIMarshallingTests.uint32_return())
261 def test_uint32_in(self):
262 number = Number(self.MAX)
264 GIMarshallingTests.uint32_in(number)
268 self.assertRaises(OverflowError, GIMarshallingTests.uint32_in, number)
269 self.assertRaises(OverflowError, GIMarshallingTests.uint32_in, Number(-1))
271 self.assertRaises(TypeError, GIMarshallingTests.uint32_in, "self.MAX")
273 def test_uint32_out(self):
274 self.assertEqual(self.MAX, GIMarshallingTests.uint32_out())
276 def test_uint32_inout(self):
277 self.assertEqual(0, GIMarshallingTests.uint32_inout(Number(self.MAX)))
280 class TestInt64(unittest.TestCase):
285 def test_int64_return(self):
286 self.assertEqual(self.MAX, GIMarshallingTests.int64_return_max())
287 self.assertEqual(self.MIN, GIMarshallingTests.int64_return_min())
289 def test_int64_in(self):
290 max = Number(self.MAX)
291 min = Number(self.MIN)
293 GIMarshallingTests.int64_in_max(max)
294 GIMarshallingTests.int64_in_min(min)
299 self.assertRaises(OverflowError, GIMarshallingTests.int64_in_max, max)
300 self.assertRaises(OverflowError, GIMarshallingTests.int64_in_min, min)
302 self.assertRaises(TypeError, GIMarshallingTests.int64_in_max, "self.MAX")
304 def test_int64_out(self):
305 self.assertEqual(self.MAX, GIMarshallingTests.int64_out_max())
306 self.assertEqual(self.MIN, GIMarshallingTests.int64_out_min())
308 def test_int64_inout(self):
309 self.assertEqual(self.MIN, GIMarshallingTests.int64_inout_max_min(Number(self.MAX)))
310 self.assertEqual(self.MAX, GIMarshallingTests.int64_inout_min_max(Number(self.MIN)))
313 class TestUInt64(unittest.TestCase):
317 def test_uint64_return(self):
318 self.assertEqual(self.MAX, GIMarshallingTests.uint64_return())
320 def test_uint64_in(self):
321 number = Number(self.MAX)
323 GIMarshallingTests.uint64_in(number)
327 self.assertRaises(OverflowError, GIMarshallingTests.uint64_in, number)
328 self.assertRaises(OverflowError, GIMarshallingTests.uint64_in, Number(-1))
330 self.assertRaises(TypeError, GIMarshallingTests.uint64_in, "self.MAX")
332 def test_uint64_out(self):
333 self.assertEqual(self.MAX, GIMarshallingTests.uint64_out())
335 def test_uint64_inout(self):
336 self.assertEqual(0, GIMarshallingTests.uint64_inout(Number(self.MAX)))
339 class TestShort(unittest.TestCase):
341 MAX = GObject.G_MAXSHORT
342 MIN = GObject.G_MINSHORT
344 def test_short_return(self):
345 self.assertEqual(self.MAX, GIMarshallingTests.short_return_max())
346 self.assertEqual(self.MIN, GIMarshallingTests.short_return_min())
348 def test_short_in(self):
349 max = Number(self.MAX)
350 min = Number(self.MIN)
352 GIMarshallingTests.short_in_max(max)
353 GIMarshallingTests.short_in_min(min)
358 self.assertRaises(OverflowError, GIMarshallingTests.short_in_max, max)
359 self.assertRaises(OverflowError, GIMarshallingTests.short_in_min, min)
361 self.assertRaises(TypeError, GIMarshallingTests.short_in_max, "self.MAX")
363 def test_short_out(self):
364 self.assertEqual(self.MAX, GIMarshallingTests.short_out_max())
365 self.assertEqual(self.MIN, GIMarshallingTests.short_out_min())
367 def test_short_inout(self):
368 self.assertEqual(self.MIN, GIMarshallingTests.short_inout_max_min(Number(self.MAX)))
369 self.assertEqual(self.MAX, GIMarshallingTests.short_inout_min_max(Number(self.MIN)))
372 class TestUShort(unittest.TestCase):
374 MAX = GObject.G_MAXUSHORT
376 def test_ushort_return(self):
377 self.assertEqual(self.MAX, GIMarshallingTests.ushort_return())
379 def test_ushort_in(self):
380 number = Number(self.MAX)
382 GIMarshallingTests.ushort_in(number)
386 self.assertRaises(OverflowError, GIMarshallingTests.ushort_in, number)
387 self.assertRaises(OverflowError, GIMarshallingTests.ushort_in, Number(-1))
389 self.assertRaises(TypeError, GIMarshallingTests.ushort_in, "self.MAX")
391 def test_ushort_out(self):
392 self.assertEqual(self.MAX, GIMarshallingTests.ushort_out())
394 def test_ushort_inout(self):
395 self.assertEqual(0, GIMarshallingTests.ushort_inout(Number(self.MAX)))
398 class TestInt(unittest.TestCase):
400 MAX = GObject.G_MAXINT
401 MIN = GObject.G_MININT
403 def test_int_return(self):
404 self.assertEqual(self.MAX, GIMarshallingTests.int_return_max())
405 self.assertEqual(self.MIN, GIMarshallingTests.int_return_min())
407 def test_int_in(self):
408 max = Number(self.MAX)
409 min = Number(self.MIN)
411 GIMarshallingTests.int_in_max(max)
412 GIMarshallingTests.int_in_min(min)
417 self.assertRaises(OverflowError, GIMarshallingTests.int_in_max, max)
418 self.assertRaises(OverflowError, GIMarshallingTests.int_in_min, min)
420 self.assertRaises(TypeError, GIMarshallingTests.int_in_max, "self.MAX")
422 def test_int_out(self):
423 self.assertEqual(self.MAX, GIMarshallingTests.int_out_max())
424 self.assertEqual(self.MIN, GIMarshallingTests.int_out_min())
426 def test_int_inout(self):
427 self.assertEqual(self.MIN, GIMarshallingTests.int_inout_max_min(Number(self.MAX)))
428 self.assertEqual(self.MAX, GIMarshallingTests.int_inout_min_max(Number(self.MIN)))
429 self.assertRaises(TypeError, GIMarshallingTests.int_inout_min_max, Number(self.MIN), CONSTANT_NUMBER)
432 class TestUInt(unittest.TestCase):
434 MAX = GObject.G_MAXUINT
436 def test_uint_return(self):
437 self.assertEqual(self.MAX, GIMarshallingTests.uint_return())
439 def test_uint_in(self):
440 number = Number(self.MAX)
442 GIMarshallingTests.uint_in(number)
446 self.assertRaises(OverflowError, GIMarshallingTests.uint_in, number)
447 self.assertRaises(OverflowError, GIMarshallingTests.uint_in, Number(-1))
449 self.assertRaises(TypeError, GIMarshallingTests.uint_in, "self.MAX")
451 def test_uint_out(self):
452 self.assertEqual(self.MAX, GIMarshallingTests.uint_out())
454 def test_uint_inout(self):
455 self.assertEqual(0, GIMarshallingTests.uint_inout(Number(self.MAX)))
458 class TestLong(unittest.TestCase):
460 MAX = GObject.G_MAXLONG
461 MIN = GObject.G_MINLONG
463 def test_long_return(self):
464 self.assertEqual(self.MAX, GIMarshallingTests.long_return_max())
465 self.assertEqual(self.MIN, GIMarshallingTests.long_return_min())
467 def test_long_in(self):
468 max = Number(self.MAX)
469 min = Number(self.MIN)
471 GIMarshallingTests.long_in_max(max)
472 GIMarshallingTests.long_in_min(min)
477 self.assertRaises(OverflowError, GIMarshallingTests.long_in_max, max)
478 self.assertRaises(OverflowError, GIMarshallingTests.long_in_min, min)
480 self.assertRaises(TypeError, GIMarshallingTests.long_in_max, "self.MAX")
482 def test_long_out(self):
483 self.assertEqual(self.MAX, GIMarshallingTests.long_out_max())
484 self.assertEqual(self.MIN, GIMarshallingTests.long_out_min())
486 def test_long_inout(self):
487 self.assertEqual(self.MIN, GIMarshallingTests.long_inout_max_min(Number(self.MAX)))
488 self.assertEqual(self.MAX, GIMarshallingTests.long_inout_min_max(Number(self.MIN)))
491 class TestULong(unittest.TestCase):
493 MAX = GObject.G_MAXULONG
495 def test_ulong_return(self):
496 self.assertEqual(self.MAX, GIMarshallingTests.ulong_return())
498 def test_ulong_in(self):
499 number = Number(self.MAX)
501 GIMarshallingTests.ulong_in(number)
505 self.assertRaises(OverflowError, GIMarshallingTests.ulong_in, number)
506 self.assertRaises(OverflowError, GIMarshallingTests.ulong_in, Number(-1))
508 self.assertRaises(TypeError, GIMarshallingTests.ulong_in, "self.MAX")
510 def test_ulong_out(self):
511 self.assertEqual(self.MAX, GIMarshallingTests.ulong_out())
513 def test_ulong_inout(self):
514 self.assertEqual(0, GIMarshallingTests.ulong_inout(Number(self.MAX)))
517 class TestSSize(unittest.TestCase):
519 MAX = GObject.G_MAXLONG
520 MIN = GObject.G_MINLONG
522 def test_ssize_return(self):
523 self.assertEqual(self.MAX, GIMarshallingTests.ssize_return_max())
524 self.assertEqual(self.MIN, GIMarshallingTests.ssize_return_min())
526 def test_ssize_in(self):
527 max = Number(self.MAX)
528 min = Number(self.MIN)
530 GIMarshallingTests.ssize_in_max(max)
531 GIMarshallingTests.ssize_in_min(min)
536 self.assertRaises(OverflowError, GIMarshallingTests.ssize_in_max, max)
537 self.assertRaises(OverflowError, GIMarshallingTests.ssize_in_min, min)
539 self.assertRaises(TypeError, GIMarshallingTests.ssize_in_max, "self.MAX")
541 def test_ssize_out(self):
542 self.assertEqual(self.MAX, GIMarshallingTests.ssize_out_max())
543 self.assertEqual(self.MIN, GIMarshallingTests.ssize_out_min())
545 def test_ssize_inout(self):
546 self.assertEqual(self.MIN, GIMarshallingTests.ssize_inout_max_min(Number(self.MAX)))
547 self.assertEqual(self.MAX, GIMarshallingTests.ssize_inout_min_max(Number(self.MIN)))
550 class TestSize(unittest.TestCase):
552 MAX = GObject.G_MAXULONG
554 def test_size_return(self):
555 self.assertEqual(self.MAX, GIMarshallingTests.size_return())
557 def test_size_in(self):
558 number = Number(self.MAX)
560 GIMarshallingTests.size_in(number)
564 self.assertRaises(OverflowError, GIMarshallingTests.size_in, number)
565 self.assertRaises(OverflowError, GIMarshallingTests.size_in, Number(-1))
567 self.assertRaises(TypeError, GIMarshallingTests.size_in, "self.MAX")
569 def test_size_out(self):
570 self.assertEqual(self.MAX, GIMarshallingTests.size_out())
572 def test_size_inout(self):
573 self.assertEqual(0, GIMarshallingTests.size_inout(Number(self.MAX)))
576 class TestTimet(unittest.TestCase):
578 def test_time_t_return(self):
579 self.assertEqual(1234567890, GIMarshallingTests.time_t_return())
581 def test_time_t_in(self):
582 GIMarshallingTests.time_t_in(1234567890)
583 self.assertRaises(TypeError, GIMarshallingTests.time_t_in, "hello")
585 def test_time_t_out(self):
586 self.assertEqual(1234567890, GIMarshallingTests.time_t_out())
588 def test_time_t_inout(self):
589 self.assertEqual(0, GIMarshallingTests.time_t_inout(1234567890))
592 class TestFloat(unittest.TestCase):
594 MAX = GObject.G_MAXFLOAT
595 MIN = GObject.G_MINFLOAT
597 def test_float_return(self):
598 self.assertAlmostEqual(self.MAX, GIMarshallingTests.float_return())
600 def test_float_in(self):
601 GIMarshallingTests.float_in(Number(self.MAX))
603 self.assertRaises(TypeError, GIMarshallingTests.float_in, "self.MAX")
605 def test_float_out(self):
606 self.assertAlmostEqual(self.MAX, GIMarshallingTests.float_out())
608 def test_float_inout(self):
609 self.assertAlmostEqual(self.MIN, GIMarshallingTests.float_inout(Number(self.MAX)))
612 class TestDouble(unittest.TestCase):
614 MAX = GObject.G_MAXDOUBLE
615 MIN = GObject.G_MINDOUBLE
617 def test_double_return(self):
618 self.assertAlmostEqual(self.MAX, GIMarshallingTests.double_return())
620 def test_double_in(self):
621 GIMarshallingTests.double_in(Number(self.MAX))
623 self.assertRaises(TypeError, GIMarshallingTests.double_in, "self.MAX")
625 def test_double_out(self):
626 self.assertAlmostEqual(self.MAX, GIMarshallingTests.double_out())
628 def test_double_inout(self):
629 self.assertAlmostEqual(self.MIN, GIMarshallingTests.double_inout(Number(self.MAX)))
632 class TestGType(unittest.TestCase):
634 def test_gtype_name(self):
635 self.assertEqual("void", GObject.TYPE_NONE.name)
636 self.assertEqual("gchararray", GObject.TYPE_STRING.name)
638 def check_readonly(gtype):
641 self.assertRaises(AttributeError, check_readonly, GObject.TYPE_NONE)
642 self.assertRaises(AttributeError, check_readonly, GObject.TYPE_STRING)
644 def test_gtype_return(self):
645 self.assertEqual(GObject.TYPE_NONE, GIMarshallingTests.gtype_return())
646 self.assertEqual(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_return())
648 def test_gtype_in(self):
649 GIMarshallingTests.gtype_in(GObject.TYPE_NONE)
650 GIMarshallingTests.gtype_string_in(GObject.TYPE_STRING)
651 self.assertRaises(TypeError, GIMarshallingTests.gtype_in, "foo")
652 self.assertRaises(TypeError, GIMarshallingTests.gtype_string_in, "foo")
654 def test_gtype_out(self):
655 self.assertEqual(GObject.TYPE_NONE, GIMarshallingTests.gtype_out())
656 self.assertEqual(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_out())
658 def test_gtype_inout(self):
659 self.assertEqual(GObject.TYPE_INT, GIMarshallingTests.gtype_inout(GObject.TYPE_NONE))
662 class TestUtf8(unittest.TestCase):
664 def test_utf8_none_return(self):
665 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_none_return())
667 def test_utf8_full_return(self):
668 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_full_return())
670 def test_utf8_none_in(self):
671 GIMarshallingTests.utf8_none_in(CONSTANT_UTF8)
672 if sys.version_info < (3, 0):
673 GIMarshallingTests.utf8_none_in(PY2_UNICODE_UTF8)
675 self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, CONSTANT_NUMBER)
676 self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, None)
678 def test_utf8_none_out(self):
679 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_none_out())
681 def test_utf8_full_out(self):
682 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_full_out())
684 def test_utf8_dangling_out(self):
685 GIMarshallingTests.utf8_dangling_out()
687 def test_utf8_none_inout(self):
688 self.assertEqual("", GIMarshallingTests.utf8_none_inout(CONSTANT_UTF8))
690 def test_utf8_full_inout(self):
691 self.assertEqual("", GIMarshallingTests.utf8_full_inout(CONSTANT_UTF8))
694 class TestFilename(unittest.TestCase):
696 self.workdir = tempfile.mkdtemp()
699 shutil.rmtree(self.workdir)
701 def test_filename_in(self):
702 fname = os.path.join(self.workdir, _unicode('testäø.txt'))
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_out(self):
713 self.assertRaises(GLib.GError, GLib.Dir.make_tmp, 'test')
715 dirname = GLib.Dir.make_tmp('testäø.XXXXXX')
716 self.assertTrue('/testäø.' in dirname, dirname)
717 dirname = _bytes(dirname)
718 self.assertTrue(os.path.isdir(dirname))
721 def test_filename_type_error(self):
722 self.assertRaises(TypeError, GLib.file_get_contents, 23)
725 class TestArray(unittest.TestCase):
727 def test_array_fixed_int_return(self):
728 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_int_return())
730 def test_array_fixed_short_return(self):
731 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_short_return())
733 def test_array_fixed_int_in(self):
734 GIMarshallingTests.array_fixed_int_in(Sequence([-1, 0, 1, 2]))
736 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, Sequence([-1, '0', 1, 2]))
738 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, 42)
739 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, None)
741 def test_array_fixed_short_in(self):
742 GIMarshallingTests.array_fixed_short_in(Sequence([-1, 0, 1, 2]))
744 def test_array_fixed_out(self):
745 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_out())
747 def test_array_fixed_inout(self):
748 self.assertEqual([2, 1, 0, -1], GIMarshallingTests.array_fixed_inout([-1, 0, 1, 2]))
750 def test_array_return(self):
751 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_return())
753 def test_array_return_etc(self):
754 self.assertEqual(([5, 0, 1, 9], 14), GIMarshallingTests.array_return_etc(5, 9))
756 def test_array_in(self):
757 GIMarshallingTests.array_in(Sequence([-1, 0, 1, 2]))
758 GIMarshallingTests.array_in_guint64_len(Sequence([-1, 0, 1, 2]))
759 GIMarshallingTests.array_in_guint8_len(Sequence([-1, 0, 1, 2]))
761 def test_array_in_len_before(self):
762 GIMarshallingTests.array_in_len_before(Sequence([-1, 0, 1, 2]))
764 def test_array_in_len_zero_terminated(self):
765 GIMarshallingTests.array_in_len_zero_terminated(Sequence([-1, 0, 1, 2]))
767 def test_array_uint8_in(self):
768 GIMarshallingTests.array_uint8_in(Sequence([97, 98, 99, 100]))
769 GIMarshallingTests.array_uint8_in(_bytes("abcd"))
771 def test_array_string_in(self):
772 GIMarshallingTests.array_string_in(['foo', 'bar'])
774 def test_array_out(self):
775 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_out())
777 def test_array_out_etc(self):
778 self.assertEqual(([-5, 0, 1, 9], 4), GIMarshallingTests.array_out_etc(-5, 9))
780 def test_array_inout(self):
781 self.assertEqual([-2, -1, 0, 1, 2], GIMarshallingTests.array_inout(Sequence([-1, 0, 1, 2])))
783 def test_array_inout_etc(self):
784 self.assertEqual(([-5, -1, 0, 1, 9], 4),
785 GIMarshallingTests.array_inout_etc(-5, Sequence([-1, 0, 1, 2]), 9))
787 def test_method_array_in(self):
788 object_ = GIMarshallingTests.Object()
789 object_.method_array_in(Sequence([-1, 0, 1, 2]))
791 def test_method_array_out(self):
792 object_ = GIMarshallingTests.Object()
793 self.assertEqual([-1, 0, 1, 2], object_.method_array_out())
795 def test_method_array_inout(self):
796 object_ = GIMarshallingTests.Object()
797 self.assertEqual([-2, -1, 0, 1, 2], object_.method_array_inout(Sequence([-1, 0, 1, 2])))
799 def test_method_array_return(self):
800 object_ = GIMarshallingTests.Object()
801 self.assertEqual([-1, 0, 1, 2], object_.method_array_return())
803 def test_array_enum_in(self):
804 GIMarshallingTests.array_enum_in([GIMarshallingTests.Enum.VALUE1,
805 GIMarshallingTests.Enum.VALUE2,
806 GIMarshallingTests.Enum.VALUE3])
808 def test_array_boxed_struct_in(self):
809 struct1 = GIMarshallingTests.BoxedStruct()
811 struct2 = GIMarshallingTests.BoxedStruct()
813 struct3 = GIMarshallingTests.BoxedStruct()
816 GIMarshallingTests.array_struct_in([struct1, struct2, struct3])
818 def test_array_boxed_struct_in_item_marshal_failure(self):
819 struct1 = GIMarshallingTests.BoxedStruct()
821 struct2 = GIMarshallingTests.BoxedStruct()
824 self.assertRaises(TypeError, GIMarshallingTests.array_struct_in,
825 [struct1, struct2, 'not_a_struct'])
827 def test_array_boxed_struct_value_in(self):
828 struct1 = GIMarshallingTests.BoxedStruct()
830 struct2 = GIMarshallingTests.BoxedStruct()
832 struct3 = GIMarshallingTests.BoxedStruct()
835 GIMarshallingTests.array_struct_value_in([struct1, struct2, struct3])
837 def test_array_boxed_struct_value_in_item_marshal_failure(self):
838 struct1 = GIMarshallingTests.BoxedStruct()
840 struct2 = GIMarshallingTests.BoxedStruct()
843 self.assertRaises(TypeError, GIMarshallingTests.array_struct_value_in,
844 [struct1, struct2, 'not_a_struct'])
846 def test_array_boxed_struct_take_in(self):
847 struct1 = GIMarshallingTests.BoxedStruct()
849 struct2 = GIMarshallingTests.BoxedStruct()
851 struct3 = GIMarshallingTests.BoxedStruct()
854 GIMarshallingTests.array_struct_take_in([struct1, struct2, struct3])
856 self.assertEqual(1, struct1.long_)
858 def test_array_boxed_struct_return(self):
859 (struct1, struct2, struct3) = GIMarshallingTests.array_zero_terminated_return_struct()
860 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct1))
861 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct2))
862 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct3))
863 self.assertEqual(42, struct1.long_)
864 self.assertEqual(43, struct2.long_)
865 self.assertEqual(44, struct3.long_)
867 def test_array_simple_struct_in(self):
868 struct1 = GIMarshallingTests.SimpleStruct()
870 struct2 = GIMarshallingTests.SimpleStruct()
872 struct3 = GIMarshallingTests.SimpleStruct()
875 GIMarshallingTests.array_simple_struct_in([struct1, struct2, struct3])
877 def test_array_simple_struct_in_item_marshal_failure(self):
878 struct1 = GIMarshallingTests.SimpleStruct()
880 struct2 = GIMarshallingTests.SimpleStruct()
883 self.assertRaises(TypeError, GIMarshallingTests.array_simple_struct_in,
884 [struct1, struct2, 'not_a_struct'])
886 def test_array_multi_array_key_value_in(self):
887 GIMarshallingTests.multi_array_key_value_in(["one", "two", "three"],
890 def test_array_in_nonzero_nonlen(self):
891 GIMarshallingTests.array_in_nonzero_nonlen(1, b'abcd')
893 def test_array_fixed_out_struct(self):
894 struct1, struct2 = GIMarshallingTests.array_fixed_out_struct()
896 self.assertEqual(7, struct1.long_)
897 self.assertEqual(6, struct1.int8)
898 self.assertEqual(6, struct2.long_)
899 self.assertEqual(7, struct2.int8)
901 def test_array_zero_terminated_return(self):
902 self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_return())
904 def test_array_zero_terminated_return_null(self):
905 self.assertEqual([], GIMarshallingTests.array_zero_terminated_return_null())
907 def test_array_zero_terminated_in(self):
908 GIMarshallingTests.array_zero_terminated_in(Sequence(['0', '1', '2']))
910 def test_array_zero_terminated_out(self):
911 self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
913 def test_array_zero_terminated_inout(self):
914 self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.array_zero_terminated_inout(['0', '1', '2']))
916 def test_init_function(self):
917 self.assertEqual((True, []), GIMarshallingTests.init_function([]))
918 self.assertEqual((True, []), GIMarshallingTests.init_function(['hello']))
919 self.assertEqual((True, ['hello']),
920 GIMarshallingTests.init_function(['hello', 'world']))
923 class TestGStrv(unittest.TestCase):
925 def test_gstrv_return(self):
926 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_return())
928 def test_gstrv_in(self):
929 GIMarshallingTests.gstrv_in(Sequence(['0', '1', '2']))
931 def test_gstrv_out(self):
932 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_out())
934 def test_gstrv_inout(self):
935 self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.gstrv_inout(['0', '1', '2']))
938 class TestArrayGVariant(unittest.TestCase):
940 def test_array_gvariant_none_in(self):
941 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
942 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_none_in(v)]
943 self.assertEqual([27, "Hello"], returned)
945 def test_array_gvariant_container_in(self):
946 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
947 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_container_in(v)]
948 self.assertEqual([27, "Hello"], returned)
950 def test_array_gvariant_full_in(self):
951 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
952 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_full_in(v)]
953 self.assertEqual([27, "Hello"], returned)
955 def test_bytearray_gvariant(self):
956 v = GLib.Variant.new_bytestring(b"foo")
957 self.assertEqual(v.get_bytestring(), b"foo")
960 class TestGArray(unittest.TestCase):
962 def test_garray_int_none_return(self):
963 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.garray_int_none_return())
965 def test_garray_uint64_none_return(self):
966 self.assertEqual([0, GObject.G_MAXUINT64], GIMarshallingTests.garray_uint64_none_return())
968 def test_garray_utf8_none_return(self):
969 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_return())
971 def test_garray_utf8_container_return(self):
972 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_return())
974 def test_garray_utf8_full_return(self):
975 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_return())
977 def test_garray_int_none_in(self):
978 GIMarshallingTests.garray_int_none_in(Sequence([-1, 0, 1, 2]))
980 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, Sequence([-1, '0', 1, 2]))
982 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, 42)
983 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, None)
985 def test_garray_uint64_none_in(self):
986 GIMarshallingTests.garray_uint64_none_in(Sequence([0, GObject.G_MAXUINT64]))
988 def test_garray_utf8_none_in(self):
989 GIMarshallingTests.garray_utf8_none_in(Sequence(['0', '1', '2']))
991 def test_garray_utf8_none_out(self):
992 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_out())
994 def test_garray_utf8_container_out(self):
995 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_out())
997 def test_garray_utf8_full_out(self):
998 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out())
1000 def test_garray_utf8_full_out_caller_allocated(self):
1001 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out_caller_allocated())
1003 def test_garray_utf8_none_inout(self):
1004 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_none_inout(Sequence(('0', '1', '2'))))
1006 def test_garray_utf8_container_inout(self):
1007 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_container_inout(['0', '1', '2']))
1009 def test_garray_utf8_full_inout(self):
1010 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_full_inout(['0', '1', '2']))
1013 class TestGPtrArray(unittest.TestCase):
1015 def test_gptrarray_utf8_none_return(self):
1016 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_return())
1018 def test_gptrarray_utf8_container_return(self):
1019 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_return())
1021 def test_gptrarray_utf8_full_return(self):
1022 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_return())
1024 def test_gptrarray_utf8_none_in(self):
1025 GIMarshallingTests.gptrarray_utf8_none_in(Sequence(['0', '1', '2']))
1027 def test_gptrarray_utf8_none_out(self):
1028 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_out())
1030 def test_gptrarray_utf8_container_out(self):
1031 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_out())
1033 def test_gptrarray_utf8_full_out(self):
1034 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_out())
1036 def test_gptrarray_utf8_none_inout(self):
1037 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_none_inout(Sequence(('0', '1', '2'))))
1039 def test_gptrarray_utf8_container_inout(self):
1040 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_container_inout(['0', '1', '2']))
1042 def test_gptrarray_utf8_full_inout(self):
1043 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_full_inout(['0', '1', '2']))
1046 class TestGBytes(unittest.TestCase):
1047 def test_gbytes_create(self):
1048 b = GLib.Bytes.new(b'\x00\x01\xFF')
1049 self.assertEqual(3, b.get_size())
1050 self.assertEqual(b'\x00\x01\xFF', b.get_data())
1052 def test_gbytes_create_take(self):
1053 b = GLib.Bytes.new_take(b'\x00\x01\xFF')
1054 self.assertEqual(3, b.get_size())
1055 self.assertEqual(b'\x00\x01\xFF', b.get_data())
1057 def test_gbytes_full_return(self):
1058 b = GIMarshallingTests.gbytes_full_return()
1059 self.assertEqual(4, b.get_size())
1060 self.assertEqual(b'\x00\x31\xFF\x33', b.get_data())
1062 def test_gbytes_none_in(self):
1063 b = GIMarshallingTests.gbytes_full_return()
1064 GIMarshallingTests.gbytes_none_in(b)
1066 def test_compare(self):
1067 a1 = GLib.Bytes.new(b'\x00\x01\xFF')
1068 a2 = GLib.Bytes.new(b'\x00\x01\xFF')
1069 b = GLib.Bytes.new(b'\x00\x01\xFE')
1071 self.assertTrue(a1.equal(a2))
1072 self.assertTrue(a2.equal(a1))
1073 self.assertFalse(a1.equal(b))
1074 self.assertFalse(b.equal(a2))
1076 self.assertEqual(0, a1.compare(a2))
1077 self.assertLess(0, a1.compare(b))
1078 self.assertGreater(0, b.compare(a1))
1081 class TestGByteArray(unittest.TestCase):
1083 ba = GLib.ByteArray.new()
1084 self.assertEqual(b'', ba)
1086 ba = GLib.ByteArray.new_take(b'\x01\x02\xFF')
1087 self.assertEqual(b'\x01\x02\xFF', ba)
1089 def test_bytearray_full_return(self):
1090 self.assertEqual(b'\x001\xFF3', GIMarshallingTests.bytearray_full_return())
1092 def test_bytearray_none_in(self):
1093 GIMarshallingTests.bytearray_none_in(b'\x00\x31\xFF\x33')
1096 class TestGList(unittest.TestCase):
1098 def test_glist_int_none_return(self):
1099 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.glist_int_none_return())
1101 def test_glist_uint32_none_return(self):
1102 self.assertEqual([0, GObject.G_MAXUINT32], GIMarshallingTests.glist_uint32_none_return())
1104 def test_glist_utf8_none_return(self):
1105 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_return())
1107 def test_glist_utf8_container_return(self):
1108 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_return())
1110 def test_glist_utf8_full_return(self):
1111 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_return())
1113 def test_glist_int_none_in(self):
1114 GIMarshallingTests.glist_int_none_in(Sequence((-1, 0, 1, 2)))
1116 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, Sequence((-1, '0', 1, 2)))
1118 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, 42)
1119 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, None)
1121 def test_glist_uint32_none_in(self):
1122 GIMarshallingTests.glist_uint32_none_in(Sequence((0, GObject.G_MAXUINT32)))
1124 def test_glist_utf8_none_in(self):
1125 GIMarshallingTests.glist_utf8_none_in(Sequence(('0', '1', '2')))
1127 def test_glist_utf8_none_out(self):
1128 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_out())
1130 def test_glist_utf8_container_out(self):
1131 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_out())
1133 def test_glist_utf8_full_out(self):
1134 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_out())
1136 def test_glist_utf8_none_inout(self):
1137 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_none_inout(Sequence(('0', '1', '2'))))
1139 def test_glist_utf8_container_inout(self):
1140 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_container_inout(('0', '1', '2')))
1142 def test_glist_utf8_full_inout(self):
1143 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_full_inout(('0', '1', '2')))
1146 class TestGSList(unittest.TestCase):
1148 def test_gslist_int_none_return(self):
1149 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.gslist_int_none_return())
1151 def test_gslist_utf8_none_return(self):
1152 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_return())
1154 def test_gslist_utf8_container_return(self):
1155 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_return())
1157 def test_gslist_utf8_full_return(self):
1158 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_return())
1160 def test_gslist_int_none_in(self):
1161 GIMarshallingTests.gslist_int_none_in(Sequence((-1, 0, 1, 2)))
1163 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, Sequence((-1, '0', 1, 2)))
1165 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, 42)
1166 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, None)
1168 def test_gslist_utf8_none_in(self):
1169 GIMarshallingTests.gslist_utf8_none_in(Sequence(('0', '1', '2')))
1171 def test_gslist_utf8_none_out(self):
1172 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_out())
1174 def test_gslist_utf8_container_out(self):
1175 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_out())
1177 def test_gslist_utf8_full_out(self):
1178 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_out())
1180 def test_gslist_utf8_none_inout(self):
1181 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_none_inout(Sequence(('0', '1', '2'))))
1183 def test_gslist_utf8_container_inout(self):
1184 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_container_inout(('0', '1', '2')))
1186 def test_gslist_utf8_full_inout(self):
1187 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_full_inout(('0', '1', '2')))
1190 class TestGHashTable(unittest.TestCase):
1192 def test_ghashtable_int_none_return(self):
1193 self.assertEqual({-1: 1, 0: 0, 1: -1, 2: -2}, GIMarshallingTests.ghashtable_int_none_return())
1195 def test_ghashtable_int_none_return2(self):
1196 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_return())
1198 def test_ghashtable_int_container_return(self):
1199 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_return())
1201 def test_ghashtable_int_full_return(self):
1202 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_return())
1204 def test_ghashtable_int_none_in(self):
1205 GIMarshallingTests.ghashtable_int_none_in({-1: 1, 0: 0, 1: -1, 2: -2})
1207 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, '0': 0, 1: -1, 2: -2})
1208 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, 0: '0', 1: -1, 2: -2})
1210 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, '{-1: 1, 0: 0, 1: -1, 2: -2}')
1211 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, None)
1213 def test_ghashtable_utf8_none_in(self):
1214 GIMarshallingTests.ghashtable_utf8_none_in({'-1': '1', '0': '0', '1': '-1', '2': '-2'})
1216 def test_ghashtable_utf8_none_out(self):
1217 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_out())
1219 def test_ghashtable_utf8_container_out(self):
1220 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_out())
1222 def test_ghashtable_utf8_full_out(self):
1223 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_out())
1225 def test_ghashtable_utf8_none_inout(self):
1226 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1227 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1228 GIMarshallingTests.ghashtable_utf8_none_inout(i))
1230 def test_ghashtable_utf8_container_inout(self):
1231 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1232 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1233 GIMarshallingTests.ghashtable_utf8_container_inout(i))
1235 def test_ghashtable_utf8_full_inout(self):
1236 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1237 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1238 GIMarshallingTests.ghashtable_utf8_full_inout(i))
1241 class TestGValue(unittest.TestCase):
1243 def test_gvalue_return(self):
1244 self.assertEqual(42, GIMarshallingTests.gvalue_return())
1246 def test_gvalue_in(self):
1247 GIMarshallingTests.gvalue_in(42)
1248 value = GObject.Value(GObject.TYPE_INT, 42)
1249 GIMarshallingTests.gvalue_in(value)
1251 def test_gvalue_in_with_modification(self):
1252 value = GObject.Value(GObject.TYPE_INT, 42)
1253 GIMarshallingTests.gvalue_in_with_modification(value)
1254 self.assertEqual(value.get_int(), 24)
1256 def test_gvalue_int64_in(self):
1257 value = GObject.Value(GObject.TYPE_INT64, GObject.G_MAXINT64)
1258 GIMarshallingTests.gvalue_int64_in(value)
1260 def test_gvalue_in_with_type(self):
1261 value = GObject.Value(GObject.TYPE_STRING, 'foo')
1262 GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_STRING)
1264 value = GObject.Value(GIMarshallingTests.Flags.__gtype__,
1265 GIMarshallingTests.Flags.VALUE1)
1266 GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_FLAGS)
1268 def test_gvalue_in_enum(self):
1269 value = GObject.Value(GIMarshallingTests.Enum.__gtype__,
1270 GIMarshallingTests.Enum.VALUE3)
1271 GIMarshallingTests.gvalue_in_enum(value)
1273 def test_gvalue_out(self):
1274 self.assertEqual(42, GIMarshallingTests.gvalue_out())
1276 def test_gvalue_int64_out(self):
1277 self.assertEqual(GObject.G_MAXINT64, GIMarshallingTests.gvalue_int64_out())
1279 def test_gvalue_out_caller_allocates(self):
1280 self.assertEqual(42, GIMarshallingTests.gvalue_out_caller_allocates())
1282 def test_gvalue_inout(self):
1283 self.assertEqual('42', GIMarshallingTests.gvalue_inout(42))
1284 value = GObject.Value(int, 42)
1285 self.assertEqual('42', GIMarshallingTests.gvalue_inout(value))
1287 def test_gvalue_flat_array_in(self):
1288 # the function already asserts the correct values
1289 GIMarshallingTests.gvalue_flat_array([42, "42", True])
1291 def test_gvalue_flat_array_in_item_marshal_failure(self):
1292 # Tests the failure to marshal 2^256 to a GValue mid-way through the array marshaling.
1293 self.assertRaises(RuntimeError, GIMarshallingTests.gvalue_flat_array,
1294 [42, 2 ** 256, True])
1296 def test_gvalue_flat_array_out(self):
1297 values = GIMarshallingTests.return_gvalue_flat_array()
1298 self.assertEqual(values, [42, '42', True])
1300 def test_gvalue_gobject_ref_counts(self):
1301 # Tests a GObject held by a GValue
1302 obj = GObject.Object()
1303 ref = weakref.ref(obj)
1304 grefcount = obj.__grefcount__
1306 value = GObject.Value()
1307 value.init(GObject.TYPE_OBJECT)
1309 # TYPE_OBJECT will inc ref count as it should
1310 value.set_object(obj)
1311 self.assertEqual(obj.__grefcount__, grefcount + 1)
1313 # multiple set_object should not inc ref count
1314 value.set_object(obj)
1315 self.assertEqual(obj.__grefcount__, grefcount + 1)
1317 # get_object will re-use the same wrapper as obj
1318 res = value.get_object()
1319 self.assertEqual(obj, res)
1320 self.assertEqual(obj.__grefcount__, grefcount + 1)
1322 # multiple get_object should not inc ref count
1323 res = value.get_object()
1324 self.assertEqual(obj.__grefcount__, grefcount + 1)
1326 # deletion of the result and value holder should bring the
1327 # refcount back to where we started
1331 self.assertEqual(obj.__grefcount__, grefcount)
1335 self.assertEqual(ref(), None)
1337 def test_gvalue_boxed_ref_counts(self):
1338 # Tests a boxed type wrapping a python object pointer (TYPE_PYOBJECT)
1344 ref = weakref.ref(obj)
1345 refcount = sys.getrefcount(obj)
1347 value = GObject.Value()
1348 value.init(GObject.TYPE_PYOBJECT)
1350 # boxed TYPE_PYOBJECT will inc ref count as it should
1351 value.set_boxed(obj)
1352 self.assertEqual(sys.getrefcount(obj), refcount + 1)
1354 # multiple set_boxed should not inc ref count
1355 value.set_boxed(obj)
1356 self.assertEqual(sys.getrefcount(obj), refcount + 1)
1358 res = value.get_boxed()
1359 self.assertEqual(obj, res)
1360 self.assertEqual(sys.getrefcount(obj), refcount + 2)
1362 # multiple get_boxed should not inc ref count
1363 res = value.get_boxed()
1364 self.assertEqual(sys.getrefcount(obj), refcount + 2)
1366 # deletion of the result and value holder should bring the
1367 # refcount back to where we started
1371 self.assertEqual(sys.getrefcount(obj), refcount)
1375 self.assertEqual(ref(), None)
1378 def disabled_test_gvalue_flat_array_round_trip(self):
1379 self.assertEqual([42, '42', True],
1380 GIMarshallingTests.gvalue_flat_array_round_trip(42, '42', True))
1383 class TestGClosure(unittest.TestCase):
1386 GIMarshallingTests.gclosure_in(lambda: 42)
1388 def test_pass(self):
1389 # test passing a closure between two C calls
1390 closure = GIMarshallingTests.gclosure_return()
1391 GIMarshallingTests.gclosure_in(closure)
1393 def test_type_error(self):
1394 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, 42)
1395 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, None)
1398 class TestCallbacks(unittest.TestCase):
1399 def test_return_value_only(self):
1402 self.assertEqual(GIMarshallingTests.callback_return_value_only(cb), 5)
1404 def test_one_out_arg(self):
1407 self.assertAlmostEqual(GIMarshallingTests.callback_one_out_parameter(cb), 5.5)
1409 def test_multiple_out_args(self):
1412 res = GIMarshallingTests.callback_multiple_out_parameters(cb)
1413 self.assertAlmostEqual(res[0], 5.5)
1414 self.assertAlmostEqual(res[1], 42.0)
1416 def test_return_and_one_out_arg(self):
1419 res = GIMarshallingTests.callback_return_value_and_one_out_parameter(cb)
1420 self.assertEqual(res[0], 5)
1421 self.assertAlmostEqual(res[1], 42.0)
1423 def test_return_and_multiple_out_arg(self):
1425 return (5, 42, -1000)
1426 self.assertEqual(GIMarshallingTests.callback_return_value_and_multiple_out_parameters(cb),
1430 class TestPointer(unittest.TestCase):
1431 def test_pointer_in_return(self):
1432 self.assertEqual(GIMarshallingTests.pointer_in_return(42), 42)
1435 class TestEnum(unittest.TestCase):
1438 def setUpClass(cls):
1439 '''Run tests under a test locale.
1441 Upper case conversion of member names should not be locale specific
1442 e. g. in Turkish, "i".upper() == "i", which gives results like "iNVALiD"
1444 Run test under a locale which defines toupper('a') == 'a'
1446 cls.locale_dir = tempfile.mkdtemp()
1447 src = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'te_ST@nouppera')
1448 dest = os.path.join(cls.locale_dir, 'te_ST.UTF-8@nouppera')
1449 subprocess.check_call(['localedef', '-i', src, '-c', '-f', 'UTF-8', dest])
1450 os.environ['LOCPATH'] = cls.locale_dir
1451 locale.setlocale(locale.LC_ALL, 'te_ST.UTF-8@nouppera')
1454 def tearDownClass(cls):
1455 locale.setlocale(locale.LC_ALL, 'C')
1456 shutil.rmtree(cls.locale_dir)
1458 del os.environ['LOCPATH']
1462 def test_enum(self):
1463 self.assertTrue(issubclass(GIMarshallingTests.Enum, int))
1464 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE1, GIMarshallingTests.Enum))
1465 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE2, GIMarshallingTests.Enum))
1466 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE3, GIMarshallingTests.Enum))
1467 self.assertEqual(42, GIMarshallingTests.Enum.VALUE3)
1469 def test_value_nick_and_name(self):
1470 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_nick, 'value1')
1471 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_nick, 'value2')
1472 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_nick, 'value3')
1474 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE1')
1475 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE2')
1476 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE3')
1478 def test_enum_in(self):
1479 GIMarshallingTests.enum_in(GIMarshallingTests.Enum.VALUE3)
1480 GIMarshallingTests.enum_in(42)
1482 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 43)
1483 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 'GIMarshallingTests.Enum.VALUE3')
1485 def test_enum_return(self):
1486 enum = GIMarshallingTests.enum_returnv()
1487 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1488 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1490 def test_enum_out(self):
1491 enum = GIMarshallingTests.enum_out()
1492 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1493 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1495 def test_enum_inout(self):
1496 enum = GIMarshallingTests.enum_inout(GIMarshallingTests.Enum.VALUE3)
1497 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1498 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE1)
1500 def test_enum_second(self):
1501 # check for the bug where different non-gtype enums share the same class
1502 self.assertNotEqual(GIMarshallingTests.Enum, GIMarshallingTests.SecondEnum)
1504 # check that values are not being shared between different enums
1505 self.assertTrue(hasattr(GIMarshallingTests.SecondEnum, "SECONDVALUE1"))
1506 self.assertRaises(AttributeError, getattr, GIMarshallingTests.Enum, "SECONDVALUE1")
1507 self.assertTrue(hasattr(GIMarshallingTests.Enum, "VALUE1"))
1508 self.assertRaises(AttributeError, getattr, GIMarshallingTests.SecondEnum, "VALUE1")
1510 def test_enum_gtype_name_is_namespaced(self):
1511 self.assertEqual(GIMarshallingTests.Enum.__gtype__.name,
1512 'PyGIMarshallingTestsEnum')
1514 def test_enum_add_type_error(self):
1515 self.assertRaises(TypeError,
1517 GIMarshallingTests.NoTypeFlags.__gtype__)
1520 class TestEnumVFuncResults(unittest.TestCase):
1521 class EnumTester(GIMarshallingTests.Object):
1522 def do_vfunc_return_enum(self):
1523 return GIMarshallingTests.Enum.VALUE2
1525 def do_vfunc_out_enum(self):
1526 return GIMarshallingTests.Enum.VALUE3
1528 def test_vfunc_return_enum(self):
1529 tester = self.EnumTester()
1530 self.assertEqual(tester.vfunc_return_enum(), GIMarshallingTests.Enum.VALUE2)
1532 def test_vfunc_out_enum(self):
1533 tester = self.EnumTester()
1534 self.assertEqual(tester.vfunc_out_enum(), GIMarshallingTests.Enum.VALUE3)
1537 class TestGEnum(unittest.TestCase):
1539 def test_genum(self):
1540 self.assertTrue(issubclass(GIMarshallingTests.GEnum, GObject.GEnum))
1541 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE1, GIMarshallingTests.GEnum))
1542 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE2, GIMarshallingTests.GEnum))
1543 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE3, GIMarshallingTests.GEnum))
1544 self.assertEqual(42, GIMarshallingTests.GEnum.VALUE3)
1546 def test_value_nick_and_name(self):
1547 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_nick, 'value1')
1548 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_nick, 'value2')
1549 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_nick, 'value3')
1551 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE1')
1552 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE2')
1553 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE3')
1555 def test_genum_in(self):
1556 GIMarshallingTests.genum_in(GIMarshallingTests.GEnum.VALUE3)
1557 GIMarshallingTests.genum_in(42)
1559 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 43)
1560 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 'GIMarshallingTests.GEnum.VALUE3')
1562 def test_genum_return(self):
1563 genum = GIMarshallingTests.genum_returnv()
1564 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1565 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1567 def test_genum_out(self):
1568 genum = GIMarshallingTests.genum_out()
1569 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1570 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1572 def test_genum_inout(self):
1573 genum = GIMarshallingTests.genum_inout(GIMarshallingTests.GEnum.VALUE3)
1574 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1575 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE1)
1578 class TestGFlags(unittest.TestCase):
1580 def test_flags(self):
1581 self.assertTrue(issubclass(GIMarshallingTests.Flags, GObject.GFlags))
1582 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1, GIMarshallingTests.Flags))
1583 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE2, GIMarshallingTests.Flags))
1584 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE3, GIMarshallingTests.Flags))
1585 # __or__() operation should still return an instance, not an int.
1586 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1 | GIMarshallingTests.Flags.VALUE2,
1587 GIMarshallingTests.Flags))
1588 self.assertEqual(1 << 1, GIMarshallingTests.Flags.VALUE2)
1590 def test_value_nick_and_name(self):
1591 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_nick, 'value1')
1592 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_nick, 'value2')
1593 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_nick, 'value3')
1595 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE1')
1596 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE2')
1597 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE3')
1599 def test_flags_in(self):
1600 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2)
1601 # result of __or__() operation should still be valid instance, not an int.
1602 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE2)
1603 GIMarshallingTests.flags_in_zero(Number(0))
1605 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 1 << 1)
1606 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 'GIMarshallingTests.Flags.VALUE2')
1608 def test_flags_return(self):
1609 flags = GIMarshallingTests.flags_returnv()
1610 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1611 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1613 def test_flags_out(self):
1614 flags = GIMarshallingTests.flags_out()
1615 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1616 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1618 def test_flags_inout(self):
1619 flags = GIMarshallingTests.flags_inout(GIMarshallingTests.Flags.VALUE2)
1620 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1621 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE1)
1624 class TestNoTypeFlags(unittest.TestCase):
1626 def test_flags(self):
1627 self.assertTrue(issubclass(GIMarshallingTests.NoTypeFlags, GObject.GFlags))
1628 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1, GIMarshallingTests.NoTypeFlags))
1629 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE2, GIMarshallingTests.NoTypeFlags))
1630 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE3, GIMarshallingTests.NoTypeFlags))
1631 # __or__() operation should still return an instance, not an int.
1632 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1 | GIMarshallingTests.NoTypeFlags.VALUE2,
1633 GIMarshallingTests.NoTypeFlags))
1634 self.assertEqual(1 << 1, GIMarshallingTests.NoTypeFlags.VALUE2)
1636 def test_value_nick_and_name(self):
1637 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_nick, 'value1')
1638 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_nick, 'value2')
1639 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_nick, 'value3')
1641 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE1')
1642 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2')
1643 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE3')
1645 def test_flags_in(self):
1646 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2)
1647 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2 | GIMarshallingTests.NoTypeFlags.VALUE2)
1648 GIMarshallingTests.no_type_flags_in_zero(Number(0))
1650 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 1 << 1)
1651 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 'GIMarshallingTests.NoTypeFlags.VALUE2')
1653 def test_flags_return(self):
1654 flags = GIMarshallingTests.no_type_flags_returnv()
1655 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1656 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1658 def test_flags_out(self):
1659 flags = GIMarshallingTests.no_type_flags_out()
1660 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1661 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1663 def test_flags_inout(self):
1664 flags = GIMarshallingTests.no_type_flags_inout(GIMarshallingTests.NoTypeFlags.VALUE2)
1665 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1666 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE1)
1668 def test_flags_gtype_name_is_namespaced(self):
1669 self.assertEqual(GIMarshallingTests.NoTypeFlags.__gtype__.name,
1670 'PyGIMarshallingTestsNoTypeFlags')
1673 class TestStructure(unittest.TestCase):
1675 def test_simple_struct(self):
1676 self.assertTrue(issubclass(GIMarshallingTests.SimpleStruct, GObject.GPointer))
1678 struct = GIMarshallingTests.SimpleStruct()
1679 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1681 self.assertEqual(0, struct.long_)
1682 self.assertEqual(0, struct.int8)
1687 self.assertEqual(6, struct.long_)
1688 self.assertEqual(7, struct.int8)
1692 def test_nested_struct(self):
1693 struct = GIMarshallingTests.NestedStruct()
1695 self.assertTrue(isinstance(struct.simple_struct, GIMarshallingTests.SimpleStruct))
1697 struct.simple_struct.long_ = 42
1698 self.assertEqual(42, struct.simple_struct.long_)
1702 def test_not_simple_struct(self):
1703 struct = GIMarshallingTests.NotSimpleStruct()
1704 self.assertEqual(None, struct.pointer)
1706 def test_simple_struct_return(self):
1707 struct = GIMarshallingTests.simple_struct_returnv()
1709 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1710 self.assertEqual(6, struct.long_)
1711 self.assertEqual(7, struct.int8)
1715 def test_simple_struct_in(self):
1716 struct = GIMarshallingTests.SimpleStruct()
1720 GIMarshallingTests.SimpleStruct.inv(struct)
1724 struct = GIMarshallingTests.NestedStruct()
1726 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, struct)
1730 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, None)
1732 def test_simple_struct_method(self):
1733 struct = GIMarshallingTests.SimpleStruct()
1741 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.method)
1743 def test_pointer_struct(self):
1744 self.assertTrue(issubclass(GIMarshallingTests.PointerStruct, GObject.GPointer))
1746 struct = GIMarshallingTests.PointerStruct()
1747 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1751 def test_pointer_struct_return(self):
1752 struct = GIMarshallingTests.pointer_struct_returnv()
1754 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1755 self.assertEqual(42, struct.long_)
1759 def test_pointer_struct_in(self):
1760 struct = GIMarshallingTests.PointerStruct()
1767 def test_boxed_struct(self):
1768 self.assertTrue(issubclass(GIMarshallingTests.BoxedStruct, GObject.GBoxed))
1770 struct = GIMarshallingTests.BoxedStruct()
1771 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1773 self.assertEqual(0, struct.long_)
1774 self.assertEqual(None, struct.string_)
1775 self.assertEqual([], struct.g_strv)
1779 def test_boxed_struct_new(self):
1780 struct = GIMarshallingTests.BoxedStruct.new()
1781 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1782 self.assertEqual(struct.long_, 0)
1783 self.assertEqual(struct.string_, None)
1787 def test_boxed_struct_copy(self):
1788 struct = GIMarshallingTests.BoxedStruct()
1790 struct.string_ = 'hello'
1792 new_struct = struct.copy()
1793 self.assertTrue(isinstance(new_struct, GIMarshallingTests.BoxedStruct))
1794 self.assertEqual(new_struct.long_, 42)
1795 self.assertEqual(new_struct.string_, 'hello')
1800 def test_boxed_struct_return(self):
1801 struct = GIMarshallingTests.boxed_struct_returnv()
1803 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1804 self.assertEqual(42, struct.long_)
1805 self.assertEqual('hello', struct.string_)
1806 self.assertEqual(['0', '1', '2'], struct.g_strv)
1810 def test_boxed_struct_in(self):
1811 struct = GIMarshallingTests.BoxedStruct()
1818 def test_boxed_struct_out(self):
1819 struct = GIMarshallingTests.boxed_struct_out()
1821 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1822 self.assertEqual(42, struct.long_)
1826 def test_boxed_struct_inout(self):
1827 in_struct = GIMarshallingTests.BoxedStruct()
1828 in_struct.long_ = 42
1830 out_struct = GIMarshallingTests.boxed_struct_inout(in_struct)
1832 self.assertTrue(isinstance(out_struct, GIMarshallingTests.BoxedStruct))
1833 self.assertEqual(0, out_struct.long_)
1838 def test_struct_field_assignment(self):
1839 struct = GIMarshallingTests.BoxedStruct()
1842 struct.string_ = 'hello'
1843 self.assertEqual(struct.long_, 42)
1844 self.assertEqual(struct.string_, 'hello')
1846 def test_union(self):
1847 union = GIMarshallingTests.Union()
1849 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1851 new_union = union.copy()
1852 self.assertTrue(isinstance(new_union, GIMarshallingTests.Union))
1857 def test_union_return(self):
1858 union = GIMarshallingTests.union_returnv()
1860 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1861 self.assertEqual(42, union.long_)
1865 def test_union_in(self):
1866 union = GIMarshallingTests.Union()
1873 def test_union_method(self):
1874 union = GIMarshallingTests.Union()
1881 self.assertRaises(TypeError, GIMarshallingTests.Union.method)
1884 class TestGObject(unittest.TestCase):
1886 def test_object(self):
1887 self.assertTrue(issubclass(GIMarshallingTests.Object, GObject.GObject))
1889 object_ = GIMarshallingTests.Object()
1890 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1891 self.assertEqual(object_.__grefcount__, 1)
1893 def test_object_new(self):
1894 object_ = GIMarshallingTests.Object.new(42)
1895 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1896 self.assertEqual(object_.__grefcount__, 1)
1898 def test_object_int(self):
1899 object_ = GIMarshallingTests.Object(int=42)
1900 self.assertEqual(object_.int_, 42)
1901 # FIXME: Don't work yet.
1903 # self.assertEqual(object_.int_, 0)
1905 def test_object_static_method(self):
1906 GIMarshallingTests.Object.static_method()
1908 def test_object_method(self):
1909 GIMarshallingTests.Object(int=42).method()
1910 self.assertRaises(TypeError, GIMarshallingTests.Object.method, GObject.GObject())
1911 self.assertRaises(TypeError, GIMarshallingTests.Object.method)
1913 def test_sub_object(self):
1914 self.assertTrue(issubclass(GIMarshallingTests.SubObject, GIMarshallingTests.Object))
1916 object_ = GIMarshallingTests.SubObject()
1917 self.assertTrue(isinstance(object_, GIMarshallingTests.SubObject))
1919 def test_sub_object_new(self):
1920 self.assertRaises(TypeError, GIMarshallingTests.SubObject.new, 42)
1922 def test_sub_object_static_method(self):
1923 object_ = GIMarshallingTests.SubObject()
1924 object_.static_method()
1926 def test_sub_object_method(self):
1927 object_ = GIMarshallingTests.SubObject(int=42)
1930 def test_sub_object_sub_method(self):
1931 object_ = GIMarshallingTests.SubObject()
1932 object_.sub_method()
1934 def test_sub_object_overwritten_method(self):
1935 object_ = GIMarshallingTests.SubObject()
1936 object_.overwritten_method()
1938 self.assertRaises(TypeError, GIMarshallingTests.SubObject.overwritten_method, GIMarshallingTests.Object())
1940 def test_sub_object_int(self):
1941 object_ = GIMarshallingTests.SubObject()
1942 self.assertEqual(object_.int_, 0)
1943 # FIXME: Don't work yet.
1945 # self.assertEqual(object_.int_, 42)
1947 def test_object_none_return(self):
1948 object_ = GIMarshallingTests.Object.none_return()
1949 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1950 self.assertEqual(object_.__grefcount__, 2)
1952 def test_object_full_return(self):
1953 object_ = GIMarshallingTests.Object.full_return()
1954 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1955 self.assertEqual(object_.__grefcount__, 1)
1957 def test_object_none_in(self):
1958 object_ = GIMarshallingTests.Object(int=42)
1959 GIMarshallingTests.Object.none_in(object_)
1960 self.assertEqual(object_.__grefcount__, 1)
1962 object_ = GIMarshallingTests.SubObject(int=42)
1963 GIMarshallingTests.Object.none_in(object_)
1965 object_ = GObject.GObject()
1966 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, object_)
1968 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, None)
1970 def test_object_none_out(self):
1971 object_ = GIMarshallingTests.Object.none_out()
1972 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1973 self.assertEqual(object_.__grefcount__, 2)
1975 new_object = GIMarshallingTests.Object.none_out()
1976 self.assertTrue(new_object is object_)
1978 def test_object_full_out(self):
1979 object_ = GIMarshallingTests.Object.full_out()
1980 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1981 self.assertEqual(object_.__grefcount__, 1)
1983 def test_object_none_inout(self):
1984 object_ = GIMarshallingTests.Object(int=42)
1985 new_object = GIMarshallingTests.Object.none_inout(object_)
1987 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1989 self.assertFalse(object_ is new_object)
1991 self.assertEqual(object_.__grefcount__, 1)
1992 self.assertEqual(new_object.__grefcount__, 2)
1994 new_new_object = GIMarshallingTests.Object.none_inout(object_)
1995 self.assertTrue(new_new_object is new_object)
1997 GIMarshallingTests.Object.none_inout(GIMarshallingTests.SubObject(int=42))
1999 @unittest.expectedFailure # https://bugzilla.gnome.org/show_bug.cgi?id=709796
2000 def test_object_full_inout(self):
2001 # Using gimarshallingtests.c from GI versions > 1.38.0 will show this
2002 # test as an "unexpected success" due to reference leak fixes in that file.
2003 # TODO: remove the expectedFailure once PyGI relies on GI > 1.38.0.
2004 object_ = GIMarshallingTests.Object(int=42)
2005 new_object = GIMarshallingTests.Object.full_inout(object_)
2007 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
2009 self.assertFalse(object_ is new_object)
2011 self.assertEqual(object_.__grefcount__, 1)
2012 self.assertEqual(new_object.__grefcount__, 1)
2014 # FIXME: Doesn't actually return the same object.
2015 # def test_object_inout_same(self):
2016 # object_ = GIMarshallingTests.Object()
2017 # new_object = GIMarshallingTests.object_full_inout(object_)
2018 # self.assertTrue(object_ is new_object)
2019 # self.assertEqual(object_.__grefcount__, 1)
2022 class TestPythonGObject(unittest.TestCase):
2024 class Object(GIMarshallingTests.Object):
2025 return_for_caller_allocated_out_parameter = 'test caller alloc return'
2027 def __init__(self, int):
2028 GIMarshallingTests.Object.__init__(self)
2032 # Don't call super, which asserts that self.int == 42.
2035 def do_method_int8_in(self, int8):
2038 def do_method_int8_out(self):
2041 def do_method_int8_arg_and_out_caller(self, arg):
2044 def do_method_int8_arg_and_out_callee(self, arg):
2047 def do_method_str_arg_out_ret(self, arg):
2048 return (arg.upper(), len(arg))
2050 def do_method_with_default_implementation(self, int8):
2051 GIMarshallingTests.Object.do_method_with_default_implementation(self, int8)
2052 self.props.int += int8
2054 def do_vfunc_return_value_only(self):
2057 def do_vfunc_one_out_parameter(self):
2060 def do_vfunc_multiple_out_parameters(self):
2061 return (42.42, 3.14)
2063 def do_vfunc_return_value_and_one_out_parameter(self):
2066 def do_vfunc_return_value_and_multiple_out_parameters(self):
2069 def do_vfunc_caller_allocated_out_parameter(self):
2070 return self.return_for_caller_allocated_out_parameter
2072 class SubObject(GIMarshallingTests.SubObject):
2073 def __init__(self, int):
2074 GIMarshallingTests.SubObject.__init__(self)
2077 def do_method_with_default_implementation(self, int8):
2080 def do_vfunc_return_value_only(self):
2083 class Interface3Impl(GObject.Object, GIMarshallingTests.Interface3):
2085 GObject.Object.__init__(self)
2086 self.variants = None
2087 self.n_variants = None
2089 def do_test_variant_array_in(self, variants, n_variants):
2090 self.variants = variants
2091 self.n_variants = n_variants
2093 class ErrorObject(GIMarshallingTests.Object):
2094 def do_vfunc_return_value_only(self):
2095 raise ValueError('Return value should be 0')
2097 def test_object(self):
2098 self.assertTrue(issubclass(self.Object, GIMarshallingTests.Object))
2100 object_ = self.Object(int=42)
2101 self.assertTrue(isinstance(object_, self.Object))
2103 def test_object_method(self):
2104 self.Object(int=0).method()
2106 def test_object_vfuncs(self):
2107 object_ = self.Object(int=42)
2108 object_.method_int8_in(84)
2109 self.assertEqual(object_.val, 84)
2110 self.assertEqual(object_.method_int8_out(), 42)
2112 # can be dropped when bumping g-i dependencies to >= 1.35.2
2113 if hasattr(object_, 'method_int8_arg_and_out_caller'):
2114 self.assertEqual(object_.method_int8_arg_and_out_caller(42), 43)
2115 self.assertEqual(object_.method_int8_arg_and_out_callee(42), 43)
2116 self.assertEqual(object_.method_str_arg_out_ret('hello'), ('HELLO', 5))
2118 object_.method_with_default_implementation(42)
2119 self.assertEqual(object_.props.int, 84)
2121 self.assertEqual(object_.vfunc_return_value_only(), 4242)
2122 self.assertAlmostEqual(object_.vfunc_one_out_parameter(), 42.42, places=5)
2124 (a, b) = object_.vfunc_multiple_out_parameters()
2125 self.assertAlmostEqual(a, 42.42, places=5)
2126 self.assertAlmostEqual(b, 3.14, places=5)
2128 self.assertEqual(object_.vfunc_return_value_and_one_out_parameter(), (5, 42))
2129 self.assertEqual(object_.vfunc_return_value_and_multiple_out_parameters(), (5, 42, 99))
2131 self.assertEqual(object_.vfunc_caller_allocated_out_parameter(),
2132 object_.return_for_caller_allocated_out_parameter)
2134 class ObjectWithoutVFunc(GIMarshallingTests.Object):
2135 def __init__(self, int):
2136 GIMarshallingTests.Object.__init__(self)
2138 object_ = ObjectWithoutVFunc(int=42)
2139 object_.method_with_default_implementation(84)
2140 self.assertEqual(object_.props.int, 84)
2142 def test_vfunc_return_ref_count(self):
2143 obj = self.Object(int=42)
2144 ref_count = sys.getrefcount(obj.return_for_caller_allocated_out_parameter)
2145 ret = obj.vfunc_caller_allocated_out_parameter()
2148 # Make sure the return and what the vfunc returned
2149 # are equal but not the same object.
2150 self.assertEqual(ret, obj.return_for_caller_allocated_out_parameter)
2151 self.assertFalse(ret is obj.return_for_caller_allocated_out_parameter)
2152 self.assertEqual(sys.getrefcount(obj.return_for_caller_allocated_out_parameter),
2155 def test_subobject_parent_vfunc(self):
2156 object_ = self.SubObject(int=81)
2157 object_.method_with_default_implementation(87)
2158 self.assertEqual(object_.val, 87)
2160 def test_subobject_child_vfunc(self):
2161 object_ = self.SubObject(int=1)
2162 self.assertEqual(object_.vfunc_return_value_only(), 2121)
2164 def test_dynamic_module(self):
2165 from gi.module import DynamicModule
2166 self.assertTrue(isinstance(GObject, DynamicModule))
2168 def test_subobject_non_vfunc_do_method(self):
2169 class PythonObjectWithNonVFuncDoMethod(object):
2170 def do_not_a_vfunc(self):
2173 class ObjectOverrideNonVFuncDoMethod(GIMarshallingTests.Object, PythonObjectWithNonVFuncDoMethod):
2174 def do_not_a_vfunc(self):
2175 value = super(ObjectOverrideNonVFuncDoMethod, self).do_not_a_vfunc()
2178 object_ = ObjectOverrideNonVFuncDoMethod()
2179 self.assertEqual(18, object_.do_not_a_vfunc())
2181 def test_native_function_not_set_in_subclass_dict(self):
2182 # Previously, GI was setting virtual functions on the class as well
2183 # as any *native* class that subclasses it. Here we check that it is only
2184 # set on the class that the method is originally from.
2185 self.assertTrue('do_method_with_default_implementation' in GIMarshallingTests.Object.__dict__)
2186 self.assertTrue('do_method_with_default_implementation' not in GIMarshallingTests.SubObject.__dict__)
2188 def test_subobject_with_interface_and_non_vfunc_do_method(self):
2189 # There was a bug for searching for vfuncs in interfaces. It was
2190 # triggered by having a do_* method that wasn't overriding
2191 # a native vfunc, as well as inheriting from an interface.
2192 class GObjectSubclassWithInterface(GObject.GObject, GIMarshallingTests.Interface):
2193 def do_method_not_a_vfunc(self):
2196 def test_subsubobject(self):
2197 class SubSubSubObject(GIMarshallingTests.SubSubObject):
2198 def do_method_deep_hierarchy(self, num):
2199 self.props.int = num * 2
2201 sub_sub_sub_object = SubSubSubObject()
2202 GIMarshallingTests.SubSubObject.do_method_deep_hierarchy(sub_sub_sub_object, 5)
2203 self.assertEqual(sub_sub_sub_object.props.int, 5)
2205 def test_interface3impl(self):
2206 iface3 = self.Interface3Impl()
2207 variants = [GLib.Variant('i', 27), GLib.Variant('s', 'Hello')]
2208 iface3.test_variant_array_in(variants)
2209 self.assertEqual(iface3.n_variants, 2)
2210 self.assertEqual(iface3.variants[0].unpack(), 27)
2211 self.assertEqual(iface3.variants[1].unpack(), 'Hello')
2213 def test_python_subsubobject_vfunc(self):
2214 class PySubObject(GIMarshallingTests.Object):
2216 GIMarshallingTests.Object.__init__(self)
2217 self.sub_method_int8_called = 0
2219 def do_method_int8_in(self, int8):
2220 self.sub_method_int8_called += 1
2222 class PySubSubObject(PySubObject):
2224 PySubObject.__init__(self)
2225 self.subsub_method_int8_called = 0
2227 def do_method_int8_in(self, int8):
2228 self.subsub_method_int8_called += 1
2231 so.method_int8_in(1)
2232 self.assertEqual(so.sub_method_int8_called, 1)
2234 # it should call the method on the SubSub object only
2235 sso = PySubSubObject()
2236 sso.method_int8_in(1)
2237 self.assertEqual(sso.subsub_method_int8_called, 1)
2238 self.assertEqual(sso.sub_method_int8_called, 0)
2240 def test_callback_in_vfunc(self):
2241 class SubObject(GIMarshallingTests.Object):
2243 GObject.GObject.__init__(self)
2246 def do_vfunc_with_callback(self, callback):
2247 self.worked = callback(42) == 42
2249 _object = SubObject()
2250 _object.call_vfunc_with_callback()
2251 self.assertTrue(_object.worked)
2252 _object.worked = False
2253 _object.call_vfunc_with_callback()
2254 self.assertTrue(_object.worked)
2256 def test_exception_in_vfunc_return_value(self):
2257 obj = self.ErrorObject()
2258 self.assertEqual(obj.vfunc_return_value_only(), 0)
2261 class TestMultiOutputArgs(unittest.TestCase):
2263 def test_int_out_out(self):
2264 self.assertEqual((6, 7), GIMarshallingTests.int_out_out())
2266 def test_int_return_out(self):
2267 self.assertEqual((6, 7), GIMarshallingTests.int_return_out())
2272 class TestInterfaces(unittest.TestCase):
2274 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2276 GObject.GObject.__init__(self)
2279 def do_test_int8_in(self, int8):
2283 self.instance = self.TestInterfaceImpl()
2285 def test_wrapper(self):
2286 self.assertTrue(issubclass(GIMarshallingTests.Interface, GObject.GInterface))
2287 self.assertEqual(GIMarshallingTests.Interface.__gtype__.name, 'GIMarshallingTestsInterface')
2288 self.assertRaises(NotImplementedError, GIMarshallingTests.Interface)
2290 def test_implementation(self):
2291 self.assertTrue(issubclass(self.TestInterfaceImpl, GIMarshallingTests.Interface))
2292 self.assertTrue(isinstance(self.instance, GIMarshallingTests.Interface))
2294 def test_int8_int(self):
2295 GIMarshallingTests.test_interface_test_int8_in(self.instance, 42)
2296 self.assertEqual(self.instance.val, 42)
2298 def test_subclass(self):
2299 class TestInterfaceImplA(self.TestInterfaceImpl):
2302 class TestInterfaceImplB(TestInterfaceImplA):
2305 instance = TestInterfaceImplA()
2306 GIMarshallingTests.test_interface_test_int8_in(instance, 42)
2307 self.assertEqual(instance.val, 42)
2309 def test_subclass_override(self):
2310 class TestInterfaceImplD(TestInterfaces.TestInterfaceImpl):
2313 def do_test_int8_in(self, int8):
2316 instance = TestInterfaceImplD()
2317 self.assertEqual(instance.val, None)
2318 self.assertEqual(instance.val2, None)
2320 GIMarshallingTests.test_interface_test_int8_in(instance, 42)
2321 self.assertEqual(instance.val, None)
2322 self.assertEqual(instance.val2, 42)
2324 def test_type_mismatch(self):
2325 obj = GIMarshallingTests.Object()
2327 # wrong type for first argument: interface
2328 enum = Gio.File.new_for_path('.').enumerate_children(
2329 '', Gio.FileQueryInfoFlags.NONE, None)
2332 self.fail('call with wrong type argument unexpectedly succeeded')
2333 except TypeError as e:
2334 # should have argument name
2335 self.assertTrue('cancellable' in str(e), e)
2336 # should have expected type
2337 self.assertTrue('xpected Gio.Cancellable' in str(e), e)
2338 # should have actual type
2339 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2341 # wrong type for self argument: interface
2343 Gio.FileEnumerator.next_file(obj, None)
2344 self.fail('call with wrong type argument unexpectedly succeeded')
2345 except TypeError as e:
2346 # should have argument name
2347 self.assertTrue('self' in str(e), e)
2348 # should have expected type
2349 self.assertTrue('xpected Gio.FileEnumerator' in str(e), e)
2350 # should have actual type
2351 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2353 # wrong type for first argument: GObject
2354 var = GLib.Variant('s', 'mystring')
2355 action = Gio.SimpleAction.new('foo', var.get_type())
2357 action.activate(obj)
2358 self.fail('call with wrong type argument unexpectedly succeeded')
2359 except TypeError as e:
2360 # should have argument name
2361 self.assertTrue('parameter' in str(e), e)
2362 # should have expected type
2363 self.assertTrue('xpected GLib.Variant' in str(e), e)
2364 # should have actual type
2365 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2367 # wrong type for self argument: GObject
2369 Gio.SimpleAction.activate(obj, obj)
2370 self.fail('call with wrong type argument unexpectedly succeeded')
2371 except TypeError as e:
2372 # should have argument name
2373 self.assertTrue('self' in str(e), e)
2374 # should have expected type
2375 self.assertTrue('xpected Gio.Action' in str(e), e)
2376 # should have actual type
2377 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2380 class TestMRO(unittest.TestCase):
2382 # check that our own MRO calculation matches what we would expect
2383 # from Python's own C3 calculations
2396 class E(D, GIMarshallingTests.Object):
2399 expected = (E, D, B, C, A, GIMarshallingTests.Object,
2400 GObject.Object, GObject.Object.__base__, gi._gobject.GObject,
2402 self.assertEqual(expected, E.__mro__)
2404 def test_interface_collision(self):
2405 # there was a problem with Python bailing out because of
2406 # http://en.wikipedia.org/wiki/Diamond_problem with interfaces,
2407 # which shouldn't really be a problem.
2409 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2412 class TestInterfaceImpl2(GIMarshallingTests.Interface,
2416 class TestInterfaceImpl3(TestInterfaceImpl,
2417 GIMarshallingTests.Interface2):
2420 def test_old_style_mixin(self):
2421 # Note: Old style classes don't exist in Python 3
2425 with warnings.catch_warnings(record=True) as warn:
2426 warnings.simplefilter('always')
2428 # Dynamically create a new gi based class with an old
2430 type('GIWithOldStyleMixin', (GIMarshallingTests.Object, Mixin), {})
2432 if sys.version_info < (3, 0):
2433 self.assertTrue(issubclass(warn[0].category, RuntimeWarning))
2435 self.assertEqual(len(warn), 0)
2438 class TestInterfaceClash(unittest.TestCase):
2440 def test_clash(self):
2442 class TestClash(GObject.GObject, GIMarshallingTests.Interface, GIMarshallingTests.Interface2):
2443 def do_test_int8_in(self, int8):
2447 self.assertRaises(TypeError, create_clash)
2450 class TestOverrides(unittest.TestCase):
2452 def test_constant(self):
2453 self.assertEqual(GIMarshallingTests.OVERRIDES_CONSTANT, 7)
2455 def test_struct(self):
2456 # Test that the constructor has been overridden.
2457 struct = GIMarshallingTests.OverridesStruct(42)
2459 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2461 # Test that the method has been overridden.
2462 self.assertEqual(6, struct.method())
2466 # Test that the overrides wrapper has been registered.
2467 struct = GIMarshallingTests.overrides_struct_returnv()
2469 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2473 def test_object(self):
2474 # Test that the constructor has been overridden.
2475 object_ = GIMarshallingTests.OverridesObject(42)
2477 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2479 # Test that the alternate constructor has been overridden.
2480 object_ = GIMarshallingTests.OverridesObject.new(42)
2482 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2484 # Test that the method has been overridden.
2485 self.assertEqual(6, object_.method())
2487 # Test that the overrides wrapper has been registered.
2488 object_ = GIMarshallingTests.OverridesObject.returnv()
2490 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2492 def test_module_name(self):
2494 self.assertEqual(GIMarshallingTests.OverridesStruct.__module__, 'gi.overrides.GIMarshallingTests')
2495 self.assertEqual(GIMarshallingTests.OverridesObject.__module__, 'gi.overrides.GIMarshallingTests')
2496 self.assertEqual(GObject.Object.__module__, 'gi.overrides.GObject')
2499 self.assertEqual(GIMarshallingTests.SubObject.__module__, 'gi.repository.GIMarshallingTests')
2500 # FIXME: does not work with TEST_NAMES='test_thread test_gi.TestOverrides',
2501 # it is importlib._bootstrap then
2502 #self.assertEqual(GObject.InitiallyUnowned.__module__, 'gi.repository.GObject')
2505 class TestDir(unittest.TestCase):
2506 def test_members_list(self):
2507 list = dir(GIMarshallingTests)
2508 self.assertTrue('OverridesStruct' in list)
2509 self.assertTrue('BoxedStruct' in list)
2510 self.assertTrue('OVERRIDES_CONSTANT' in list)
2511 self.assertTrue('GEnum' in list)
2512 self.assertTrue('int32_return_max' in list)
2514 def test_modules_list(self):
2515 import gi.repository
2516 list = dir(gi.repository)
2517 self.assertTrue('GIMarshallingTests' in list)
2519 # FIXME: test to see if a module which was not imported is in the list
2520 # we should be listing every typelib we find, not just the ones
2521 # which are imported
2523 # to test this I recommend we compile a fake module which
2524 # our tests would never import and check to see if it is
2527 # self.assertTrue('DoNotImportDummyTests' in list)
2530 class TestGError(unittest.TestCase):
2531 def test_array_in_crash(self):
2532 # Previously there was a bug in invoke, in which C arrays were unwrapped
2533 # from inside GArrays to be passed to the C function. But when a GError was
2534 # set, invoke would attempt to free the C array as if it were a GArray.
2535 # This crash is only for C arrays. It does not happen for C functions which
2536 # take in GArrays. See https://bugzilla.gnome.org/show_bug.cgi?id=642708
2537 self.assertRaises(GObject.GError, GIMarshallingTests.gerror_array_in, [1, 2, 3])
2540 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2541 error, debug = GIMarshallingTests.gerror_out()
2543 self.assertIsInstance(error, GObject.GError)
2544 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2545 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2546 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2547 self.assertEqual(debug, GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE)
2549 def test_out_transfer_none(self):
2550 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2551 error, debug = GIMarshallingTests.gerror_out_transfer_none()
2553 self.assertIsInstance(error, GObject.GError)
2554 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2555 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2556 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2557 self.assertEqual(GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE, debug)
2559 def test_return(self):
2560 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2561 error = GIMarshallingTests.gerror_return()
2563 self.assertIsInstance(error, GObject.GError)
2564 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2565 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2566 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2568 def test_exception(self):
2569 self.assertRaises(GObject.GError, GIMarshallingTests.gerror)
2571 GIMarshallingTests.gerror()
2573 etype, e = sys.exc_info()[:2]
2574 self.assertEqual(e.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2575 self.assertEqual(e.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2576 self.assertEqual(e.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2579 class TestParamSpec(unittest.TestCase):
2580 # https://bugzilla.gnome.org/show_bug.cgi?id=682355
2581 @unittest.expectedFailure
2582 def test_param_spec_in_bool(self):
2583 ps = GObject.param_spec_boolean('mybool', 'test-bool', 'boolblurb',
2584 True, GObject.ParamFlags.READABLE)
2585 GIMarshallingTests.param_spec_in_bool(ps)
2587 def test_param_spec_return(self):
2588 obj = GIMarshallingTests.param_spec_return()
2589 self.assertEqual(obj.name, 'test-param')
2590 self.assertEqual(obj.nick, 'test')
2591 self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2593 def test_param_spec_out(self):
2594 obj = GIMarshallingTests.param_spec_out()
2595 self.assertEqual(obj.name, 'test-param')
2596 self.assertEqual(obj.nick, 'test')
2597 self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2600 class TestKeywordArgs(unittest.TestCase):
2602 def test_calling(self):
2603 kw_func = GIMarshallingTests.int_three_in_three_out
2605 self.assertEqual(kw_func(1, 2, 3), (1, 2, 3))
2606 self.assertEqual(kw_func(**{'a': 4, 'b': 5, 'c': 6}), (4, 5, 6))
2607 self.assertEqual(kw_func(1, **{'b': 7, 'c': 8}), (1, 7, 8))
2608 self.assertEqual(kw_func(1, 7, **{'c': 8}), (1, 7, 8))
2609 self.assertEqual(kw_func(1, c=8, **{'b': 7}), (1, 7, 8))
2610 self.assertEqual(kw_func(2, c=4, b=3), (2, 3, 4))
2611 self.assertEqual(kw_func(a=2, c=4, b=3), (2, 3, 4))
2613 def assertRaisesMessage(self, exception, message, func, *args, **kwargs):
2615 func(*args, **kwargs)
2617 (e_type, e) = sys.exc_info()[:2]
2618 if message is not None:
2619 self.assertEqual(str(e), message)
2623 msg = "%s() did not raise %s" % (func.__name__, exception.__name__)
2624 raise AssertionError(msg)
2626 def test_type_errors(self):
2628 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2629 GIMarshallingTests.int_three_in_three_out)
2630 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (1 given)",
2631 GIMarshallingTests.int_three_in_three_out, 1)
2632 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2633 GIMarshallingTests.int_three_in_three_out, *())
2634 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2635 GIMarshallingTests.int_three_in_three_out, *(), **{})
2636 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (0 given)",
2637 GIMarshallingTests.int_three_in_three_out, *(), **{'c': 4})
2639 # test too many args
2640 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (4 given)",
2641 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4))
2642 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (4 given)",
2643 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4), c=6)
2645 # test too many keyword args
2646 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got multiple values for keyword argument 'a'",
2647 GIMarshallingTests.int_three_in_three_out, 1, 2, 3, **{'a': 4, 'b': 5})
2648 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'd'",
2649 GIMarshallingTests.int_three_in_three_out, d=4)
2650 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'e'",
2651 GIMarshallingTests.int_three_in_three_out, **{'e': 2})
2653 def test_kwargs_are_not_modified(self):
2656 GIMarshallingTests.int_three_in_three_out(1, c=4, **d)
2657 self.assertEqual(d, d2)
2659 @unittest.skipUnless(hasattr(GIMarshallingTests, 'int_one_in_utf8_two_in_one_allows_none'),
2660 'Requires newer GIMarshallingTests')
2661 def test_allow_none_as_default(self):
2662 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2, '3', '4')
2663 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2, '3')
2664 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2)
2665 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2, d='4')
2667 GIMarshallingTests.array_in_utf8_two_in_out_of_order('1', [-1, 0, 1, 2])
2668 GIMarshallingTests.array_in_utf8_two_in_out_of_order('1', [-1, 0, 1, 2], '2')
2669 self.assertRaises(TypeError,
2670 GIMarshallingTests.array_in_utf8_two_in_out_of_order,
2671 [-1, 0, 1, 2], a='1')
2672 self.assertRaises(TypeError,
2673 GIMarshallingTests.array_in_utf8_two_in_out_of_order,
2676 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2], '1', '2')
2677 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2], '1')
2678 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2])
2679 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2], b='2')
2681 GIMarshallingTests.int_one_in_utf8_two_in_one_allows_none(1, '2', '3')
2682 self.assertRaises(TypeError,
2683 GIMarshallingTests.int_one_in_utf8_two_in_one_allows_none,
2685 self.assertRaises(TypeError,
2686 GIMarshallingTests.int_one_in_utf8_two_in_one_allows_none,
2690 class TestPropertiesObject(unittest.TestCase):
2693 self.obj = GIMarshallingTests.PropertiesObject()
2695 def test_boolean(self):
2696 self.assertEqual(self.obj.props.some_boolean, False)
2697 self.obj.props.some_boolean = True
2698 self.assertEqual(self.obj.props.some_boolean, True)
2700 obj = GIMarshallingTests.PropertiesObject(some_boolean=True)
2701 self.assertEqual(obj.props.some_boolean, True)
2703 def test_char(self):
2704 self.assertEqual(self.obj.props.some_char, 0)
2705 self.obj.props.some_char = GObject.G_MAXINT8
2706 self.assertEqual(self.obj.props.some_char, GObject.G_MAXINT8)
2708 obj = GIMarshallingTests.PropertiesObject(some_char=-42)
2709 self.assertEqual(obj.props.some_char, -42)
2711 def test_uchar(self):
2712 self.assertEqual(self.obj.props.some_uchar, 0)
2713 self.obj.props.some_uchar = GObject.G_MAXUINT8
2714 self.assertEqual(self.obj.props.some_uchar, GObject.G_MAXUINT8)
2716 obj = GIMarshallingTests.PropertiesObject(some_uchar=42)
2717 self.assertEqual(obj.props.some_uchar, 42)
2720 self.assertEqual(self.obj.props.some_int, 0)
2721 self.obj.props.some_int = GObject.G_MAXINT
2722 self.assertEqual(self.obj.props.some_int, GObject.G_MAXINT)
2724 obj = GIMarshallingTests.PropertiesObject(some_int=-42)
2725 self.assertEqual(obj.props.some_int, -42)
2727 self.assertRaises(TypeError, setattr, self.obj.props, 'some_int', 'foo')
2728 self.assertRaises(TypeError, setattr, self.obj.props, 'some_int', None)
2730 self.assertEqual(obj.props.some_int, -42)
2732 def test_uint(self):
2733 self.assertEqual(self.obj.props.some_uint, 0)
2734 self.obj.props.some_uint = GObject.G_MAXUINT
2735 self.assertEqual(self.obj.props.some_uint, GObject.G_MAXUINT)
2737 obj = GIMarshallingTests.PropertiesObject(some_uint=42)
2738 self.assertEqual(obj.props.some_uint, 42)
2740 self.assertRaises(TypeError, setattr, self.obj.props, 'some_uint', 'foo')
2741 self.assertRaises(TypeError, setattr, self.obj.props, 'some_uint', None)
2743 self.assertEqual(obj.props.some_uint, 42)
2745 def test_long(self):
2746 self.assertEqual(self.obj.props.some_long, 0)
2747 self.obj.props.some_long = GObject.G_MAXLONG
2748 self.assertEqual(self.obj.props.some_long, GObject.G_MAXLONG)
2750 obj = GIMarshallingTests.PropertiesObject(some_long=-42)
2751 self.assertEqual(obj.props.some_long, -42)
2753 self.assertRaises(TypeError, setattr, self.obj.props, 'some_long', 'foo')
2754 self.assertRaises(TypeError, setattr, self.obj.props, 'some_long', None)
2756 self.assertEqual(obj.props.some_long, -42)
2758 def test_ulong(self):
2759 self.assertEqual(self.obj.props.some_ulong, 0)
2760 self.obj.props.some_ulong = GObject.G_MAXULONG
2761 self.assertEqual(self.obj.props.some_ulong, GObject.G_MAXULONG)
2763 obj = GIMarshallingTests.PropertiesObject(some_ulong=42)
2764 self.assertEqual(obj.props.some_ulong, 42)
2766 self.assertRaises(TypeError, setattr, self.obj.props, 'some_ulong', 'foo')
2767 self.assertRaises(TypeError, setattr, self.obj.props, 'some_ulong', None)
2769 self.assertEqual(obj.props.some_ulong, 42)
2771 def test_int64(self):
2772 self.assertEqual(self.obj.props.some_int64, 0)
2773 self.obj.props.some_int64 = GObject.G_MAXINT64
2774 self.assertEqual(self.obj.props.some_int64, GObject.G_MAXINT64)
2776 obj = GIMarshallingTests.PropertiesObject(some_int64=-4200000000000000)
2777 self.assertEqual(obj.props.some_int64, -4200000000000000)
2779 def test_uint64(self):
2780 self.assertEqual(self.obj.props.some_uint64, 0)
2781 self.obj.props.some_uint64 = GObject.G_MAXUINT64
2782 self.assertEqual(self.obj.props.some_uint64, GObject.G_MAXUINT64)
2784 obj = GIMarshallingTests.PropertiesObject(some_uint64=4200000000000000)
2785 self.assertEqual(obj.props.some_uint64, 4200000000000000)
2787 def test_float(self):
2788 self.assertEqual(self.obj.props.some_float, 0)
2789 self.obj.props.some_float = GObject.G_MAXFLOAT
2790 self.assertEqual(self.obj.props.some_float, GObject.G_MAXFLOAT)
2792 obj = GIMarshallingTests.PropertiesObject(some_float=42.42)
2793 self.assertAlmostEqual(obj.props.some_float, 42.42, 4)
2795 obj = GIMarshallingTests.PropertiesObject(some_float=42)
2796 self.assertAlmostEqual(obj.props.some_float, 42.0, 4)
2798 self.assertRaises(TypeError, setattr, self.obj.props, 'some_float', 'foo')
2799 self.assertRaises(TypeError, setattr, self.obj.props, 'some_float', None)
2801 self.assertAlmostEqual(obj.props.some_float, 42.0, 4)
2803 def test_double(self):
2804 self.assertEqual(self.obj.props.some_double, 0)
2805 self.obj.props.some_double = GObject.G_MAXDOUBLE
2806 self.assertEqual(self.obj.props.some_double, GObject.G_MAXDOUBLE)
2808 obj = GIMarshallingTests.PropertiesObject(some_double=42.42)
2809 self.assertAlmostEqual(obj.props.some_double, 42.42)
2811 obj = GIMarshallingTests.PropertiesObject(some_double=42)
2812 self.assertAlmostEqual(obj.props.some_double, 42.0)
2814 self.assertRaises(TypeError, setattr, self.obj.props, 'some_double', 'foo')
2815 self.assertRaises(TypeError, setattr, self.obj.props, 'some_double', None)
2817 self.assertAlmostEqual(obj.props.some_double, 42.0)
2819 def test_strv(self):
2820 self.assertEqual(self.obj.props.some_strv, [])
2821 self.obj.props.some_strv = ['hello', 'world']
2822 self.assertEqual(self.obj.props.some_strv, ['hello', 'world'])
2824 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', 1)
2825 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', 'foo')
2826 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', [1, 2])
2827 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', ['foo', 1])
2829 self.assertEqual(self.obj.props.some_strv, ['hello', 'world'])
2831 obj = GIMarshallingTests.PropertiesObject(some_strv=['hello', 'world'])
2832 self.assertEqual(obj.props.some_strv, ['hello', 'world'])
2834 def test_boxed_struct(self):
2835 self.assertEqual(self.obj.props.some_boxed_struct, None)
2838 __gtype__ = GObject.TYPE_STRV
2840 struct1 = GIMarshallingTests.BoxedStruct()
2843 self.obj.props.some_boxed_struct = struct1
2844 self.assertEqual(self.obj.props.some_boxed_struct.long_, 1)
2845 self.assertEqual(self.obj.some_boxed_struct.long_, 1)
2847 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_struct', 1)
2848 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_struct', 'foo')
2850 obj = GIMarshallingTests.PropertiesObject(some_boxed_struct=struct1)
2851 self.assertEqual(obj.props.some_boxed_struct.long_, 1)
2853 def test_boxed_glist(self):
2854 self.assertEqual(self.obj.props.some_boxed_glist, [])
2856 l = [GObject.G_MININT, 42, GObject.G_MAXINT]
2857 self.obj.props.some_boxed_glist = l
2858 self.assertEqual(self.obj.props.some_boxed_glist, l)
2859 self.obj.props.some_boxed_glist = []
2860 self.assertEqual(self.obj.props.some_boxed_glist, [])
2862 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_glist', 1)
2863 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_glist', 'foo')
2864 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_glist', ['a'])
2866 @unittest.expectedFailure
2867 def test_boxed_glist_ctor(self):
2868 l = [GObject.G_MININT, 42, GObject.G_MAXINT]
2869 obj = GIMarshallingTests.PropertiesObject(some_boxed_glist=l)
2870 self.assertEqual(obj.props.some_boxed_glist, l)
2872 def test_variant(self):
2873 self.assertEqual(self.obj.props.some_variant, None)
2875 self.obj.props.some_variant = GLib.Variant('o', '/myobj')
2876 self.assertEqual(self.obj.props.some_variant.get_type_string(), 'o')
2877 self.assertEqual(self.obj.props.some_variant.print_(False), "'/myobj'")
2879 self.obj.props.some_variant = None
2880 self.assertEqual(self.obj.props.some_variant, None)
2882 obj = GIMarshallingTests.PropertiesObject(some_variant=GLib.Variant('b', True))
2883 self.assertEqual(obj.props.some_variant.get_type_string(), 'b')
2884 self.assertEqual(obj.props.some_variant.get_boolean(), True)
2886 self.assertRaises(TypeError, setattr, self.obj.props, 'some_variant', 'foo')
2887 self.assertRaises(TypeError, setattr, self.obj.props, 'some_variant', 23)
2889 self.assertEqual(obj.props.some_variant.get_type_string(), 'b')
2890 self.assertEqual(obj.props.some_variant.get_boolean(), True)
2892 def test_setting_several_properties(self):
2893 obj = GIMarshallingTests.PropertiesObject()
2894 obj.set_properties(some_uchar=54, some_int=42)
2895 self.assertEqual(42, obj.props.some_int)
2896 self.assertEqual(54, obj.props.some_uchar)
2898 def test_props_accessor_dir(self):
2900 props = dir(GIMarshallingTests.PropertiesObject.props)
2901 self.assertTrue('some_float' in props)
2902 self.assertTrue('some_double' in props)
2903 self.assertTrue('some_variant' in props)
2906 obj = GIMarshallingTests.PropertiesObject()
2907 props = dir(obj.props)
2908 self.assertTrue('some_float' in props)
2909 self.assertTrue('some_double' in props)
2910 self.assertTrue('some_variant' in props)
2912 def test_param_spec_dir(self):
2913 attrs = dir(GIMarshallingTests.PropertiesObject.props.some_float)
2914 self.assertTrue('name' in attrs)
2915 self.assertTrue('nick' in attrs)
2916 self.assertTrue('blurb' in attrs)
2917 self.assertTrue('flags' in attrs)
2918 self.assertTrue('default_value' in attrs)
2919 self.assertTrue('minimum' in attrs)
2920 self.assertTrue('maximum' in attrs)
2923 class TestKeywords(unittest.TestCase):
2924 def test_method(self):
2926 v = GLib.Variant('i', 1)
2927 self.assertEqual(v.print_(False), '1')
2929 def test_function(self):
2931 self.assertEqual(GLib.Thread.yield_(), None)
2933 def test_struct_method(self):
2934 # g_timer_continue()
2935 # we cannot currently instantiate GLib.Timer objects, so just ensure
2937 self.assertTrue(callable(GLib.Timer.continue_))
2939 def test_uppercase(self):
2940 self.assertEqual(GLib.IOCondition.IN.value_nicks, ['in'])
2943 class TestModule(unittest.TestCase):
2944 def test_path(self):
2945 self.assertTrue(GIMarshallingTests.__path__.endswith('GIMarshallingTests-1.0.typelib'),
2946 GIMarshallingTests.__path__)
2949 self.assertTrue("'GIMarshallingTests' from '" in str(GIMarshallingTests),
2950 str(GIMarshallingTests))
2953 _dir = dir(GIMarshallingTests)
2954 self.assertGreater(len(_dir), 10)
2956 self.assertTrue('SimpleStruct' in _dir)
2957 self.assertTrue('Interface2' in _dir)
2958 self.assertTrue('CONSTANT_GERROR_CODE' in _dir)
2959 self.assertTrue('array_zero_terminated_inout' in _dir)
2961 # assert that dir() does not contain garbage
2962 for item_name in _dir:
2963 item = getattr(GIMarshallingTests, item_name)
2964 self.assertTrue(hasattr(item, '__class__'))
2966 def test_help(self):
2967 orig_stdout = sys.stdout
2969 if sys.version_info < (3, 0):
2970 sys.stdout = BytesIO()
2972 sys.stdout = StringIO()
2973 help(GIMarshallingTests)
2974 output = sys.stdout.getvalue()
2976 sys.stdout = orig_stdout
2978 self.assertTrue('SimpleStruct' in output, output)
2979 self.assertTrue('Interface2' in output, output)
2980 self.assertTrue('method_array_inout' in output, output)
2983 class TestProjectVersion(unittest.TestCase):
2984 def test_version_str(self):
2985 self.assertGreater(gi.__version__, "3.")
2987 def test_version_info(self):
2988 self.assertEqual(len(gi.version_info), 3)
2989 self.assertGreaterEqual(gi.version_info, (3, 3, 5))
2991 def test_check_version(self):
2992 self.assertRaises(ValueError, gi.check_version, (99, 0, 0))
2993 self.assertRaises(ValueError, gi.check_version, "99.0.0")
2994 gi.check_version((3, 3, 5))
2995 gi.check_version("3.3.5")
2998 class TestDeprecation(unittest.TestCase):
2999 def test_method(self):
3001 with warnings.catch_warnings(record=True) as warn:
3002 warnings.simplefilter('always')
3004 self.assertTrue(issubclass(warn[0].category, DeprecationWarning))
3006 def test_deprecated_init_no_keywords(self):
3007 def init(self, **kwargs):
3008 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
3010 fn = gi.overrides.deprecated_init(init, arg_names=('a', 'b', 'c'))
3011 with warnings.catch_warnings(record=True) as warn:
3012 warnings.simplefilter('always')
3014 self.assertEqual(len(warn), 1)
3015 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3016 self.assertRegexpMatches(str(warn[0].message),
3017 '.*keywords.*a, b, c.*')
3019 def test_deprecated_init_no_keywords_out_of_order(self):
3020 def init(self, **kwargs):
3021 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
3023 fn = gi.overrides.deprecated_init(init, arg_names=('b', 'a', 'c'))
3024 with warnings.catch_warnings(record=True) as warn:
3025 warnings.simplefilter('always')
3027 self.assertEqual(len(warn), 1)
3028 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3029 self.assertRegexpMatches(str(warn[0].message),
3030 '.*keywords.*b, a, c.*')
3032 def test_deprecated_init_ignored_keyword(self):
3033 def init(self, **kwargs):
3034 self.assertDictEqual(kwargs, {'a': 1, 'c': 3})
3036 fn = gi.overrides.deprecated_init(init,
3037 arg_names=('a', 'b', 'c'),
3039 with warnings.catch_warnings(record=True) as warn:
3040 warnings.simplefilter('always')
3042 self.assertEqual(len(warn), 1)
3043 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3044 self.assertRegexpMatches(str(warn[0].message),
3045 '.*keywords.*a, b, c.*')
3047 def test_deprecated_init_with_aliases(self):
3048 def init(self, **kwargs):
3049 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
3051 fn = gi.overrides.deprecated_init(init,
3052 arg_names=('a', 'b', 'c'),
3053 deprecated_aliases={'b': 'bb', 'c': 'cc'})
3054 with warnings.catch_warnings(record=True) as warn:
3055 warnings.simplefilter('always')
3057 fn(self, a=1, bb=2, cc=3)
3058 self.assertEqual(len(warn), 1)
3059 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3060 self.assertRegexpMatches(str(warn[0].message),
3061 '.*keyword.*"bb, cc".*deprecated.*"b, c" respectively')
3063 def test_deprecated_init_with_defaults(self):
3064 def init(self, **kwargs):
3065 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
3067 fn = gi.overrides.deprecated_init(init,
3068 arg_names=('a', 'b', 'c'),
3069 deprecated_defaults={'b': 2, 'c': 3})
3070 with warnings.catch_warnings(record=True) as warn:
3071 warnings.simplefilter('always')
3073 self.assertEqual(len(warn), 1)
3074 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3075 self.assertRegexpMatches(str(warn[0].message),
3076 '.*relying on deprecated non-standard defaults.*'
3077 'explicitly use: b=2, c=3')