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 def test_constant_utf8(self):
65 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.CONSTANT_UTF8)
67 def test_constant_number(self):
68 self.assertEqual(CONSTANT_NUMBER, GIMarshallingTests.CONSTANT_NUMBER)
70 def test_min_max_int(self):
71 self.assertEqual(GLib.MAXINT32, 2 ** 31 - 1)
72 self.assertEqual(GLib.MININT32, -2 ** 31)
73 self.assertEqual(GLib.MAXUINT32, 2 ** 32 - 1)
75 self.assertEqual(GLib.MAXINT64, 2 ** 63 - 1)
76 self.assertEqual(GLib.MININT64, -2 ** 63)
77 self.assertEqual(GLib.MAXUINT64, 2 ** 64 - 1)
80 class TestBoolean(unittest.TestCase):
82 def test_boolean_return(self):
83 self.assertEqual(True, GIMarshallingTests.boolean_return_true())
84 self.assertEqual(False, GIMarshallingTests.boolean_return_false())
86 def test_boolean_in(self):
87 GIMarshallingTests.boolean_in_true(True)
88 GIMarshallingTests.boolean_in_false(False)
90 GIMarshallingTests.boolean_in_true(1)
91 GIMarshallingTests.boolean_in_false(0)
93 def test_boolean_out(self):
94 self.assertEqual(True, GIMarshallingTests.boolean_out_true())
95 self.assertEqual(False, GIMarshallingTests.boolean_out_false())
97 def test_boolean_inout(self):
98 self.assertEqual(False, GIMarshallingTests.boolean_inout_true_false(True))
99 self.assertEqual(True, GIMarshallingTests.boolean_inout_false_true(False))
102 class TestInt8(unittest.TestCase):
104 MAX = GObject.G_MAXINT8
105 MIN = GObject.G_MININT8
107 def test_int8_return(self):
108 self.assertEqual(self.MAX, GIMarshallingTests.int8_return_max())
109 self.assertEqual(self.MIN, GIMarshallingTests.int8_return_min())
111 def test_int8_in(self):
112 max = Number(self.MAX)
113 min = Number(self.MIN)
115 GIMarshallingTests.int8_in_max(max)
116 GIMarshallingTests.int8_in_min(min)
121 self.assertRaises(OverflowError, GIMarshallingTests.int8_in_max, max)
122 self.assertRaises(OverflowError, GIMarshallingTests.int8_in_min, min)
124 self.assertRaises(TypeError, GIMarshallingTests.int8_in_max, "self.MAX")
126 def test_int8_out(self):
127 self.assertEqual(self.MAX, GIMarshallingTests.int8_out_max())
128 self.assertEqual(self.MIN, GIMarshallingTests.int8_out_min())
130 def test_int8_inout(self):
131 self.assertEqual(self.MIN, GIMarshallingTests.int8_inout_max_min(Number(self.MAX)))
132 self.assertEqual(self.MAX, GIMarshallingTests.int8_inout_min_max(Number(self.MIN)))
135 class TestUInt8(unittest.TestCase):
137 MAX = GObject.G_MAXUINT8
139 def test_uint8_return(self):
140 self.assertEqual(self.MAX, GIMarshallingTests.uint8_return())
142 def test_uint8_in(self):
143 number = Number(self.MAX)
145 GIMarshallingTests.uint8_in(number)
146 GIMarshallingTests.uint8_in(CHAR_255)
149 self.assertRaises(OverflowError, GIMarshallingTests.uint8_in, number)
150 self.assertRaises(OverflowError, GIMarshallingTests.uint8_in, Number(-1))
152 self.assertRaises(TypeError, GIMarshallingTests.uint8_in, "self.MAX")
154 def test_uint8_out(self):
155 self.assertEqual(self.MAX, GIMarshallingTests.uint8_out())
157 def test_uint8_inout(self):
158 self.assertEqual(0, GIMarshallingTests.uint8_inout(Number(self.MAX)))
161 class TestInt16(unittest.TestCase):
163 MAX = GObject.G_MAXINT16
164 MIN = GObject.G_MININT16
166 def test_int16_return(self):
167 self.assertEqual(self.MAX, GIMarshallingTests.int16_return_max())
168 self.assertEqual(self.MIN, GIMarshallingTests.int16_return_min())
170 def test_int16_in(self):
171 max = Number(self.MAX)
172 min = Number(self.MIN)
174 GIMarshallingTests.int16_in_max(max)
175 GIMarshallingTests.int16_in_min(min)
180 self.assertRaises(OverflowError, GIMarshallingTests.int16_in_max, max)
181 self.assertRaises(OverflowError, GIMarshallingTests.int16_in_min, min)
183 self.assertRaises(TypeError, GIMarshallingTests.int16_in_max, "self.MAX")
185 def test_int16_out(self):
186 self.assertEqual(self.MAX, GIMarshallingTests.int16_out_max())
187 self.assertEqual(self.MIN, GIMarshallingTests.int16_out_min())
189 def test_int16_inout(self):
190 self.assertEqual(self.MIN, GIMarshallingTests.int16_inout_max_min(Number(self.MAX)))
191 self.assertEqual(self.MAX, GIMarshallingTests.int16_inout_min_max(Number(self.MIN)))
194 class TestUInt16(unittest.TestCase):
196 MAX = GObject.G_MAXUINT16
198 def test_uint16_return(self):
199 self.assertEqual(self.MAX, GIMarshallingTests.uint16_return())
201 def test_uint16_in(self):
202 number = Number(self.MAX)
204 GIMarshallingTests.uint16_in(number)
208 self.assertRaises(OverflowError, GIMarshallingTests.uint16_in, number)
209 self.assertRaises(OverflowError, GIMarshallingTests.uint16_in, Number(-1))
211 self.assertRaises(TypeError, GIMarshallingTests.uint16_in, "self.MAX")
213 def test_uint16_out(self):
214 self.assertEqual(self.MAX, GIMarshallingTests.uint16_out())
216 def test_uint16_inout(self):
217 self.assertEqual(0, GIMarshallingTests.uint16_inout(Number(self.MAX)))
220 class TestInt32(unittest.TestCase):
222 MAX = GObject.G_MAXINT32
223 MIN = GObject.G_MININT32
225 def test_int32_return(self):
226 self.assertEqual(self.MAX, GIMarshallingTests.int32_return_max())
227 self.assertEqual(self.MIN, GIMarshallingTests.int32_return_min())
229 def test_int32_in(self):
230 max = Number(self.MAX)
231 min = Number(self.MIN)
233 GIMarshallingTests.int32_in_max(max)
234 GIMarshallingTests.int32_in_min(min)
239 self.assertRaises(OverflowError, GIMarshallingTests.int32_in_max, max)
240 self.assertRaises(OverflowError, GIMarshallingTests.int32_in_min, min)
242 self.assertRaises(TypeError, GIMarshallingTests.int32_in_max, "self.MAX")
244 def test_int32_out(self):
245 self.assertEqual(self.MAX, GIMarshallingTests.int32_out_max())
246 self.assertEqual(self.MIN, GIMarshallingTests.int32_out_min())
248 def test_int32_inout(self):
249 self.assertEqual(self.MIN, GIMarshallingTests.int32_inout_max_min(Number(self.MAX)))
250 self.assertEqual(self.MAX, GIMarshallingTests.int32_inout_min_max(Number(self.MIN)))
253 class TestUInt32(unittest.TestCase):
255 MAX = GObject.G_MAXUINT32
257 def test_uint32_return(self):
258 self.assertEqual(self.MAX, GIMarshallingTests.uint32_return())
260 def test_uint32_in(self):
261 number = Number(self.MAX)
263 GIMarshallingTests.uint32_in(number)
267 self.assertRaises(OverflowError, GIMarshallingTests.uint32_in, number)
268 self.assertRaises(OverflowError, GIMarshallingTests.uint32_in, Number(-1))
270 self.assertRaises(TypeError, GIMarshallingTests.uint32_in, "self.MAX")
272 def test_uint32_out(self):
273 self.assertEqual(self.MAX, GIMarshallingTests.uint32_out())
275 def test_uint32_inout(self):
276 self.assertEqual(0, GIMarshallingTests.uint32_inout(Number(self.MAX)))
279 class TestInt64(unittest.TestCase):
284 def test_int64_return(self):
285 self.assertEqual(self.MAX, GIMarshallingTests.int64_return_max())
286 self.assertEqual(self.MIN, GIMarshallingTests.int64_return_min())
288 def test_int64_in(self):
289 max = Number(self.MAX)
290 min = Number(self.MIN)
292 GIMarshallingTests.int64_in_max(max)
293 GIMarshallingTests.int64_in_min(min)
298 self.assertRaises(OverflowError, GIMarshallingTests.int64_in_max, max)
299 self.assertRaises(OverflowError, GIMarshallingTests.int64_in_min, min)
301 self.assertRaises(TypeError, GIMarshallingTests.int64_in_max, "self.MAX")
303 def test_int64_out(self):
304 self.assertEqual(self.MAX, GIMarshallingTests.int64_out_max())
305 self.assertEqual(self.MIN, GIMarshallingTests.int64_out_min())
307 def test_int64_inout(self):
308 self.assertEqual(self.MIN, GIMarshallingTests.int64_inout_max_min(Number(self.MAX)))
309 self.assertEqual(self.MAX, GIMarshallingTests.int64_inout_min_max(Number(self.MIN)))
312 class TestUInt64(unittest.TestCase):
316 def test_uint64_return(self):
317 self.assertEqual(self.MAX, GIMarshallingTests.uint64_return())
319 def test_uint64_in(self):
320 number = Number(self.MAX)
322 GIMarshallingTests.uint64_in(number)
326 self.assertRaises(OverflowError, GIMarshallingTests.uint64_in, number)
327 self.assertRaises(OverflowError, GIMarshallingTests.uint64_in, Number(-1))
329 self.assertRaises(TypeError, GIMarshallingTests.uint64_in, "self.MAX")
331 def test_uint64_out(self):
332 self.assertEqual(self.MAX, GIMarshallingTests.uint64_out())
334 def test_uint64_inout(self):
335 self.assertEqual(0, GIMarshallingTests.uint64_inout(Number(self.MAX)))
338 class TestShort(unittest.TestCase):
340 MAX = GObject.G_MAXSHORT
341 MIN = GObject.G_MINSHORT
343 def test_short_return(self):
344 self.assertEqual(self.MAX, GIMarshallingTests.short_return_max())
345 self.assertEqual(self.MIN, GIMarshallingTests.short_return_min())
347 def test_short_in(self):
348 max = Number(self.MAX)
349 min = Number(self.MIN)
351 GIMarshallingTests.short_in_max(max)
352 GIMarshallingTests.short_in_min(min)
357 self.assertRaises(OverflowError, GIMarshallingTests.short_in_max, max)
358 self.assertRaises(OverflowError, GIMarshallingTests.short_in_min, min)
360 self.assertRaises(TypeError, GIMarshallingTests.short_in_max, "self.MAX")
362 def test_short_out(self):
363 self.assertEqual(self.MAX, GIMarshallingTests.short_out_max())
364 self.assertEqual(self.MIN, GIMarshallingTests.short_out_min())
366 def test_short_inout(self):
367 self.assertEqual(self.MIN, GIMarshallingTests.short_inout_max_min(Number(self.MAX)))
368 self.assertEqual(self.MAX, GIMarshallingTests.short_inout_min_max(Number(self.MIN)))
371 class TestUShort(unittest.TestCase):
373 MAX = GObject.G_MAXUSHORT
375 def test_ushort_return(self):
376 self.assertEqual(self.MAX, GIMarshallingTests.ushort_return())
378 def test_ushort_in(self):
379 number = Number(self.MAX)
381 GIMarshallingTests.ushort_in(number)
385 self.assertRaises(OverflowError, GIMarshallingTests.ushort_in, number)
386 self.assertRaises(OverflowError, GIMarshallingTests.ushort_in, Number(-1))
388 self.assertRaises(TypeError, GIMarshallingTests.ushort_in, "self.MAX")
390 def test_ushort_out(self):
391 self.assertEqual(self.MAX, GIMarshallingTests.ushort_out())
393 def test_ushort_inout(self):
394 self.assertEqual(0, GIMarshallingTests.ushort_inout(Number(self.MAX)))
397 class TestInt(unittest.TestCase):
399 MAX = GObject.G_MAXINT
400 MIN = GObject.G_MININT
402 def test_int_return(self):
403 self.assertEqual(self.MAX, GIMarshallingTests.int_return_max())
404 self.assertEqual(self.MIN, GIMarshallingTests.int_return_min())
406 def test_int_in(self):
407 max = Number(self.MAX)
408 min = Number(self.MIN)
410 GIMarshallingTests.int_in_max(max)
411 GIMarshallingTests.int_in_min(min)
416 self.assertRaises(OverflowError, GIMarshallingTests.int_in_max, max)
417 self.assertRaises(OverflowError, GIMarshallingTests.int_in_min, min)
419 self.assertRaises(TypeError, GIMarshallingTests.int_in_max, "self.MAX")
421 def test_int_out(self):
422 self.assertEqual(self.MAX, GIMarshallingTests.int_out_max())
423 self.assertEqual(self.MIN, GIMarshallingTests.int_out_min())
425 def test_int_inout(self):
426 self.assertEqual(self.MIN, GIMarshallingTests.int_inout_max_min(Number(self.MAX)))
427 self.assertEqual(self.MAX, GIMarshallingTests.int_inout_min_max(Number(self.MIN)))
428 self.assertRaises(TypeError, GIMarshallingTests.int_inout_min_max, Number(self.MIN), CONSTANT_NUMBER)
431 class TestUInt(unittest.TestCase):
433 MAX = GObject.G_MAXUINT
435 def test_uint_return(self):
436 self.assertEqual(self.MAX, GIMarshallingTests.uint_return())
438 def test_uint_in(self):
439 number = Number(self.MAX)
441 GIMarshallingTests.uint_in(number)
445 self.assertRaises(OverflowError, GIMarshallingTests.uint_in, number)
446 self.assertRaises(OverflowError, GIMarshallingTests.uint_in, Number(-1))
448 self.assertRaises(TypeError, GIMarshallingTests.uint_in, "self.MAX")
450 def test_uint_out(self):
451 self.assertEqual(self.MAX, GIMarshallingTests.uint_out())
453 def test_uint_inout(self):
454 self.assertEqual(0, GIMarshallingTests.uint_inout(Number(self.MAX)))
457 class TestLong(unittest.TestCase):
459 MAX = GObject.G_MAXLONG
460 MIN = GObject.G_MINLONG
462 def test_long_return(self):
463 self.assertEqual(self.MAX, GIMarshallingTests.long_return_max())
464 self.assertEqual(self.MIN, GIMarshallingTests.long_return_min())
466 def test_long_in(self):
467 max = Number(self.MAX)
468 min = Number(self.MIN)
470 GIMarshallingTests.long_in_max(max)
471 GIMarshallingTests.long_in_min(min)
476 self.assertRaises(OverflowError, GIMarshallingTests.long_in_max, max)
477 self.assertRaises(OverflowError, GIMarshallingTests.long_in_min, min)
479 self.assertRaises(TypeError, GIMarshallingTests.long_in_max, "self.MAX")
481 def test_long_out(self):
482 self.assertEqual(self.MAX, GIMarshallingTests.long_out_max())
483 self.assertEqual(self.MIN, GIMarshallingTests.long_out_min())
485 def test_long_inout(self):
486 self.assertEqual(self.MIN, GIMarshallingTests.long_inout_max_min(Number(self.MAX)))
487 self.assertEqual(self.MAX, GIMarshallingTests.long_inout_min_max(Number(self.MIN)))
490 class TestULong(unittest.TestCase):
492 MAX = GObject.G_MAXULONG
494 def test_ulong_return(self):
495 self.assertEqual(self.MAX, GIMarshallingTests.ulong_return())
497 def test_ulong_in(self):
498 number = Number(self.MAX)
500 GIMarshallingTests.ulong_in(number)
504 self.assertRaises(OverflowError, GIMarshallingTests.ulong_in, number)
505 self.assertRaises(OverflowError, GIMarshallingTests.ulong_in, Number(-1))
507 self.assertRaises(TypeError, GIMarshallingTests.ulong_in, "self.MAX")
509 def test_ulong_out(self):
510 self.assertEqual(self.MAX, GIMarshallingTests.ulong_out())
512 def test_ulong_inout(self):
513 self.assertEqual(0, GIMarshallingTests.ulong_inout(Number(self.MAX)))
516 class TestSSize(unittest.TestCase):
518 MAX = GObject.G_MAXLONG
519 MIN = GObject.G_MINLONG
521 def test_ssize_return(self):
522 self.assertEqual(self.MAX, GIMarshallingTests.ssize_return_max())
523 self.assertEqual(self.MIN, GIMarshallingTests.ssize_return_min())
525 def test_ssize_in(self):
526 max = Number(self.MAX)
527 min = Number(self.MIN)
529 GIMarshallingTests.ssize_in_max(max)
530 GIMarshallingTests.ssize_in_min(min)
535 self.assertRaises(OverflowError, GIMarshallingTests.ssize_in_max, max)
536 self.assertRaises(OverflowError, GIMarshallingTests.ssize_in_min, min)
538 self.assertRaises(TypeError, GIMarshallingTests.ssize_in_max, "self.MAX")
540 def test_ssize_out(self):
541 self.assertEqual(self.MAX, GIMarshallingTests.ssize_out_max())
542 self.assertEqual(self.MIN, GIMarshallingTests.ssize_out_min())
544 def test_ssize_inout(self):
545 self.assertEqual(self.MIN, GIMarshallingTests.ssize_inout_max_min(Number(self.MAX)))
546 self.assertEqual(self.MAX, GIMarshallingTests.ssize_inout_min_max(Number(self.MIN)))
549 class TestSize(unittest.TestCase):
551 MAX = GObject.G_MAXULONG
553 def test_size_return(self):
554 self.assertEqual(self.MAX, GIMarshallingTests.size_return())
556 def test_size_in(self):
557 number = Number(self.MAX)
559 GIMarshallingTests.size_in(number)
563 self.assertRaises(OverflowError, GIMarshallingTests.size_in, number)
564 self.assertRaises(OverflowError, GIMarshallingTests.size_in, Number(-1))
566 self.assertRaises(TypeError, GIMarshallingTests.size_in, "self.MAX")
568 def test_size_out(self):
569 self.assertEqual(self.MAX, GIMarshallingTests.size_out())
571 def test_size_inout(self):
572 self.assertEqual(0, GIMarshallingTests.size_inout(Number(self.MAX)))
575 class TestTimet(unittest.TestCase):
577 def test_time_t_return(self):
578 self.assertEqual(1234567890, GIMarshallingTests.time_t_return())
580 def test_time_t_in(self):
581 GIMarshallingTests.time_t_in(1234567890)
582 self.assertRaises(TypeError, GIMarshallingTests.time_t_in, "hello")
584 def test_time_t_out(self):
585 self.assertEqual(1234567890, GIMarshallingTests.time_t_out())
587 def test_time_t_inout(self):
588 self.assertEqual(0, GIMarshallingTests.time_t_inout(1234567890))
591 class TestFloat(unittest.TestCase):
593 MAX = GObject.G_MAXFLOAT
594 MIN = GObject.G_MINFLOAT
596 def test_float_return(self):
597 self.assertAlmostEqual(self.MAX, GIMarshallingTests.float_return())
599 def test_float_in(self):
600 GIMarshallingTests.float_in(Number(self.MAX))
602 self.assertRaises(TypeError, GIMarshallingTests.float_in, "self.MAX")
604 def test_float_out(self):
605 self.assertAlmostEqual(self.MAX, GIMarshallingTests.float_out())
607 def test_float_inout(self):
608 self.assertAlmostEqual(self.MIN, GIMarshallingTests.float_inout(Number(self.MAX)))
611 class TestDouble(unittest.TestCase):
613 MAX = GObject.G_MAXDOUBLE
614 MIN = GObject.G_MINDOUBLE
616 def test_double_return(self):
617 self.assertAlmostEqual(self.MAX, GIMarshallingTests.double_return())
619 def test_double_in(self):
620 GIMarshallingTests.double_in(Number(self.MAX))
622 self.assertRaises(TypeError, GIMarshallingTests.double_in, "self.MAX")
624 def test_double_out(self):
625 self.assertAlmostEqual(self.MAX, GIMarshallingTests.double_out())
627 def test_double_inout(self):
628 self.assertAlmostEqual(self.MIN, GIMarshallingTests.double_inout(Number(self.MAX)))
631 class TestGType(unittest.TestCase):
633 def test_gtype_name(self):
634 self.assertEqual("void", GObject.TYPE_NONE.name)
635 self.assertEqual("gchararray", GObject.TYPE_STRING.name)
637 def check_readonly(gtype):
640 self.assertRaises(AttributeError, check_readonly, GObject.TYPE_NONE)
641 self.assertRaises(AttributeError, check_readonly, GObject.TYPE_STRING)
643 def test_gtype_return(self):
644 self.assertEqual(GObject.TYPE_NONE, GIMarshallingTests.gtype_return())
645 self.assertEqual(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_return())
647 def test_gtype_in(self):
648 GIMarshallingTests.gtype_in(GObject.TYPE_NONE)
649 GIMarshallingTests.gtype_string_in(GObject.TYPE_STRING)
650 self.assertRaises(TypeError, GIMarshallingTests.gtype_in, "foo")
651 self.assertRaises(TypeError, GIMarshallingTests.gtype_string_in, "foo")
653 def test_gtype_out(self):
654 self.assertEqual(GObject.TYPE_NONE, GIMarshallingTests.gtype_out())
655 self.assertEqual(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_out())
657 def test_gtype_inout(self):
658 self.assertEqual(GObject.TYPE_INT, GIMarshallingTests.gtype_inout(GObject.TYPE_NONE))
661 class TestUtf8(unittest.TestCase):
663 def test_utf8_none_return(self):
664 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_none_return())
666 def test_utf8_full_return(self):
667 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_full_return())
669 def test_utf8_none_in(self):
670 GIMarshallingTests.utf8_none_in(CONSTANT_UTF8)
671 if sys.version_info < (3, 0):
672 GIMarshallingTests.utf8_none_in(PY2_UNICODE_UTF8)
674 self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, CONSTANT_NUMBER)
675 self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, None)
677 def test_utf8_none_out(self):
678 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_none_out())
680 def test_utf8_full_out(self):
681 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_full_out())
683 def test_utf8_dangling_out(self):
684 GIMarshallingTests.utf8_dangling_out()
686 def test_utf8_none_inout(self):
687 self.assertEqual("", GIMarshallingTests.utf8_none_inout(CONSTANT_UTF8))
689 def test_utf8_full_inout(self):
690 self.assertEqual("", GIMarshallingTests.utf8_full_inout(CONSTANT_UTF8))
693 class TestFilename(unittest.TestCase):
695 self.workdir = tempfile.mkdtemp()
698 shutil.rmtree(self.workdir)
700 def test_filename_in(self):
701 fname = os.path.join(self.workdir, _unicode('testäø.txt'))
702 self.assertRaises(GLib.GError, GLib.file_get_contents, fname)
704 with open(fname.encode('UTF-8'), 'wb') as f:
705 f.write(b'hello world!\n\x01\x02')
707 (result, contents) = GLib.file_get_contents(fname)
708 self.assertEqual(result, True)
709 self.assertEqual(contents, b'hello world!\n\x01\x02')
711 def test_filename_out(self):
712 self.assertRaises(GLib.GError, GLib.Dir.make_tmp, 'test')
714 dirname = GLib.Dir.make_tmp('testäø.XXXXXX')
715 self.assertTrue('/testäø.' in dirname, dirname)
716 dirname = _bytes(dirname)
717 self.assertTrue(os.path.isdir(dirname))
720 def test_filename_type_error(self):
721 self.assertRaises(TypeError, GLib.file_get_contents, 23)
724 class TestArray(unittest.TestCase):
726 def test_array_fixed_int_return(self):
727 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_int_return())
729 def test_array_fixed_short_return(self):
730 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_short_return())
732 def test_array_fixed_int_in(self):
733 GIMarshallingTests.array_fixed_int_in(Sequence([-1, 0, 1, 2]))
735 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, Sequence([-1, '0', 1, 2]))
737 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, 42)
738 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, None)
740 def test_array_fixed_short_in(self):
741 GIMarshallingTests.array_fixed_short_in(Sequence([-1, 0, 1, 2]))
743 def test_array_fixed_out(self):
744 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_out())
746 def test_array_fixed_inout(self):
747 self.assertEqual([2, 1, 0, -1], GIMarshallingTests.array_fixed_inout([-1, 0, 1, 2]))
749 def test_array_return(self):
750 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_return())
752 def test_array_return_etc(self):
753 self.assertEqual(([5, 0, 1, 9], 14), GIMarshallingTests.array_return_etc(5, 9))
755 def test_array_in(self):
756 GIMarshallingTests.array_in(Sequence([-1, 0, 1, 2]))
757 GIMarshallingTests.array_in_guint64_len(Sequence([-1, 0, 1, 2]))
758 GIMarshallingTests.array_in_guint8_len(Sequence([-1, 0, 1, 2]))
760 def test_array_in_len_before(self):
761 GIMarshallingTests.array_in_len_before(Sequence([-1, 0, 1, 2]))
763 def test_array_in_len_zero_terminated(self):
764 GIMarshallingTests.array_in_len_zero_terminated(Sequence([-1, 0, 1, 2]))
766 def test_array_uint8_in(self):
767 GIMarshallingTests.array_uint8_in(Sequence([97, 98, 99, 100]))
768 GIMarshallingTests.array_uint8_in(_bytes("abcd"))
770 def test_array_string_in(self):
771 GIMarshallingTests.array_string_in(['foo', 'bar'])
773 def test_array_out(self):
774 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_out())
776 def test_array_out_etc(self):
777 self.assertEqual(([-5, 0, 1, 9], 4), GIMarshallingTests.array_out_etc(-5, 9))
779 def test_array_inout(self):
780 self.assertEqual([-2, -1, 0, 1, 2], GIMarshallingTests.array_inout(Sequence([-1, 0, 1, 2])))
782 def test_array_inout_etc(self):
783 self.assertEqual(([-5, -1, 0, 1, 9], 4),
784 GIMarshallingTests.array_inout_etc(-5, Sequence([-1, 0, 1, 2]), 9))
786 def test_method_array_in(self):
787 object_ = GIMarshallingTests.Object()
788 object_.method_array_in(Sequence([-1, 0, 1, 2]))
790 def test_method_array_out(self):
791 object_ = GIMarshallingTests.Object()
792 self.assertEqual([-1, 0, 1, 2], object_.method_array_out())
794 def test_method_array_inout(self):
795 object_ = GIMarshallingTests.Object()
796 self.assertEqual([-2, -1, 0, 1, 2], object_.method_array_inout(Sequence([-1, 0, 1, 2])))
798 def test_method_array_return(self):
799 object_ = GIMarshallingTests.Object()
800 self.assertEqual([-1, 0, 1, 2], object_.method_array_return())
802 def test_array_enum_in(self):
803 GIMarshallingTests.array_enum_in([GIMarshallingTests.Enum.VALUE1,
804 GIMarshallingTests.Enum.VALUE2,
805 GIMarshallingTests.Enum.VALUE3])
807 def test_array_boxed_struct_in(self):
808 struct1 = GIMarshallingTests.BoxedStruct()
810 struct2 = GIMarshallingTests.BoxedStruct()
812 struct3 = GIMarshallingTests.BoxedStruct()
815 GIMarshallingTests.array_struct_in([struct1, struct2, struct3])
817 def test_array_boxed_struct_in_item_marshal_failure(self):
818 struct1 = GIMarshallingTests.BoxedStruct()
820 struct2 = GIMarshallingTests.BoxedStruct()
823 self.assertRaises(TypeError, GIMarshallingTests.array_struct_in,
824 [struct1, struct2, 'not_a_struct'])
826 def test_array_boxed_struct_value_in(self):
827 struct1 = GIMarshallingTests.BoxedStruct()
829 struct2 = GIMarshallingTests.BoxedStruct()
831 struct3 = GIMarshallingTests.BoxedStruct()
834 GIMarshallingTests.array_struct_value_in([struct1, struct2, struct3])
836 def test_array_boxed_struct_value_in_item_marshal_failure(self):
837 struct1 = GIMarshallingTests.BoxedStruct()
839 struct2 = GIMarshallingTests.BoxedStruct()
842 self.assertRaises(TypeError, GIMarshallingTests.array_struct_value_in,
843 [struct1, struct2, 'not_a_struct'])
845 def test_array_boxed_struct_take_in(self):
846 struct1 = GIMarshallingTests.BoxedStruct()
848 struct2 = GIMarshallingTests.BoxedStruct()
850 struct3 = GIMarshallingTests.BoxedStruct()
853 GIMarshallingTests.array_struct_take_in([struct1, struct2, struct3])
855 self.assertEqual(1, struct1.long_)
857 def test_array_boxed_struct_return(self):
858 (struct1, struct2, struct3) = GIMarshallingTests.array_zero_terminated_return_struct()
859 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct1))
860 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct2))
861 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct3))
862 self.assertEqual(42, struct1.long_)
863 self.assertEqual(43, struct2.long_)
864 self.assertEqual(44, struct3.long_)
866 def test_array_simple_struct_in(self):
867 struct1 = GIMarshallingTests.SimpleStruct()
869 struct2 = GIMarshallingTests.SimpleStruct()
871 struct3 = GIMarshallingTests.SimpleStruct()
874 GIMarshallingTests.array_simple_struct_in([struct1, struct2, struct3])
876 def test_array_simple_struct_in_item_marshal_failure(self):
877 struct1 = GIMarshallingTests.SimpleStruct()
879 struct2 = GIMarshallingTests.SimpleStruct()
882 self.assertRaises(TypeError, GIMarshallingTests.array_simple_struct_in,
883 [struct1, struct2, 'not_a_struct'])
885 def test_array_multi_array_key_value_in(self):
886 GIMarshallingTests.multi_array_key_value_in(["one", "two", "three"],
889 def test_array_in_nonzero_nonlen(self):
890 GIMarshallingTests.array_in_nonzero_nonlen(1, b'abcd')
892 def test_array_fixed_out_struct(self):
893 struct1, struct2 = GIMarshallingTests.array_fixed_out_struct()
895 self.assertEqual(7, struct1.long_)
896 self.assertEqual(6, struct1.int8)
897 self.assertEqual(6, struct2.long_)
898 self.assertEqual(7, struct2.int8)
900 def test_array_zero_terminated_return(self):
901 self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_return())
903 def test_array_zero_terminated_return_null(self):
904 self.assertEqual([], GIMarshallingTests.array_zero_terminated_return_null())
906 def test_array_zero_terminated_in(self):
907 GIMarshallingTests.array_zero_terminated_in(Sequence(['0', '1', '2']))
909 def test_array_zero_terminated_out(self):
910 self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
912 def test_array_zero_terminated_inout(self):
913 self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.array_zero_terminated_inout(['0', '1', '2']))
915 def test_init_function(self):
916 self.assertEqual((True, []), GIMarshallingTests.init_function([]))
917 self.assertEqual((True, []), GIMarshallingTests.init_function(['hello']))
918 self.assertEqual((True, ['hello']),
919 GIMarshallingTests.init_function(['hello', 'world']))
922 class TestGStrv(unittest.TestCase):
924 def test_gstrv_return(self):
925 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_return())
927 def test_gstrv_in(self):
928 GIMarshallingTests.gstrv_in(Sequence(['0', '1', '2']))
930 def test_gstrv_out(self):
931 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_out())
933 def test_gstrv_inout(self):
934 self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.gstrv_inout(['0', '1', '2']))
937 class TestArrayGVariant(unittest.TestCase):
939 def test_array_gvariant_none_in(self):
940 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
941 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_none_in(v)]
942 self.assertEqual([27, "Hello"], returned)
944 def test_array_gvariant_container_in(self):
945 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
946 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_container_in(v)]
947 self.assertEqual([27, "Hello"], returned)
949 def test_array_gvariant_full_in(self):
950 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
951 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_full_in(v)]
952 self.assertEqual([27, "Hello"], returned)
954 def test_bytearray_gvariant(self):
955 v = GLib.Variant.new_bytestring(b"foo")
956 self.assertEqual(v.get_bytestring(), b"foo")
959 class TestGArray(unittest.TestCase):
961 def test_garray_int_none_return(self):
962 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.garray_int_none_return())
964 def test_garray_uint64_none_return(self):
965 self.assertEqual([0, GObject.G_MAXUINT64], GIMarshallingTests.garray_uint64_none_return())
967 def test_garray_utf8_none_return(self):
968 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_return())
970 def test_garray_utf8_container_return(self):
971 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_return())
973 def test_garray_utf8_full_return(self):
974 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_return())
976 def test_garray_int_none_in(self):
977 GIMarshallingTests.garray_int_none_in(Sequence([-1, 0, 1, 2]))
979 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, Sequence([-1, '0', 1, 2]))
981 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, 42)
982 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, None)
984 def test_garray_uint64_none_in(self):
985 GIMarshallingTests.garray_uint64_none_in(Sequence([0, GObject.G_MAXUINT64]))
987 def test_garray_utf8_none_in(self):
988 GIMarshallingTests.garray_utf8_none_in(Sequence(['0', '1', '2']))
990 def test_garray_utf8_none_out(self):
991 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_out())
993 def test_garray_utf8_container_out(self):
994 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_out())
996 def test_garray_utf8_full_out(self):
997 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out())
999 def test_garray_utf8_full_out_caller_allocated(self):
1000 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out_caller_allocated())
1002 def test_garray_utf8_none_inout(self):
1003 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_none_inout(Sequence(('0', '1', '2'))))
1005 def test_garray_utf8_container_inout(self):
1006 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_container_inout(['0', '1', '2']))
1008 def test_garray_utf8_full_inout(self):
1009 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_full_inout(['0', '1', '2']))
1012 class TestGPtrArray(unittest.TestCase):
1014 def test_gptrarray_utf8_none_return(self):
1015 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_return())
1017 def test_gptrarray_utf8_container_return(self):
1018 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_return())
1020 def test_gptrarray_utf8_full_return(self):
1021 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_return())
1023 def test_gptrarray_utf8_none_in(self):
1024 GIMarshallingTests.gptrarray_utf8_none_in(Sequence(['0', '1', '2']))
1026 def test_gptrarray_utf8_none_out(self):
1027 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_out())
1029 def test_gptrarray_utf8_container_out(self):
1030 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_out())
1032 def test_gptrarray_utf8_full_out(self):
1033 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_out())
1035 def test_gptrarray_utf8_none_inout(self):
1036 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_none_inout(Sequence(('0', '1', '2'))))
1038 def test_gptrarray_utf8_container_inout(self):
1039 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_container_inout(['0', '1', '2']))
1041 def test_gptrarray_utf8_full_inout(self):
1042 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_full_inout(['0', '1', '2']))
1045 class TestGBytes(unittest.TestCase):
1046 def test_gbytes_create(self):
1047 b = GLib.Bytes.new(b'\x00\x01\xFF')
1048 self.assertEqual(3, b.get_size())
1049 self.assertEqual(b'\x00\x01\xFF', b.get_data())
1051 def test_gbytes_create_take(self):
1052 b = GLib.Bytes.new_take(b'\x00\x01\xFF')
1053 self.assertEqual(3, b.get_size())
1054 self.assertEqual(b'\x00\x01\xFF', b.get_data())
1056 def test_gbytes_full_return(self):
1057 b = GIMarshallingTests.gbytes_full_return()
1058 self.assertEqual(4, b.get_size())
1059 self.assertEqual(b'\x00\x31\xFF\x33', b.get_data())
1061 def test_gbytes_none_in(self):
1062 b = GIMarshallingTests.gbytes_full_return()
1063 GIMarshallingTests.gbytes_none_in(b)
1065 def test_compare(self):
1066 a1 = GLib.Bytes.new(b'\x00\x01\xFF')
1067 a2 = GLib.Bytes.new(b'\x00\x01\xFF')
1068 b = GLib.Bytes.new(b'\x00\x01\xFE')
1070 self.assertTrue(a1.equal(a2))
1071 self.assertTrue(a2.equal(a1))
1072 self.assertFalse(a1.equal(b))
1073 self.assertFalse(b.equal(a2))
1075 self.assertEqual(0, a1.compare(a2))
1076 self.assertLess(0, a1.compare(b))
1077 self.assertGreater(0, b.compare(a1))
1080 class TestGByteArray(unittest.TestCase):
1082 ba = GLib.ByteArray.new()
1083 self.assertEqual(b'', ba)
1085 ba = GLib.ByteArray.new_take(b'\x01\x02\xFF')
1086 self.assertEqual(b'\x01\x02\xFF', ba)
1088 def test_bytearray_full_return(self):
1089 self.assertEqual(b'\x001\xFF3', GIMarshallingTests.bytearray_full_return())
1091 def test_bytearray_none_in(self):
1092 GIMarshallingTests.bytearray_none_in(b'\x00\x31\xFF\x33')
1095 class TestGList(unittest.TestCase):
1097 def test_glist_int_none_return(self):
1098 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.glist_int_none_return())
1100 def test_glist_uint32_none_return(self):
1101 self.assertEqual([0, GObject.G_MAXUINT32], GIMarshallingTests.glist_uint32_none_return())
1103 def test_glist_utf8_none_return(self):
1104 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_return())
1106 def test_glist_utf8_container_return(self):
1107 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_return())
1109 def test_glist_utf8_full_return(self):
1110 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_return())
1112 def test_glist_int_none_in(self):
1113 GIMarshallingTests.glist_int_none_in(Sequence((-1, 0, 1, 2)))
1115 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, Sequence((-1, '0', 1, 2)))
1117 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, 42)
1118 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, None)
1120 def test_glist_uint32_none_in(self):
1121 GIMarshallingTests.glist_uint32_none_in(Sequence((0, GObject.G_MAXUINT32)))
1123 def test_glist_utf8_none_in(self):
1124 GIMarshallingTests.glist_utf8_none_in(Sequence(('0', '1', '2')))
1126 def test_glist_utf8_none_out(self):
1127 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_out())
1129 def test_glist_utf8_container_out(self):
1130 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_out())
1132 def test_glist_utf8_full_out(self):
1133 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_out())
1135 def test_glist_utf8_none_inout(self):
1136 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_none_inout(Sequence(('0', '1', '2'))))
1138 def test_glist_utf8_container_inout(self):
1139 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_container_inout(('0', '1', '2')))
1141 def test_glist_utf8_full_inout(self):
1142 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_full_inout(('0', '1', '2')))
1145 class TestGSList(unittest.TestCase):
1147 def test_gslist_int_none_return(self):
1148 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.gslist_int_none_return())
1150 def test_gslist_utf8_none_return(self):
1151 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_return())
1153 def test_gslist_utf8_container_return(self):
1154 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_return())
1156 def test_gslist_utf8_full_return(self):
1157 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_return())
1159 def test_gslist_int_none_in(self):
1160 GIMarshallingTests.gslist_int_none_in(Sequence((-1, 0, 1, 2)))
1162 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, Sequence((-1, '0', 1, 2)))
1164 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, 42)
1165 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, None)
1167 def test_gslist_utf8_none_in(self):
1168 GIMarshallingTests.gslist_utf8_none_in(Sequence(('0', '1', '2')))
1170 def test_gslist_utf8_none_out(self):
1171 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_out())
1173 def test_gslist_utf8_container_out(self):
1174 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_out())
1176 def test_gslist_utf8_full_out(self):
1177 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_out())
1179 def test_gslist_utf8_none_inout(self):
1180 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_none_inout(Sequence(('0', '1', '2'))))
1182 def test_gslist_utf8_container_inout(self):
1183 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_container_inout(('0', '1', '2')))
1185 def test_gslist_utf8_full_inout(self):
1186 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_full_inout(('0', '1', '2')))
1189 class TestGHashTable(unittest.TestCase):
1191 def test_ghashtable_int_none_return(self):
1192 self.assertEqual({-1: 1, 0: 0, 1: -1, 2: -2}, GIMarshallingTests.ghashtable_int_none_return())
1194 def test_ghashtable_int_none_return2(self):
1195 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_return())
1197 def test_ghashtable_int_container_return(self):
1198 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_return())
1200 def test_ghashtable_int_full_return(self):
1201 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_return())
1203 def test_ghashtable_int_none_in(self):
1204 GIMarshallingTests.ghashtable_int_none_in({-1: 1, 0: 0, 1: -1, 2: -2})
1206 self.assertRaises(TypeError, 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})
1209 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, None)
1212 def test_ghashtable_utf8_none_in(self):
1213 GIMarshallingTests.ghashtable_utf8_none_in({'-1': '1', '0': '0', '1': '-1', '2': '-2'})
1215 def test_ghashtable_utf8_none_out(self):
1216 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_out())
1218 def test_ghashtable_utf8_container_out(self):
1219 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_out())
1221 def test_ghashtable_utf8_full_out(self):
1222 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_out())
1224 def test_ghashtable_utf8_none_inout(self):
1225 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1226 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1227 GIMarshallingTests.ghashtable_utf8_none_inout(i))
1229 def test_ghashtable_utf8_container_inout(self):
1230 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1231 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1232 GIMarshallingTests.ghashtable_utf8_container_inout(i))
1234 def test_ghashtable_utf8_full_inout(self):
1235 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1236 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1237 GIMarshallingTests.ghashtable_utf8_full_inout(i))
1240 class TestGValue(unittest.TestCase):
1242 def test_gvalue_return(self):
1243 self.assertEqual(42, GIMarshallingTests.gvalue_return())
1245 def test_gvalue_in(self):
1246 GIMarshallingTests.gvalue_in(42)
1247 value = GObject.Value(GObject.TYPE_INT, 42)
1248 GIMarshallingTests.gvalue_in(value)
1250 def test_gvalue_in_with_modification(self):
1251 value = GObject.Value(GObject.TYPE_INT, 42)
1252 GIMarshallingTests.gvalue_in_with_modification(value)
1253 self.assertEqual(value.get_int(), 24)
1255 def test_gvalue_int64_in(self):
1256 value = GObject.Value(GObject.TYPE_INT64, GObject.G_MAXINT64)
1257 GIMarshallingTests.gvalue_int64_in(value)
1259 def test_gvalue_in_with_type(self):
1260 value = GObject.Value(GObject.TYPE_STRING, 'foo')
1261 GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_STRING)
1263 value = GObject.Value(GIMarshallingTests.Flags.__gtype__,
1264 GIMarshallingTests.Flags.VALUE1)
1265 GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_FLAGS)
1267 def test_gvalue_in_enum(self):
1268 value = GObject.Value(GIMarshallingTests.Enum.__gtype__,
1269 GIMarshallingTests.Enum.VALUE3)
1270 GIMarshallingTests.gvalue_in_enum(value)
1272 def test_gvalue_out(self):
1273 self.assertEqual(42, GIMarshallingTests.gvalue_out())
1275 def test_gvalue_int64_out(self):
1276 self.assertEqual(GObject.G_MAXINT64, GIMarshallingTests.gvalue_int64_out())
1278 def test_gvalue_out_caller_allocates(self):
1279 self.assertEqual(42, GIMarshallingTests.gvalue_out_caller_allocates())
1281 def test_gvalue_inout(self):
1282 self.assertEqual('42', GIMarshallingTests.gvalue_inout(42))
1283 value = GObject.Value(int, 42)
1284 self.assertEqual('42', GIMarshallingTests.gvalue_inout(value))
1286 def test_gvalue_flat_array_in(self):
1287 # the function already asserts the correct values
1288 GIMarshallingTests.gvalue_flat_array([42, "42", True])
1290 def test_gvalue_flat_array_in_item_marshal_failure(self):
1291 # Tests the failure to marshal 2^256 to a GValue mid-way through the array marshaling.
1292 self.assertRaises(RuntimeError, GIMarshallingTests.gvalue_flat_array,
1293 [42, 2 ** 256, True])
1295 def test_gvalue_flat_array_out(self):
1296 values = GIMarshallingTests.return_gvalue_flat_array()
1297 self.assertEqual(values, [42, '42', True])
1299 def test_gvalue_gobject_ref_counts(self):
1300 # Tests a GObject held by a GValue
1301 obj = GObject.Object()
1302 ref = weakref.ref(obj)
1303 grefcount = obj.__grefcount__
1305 value = GObject.Value()
1306 value.init(GObject.TYPE_OBJECT)
1308 # TYPE_OBJECT will inc ref count as it should
1309 value.set_object(obj)
1310 self.assertEqual(obj.__grefcount__, grefcount + 1)
1312 # multiple set_object should not inc ref count
1313 value.set_object(obj)
1314 self.assertEqual(obj.__grefcount__, grefcount + 1)
1316 # get_object will re-use the same wrapper as obj
1317 res = value.get_object()
1318 self.assertEqual(obj, res)
1319 self.assertEqual(obj.__grefcount__, grefcount + 1)
1321 # multiple get_object should not inc ref count
1322 res = value.get_object()
1323 self.assertEqual(obj.__grefcount__, grefcount + 1)
1325 # deletion of the result and value holder should bring the
1326 # refcount back to where we started
1330 self.assertEqual(obj.__grefcount__, grefcount)
1334 self.assertEqual(ref(), None)
1336 def test_gvalue_boxed_ref_counts(self):
1337 # Tests a boxed type wrapping a python object pointer (TYPE_PYOBJECT)
1343 ref = weakref.ref(obj)
1344 refcount = sys.getrefcount(obj)
1346 value = GObject.Value()
1347 value.init(GObject.TYPE_PYOBJECT)
1349 # boxed TYPE_PYOBJECT will inc ref count as it should
1350 value.set_boxed(obj)
1351 self.assertEqual(sys.getrefcount(obj), refcount + 1)
1353 # multiple set_boxed should not inc ref count
1354 value.set_boxed(obj)
1355 self.assertEqual(sys.getrefcount(obj), refcount + 1)
1357 res = value.get_boxed()
1358 self.assertEqual(obj, res)
1359 self.assertEqual(sys.getrefcount(obj), refcount + 2)
1361 # multiple get_boxed should not inc ref count
1362 res = value.get_boxed()
1363 self.assertEqual(sys.getrefcount(obj), refcount + 2)
1365 # deletion of the result and value holder should bring the
1366 # refcount back to where we started
1370 self.assertEqual(sys.getrefcount(obj), refcount)
1374 self.assertEqual(ref(), None)
1377 def disabled_test_gvalue_flat_array_round_trip(self):
1378 self.assertEqual([42, '42', True],
1379 GIMarshallingTests.gvalue_flat_array_round_trip(42, '42', True))
1382 class TestGClosure(unittest.TestCase):
1385 GIMarshallingTests.gclosure_in(lambda: 42)
1387 def test_pass(self):
1388 # test passing a closure between two C calls
1389 closure = GIMarshallingTests.gclosure_return()
1390 GIMarshallingTests.gclosure_in(closure)
1392 def test_type_error(self):
1393 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, 42)
1394 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, None)
1397 class TestCallbacks(unittest.TestCase):
1398 def test_return_value_only(self):
1401 self.assertEqual(GIMarshallingTests.callback_return_value_only(cb), 5)
1403 def test_one_out_arg(self):
1406 self.assertAlmostEqual(GIMarshallingTests.callback_one_out_parameter(cb), 5.5)
1408 def test_multiple_out_args(self):
1411 res = GIMarshallingTests.callback_multiple_out_parameters(cb)
1412 self.assertAlmostEqual(res[0], 5.5)
1413 self.assertAlmostEqual(res[1], 42.0)
1415 def test_return_and_one_out_arg(self):
1418 res = GIMarshallingTests.callback_return_value_and_one_out_parameter(cb)
1419 self.assertEqual(res[0], 5)
1420 self.assertAlmostEqual(res[1], 42.0)
1422 def test_return_and_multiple_out_arg(self):
1424 return (5, 42, -1000)
1425 self.assertEqual(GIMarshallingTests.callback_return_value_and_multiple_out_parameters(cb),
1429 class TestPointer(unittest.TestCase):
1430 def test_pointer_in_return(self):
1431 self.assertEqual(GIMarshallingTests.pointer_in_return(42), 42)
1434 class TestEnum(unittest.TestCase):
1437 def setUpClass(cls):
1438 '''Run tests under a test locale.
1440 Upper case conversion of member names should not be locale specific
1441 e. g. in Turkish, "i".upper() == "i", which gives results like "iNVALiD"
1443 Run test under a locale which defines toupper('a') == 'a'
1445 cls.locale_dir = tempfile.mkdtemp()
1446 src = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'te_ST@nouppera')
1447 dest = os.path.join(cls.locale_dir, 'te_ST.UTF-8@nouppera')
1448 subprocess.check_call(['localedef', '-i', src, '-c', '-f', 'UTF-8', dest])
1449 os.environ['LOCPATH'] = cls.locale_dir
1450 locale.setlocale(locale.LC_ALL, 'te_ST.UTF-8@nouppera')
1453 def tearDownClass(cls):
1454 locale.setlocale(locale.LC_ALL, 'C')
1455 shutil.rmtree(cls.locale_dir)
1457 del os.environ['LOCPATH']
1461 def test_enum(self):
1462 self.assertTrue(issubclass(GIMarshallingTests.Enum, int))
1463 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE1, GIMarshallingTests.Enum))
1464 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE2, GIMarshallingTests.Enum))
1465 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE3, GIMarshallingTests.Enum))
1466 self.assertEqual(42, GIMarshallingTests.Enum.VALUE3)
1468 def test_value_nick_and_name(self):
1469 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_nick, 'value1')
1470 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_nick, 'value2')
1471 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_nick, 'value3')
1473 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE1')
1474 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE2')
1475 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE3')
1477 def test_enum_in(self):
1478 GIMarshallingTests.enum_in(GIMarshallingTests.Enum.VALUE3)
1479 GIMarshallingTests.enum_in(42)
1481 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 43)
1482 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 'GIMarshallingTests.Enum.VALUE3')
1484 def test_enum_return(self):
1485 enum = GIMarshallingTests.enum_returnv()
1486 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1487 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1489 def test_enum_out(self):
1490 enum = GIMarshallingTests.enum_out()
1491 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1492 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1494 def test_enum_inout(self):
1495 enum = GIMarshallingTests.enum_inout(GIMarshallingTests.Enum.VALUE3)
1496 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1497 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE1)
1499 def test_enum_second(self):
1500 # check for the bug where different non-gtype enums share the same class
1501 self.assertNotEqual(GIMarshallingTests.Enum, GIMarshallingTests.SecondEnum)
1503 # check that values are not being shared between different enums
1504 self.assertTrue(hasattr(GIMarshallingTests.SecondEnum, "SECONDVALUE1"))
1505 self.assertRaises(AttributeError, getattr, GIMarshallingTests.Enum, "SECONDVALUE1")
1506 self.assertTrue(hasattr(GIMarshallingTests.Enum, "VALUE1"))
1507 self.assertRaises(AttributeError, getattr, GIMarshallingTests.SecondEnum, "VALUE1")
1509 def test_enum_gtype_name_is_namespaced(self):
1510 self.assertEqual(GIMarshallingTests.Enum.__gtype__.name,
1511 'PyGIMarshallingTestsEnum')
1513 def test_enum_add_type_error(self):
1514 self.assertRaises(TypeError,
1516 GIMarshallingTests.NoTypeFlags.__gtype__)
1519 class TestEnumVFuncResults(unittest.TestCase):
1520 class EnumTester(GIMarshallingTests.Object):
1521 def do_vfunc_return_enum(self):
1522 return GIMarshallingTests.Enum.VALUE2
1524 def do_vfunc_out_enum(self):
1525 return GIMarshallingTests.Enum.VALUE3
1527 def test_vfunc_return_enum(self):
1528 tester = self.EnumTester()
1529 self.assertEqual(tester.vfunc_return_enum(), GIMarshallingTests.Enum.VALUE2)
1531 def test_vfunc_out_enum(self):
1532 tester = self.EnumTester()
1533 self.assertEqual(tester.vfunc_out_enum(), GIMarshallingTests.Enum.VALUE3)
1536 class TestGEnum(unittest.TestCase):
1538 def test_genum(self):
1539 self.assertTrue(issubclass(GIMarshallingTests.GEnum, GObject.GEnum))
1540 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE1, GIMarshallingTests.GEnum))
1541 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE2, GIMarshallingTests.GEnum))
1542 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE3, GIMarshallingTests.GEnum))
1543 self.assertEqual(42, GIMarshallingTests.GEnum.VALUE3)
1545 def test_value_nick_and_name(self):
1546 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_nick, 'value1')
1547 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_nick, 'value2')
1548 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_nick, 'value3')
1550 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE1')
1551 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE2')
1552 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE3')
1554 def test_genum_in(self):
1555 GIMarshallingTests.genum_in(GIMarshallingTests.GEnum.VALUE3)
1556 GIMarshallingTests.genum_in(42)
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 genum = GIMarshallingTests.GEnum.out()
1570 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1571 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1573 def test_genum_inout(self):
1574 genum = GIMarshallingTests.genum_inout(GIMarshallingTests.GEnum.VALUE3)
1575 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1576 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE1)
1579 class TestGFlags(unittest.TestCase):
1581 def test_flags(self):
1582 self.assertTrue(issubclass(GIMarshallingTests.Flags, GObject.GFlags))
1583 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1, GIMarshallingTests.Flags))
1584 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE2, GIMarshallingTests.Flags))
1585 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE3, GIMarshallingTests.Flags))
1586 # __or__() operation should still return an instance, not an int.
1587 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1 | GIMarshallingTests.Flags.VALUE2,
1588 GIMarshallingTests.Flags))
1589 self.assertEqual(1 << 1, GIMarshallingTests.Flags.VALUE2)
1591 def test_value_nick_and_name(self):
1592 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_nick, 'value1')
1593 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_nick, 'value2')
1594 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_nick, 'value3')
1596 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE1')
1597 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE2')
1598 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE3')
1600 def test_flags_in(self):
1601 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2)
1602 GIMarshallingTests.Flags.in_(GIMarshallingTests.Flags.VALUE2)
1603 # result of __or__() operation should still be valid instance, not an int.
1604 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE2)
1605 GIMarshallingTests.flags_in_zero(Number(0))
1606 GIMarshallingTests.Flags.in_zero(Number(0))
1608 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 1 << 1)
1609 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 'GIMarshallingTests.Flags.VALUE2')
1611 def test_flags_return(self):
1612 flags = GIMarshallingTests.flags_returnv()
1613 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1614 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1616 def test_flags_return_method(self):
1617 flags = GIMarshallingTests.Flags.returnv()
1618 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1619 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1621 def test_flags_out(self):
1622 flags = GIMarshallingTests.flags_out()
1623 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1624 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1626 def test_flags_inout(self):
1627 flags = GIMarshallingTests.flags_inout(GIMarshallingTests.Flags.VALUE2)
1628 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1629 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE1)
1632 class TestNoTypeFlags(unittest.TestCase):
1634 def test_flags(self):
1635 self.assertTrue(issubclass(GIMarshallingTests.NoTypeFlags, GObject.GFlags))
1636 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1, GIMarshallingTests.NoTypeFlags))
1637 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE2, GIMarshallingTests.NoTypeFlags))
1638 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE3, GIMarshallingTests.NoTypeFlags))
1639 # __or__() operation should still return an instance, not an int.
1640 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1 | GIMarshallingTests.NoTypeFlags.VALUE2,
1641 GIMarshallingTests.NoTypeFlags))
1642 self.assertEqual(1 << 1, GIMarshallingTests.NoTypeFlags.VALUE2)
1644 def test_value_nick_and_name(self):
1645 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_nick, 'value1')
1646 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_nick, 'value2')
1647 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_nick, 'value3')
1649 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE1')
1650 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2')
1651 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE3')
1653 def test_flags_in(self):
1654 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2)
1655 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2 | GIMarshallingTests.NoTypeFlags.VALUE2)
1656 GIMarshallingTests.no_type_flags_in_zero(Number(0))
1658 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 1 << 1)
1659 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 'GIMarshallingTests.NoTypeFlags.VALUE2')
1661 def test_flags_return(self):
1662 flags = GIMarshallingTests.no_type_flags_returnv()
1663 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1664 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1666 def test_flags_out(self):
1667 flags = GIMarshallingTests.no_type_flags_out()
1668 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1669 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1671 def test_flags_inout(self):
1672 flags = GIMarshallingTests.no_type_flags_inout(GIMarshallingTests.NoTypeFlags.VALUE2)
1673 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1674 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE1)
1676 def test_flags_gtype_name_is_namespaced(self):
1677 self.assertEqual(GIMarshallingTests.NoTypeFlags.__gtype__.name,
1678 'PyGIMarshallingTestsNoTypeFlags')
1681 class TestStructure(unittest.TestCase):
1683 def test_simple_struct(self):
1684 self.assertTrue(issubclass(GIMarshallingTests.SimpleStruct, GObject.GPointer))
1686 struct = GIMarshallingTests.SimpleStruct()
1687 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1689 self.assertEqual(0, struct.long_)
1690 self.assertEqual(0, struct.int8)
1695 self.assertEqual(6, struct.long_)
1696 self.assertEqual(7, struct.int8)
1700 def test_nested_struct(self):
1701 struct = GIMarshallingTests.NestedStruct()
1703 self.assertTrue(isinstance(struct.simple_struct, GIMarshallingTests.SimpleStruct))
1705 struct.simple_struct.long_ = 42
1706 self.assertEqual(42, struct.simple_struct.long_)
1710 def test_not_simple_struct(self):
1711 struct = GIMarshallingTests.NotSimpleStruct()
1712 self.assertEqual(None, struct.pointer)
1714 def test_simple_struct_return(self):
1715 struct = GIMarshallingTests.simple_struct_returnv()
1717 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1718 self.assertEqual(6, struct.long_)
1719 self.assertEqual(7, struct.int8)
1723 def test_simple_struct_in(self):
1724 struct = GIMarshallingTests.SimpleStruct()
1728 GIMarshallingTests.SimpleStruct.inv(struct)
1732 struct = GIMarshallingTests.NestedStruct()
1734 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, struct)
1738 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, None)
1740 def test_simple_struct_method(self):
1741 struct = GIMarshallingTests.SimpleStruct()
1749 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.method)
1751 def test_pointer_struct(self):
1752 self.assertTrue(issubclass(GIMarshallingTests.PointerStruct, GObject.GPointer))
1754 struct = GIMarshallingTests.PointerStruct()
1755 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1759 def test_pointer_struct_return(self):
1760 struct = GIMarshallingTests.pointer_struct_returnv()
1762 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1763 self.assertEqual(42, struct.long_)
1767 def test_pointer_struct_in(self):
1768 struct = GIMarshallingTests.PointerStruct()
1775 def test_boxed_struct(self):
1776 self.assertTrue(issubclass(GIMarshallingTests.BoxedStruct, GObject.GBoxed))
1778 struct = GIMarshallingTests.BoxedStruct()
1779 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1781 self.assertEqual(0, struct.long_)
1782 self.assertEqual(None, struct.string_)
1783 self.assertEqual([], struct.g_strv)
1787 def test_boxed_struct_new(self):
1788 struct = GIMarshallingTests.BoxedStruct.new()
1789 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1790 self.assertEqual(struct.long_, 0)
1791 self.assertEqual(struct.string_, None)
1795 def test_boxed_struct_copy(self):
1796 struct = GIMarshallingTests.BoxedStruct()
1798 struct.string_ = 'hello'
1800 new_struct = struct.copy()
1801 self.assertTrue(isinstance(new_struct, GIMarshallingTests.BoxedStruct))
1802 self.assertEqual(new_struct.long_, 42)
1803 self.assertEqual(new_struct.string_, 'hello')
1808 def test_boxed_struct_return(self):
1809 struct = GIMarshallingTests.boxed_struct_returnv()
1811 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1812 self.assertEqual(42, struct.long_)
1813 self.assertEqual('hello', struct.string_)
1814 self.assertEqual(['0', '1', '2'], struct.g_strv)
1818 def test_boxed_struct_in(self):
1819 struct = GIMarshallingTests.BoxedStruct()
1826 def test_boxed_struct_out(self):
1827 struct = GIMarshallingTests.boxed_struct_out()
1829 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1830 self.assertEqual(42, struct.long_)
1834 def test_boxed_struct_inout(self):
1835 in_struct = GIMarshallingTests.BoxedStruct()
1836 in_struct.long_ = 42
1838 out_struct = GIMarshallingTests.boxed_struct_inout(in_struct)
1840 self.assertTrue(isinstance(out_struct, GIMarshallingTests.BoxedStruct))
1841 self.assertEqual(0, out_struct.long_)
1846 def test_struct_field_assignment(self):
1847 struct = GIMarshallingTests.BoxedStruct()
1850 struct.string_ = 'hello'
1851 self.assertEqual(struct.long_, 42)
1852 self.assertEqual(struct.string_, 'hello')
1854 def test_union(self):
1855 union = GIMarshallingTests.Union()
1857 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1859 new_union = union.copy()
1860 self.assertTrue(isinstance(new_union, GIMarshallingTests.Union))
1865 def test_union_return(self):
1866 union = GIMarshallingTests.union_returnv()
1868 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1869 self.assertEqual(42, union.long_)
1873 def test_union_in(self):
1874 union = GIMarshallingTests.Union()
1881 def test_union_method(self):
1882 union = GIMarshallingTests.Union()
1889 self.assertRaises(TypeError, GIMarshallingTests.Union.method)
1892 class TestGObject(unittest.TestCase):
1894 def test_object(self):
1895 self.assertTrue(issubclass(GIMarshallingTests.Object, GObject.GObject))
1897 object_ = GIMarshallingTests.Object()
1898 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1899 self.assertEqual(object_.__grefcount__, 1)
1901 def test_object_new(self):
1902 object_ = GIMarshallingTests.Object.new(42)
1903 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1904 self.assertEqual(object_.__grefcount__, 1)
1906 def test_object_int(self):
1907 object_ = GIMarshallingTests.Object(int=42)
1908 self.assertEqual(object_.int_, 42)
1909 # FIXME: Don't work yet.
1911 # self.assertEqual(object_.int_, 0)
1913 def test_object_static_method(self):
1914 GIMarshallingTests.Object.static_method()
1916 def test_object_method(self):
1917 GIMarshallingTests.Object(int=42).method()
1918 self.assertRaises(TypeError, GIMarshallingTests.Object.method, GObject.GObject())
1919 self.assertRaises(TypeError, GIMarshallingTests.Object.method)
1921 def test_sub_object(self):
1922 self.assertTrue(issubclass(GIMarshallingTests.SubObject, GIMarshallingTests.Object))
1924 object_ = GIMarshallingTests.SubObject()
1925 self.assertTrue(isinstance(object_, GIMarshallingTests.SubObject))
1927 def test_sub_object_new(self):
1928 self.assertRaises(TypeError, GIMarshallingTests.SubObject.new, 42)
1930 def test_sub_object_static_method(self):
1931 object_ = GIMarshallingTests.SubObject()
1932 object_.static_method()
1934 def test_sub_object_method(self):
1935 object_ = GIMarshallingTests.SubObject(int=42)
1938 def test_sub_object_sub_method(self):
1939 object_ = GIMarshallingTests.SubObject()
1940 object_.sub_method()
1942 def test_sub_object_overwritten_method(self):
1943 object_ = GIMarshallingTests.SubObject()
1944 object_.overwritten_method()
1946 self.assertRaises(TypeError, GIMarshallingTests.SubObject.overwritten_method, GIMarshallingTests.Object())
1948 def test_sub_object_int(self):
1949 object_ = GIMarshallingTests.SubObject()
1950 self.assertEqual(object_.int_, 0)
1951 # FIXME: Don't work yet.
1953 # self.assertEqual(object_.int_, 42)
1955 def test_object_none_return(self):
1956 object_ = GIMarshallingTests.Object.none_return()
1957 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1958 self.assertEqual(object_.__grefcount__, 2)
1960 def test_object_full_return(self):
1961 object_ = GIMarshallingTests.Object.full_return()
1962 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1963 self.assertEqual(object_.__grefcount__, 1)
1965 def test_object_none_in(self):
1966 object_ = GIMarshallingTests.Object(int=42)
1967 GIMarshallingTests.Object.none_in(object_)
1968 self.assertEqual(object_.__grefcount__, 1)
1970 object_ = GIMarshallingTests.SubObject(int=42)
1971 GIMarshallingTests.Object.none_in(object_)
1973 object_ = GObject.GObject()
1974 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, object_)
1976 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, None)
1978 def test_object_none_out(self):
1979 object_ = GIMarshallingTests.Object.none_out()
1980 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1981 self.assertEqual(object_.__grefcount__, 2)
1983 new_object = GIMarshallingTests.Object.none_out()
1984 self.assertTrue(new_object is object_)
1986 def test_object_full_out(self):
1987 object_ = GIMarshallingTests.Object.full_out()
1988 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1989 self.assertEqual(object_.__grefcount__, 1)
1991 def test_object_none_inout(self):
1992 object_ = GIMarshallingTests.Object(int=42)
1993 new_object = GIMarshallingTests.Object.none_inout(object_)
1995 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1997 self.assertFalse(object_ is new_object)
1999 self.assertEqual(object_.__grefcount__, 1)
2000 self.assertEqual(new_object.__grefcount__, 2)
2002 new_new_object = GIMarshallingTests.Object.none_inout(object_)
2003 self.assertTrue(new_new_object is new_object)
2005 GIMarshallingTests.Object.none_inout(GIMarshallingTests.SubObject(int=42))
2007 @unittest.skip('https://bugzilla.gnome.org/show_bug.cgi?id=709796')
2008 def test_object_full_inout(self):
2009 # Using gimarshallingtests.c from GI versions > 1.38.0 will show this
2010 # test as an "unexpected success" due to reference leak fixes in that file.
2011 # TODO: remove the expectedFailure once PyGI relies on GI > 1.38.0.
2012 object_ = GIMarshallingTests.Object(int=42)
2013 new_object = GIMarshallingTests.Object.full_inout(object_)
2015 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
2017 self.assertFalse(object_ is new_object)
2019 self.assertEqual(object_.__grefcount__, 1)
2020 self.assertEqual(new_object.__grefcount__, 1)
2022 # FIXME: Doesn't actually return the same object.
2023 # def test_object_inout_same(self):
2024 # object_ = GIMarshallingTests.Object()
2025 # new_object = GIMarshallingTests.object_full_inout(object_)
2026 # self.assertTrue(object_ is new_object)
2027 # self.assertEqual(object_.__grefcount__, 1)
2030 class TestPythonGObject(unittest.TestCase):
2032 class Object(GIMarshallingTests.Object):
2033 return_for_caller_allocated_out_parameter = 'test caller alloc return'
2035 def __init__(self, int):
2036 GIMarshallingTests.Object.__init__(self)
2040 # Don't call super, which asserts that self.int == 42.
2043 def do_method_int8_in(self, int8):
2046 def do_method_int8_out(self):
2049 def do_method_int8_arg_and_out_caller(self, arg):
2052 def do_method_int8_arg_and_out_callee(self, arg):
2055 def do_method_str_arg_out_ret(self, arg):
2056 return (arg.upper(), len(arg))
2058 def do_method_with_default_implementation(self, int8):
2059 GIMarshallingTests.Object.do_method_with_default_implementation(self, int8)
2060 self.props.int += int8
2062 def do_vfunc_return_value_only(self):
2065 def do_vfunc_one_out_parameter(self):
2068 def do_vfunc_multiple_out_parameters(self):
2069 return (42.42, 3.14)
2071 def do_vfunc_return_value_and_one_out_parameter(self):
2074 def do_vfunc_return_value_and_multiple_out_parameters(self):
2077 def do_vfunc_caller_allocated_out_parameter(self):
2078 return self.return_for_caller_allocated_out_parameter
2080 class SubObject(GIMarshallingTests.SubObject):
2081 def __init__(self, int):
2082 GIMarshallingTests.SubObject.__init__(self)
2085 def do_method_with_default_implementation(self, int8):
2088 def do_vfunc_return_value_only(self):
2091 class Interface3Impl(GObject.Object, GIMarshallingTests.Interface3):
2093 GObject.Object.__init__(self)
2094 self.variants = None
2095 self.n_variants = None
2097 def do_test_variant_array_in(self, variants, n_variants):
2098 self.variants = variants
2099 self.n_variants = n_variants
2101 class ErrorObject(GIMarshallingTests.Object):
2102 def do_vfunc_return_value_only(self):
2103 raise ValueError('Return value should be 0')
2105 def test_object(self):
2106 self.assertTrue(issubclass(self.Object, GIMarshallingTests.Object))
2108 object_ = self.Object(int=42)
2109 self.assertTrue(isinstance(object_, self.Object))
2111 def test_object_method(self):
2112 self.Object(int=0).method()
2114 def test_object_vfuncs(self):
2115 object_ = self.Object(int=42)
2116 object_.method_int8_in(84)
2117 self.assertEqual(object_.val, 84)
2118 self.assertEqual(object_.method_int8_out(), 42)
2120 # can be dropped when bumping g-i dependencies to >= 1.35.2
2121 if hasattr(object_, 'method_int8_arg_and_out_caller'):
2122 self.assertEqual(object_.method_int8_arg_and_out_caller(42), 43)
2123 self.assertEqual(object_.method_int8_arg_and_out_callee(42), 43)
2124 self.assertEqual(object_.method_str_arg_out_ret('hello'), ('HELLO', 5))
2126 object_.method_with_default_implementation(42)
2127 self.assertEqual(object_.props.int, 84)
2129 self.assertEqual(object_.vfunc_return_value_only(), 4242)
2130 self.assertAlmostEqual(object_.vfunc_one_out_parameter(), 42.42, places=5)
2132 (a, b) = object_.vfunc_multiple_out_parameters()
2133 self.assertAlmostEqual(a, 42.42, places=5)
2134 self.assertAlmostEqual(b, 3.14, places=5)
2136 self.assertEqual(object_.vfunc_return_value_and_one_out_parameter(), (5, 42))
2137 self.assertEqual(object_.vfunc_return_value_and_multiple_out_parameters(), (5, 42, 99))
2139 self.assertEqual(object_.vfunc_caller_allocated_out_parameter(),
2140 object_.return_for_caller_allocated_out_parameter)
2142 class ObjectWithoutVFunc(GIMarshallingTests.Object):
2143 def __init__(self, int):
2144 GIMarshallingTests.Object.__init__(self)
2146 object_ = ObjectWithoutVFunc(int=42)
2147 object_.method_with_default_implementation(84)
2148 self.assertEqual(object_.props.int, 84)
2150 def test_vfunc_return_ref_count(self):
2151 obj = self.Object(int=42)
2152 ref_count = sys.getrefcount(obj.return_for_caller_allocated_out_parameter)
2153 ret = obj.vfunc_caller_allocated_out_parameter()
2156 # Make sure the return and what the vfunc returned
2157 # are equal but not the same object.
2158 self.assertEqual(ret, obj.return_for_caller_allocated_out_parameter)
2159 self.assertFalse(ret is obj.return_for_caller_allocated_out_parameter)
2160 self.assertEqual(sys.getrefcount(obj.return_for_caller_allocated_out_parameter),
2163 def test_subobject_parent_vfunc(self):
2164 object_ = self.SubObject(int=81)
2165 object_.method_with_default_implementation(87)
2166 self.assertEqual(object_.val, 87)
2168 def test_subobject_child_vfunc(self):
2169 object_ = self.SubObject(int=1)
2170 self.assertEqual(object_.vfunc_return_value_only(), 2121)
2172 def test_dynamic_module(self):
2173 from gi.module import DynamicModule
2174 self.assertTrue(isinstance(GObject, DynamicModule))
2176 def test_subobject_non_vfunc_do_method(self):
2177 class PythonObjectWithNonVFuncDoMethod(object):
2178 def do_not_a_vfunc(self):
2181 class ObjectOverrideNonVFuncDoMethod(GIMarshallingTests.Object, PythonObjectWithNonVFuncDoMethod):
2182 def do_not_a_vfunc(self):
2183 value = super(ObjectOverrideNonVFuncDoMethod, self).do_not_a_vfunc()
2186 object_ = ObjectOverrideNonVFuncDoMethod()
2187 self.assertEqual(18, object_.do_not_a_vfunc())
2189 def test_native_function_not_set_in_subclass_dict(self):
2190 # Previously, GI was setting virtual functions on the class as well
2191 # as any *native* class that subclasses it. Here we check that it is only
2192 # set on the class that the method is originally from.
2193 self.assertTrue('do_method_with_default_implementation' in GIMarshallingTests.Object.__dict__)
2194 self.assertTrue('do_method_with_default_implementation' not in GIMarshallingTests.SubObject.__dict__)
2196 def test_subobject_with_interface_and_non_vfunc_do_method(self):
2197 # There was a bug for searching for vfuncs in interfaces. It was
2198 # triggered by having a do_* method that wasn't overriding
2199 # a native vfunc, as well as inheriting from an interface.
2200 class GObjectSubclassWithInterface(GObject.GObject, GIMarshallingTests.Interface):
2201 def do_method_not_a_vfunc(self):
2204 def test_subsubobject(self):
2205 class SubSubSubObject(GIMarshallingTests.SubSubObject):
2206 def do_method_deep_hierarchy(self, num):
2207 self.props.int = num * 2
2209 sub_sub_sub_object = SubSubSubObject()
2210 GIMarshallingTests.SubSubObject.do_method_deep_hierarchy(sub_sub_sub_object, 5)
2211 self.assertEqual(sub_sub_sub_object.props.int, 5)
2213 def test_interface3impl(self):
2214 iface3 = self.Interface3Impl()
2215 variants = [GLib.Variant('i', 27), GLib.Variant('s', 'Hello')]
2216 iface3.test_variant_array_in(variants)
2217 self.assertEqual(iface3.n_variants, 2)
2218 self.assertEqual(iface3.variants[0].unpack(), 27)
2219 self.assertEqual(iface3.variants[1].unpack(), 'Hello')
2221 def test_python_subsubobject_vfunc(self):
2222 class PySubObject(GIMarshallingTests.Object):
2224 GIMarshallingTests.Object.__init__(self)
2225 self.sub_method_int8_called = 0
2227 def do_method_int8_in(self, int8):
2228 self.sub_method_int8_called += 1
2230 class PySubSubObject(PySubObject):
2232 PySubObject.__init__(self)
2233 self.subsub_method_int8_called = 0
2235 def do_method_int8_in(self, int8):
2236 self.subsub_method_int8_called += 1
2239 so.method_int8_in(1)
2240 self.assertEqual(so.sub_method_int8_called, 1)
2242 # it should call the method on the SubSub object only
2243 sso = PySubSubObject()
2244 sso.method_int8_in(1)
2245 self.assertEqual(sso.subsub_method_int8_called, 1)
2246 self.assertEqual(sso.sub_method_int8_called, 0)
2248 def test_callback_in_vfunc(self):
2249 class SubObject(GIMarshallingTests.Object):
2251 GObject.GObject.__init__(self)
2254 def do_vfunc_with_callback(self, callback):
2255 self.worked = callback(42) == 42
2257 _object = SubObject()
2258 _object.call_vfunc_with_callback()
2259 self.assertTrue(_object.worked)
2260 _object.worked = False
2261 _object.call_vfunc_with_callback()
2262 self.assertTrue(_object.worked)
2264 def test_exception_in_vfunc_return_value(self):
2265 obj = self.ErrorObject()
2266 self.assertEqual(obj.vfunc_return_value_only(), 0)
2268 @unittest.skipUnless(hasattr(GIMarshallingTests, 'callback_owned_boxed'),
2269 'requires newer version of GI')
2270 @unittest.expectedFailure # bug 722899
2271 def test_callback_owned_box(self):
2272 def callback(box, data):
2275 def nop_callback(box, data):
2278 GIMarshallingTests.callback_owned_boxed(callback, None)
2279 GIMarshallingTests.callback_owned_boxed(nop_callback, None)
2280 self.assertEqual(self.box.long_, 1)
2283 class TestMultiOutputArgs(unittest.TestCase):
2285 def test_int_out_out(self):
2286 self.assertEqual((6, 7), GIMarshallingTests.int_out_out())
2288 def test_int_return_out(self):
2289 self.assertEqual((6, 7), GIMarshallingTests.int_return_out())
2294 class TestInterfaces(unittest.TestCase):
2296 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2298 GObject.GObject.__init__(self)
2301 def do_test_int8_in(self, int8):
2305 self.instance = self.TestInterfaceImpl()
2307 def test_wrapper(self):
2308 self.assertTrue(issubclass(GIMarshallingTests.Interface, GObject.GInterface))
2309 self.assertEqual(GIMarshallingTests.Interface.__gtype__.name, 'GIMarshallingTestsInterface')
2310 self.assertRaises(NotImplementedError, GIMarshallingTests.Interface)
2312 def test_implementation(self):
2313 self.assertTrue(issubclass(self.TestInterfaceImpl, GIMarshallingTests.Interface))
2314 self.assertTrue(isinstance(self.instance, GIMarshallingTests.Interface))
2316 def test_int8_int(self):
2317 GIMarshallingTests.test_interface_test_int8_in(self.instance, 42)
2318 self.assertEqual(self.instance.val, 42)
2320 def test_subclass(self):
2321 class TestInterfaceImplA(self.TestInterfaceImpl):
2324 class TestInterfaceImplB(TestInterfaceImplA):
2327 instance = TestInterfaceImplA()
2328 GIMarshallingTests.test_interface_test_int8_in(instance, 42)
2329 self.assertEqual(instance.val, 42)
2331 def test_subclass_override(self):
2332 class TestInterfaceImplD(TestInterfaces.TestInterfaceImpl):
2335 def do_test_int8_in(self, int8):
2338 instance = TestInterfaceImplD()
2339 self.assertEqual(instance.val, None)
2340 self.assertEqual(instance.val2, None)
2342 GIMarshallingTests.test_interface_test_int8_in(instance, 42)
2343 self.assertEqual(instance.val, None)
2344 self.assertEqual(instance.val2, 42)
2346 def test_type_mismatch(self):
2347 obj = GIMarshallingTests.Object()
2349 # wrong type for first argument: interface
2350 enum = Gio.File.new_for_path('.').enumerate_children(
2351 '', Gio.FileQueryInfoFlags.NONE, None)
2354 self.fail('call with wrong type argument unexpectedly succeeded')
2355 except TypeError as e:
2356 # should have argument name
2357 self.assertTrue('cancellable' in str(e), e)
2358 # should have expected type
2359 self.assertTrue('xpected Gio.Cancellable' in str(e), e)
2360 # should have actual type
2361 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2363 # wrong type for self argument: interface
2365 Gio.FileEnumerator.next_file(obj, None)
2366 self.fail('call with wrong type argument unexpectedly succeeded')
2367 except TypeError as e:
2368 # should have argument name
2369 self.assertTrue('self' in str(e), e)
2370 # should have expected type
2371 self.assertTrue('xpected Gio.FileEnumerator' in str(e), e)
2372 # should have actual type
2373 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2375 # wrong type for first argument: GObject
2376 var = GLib.Variant('s', 'mystring')
2377 action = Gio.SimpleAction.new('foo', var.get_type())
2379 action.activate(obj)
2380 self.fail('call with wrong type argument unexpectedly succeeded')
2381 except TypeError as e:
2382 # should have argument name
2383 self.assertTrue('parameter' in str(e), e)
2384 # should have expected type
2385 self.assertTrue('xpected GLib.Variant' in str(e), e)
2386 # should have actual type
2387 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2389 # wrong type for self argument: GObject
2391 Gio.SimpleAction.activate(obj, obj)
2392 self.fail('call with wrong type argument unexpectedly succeeded')
2393 except TypeError as e:
2394 # should have argument name
2395 self.assertTrue('self' in str(e), e)
2396 # should have expected type
2397 self.assertTrue('xpected Gio.Action' in str(e), e)
2398 # should have actual type
2399 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2402 class TestMRO(unittest.TestCase):
2404 # check that our own MRO calculation matches what we would expect
2405 # from Python's own C3 calculations
2418 class E(D, GIMarshallingTests.Object):
2421 expected = (E, D, B, C, A, GIMarshallingTests.Object,
2422 GObject.Object, GObject.Object.__base__, gi._gi._gobject.GObject,
2424 self.assertEqual(expected, E.__mro__)
2426 def test_interface_collision(self):
2427 # there was a problem with Python bailing out because of
2428 # http://en.wikipedia.org/wiki/Diamond_problem with interfaces,
2429 # which shouldn't really be a problem.
2431 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2434 class TestInterfaceImpl2(GIMarshallingTests.Interface,
2438 class TestInterfaceImpl3(TestInterfaceImpl,
2439 GIMarshallingTests.Interface2):
2442 def test_old_style_mixin(self):
2443 # Note: Old style classes don't exist in Python 3
2447 with warnings.catch_warnings(record=True) as warn:
2448 warnings.simplefilter('always')
2450 # Dynamically create a new gi based class with an old
2452 type('GIWithOldStyleMixin', (GIMarshallingTests.Object, Mixin), {})
2454 if sys.version_info < (3, 0):
2455 self.assertTrue(issubclass(warn[0].category, RuntimeWarning))
2457 self.assertEqual(len(warn), 0)
2460 class TestInterfaceClash(unittest.TestCase):
2462 def test_clash(self):
2464 class TestClash(GObject.GObject, GIMarshallingTests.Interface, GIMarshallingTests.Interface2):
2465 def do_test_int8_in(self, int8):
2469 self.assertRaises(TypeError, create_clash)
2472 class TestOverrides(unittest.TestCase):
2474 def test_constant(self):
2475 self.assertEqual(GIMarshallingTests.OVERRIDES_CONSTANT, 7)
2477 def test_struct(self):
2478 # Test that the constructor has been overridden.
2479 struct = GIMarshallingTests.OverridesStruct(42)
2481 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2483 # Test that the method has been overridden.
2484 self.assertEqual(6, struct.method())
2488 # Test that the overrides wrapper has been registered.
2489 struct = GIMarshallingTests.overrides_struct_returnv()
2491 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2495 def test_object(self):
2496 # Test that the constructor has been overridden.
2497 object_ = GIMarshallingTests.OverridesObject(42)
2499 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2501 # Test that the alternate constructor has been overridden.
2502 object_ = GIMarshallingTests.OverridesObject.new(42)
2504 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2506 # Test that the method has been overridden.
2507 self.assertEqual(6, object_.method())
2509 # Test that the overrides wrapper has been registered.
2510 object_ = GIMarshallingTests.OverridesObject.returnv()
2512 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2514 def test_module_name(self):
2516 self.assertEqual(GIMarshallingTests.OverridesStruct.__module__, 'gi.overrides.GIMarshallingTests')
2517 self.assertEqual(GIMarshallingTests.OverridesObject.__module__, 'gi.overrides.GIMarshallingTests')
2518 self.assertEqual(GObject.Object.__module__, 'gi.overrides.GObject')
2521 self.assertEqual(GIMarshallingTests.SubObject.__module__, 'gi.repository.GIMarshallingTests')
2522 self.assertEqual(GObject.InitiallyUnowned.__module__, 'gi.repository.GObject')
2525 class TestDir(unittest.TestCase):
2526 def test_members_list(self):
2527 list = dir(GIMarshallingTests)
2528 self.assertTrue('OverridesStruct' in list)
2529 self.assertTrue('BoxedStruct' in list)
2530 self.assertTrue('OVERRIDES_CONSTANT' in list)
2531 self.assertTrue('GEnum' in list)
2532 self.assertTrue('int32_return_max' in list)
2534 def test_modules_list(self):
2535 import gi.repository
2536 list = dir(gi.repository)
2537 self.assertTrue('GIMarshallingTests' in list)
2539 # FIXME: test to see if a module which was not imported is in the list
2540 # we should be listing every typelib we find, not just the ones
2541 # which are imported
2543 # to test this I recommend we compile a fake module which
2544 # our tests would never import and check to see if it is
2547 # self.assertTrue('DoNotImportDummyTests' in list)
2550 class TestGError(unittest.TestCase):
2551 def test_array_in_crash(self):
2552 # Previously there was a bug in invoke, in which C arrays were unwrapped
2553 # from inside GArrays to be passed to the C function. But when a GError was
2554 # set, invoke would attempt to free the C array as if it were a GArray.
2555 # This crash is only for C arrays. It does not happen for C functions which
2556 # take in GArrays. See https://bugzilla.gnome.org/show_bug.cgi?id=642708
2557 self.assertRaises(GObject.GError, GIMarshallingTests.gerror_array_in, [1, 2, 3])
2560 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2561 error, debug = GIMarshallingTests.gerror_out()
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)
2567 self.assertEqual(debug, GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE)
2569 def test_out_transfer_none(self):
2570 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2571 error, debug = GIMarshallingTests.gerror_out_transfer_none()
2573 self.assertIsInstance(error, GObject.GError)
2574 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2575 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2576 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2577 self.assertEqual(GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE, debug)
2579 def test_return(self):
2580 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2581 error = GIMarshallingTests.gerror_return()
2583 self.assertIsInstance(error, GObject.GError)
2584 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2585 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2586 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2588 def test_exception(self):
2589 self.assertRaises(GObject.GError, GIMarshallingTests.gerror)
2591 GIMarshallingTests.gerror()
2593 etype, e = sys.exc_info()[:2]
2594 self.assertEqual(e.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2595 self.assertEqual(e.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2596 self.assertEqual(e.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2599 class TestParamSpec(unittest.TestCase):
2600 # https://bugzilla.gnome.org/show_bug.cgi?id=682355
2601 @unittest.expectedFailure
2602 def test_param_spec_in_bool(self):
2603 ps = GObject.param_spec_boolean('mybool', 'test-bool', 'boolblurb',
2604 True, GObject.ParamFlags.READABLE)
2605 GIMarshallingTests.param_spec_in_bool(ps)
2607 def test_param_spec_return(self):
2608 obj = GIMarshallingTests.param_spec_return()
2609 self.assertEqual(obj.name, 'test-param')
2610 self.assertEqual(obj.nick, 'test')
2611 self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2613 def test_param_spec_out(self):
2614 obj = GIMarshallingTests.param_spec_out()
2615 self.assertEqual(obj.name, 'test-param')
2616 self.assertEqual(obj.nick, 'test')
2617 self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2620 class TestKeywordArgs(unittest.TestCase):
2622 def test_calling(self):
2623 kw_func = GIMarshallingTests.int_three_in_three_out
2625 self.assertEqual(kw_func(1, 2, 3), (1, 2, 3))
2626 self.assertEqual(kw_func(**{'a': 4, 'b': 5, 'c': 6}), (4, 5, 6))
2627 self.assertEqual(kw_func(1, **{'b': 7, 'c': 8}), (1, 7, 8))
2628 self.assertEqual(kw_func(1, 7, **{'c': 8}), (1, 7, 8))
2629 self.assertEqual(kw_func(1, c=8, **{'b': 7}), (1, 7, 8))
2630 self.assertEqual(kw_func(2, c=4, b=3), (2, 3, 4))
2631 self.assertEqual(kw_func(a=2, c=4, b=3), (2, 3, 4))
2633 def assertRaisesMessage(self, exception, message, func, *args, **kwargs):
2635 func(*args, **kwargs)
2637 (e_type, e) = sys.exc_info()[:2]
2638 if message is not None:
2639 self.assertEqual(str(e), message)
2643 msg = "%s() did not raise %s" % (func.__name__, exception.__name__)
2644 raise AssertionError(msg)
2646 def test_type_errors(self):
2648 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2649 GIMarshallingTests.int_three_in_three_out)
2650 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (1 given)",
2651 GIMarshallingTests.int_three_in_three_out, 1)
2652 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2653 GIMarshallingTests.int_three_in_three_out, *())
2654 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2655 GIMarshallingTests.int_three_in_three_out, *(), **{})
2656 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (0 given)",
2657 GIMarshallingTests.int_three_in_three_out, *(), **{'c': 4})
2659 # test too many args
2660 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (4 given)",
2661 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4))
2662 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (4 given)",
2663 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4), c=6)
2665 # test too many keyword args
2666 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got multiple values for keyword argument 'a'",
2667 GIMarshallingTests.int_three_in_three_out, 1, 2, 3, **{'a': 4, 'b': 5})
2668 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'd'",
2669 GIMarshallingTests.int_three_in_three_out, d=4)
2670 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'e'",
2671 GIMarshallingTests.int_three_in_three_out, **{'e': 2})
2673 def test_kwargs_are_not_modified(self):
2676 GIMarshallingTests.int_three_in_three_out(1, c=4, **d)
2677 self.assertEqual(d, d2)
2679 @unittest.skipUnless(hasattr(GIMarshallingTests, 'int_one_in_utf8_two_in_one_allows_none'),
2680 'Requires newer GIMarshallingTests')
2681 def test_allow_none_as_default(self):
2682 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2, '3', '4')
2683 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2, '3')
2684 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2)
2685 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2, d='4')
2687 GIMarshallingTests.array_in_utf8_two_in_out_of_order('1', [-1, 0, 1, 2])
2688 GIMarshallingTests.array_in_utf8_two_in_out_of_order('1', [-1, 0, 1, 2], '2')
2689 self.assertRaises(TypeError,
2690 GIMarshallingTests.array_in_utf8_two_in_out_of_order,
2691 [-1, 0, 1, 2], a='1')
2692 self.assertRaises(TypeError,
2693 GIMarshallingTests.array_in_utf8_two_in_out_of_order,
2696 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2], '1', '2')
2697 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2], '1')
2698 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2])
2699 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2], b='2')
2701 GIMarshallingTests.int_one_in_utf8_two_in_one_allows_none(1, '2', '3')
2702 self.assertRaises(TypeError,
2703 GIMarshallingTests.int_one_in_utf8_two_in_one_allows_none,
2705 self.assertRaises(TypeError,
2706 GIMarshallingTests.int_one_in_utf8_two_in_one_allows_none,
2710 class TestPropertiesObject(unittest.TestCase):
2713 self.obj = GIMarshallingTests.PropertiesObject()
2715 def test_boolean(self):
2716 self.assertEqual(self.obj.props.some_boolean, False)
2717 self.obj.props.some_boolean = True
2718 self.assertEqual(self.obj.props.some_boolean, True)
2720 obj = GIMarshallingTests.PropertiesObject(some_boolean=True)
2721 self.assertEqual(obj.props.some_boolean, True)
2723 def test_char(self):
2724 self.assertEqual(self.obj.props.some_char, 0)
2725 self.obj.props.some_char = GObject.G_MAXINT8
2726 self.assertEqual(self.obj.props.some_char, GObject.G_MAXINT8)
2728 obj = GIMarshallingTests.PropertiesObject(some_char=-42)
2729 self.assertEqual(obj.props.some_char, -42)
2731 def test_uchar(self):
2732 self.assertEqual(self.obj.props.some_uchar, 0)
2733 self.obj.props.some_uchar = GObject.G_MAXUINT8
2734 self.assertEqual(self.obj.props.some_uchar, GObject.G_MAXUINT8)
2736 obj = GIMarshallingTests.PropertiesObject(some_uchar=42)
2737 self.assertEqual(obj.props.some_uchar, 42)
2740 self.assertEqual(self.obj.props.some_int, 0)
2741 self.obj.props.some_int = GObject.G_MAXINT
2742 self.assertEqual(self.obj.props.some_int, GObject.G_MAXINT)
2744 obj = GIMarshallingTests.PropertiesObject(some_int=-42)
2745 self.assertEqual(obj.props.some_int, -42)
2747 self.assertRaises(TypeError, setattr, self.obj.props, 'some_int', 'foo')
2748 self.assertRaises(TypeError, setattr, self.obj.props, 'some_int', None)
2750 self.assertEqual(obj.props.some_int, -42)
2752 def test_uint(self):
2753 self.assertEqual(self.obj.props.some_uint, 0)
2754 self.obj.props.some_uint = GObject.G_MAXUINT
2755 self.assertEqual(self.obj.props.some_uint, GObject.G_MAXUINT)
2757 obj = GIMarshallingTests.PropertiesObject(some_uint=42)
2758 self.assertEqual(obj.props.some_uint, 42)
2760 self.assertRaises(TypeError, setattr, self.obj.props, 'some_uint', 'foo')
2761 self.assertRaises(TypeError, setattr, self.obj.props, 'some_uint', None)
2763 self.assertEqual(obj.props.some_uint, 42)
2765 def test_long(self):
2766 self.assertEqual(self.obj.props.some_long, 0)
2767 self.obj.props.some_long = GObject.G_MAXLONG
2768 self.assertEqual(self.obj.props.some_long, GObject.G_MAXLONG)
2770 obj = GIMarshallingTests.PropertiesObject(some_long=-42)
2771 self.assertEqual(obj.props.some_long, -42)
2773 self.assertRaises(TypeError, setattr, self.obj.props, 'some_long', 'foo')
2774 self.assertRaises(TypeError, setattr, self.obj.props, 'some_long', None)
2776 self.assertEqual(obj.props.some_long, -42)
2778 def test_ulong(self):
2779 self.assertEqual(self.obj.props.some_ulong, 0)
2780 self.obj.props.some_ulong = GObject.G_MAXULONG
2781 self.assertEqual(self.obj.props.some_ulong, GObject.G_MAXULONG)
2783 obj = GIMarshallingTests.PropertiesObject(some_ulong=42)
2784 self.assertEqual(obj.props.some_ulong, 42)
2786 self.assertRaises(TypeError, setattr, self.obj.props, 'some_ulong', 'foo')
2787 self.assertRaises(TypeError, setattr, self.obj.props, 'some_ulong', None)
2789 self.assertEqual(obj.props.some_ulong, 42)
2791 def test_int64(self):
2792 self.assertEqual(self.obj.props.some_int64, 0)
2793 self.obj.props.some_int64 = GObject.G_MAXINT64
2794 self.assertEqual(self.obj.props.some_int64, GObject.G_MAXINT64)
2796 obj = GIMarshallingTests.PropertiesObject(some_int64=-4200000000000000)
2797 self.assertEqual(obj.props.some_int64, -4200000000000000)
2799 def test_uint64(self):
2800 self.assertEqual(self.obj.props.some_uint64, 0)
2801 self.obj.props.some_uint64 = GObject.G_MAXUINT64
2802 self.assertEqual(self.obj.props.some_uint64, GObject.G_MAXUINT64)
2804 obj = GIMarshallingTests.PropertiesObject(some_uint64=4200000000000000)
2805 self.assertEqual(obj.props.some_uint64, 4200000000000000)
2807 def test_float(self):
2808 self.assertEqual(self.obj.props.some_float, 0)
2809 self.obj.props.some_float = GObject.G_MAXFLOAT
2810 self.assertEqual(self.obj.props.some_float, GObject.G_MAXFLOAT)
2812 obj = GIMarshallingTests.PropertiesObject(some_float=42.42)
2813 self.assertAlmostEqual(obj.props.some_float, 42.42, 4)
2815 obj = GIMarshallingTests.PropertiesObject(some_float=42)
2816 self.assertAlmostEqual(obj.props.some_float, 42.0, 4)
2818 self.assertRaises(TypeError, setattr, self.obj.props, 'some_float', 'foo')
2819 self.assertRaises(TypeError, setattr, self.obj.props, 'some_float', None)
2821 self.assertAlmostEqual(obj.props.some_float, 42.0, 4)
2823 def test_double(self):
2824 self.assertEqual(self.obj.props.some_double, 0)
2825 self.obj.props.some_double = GObject.G_MAXDOUBLE
2826 self.assertEqual(self.obj.props.some_double, GObject.G_MAXDOUBLE)
2828 obj = GIMarshallingTests.PropertiesObject(some_double=42.42)
2829 self.assertAlmostEqual(obj.props.some_double, 42.42)
2831 obj = GIMarshallingTests.PropertiesObject(some_double=42)
2832 self.assertAlmostEqual(obj.props.some_double, 42.0)
2834 self.assertRaises(TypeError, setattr, self.obj.props, 'some_double', 'foo')
2835 self.assertRaises(TypeError, setattr, self.obj.props, 'some_double', None)
2837 self.assertAlmostEqual(obj.props.some_double, 42.0)
2839 def test_strv(self):
2840 self.assertEqual(self.obj.props.some_strv, [])
2841 self.obj.props.some_strv = ['hello', 'world']
2842 self.assertEqual(self.obj.props.some_strv, ['hello', 'world'])
2844 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', 1)
2845 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', 'foo')
2846 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', [1, 2])
2847 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', ['foo', 1])
2849 self.assertEqual(self.obj.props.some_strv, ['hello', 'world'])
2851 obj = GIMarshallingTests.PropertiesObject(some_strv=['hello', 'world'])
2852 self.assertEqual(obj.props.some_strv, ['hello', 'world'])
2854 def test_boxed_struct(self):
2855 self.assertEqual(self.obj.props.some_boxed_struct, None)
2858 __gtype__ = GObject.TYPE_STRV
2860 struct1 = GIMarshallingTests.BoxedStruct()
2863 self.obj.props.some_boxed_struct = struct1
2864 self.assertEqual(self.obj.props.some_boxed_struct.long_, 1)
2865 self.assertEqual(self.obj.some_boxed_struct.long_, 1)
2867 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_struct', 1)
2868 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_struct', 'foo')
2870 obj = GIMarshallingTests.PropertiesObject(some_boxed_struct=struct1)
2871 self.assertEqual(obj.props.some_boxed_struct.long_, 1)
2873 def test_boxed_glist(self):
2874 self.assertEqual(self.obj.props.some_boxed_glist, [])
2876 l = [GObject.G_MININT, 42, GObject.G_MAXINT]
2877 self.obj.props.some_boxed_glist = l
2878 self.assertEqual(self.obj.props.some_boxed_glist, l)
2879 self.obj.props.some_boxed_glist = []
2880 self.assertEqual(self.obj.props.some_boxed_glist, [])
2882 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_glist', 1)
2883 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_glist', 'foo')
2884 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_glist', ['a'])
2886 @unittest.expectedFailure
2887 def test_boxed_glist_ctor(self):
2888 l = [GObject.G_MININT, 42, GObject.G_MAXINT]
2889 obj = GIMarshallingTests.PropertiesObject(some_boxed_glist=l)
2890 self.assertEqual(obj.props.some_boxed_glist, l)
2892 def test_variant(self):
2893 self.assertEqual(self.obj.props.some_variant, None)
2895 self.obj.props.some_variant = GLib.Variant('o', '/myobj')
2896 self.assertEqual(self.obj.props.some_variant.get_type_string(), 'o')
2897 self.assertEqual(self.obj.props.some_variant.print_(False), "'/myobj'")
2899 self.obj.props.some_variant = None
2900 self.assertEqual(self.obj.props.some_variant, None)
2902 obj = GIMarshallingTests.PropertiesObject(some_variant=GLib.Variant('b', True))
2903 self.assertEqual(obj.props.some_variant.get_type_string(), 'b')
2904 self.assertEqual(obj.props.some_variant.get_boolean(), True)
2906 self.assertRaises(TypeError, setattr, self.obj.props, 'some_variant', 'foo')
2907 self.assertRaises(TypeError, setattr, self.obj.props, 'some_variant', 23)
2909 self.assertEqual(obj.props.some_variant.get_type_string(), 'b')
2910 self.assertEqual(obj.props.some_variant.get_boolean(), True)
2912 def test_setting_several_properties(self):
2913 obj = GIMarshallingTests.PropertiesObject()
2914 obj.set_properties(some_uchar=54, some_int=42)
2915 self.assertEqual(42, obj.props.some_int)
2916 self.assertEqual(54, obj.props.some_uchar)
2918 def test_props_accessor_dir(self):
2920 props = dir(GIMarshallingTests.PropertiesObject.props)
2921 self.assertTrue('some_float' in props)
2922 self.assertTrue('some_double' in props)
2923 self.assertTrue('some_variant' in props)
2926 obj = GIMarshallingTests.PropertiesObject()
2927 props = dir(obj.props)
2928 self.assertTrue('some_float' in props)
2929 self.assertTrue('some_double' in props)
2930 self.assertTrue('some_variant' in props)
2932 def test_param_spec_dir(self):
2933 attrs = dir(GIMarshallingTests.PropertiesObject.props.some_float)
2934 self.assertTrue('name' in attrs)
2935 self.assertTrue('nick' in attrs)
2936 self.assertTrue('blurb' in attrs)
2937 self.assertTrue('flags' in attrs)
2938 self.assertTrue('default_value' in attrs)
2939 self.assertTrue('minimum' in attrs)
2940 self.assertTrue('maximum' in attrs)
2943 class TestKeywords(unittest.TestCase):
2944 def test_method(self):
2946 v = GLib.Variant('i', 1)
2947 self.assertEqual(v.print_(False), '1')
2949 def test_function(self):
2951 self.assertEqual(GLib.Thread.yield_(), None)
2953 def test_struct_method(self):
2954 # g_timer_continue()
2955 # we cannot currently instantiate GLib.Timer objects, so just ensure
2957 self.assertTrue(callable(GLib.Timer.continue_))
2959 def test_uppercase(self):
2960 self.assertEqual(GLib.IOCondition.IN.value_nicks, ['in'])
2963 class TestModule(unittest.TestCase):
2964 def test_path(self):
2965 self.assertTrue(GIMarshallingTests.__path__.endswith('GIMarshallingTests-1.0.typelib'),
2966 GIMarshallingTests.__path__)
2969 self.assertTrue("'GIMarshallingTests' from '" in str(GIMarshallingTests),
2970 str(GIMarshallingTests))
2973 _dir = dir(GIMarshallingTests)
2974 self.assertGreater(len(_dir), 10)
2976 self.assertTrue('SimpleStruct' in _dir)
2977 self.assertTrue('Interface2' in _dir)
2978 self.assertTrue('CONSTANT_GERROR_CODE' in _dir)
2979 self.assertTrue('array_zero_terminated_inout' in _dir)
2981 # assert that dir() does not contain garbage
2982 for item_name in _dir:
2983 item = getattr(GIMarshallingTests, item_name)
2984 self.assertTrue(hasattr(item, '__class__'))
2986 def test_help(self):
2987 orig_stdout = sys.stdout
2989 if sys.version_info < (3, 0):
2990 sys.stdout = BytesIO()
2992 sys.stdout = StringIO()
2993 help(GIMarshallingTests)
2994 output = sys.stdout.getvalue()
2996 sys.stdout = orig_stdout
2998 self.assertTrue('SimpleStruct' in output, output)
2999 self.assertTrue('Interface2' in output, output)
3000 self.assertTrue('method_array_inout' in output, output)
3003 class TestProjectVersion(unittest.TestCase):
3004 def test_version_str(self):
3005 self.assertGreater(gi.__version__, "3.")
3007 def test_version_info(self):
3008 self.assertEqual(len(gi.version_info), 3)
3009 self.assertGreaterEqual(gi.version_info, (3, 3, 5))
3011 def test_check_version(self):
3012 self.assertRaises(ValueError, gi.check_version, (99, 0, 0))
3013 self.assertRaises(ValueError, gi.check_version, "99.0.0")
3014 gi.check_version((3, 3, 5))
3015 gi.check_version("3.3.5")
3018 class TestDeprecation(unittest.TestCase):
3019 def test_method(self):
3021 with warnings.catch_warnings(record=True) as warn:
3022 warnings.simplefilter('always')
3024 self.assertTrue(issubclass(warn[0].category, DeprecationWarning))
3026 def test_deprecated_init_no_keywords(self):
3027 def init(self, **kwargs):
3028 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
3030 fn = gi.overrides.deprecated_init(init, arg_names=('a', 'b', 'c'))
3031 with warnings.catch_warnings(record=True) as warn:
3032 warnings.simplefilter('always')
3034 self.assertEqual(len(warn), 1)
3035 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3036 self.assertRegexpMatches(str(warn[0].message),
3037 '.*keyword.*a, b, c.*')
3039 def test_deprecated_init_no_keywords_out_of_order(self):
3040 def init(self, **kwargs):
3041 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
3043 fn = gi.overrides.deprecated_init(init, arg_names=('b', 'a', 'c'))
3044 with warnings.catch_warnings(record=True) as warn:
3045 warnings.simplefilter('always')
3047 self.assertEqual(len(warn), 1)
3048 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3049 self.assertRegexpMatches(str(warn[0].message),
3050 '.*keyword.*b, a, c.*')
3052 def test_deprecated_init_ignored_keyword(self):
3053 def init(self, **kwargs):
3054 self.assertDictEqual(kwargs, {'a': 1, 'c': 3})
3056 fn = gi.overrides.deprecated_init(init,
3057 arg_names=('a', 'b', 'c'),
3059 with warnings.catch_warnings(record=True) as warn:
3060 warnings.simplefilter('always')
3062 self.assertEqual(len(warn), 1)
3063 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3064 self.assertRegexpMatches(str(warn[0].message),
3065 '.*keyword.*a, b, c.*')
3067 def test_deprecated_init_with_aliases(self):
3068 def init(self, **kwargs):
3069 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
3071 fn = gi.overrides.deprecated_init(init,
3072 arg_names=('a', 'b', 'c'),
3073 deprecated_aliases={'b': 'bb', 'c': 'cc'})
3074 with warnings.catch_warnings(record=True) as warn:
3075 warnings.simplefilter('always')
3077 fn(self, a=1, bb=2, cc=3)
3078 self.assertEqual(len(warn), 1)
3079 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3080 self.assertRegexpMatches(str(warn[0].message),
3081 '.*keyword.*"bb, cc".*deprecated.*"b, c" respectively')
3083 def test_deprecated_init_with_defaults(self):
3084 def init(self, **kwargs):
3085 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
3087 fn = gi.overrides.deprecated_init(init,
3088 arg_names=('a', 'b', 'c'),
3089 deprecated_defaults={'b': 2, 'c': 3})
3090 with warnings.catch_warnings(record=True) as warn:
3091 warnings.simplefilter('always')
3093 self.assertEqual(len(warn), 1)
3094 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3095 self.assertRegexpMatches(str(warn[0].message),
3096 '.*relying on deprecated non-standard defaults.*'
3097 'explicitly use: b=2, c=3')