1 # -*- Mode: Python; py-indent-offset: 4 -*-
3 # vim: tabstop=4 shiftwidth=4 expandtab
15 from io import StringIO, BytesIO
18 from gi.repository import GObject, GLib, Gio
20 from gi.repository import GIMarshallingTests
22 from compathelper import _bytes, _unicode
24 if sys.version_info < (3, 0):
25 CONSTANT_UTF8 = "const \xe2\x99\xa5 utf8"
26 PY2_UNICODE_UTF8 = unicode(CONSTANT_UTF8, 'UTF-8')
29 CONSTANT_UTF8 = "const ♥ utf8"
30 CHAR_255 = bytes([255])
37 def __init__(self, value):
41 return int(self.value)
44 return float(self.value)
47 class Sequence(object):
49 def __init__(self, sequence):
50 self.sequence = sequence
53 return len(self.sequence)
55 def __getitem__(self, key):
56 return self.sequence[key]
59 class TestConstant(unittest.TestCase):
61 # Blocked by https://bugzilla.gnome.org/show_bug.cgi?id=595773
62 # def test_constant_utf8(self):
63 # self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.CONSTANT_UTF8)
65 def test_constant_number(self):
66 self.assertEqual(CONSTANT_NUMBER, GIMarshallingTests.CONSTANT_NUMBER)
68 def test_min_max_int(self):
69 self.assertEqual(GLib.MAXINT32, 2 ** 31 - 1)
70 self.assertEqual(GLib.MININT32, -2 ** 31)
71 self.assertEqual(GLib.MAXUINT32, 2 ** 32 - 1)
73 self.assertEqual(GLib.MAXINT64, 2 ** 63 - 1)
74 self.assertEqual(GLib.MININT64, -2 ** 63)
75 self.assertEqual(GLib.MAXUINT64, 2 ** 64 - 1)
78 class TestBoolean(unittest.TestCase):
80 def test_boolean_return(self):
81 self.assertEqual(True, GIMarshallingTests.boolean_return_true())
82 self.assertEqual(False, GIMarshallingTests.boolean_return_false())
84 def test_boolean_in(self):
85 GIMarshallingTests.boolean_in_true(True)
86 GIMarshallingTests.boolean_in_false(False)
88 GIMarshallingTests.boolean_in_true(1)
89 GIMarshallingTests.boolean_in_false(0)
91 def test_boolean_out(self):
92 self.assertEqual(True, GIMarshallingTests.boolean_out_true())
93 self.assertEqual(False, GIMarshallingTests.boolean_out_false())
95 def test_boolean_inout(self):
96 self.assertEqual(False, GIMarshallingTests.boolean_inout_true_false(True))
97 self.assertEqual(True, GIMarshallingTests.boolean_inout_false_true(False))
100 class TestInt8(unittest.TestCase):
102 MAX = GObject.G_MAXINT8
103 MIN = GObject.G_MININT8
105 def test_int8_return(self):
106 self.assertEqual(self.MAX, GIMarshallingTests.int8_return_max())
107 self.assertEqual(self.MIN, GIMarshallingTests.int8_return_min())
109 def test_int8_in(self):
110 max = Number(self.MAX)
111 min = Number(self.MIN)
113 GIMarshallingTests.int8_in_max(max)
114 GIMarshallingTests.int8_in_min(min)
119 self.assertRaises(ValueError, GIMarshallingTests.int8_in_max, max)
120 self.assertRaises(ValueError, GIMarshallingTests.int8_in_min, min)
122 self.assertRaises(TypeError, GIMarshallingTests.int8_in_max, "self.MAX")
124 def test_int8_out(self):
125 self.assertEqual(self.MAX, GIMarshallingTests.int8_out_max())
126 self.assertEqual(self.MIN, GIMarshallingTests.int8_out_min())
128 def test_int8_inout(self):
129 self.assertEqual(self.MIN, GIMarshallingTests.int8_inout_max_min(Number(self.MAX)))
130 self.assertEqual(self.MAX, GIMarshallingTests.int8_inout_min_max(Number(self.MIN)))
133 class TestUInt8(unittest.TestCase):
135 MAX = GObject.G_MAXUINT8
137 def test_uint8_return(self):
138 self.assertEqual(self.MAX, GIMarshallingTests.uint8_return())
140 def test_uint8_in(self):
141 number = Number(self.MAX)
143 GIMarshallingTests.uint8_in(number)
144 GIMarshallingTests.uint8_in(CHAR_255)
147 self.assertRaises(ValueError, GIMarshallingTests.uint8_in, number)
148 self.assertRaises(ValueError, GIMarshallingTests.uint8_in, Number(-1))
150 self.assertRaises(TypeError, GIMarshallingTests.uint8_in, "self.MAX")
152 def test_uint8_out(self):
153 self.assertEqual(self.MAX, GIMarshallingTests.uint8_out())
155 def test_uint8_inout(self):
156 self.assertEqual(0, GIMarshallingTests.uint8_inout(Number(self.MAX)))
159 class TestInt16(unittest.TestCase):
161 MAX = GObject.G_MAXINT16
162 MIN = GObject.G_MININT16
164 def test_int16_return(self):
165 self.assertEqual(self.MAX, GIMarshallingTests.int16_return_max())
166 self.assertEqual(self.MIN, GIMarshallingTests.int16_return_min())
168 def test_int16_in(self):
169 max = Number(self.MAX)
170 min = Number(self.MIN)
172 GIMarshallingTests.int16_in_max(max)
173 GIMarshallingTests.int16_in_min(min)
178 self.assertRaises(ValueError, GIMarshallingTests.int16_in_max, max)
179 self.assertRaises(ValueError, GIMarshallingTests.int16_in_min, min)
181 self.assertRaises(TypeError, GIMarshallingTests.int16_in_max, "self.MAX")
183 def test_int16_out(self):
184 self.assertEqual(self.MAX, GIMarshallingTests.int16_out_max())
185 self.assertEqual(self.MIN, GIMarshallingTests.int16_out_min())
187 def test_int16_inout(self):
188 self.assertEqual(self.MIN, GIMarshallingTests.int16_inout_max_min(Number(self.MAX)))
189 self.assertEqual(self.MAX, GIMarshallingTests.int16_inout_min_max(Number(self.MIN)))
192 class TestUInt16(unittest.TestCase):
194 MAX = GObject.G_MAXUINT16
196 def test_uint16_return(self):
197 self.assertEqual(self.MAX, GIMarshallingTests.uint16_return())
199 def test_uint16_in(self):
200 number = Number(self.MAX)
202 GIMarshallingTests.uint16_in(number)
206 self.assertRaises(ValueError, GIMarshallingTests.uint16_in, number)
207 self.assertRaises(ValueError, GIMarshallingTests.uint16_in, Number(-1))
209 self.assertRaises(TypeError, GIMarshallingTests.uint16_in, "self.MAX")
211 def test_uint16_out(self):
212 self.assertEqual(self.MAX, GIMarshallingTests.uint16_out())
214 def test_uint16_inout(self):
215 self.assertEqual(0, GIMarshallingTests.uint16_inout(Number(self.MAX)))
218 class TestInt32(unittest.TestCase):
220 MAX = GObject.G_MAXINT32
221 MIN = GObject.G_MININT32
223 def test_int32_return(self):
224 self.assertEqual(self.MAX, GIMarshallingTests.int32_return_max())
225 self.assertEqual(self.MIN, GIMarshallingTests.int32_return_min())
227 def test_int32_in(self):
228 max = Number(self.MAX)
229 min = Number(self.MIN)
231 GIMarshallingTests.int32_in_max(max)
232 GIMarshallingTests.int32_in_min(min)
237 self.assertRaises(ValueError, GIMarshallingTests.int32_in_max, max)
238 self.assertRaises(ValueError, GIMarshallingTests.int32_in_min, min)
240 self.assertRaises(TypeError, GIMarshallingTests.int32_in_max, "self.MAX")
242 def test_int32_out(self):
243 self.assertEqual(self.MAX, GIMarshallingTests.int32_out_max())
244 self.assertEqual(self.MIN, GIMarshallingTests.int32_out_min())
246 def test_int32_inout(self):
247 self.assertEqual(self.MIN, GIMarshallingTests.int32_inout_max_min(Number(self.MAX)))
248 self.assertEqual(self.MAX, GIMarshallingTests.int32_inout_min_max(Number(self.MIN)))
251 class TestUInt32(unittest.TestCase):
253 MAX = GObject.G_MAXUINT32
255 def test_uint32_return(self):
256 self.assertEqual(self.MAX, GIMarshallingTests.uint32_return())
258 def test_uint32_in(self):
259 number = Number(self.MAX)
261 GIMarshallingTests.uint32_in(number)
265 self.assertRaises(ValueError, GIMarshallingTests.uint32_in, number)
266 self.assertRaises(ValueError, GIMarshallingTests.uint32_in, Number(-1))
268 self.assertRaises(TypeError, GIMarshallingTests.uint32_in, "self.MAX")
270 def test_uint32_out(self):
271 self.assertEqual(self.MAX, GIMarshallingTests.uint32_out())
273 def test_uint32_inout(self):
274 self.assertEqual(0, GIMarshallingTests.uint32_inout(Number(self.MAX)))
277 class TestInt64(unittest.TestCase):
282 def test_int64_return(self):
283 self.assertEqual(self.MAX, GIMarshallingTests.int64_return_max())
284 self.assertEqual(self.MIN, GIMarshallingTests.int64_return_min())
286 def test_int64_in(self):
287 max = Number(self.MAX)
288 min = Number(self.MIN)
290 GIMarshallingTests.int64_in_max(max)
291 GIMarshallingTests.int64_in_min(min)
296 self.assertRaises(ValueError, GIMarshallingTests.int64_in_max, max)
297 self.assertRaises(ValueError, GIMarshallingTests.int64_in_min, min)
299 self.assertRaises(TypeError, GIMarshallingTests.int64_in_max, "self.MAX")
301 def test_int64_out(self):
302 self.assertEqual(self.MAX, GIMarshallingTests.int64_out_max())
303 self.assertEqual(self.MIN, GIMarshallingTests.int64_out_min())
305 def test_int64_inout(self):
306 self.assertEqual(self.MIN, GIMarshallingTests.int64_inout_max_min(Number(self.MAX)))
307 self.assertEqual(self.MAX, GIMarshallingTests.int64_inout_min_max(Number(self.MIN)))
310 class TestUInt64(unittest.TestCase):
314 def test_uint64_return(self):
315 self.assertEqual(self.MAX, GIMarshallingTests.uint64_return())
317 def test_uint64_in(self):
318 number = Number(self.MAX)
320 GIMarshallingTests.uint64_in(number)
324 self.assertRaises(ValueError, GIMarshallingTests.uint64_in, number)
325 self.assertRaises(ValueError, GIMarshallingTests.uint64_in, Number(-1))
327 self.assertRaises(TypeError, GIMarshallingTests.uint64_in, "self.MAX")
329 def test_uint64_out(self):
330 self.assertEqual(self.MAX, GIMarshallingTests.uint64_out())
332 def test_uint64_inout(self):
333 self.assertEqual(0, GIMarshallingTests.uint64_inout(Number(self.MAX)))
336 class TestShort(unittest.TestCase):
338 MAX = GObject.G_MAXSHORT
339 MIN = GObject.G_MINSHORT
341 def test_short_return(self):
342 self.assertEqual(self.MAX, GIMarshallingTests.short_return_max())
343 self.assertEqual(self.MIN, GIMarshallingTests.short_return_min())
345 def test_short_in(self):
346 max = Number(self.MAX)
347 min = Number(self.MIN)
349 GIMarshallingTests.short_in_max(max)
350 GIMarshallingTests.short_in_min(min)
355 self.assertRaises(ValueError, GIMarshallingTests.short_in_max, max)
356 self.assertRaises(ValueError, GIMarshallingTests.short_in_min, min)
358 self.assertRaises(TypeError, GIMarshallingTests.short_in_max, "self.MAX")
360 def test_short_out(self):
361 self.assertEqual(self.MAX, GIMarshallingTests.short_out_max())
362 self.assertEqual(self.MIN, GIMarshallingTests.short_out_min())
364 def test_short_inout(self):
365 self.assertEqual(self.MIN, GIMarshallingTests.short_inout_max_min(Number(self.MAX)))
366 self.assertEqual(self.MAX, GIMarshallingTests.short_inout_min_max(Number(self.MIN)))
369 class TestUShort(unittest.TestCase):
371 MAX = GObject.G_MAXUSHORT
373 def test_ushort_return(self):
374 self.assertEqual(self.MAX, GIMarshallingTests.ushort_return())
376 def test_ushort_in(self):
377 number = Number(self.MAX)
379 GIMarshallingTests.ushort_in(number)
383 self.assertRaises(ValueError, GIMarshallingTests.ushort_in, number)
384 self.assertRaises(ValueError, GIMarshallingTests.ushort_in, Number(-1))
386 self.assertRaises(TypeError, GIMarshallingTests.ushort_in, "self.MAX")
388 def test_ushort_out(self):
389 self.assertEqual(self.MAX, GIMarshallingTests.ushort_out())
391 def test_ushort_inout(self):
392 self.assertEqual(0, GIMarshallingTests.ushort_inout(Number(self.MAX)))
395 class TestInt(unittest.TestCase):
397 MAX = GObject.G_MAXINT
398 MIN = GObject.G_MININT
400 def test_int_return(self):
401 self.assertEqual(self.MAX, GIMarshallingTests.int_return_max())
402 self.assertEqual(self.MIN, GIMarshallingTests.int_return_min())
404 def test_int_in(self):
405 max = Number(self.MAX)
406 min = Number(self.MIN)
408 GIMarshallingTests.int_in_max(max)
409 GIMarshallingTests.int_in_min(min)
414 self.assertRaises(ValueError, GIMarshallingTests.int_in_max, max)
415 self.assertRaises(ValueError, GIMarshallingTests.int_in_min, min)
417 self.assertRaises(TypeError, GIMarshallingTests.int_in_max, "self.MAX")
419 def test_int_out(self):
420 self.assertEqual(self.MAX, GIMarshallingTests.int_out_max())
421 self.assertEqual(self.MIN, GIMarshallingTests.int_out_min())
423 def test_int_inout(self):
424 self.assertEqual(self.MIN, GIMarshallingTests.int_inout_max_min(Number(self.MAX)))
425 self.assertEqual(self.MAX, GIMarshallingTests.int_inout_min_max(Number(self.MIN)))
426 self.assertRaises(TypeError, GIMarshallingTests.int_inout_min_max, Number(self.MIN), CONSTANT_NUMBER)
429 class TestUInt(unittest.TestCase):
431 MAX = GObject.G_MAXUINT
433 def test_uint_return(self):
434 self.assertEqual(self.MAX, GIMarshallingTests.uint_return())
436 def test_uint_in(self):
437 number = Number(self.MAX)
439 GIMarshallingTests.uint_in(number)
443 self.assertRaises(ValueError, GIMarshallingTests.uint_in, number)
444 self.assertRaises(ValueError, GIMarshallingTests.uint_in, Number(-1))
446 self.assertRaises(TypeError, GIMarshallingTests.uint_in, "self.MAX")
448 def test_uint_out(self):
449 self.assertEqual(self.MAX, GIMarshallingTests.uint_out())
451 def test_uint_inout(self):
452 self.assertEqual(0, GIMarshallingTests.uint_inout(Number(self.MAX)))
455 class TestLong(unittest.TestCase):
457 MAX = GObject.G_MAXLONG
458 MIN = GObject.G_MINLONG
460 def test_long_return(self):
461 self.assertEqual(self.MAX, GIMarshallingTests.long_return_max())
462 self.assertEqual(self.MIN, GIMarshallingTests.long_return_min())
464 def test_long_in(self):
465 max = Number(self.MAX)
466 min = Number(self.MIN)
468 GIMarshallingTests.long_in_max(max)
469 GIMarshallingTests.long_in_min(min)
474 self.assertRaises(ValueError, GIMarshallingTests.long_in_max, max)
475 self.assertRaises(ValueError, GIMarshallingTests.long_in_min, min)
477 self.assertRaises(TypeError, GIMarshallingTests.long_in_max, "self.MAX")
479 def test_long_out(self):
480 self.assertEqual(self.MAX, GIMarshallingTests.long_out_max())
481 self.assertEqual(self.MIN, GIMarshallingTests.long_out_min())
483 def test_long_inout(self):
484 self.assertEqual(self.MIN, GIMarshallingTests.long_inout_max_min(Number(self.MAX)))
485 self.assertEqual(self.MAX, GIMarshallingTests.long_inout_min_max(Number(self.MIN)))
488 class TestULong(unittest.TestCase):
490 MAX = GObject.G_MAXULONG
492 def test_ulong_return(self):
493 self.assertEqual(self.MAX, GIMarshallingTests.ulong_return())
495 def test_ulong_in(self):
496 number = Number(self.MAX)
498 GIMarshallingTests.ulong_in(number)
502 self.assertRaises(ValueError, GIMarshallingTests.ulong_in, number)
503 self.assertRaises(ValueError, GIMarshallingTests.ulong_in, Number(-1))
505 self.assertRaises(TypeError, GIMarshallingTests.ulong_in, "self.MAX")
507 def test_ulong_out(self):
508 self.assertEqual(self.MAX, GIMarshallingTests.ulong_out())
510 def test_ulong_inout(self):
511 self.assertEqual(0, GIMarshallingTests.ulong_inout(Number(self.MAX)))
514 class TestSSize(unittest.TestCase):
516 MAX = GObject.G_MAXLONG
517 MIN = GObject.G_MINLONG
519 def test_ssize_return(self):
520 self.assertEqual(self.MAX, GIMarshallingTests.ssize_return_max())
521 self.assertEqual(self.MIN, GIMarshallingTests.ssize_return_min())
523 def test_ssize_in(self):
524 max = Number(self.MAX)
525 min = Number(self.MIN)
527 GIMarshallingTests.ssize_in_max(max)
528 GIMarshallingTests.ssize_in_min(min)
533 self.assertRaises(ValueError, GIMarshallingTests.ssize_in_max, max)
534 self.assertRaises(ValueError, GIMarshallingTests.ssize_in_min, min)
536 self.assertRaises(TypeError, GIMarshallingTests.ssize_in_max, "self.MAX")
538 def test_ssize_out(self):
539 self.assertEqual(self.MAX, GIMarshallingTests.ssize_out_max())
540 self.assertEqual(self.MIN, GIMarshallingTests.ssize_out_min())
542 def test_ssize_inout(self):
543 self.assertEqual(self.MIN, GIMarshallingTests.ssize_inout_max_min(Number(self.MAX)))
544 self.assertEqual(self.MAX, GIMarshallingTests.ssize_inout_min_max(Number(self.MIN)))
547 class TestSize(unittest.TestCase):
549 MAX = GObject.G_MAXULONG
551 def test_size_return(self):
552 self.assertEqual(self.MAX, GIMarshallingTests.size_return())
554 def test_size_in(self):
555 number = Number(self.MAX)
557 GIMarshallingTests.size_in(number)
561 self.assertRaises(ValueError, GIMarshallingTests.size_in, number)
562 self.assertRaises(ValueError, GIMarshallingTests.size_in, Number(-1))
564 self.assertRaises(TypeError, GIMarshallingTests.size_in, "self.MAX")
566 def test_size_out(self):
567 self.assertEqual(self.MAX, GIMarshallingTests.size_out())
569 def test_size_inout(self):
570 self.assertEqual(0, GIMarshallingTests.size_inout(Number(self.MAX)))
573 class TestTimet(unittest.TestCase):
575 def test_time_t_return(self):
576 self.assertEqual(1234567890, GIMarshallingTests.time_t_return())
578 def test_time_t_in(self):
579 GIMarshallingTests.time_t_in(1234567890)
580 self.assertRaises(TypeError, GIMarshallingTests.time_t_in, "hello")
582 def test_time_t_out(self):
583 self.assertEqual(1234567890, GIMarshallingTests.time_t_out())
585 def test_time_t_inout(self):
586 self.assertEqual(0, GIMarshallingTests.time_t_inout(1234567890))
589 class TestFloat(unittest.TestCase):
591 MAX = GObject.G_MAXFLOAT
592 MIN = GObject.G_MINFLOAT
594 def test_float_return(self):
595 self.assertAlmostEqual(self.MAX, GIMarshallingTests.float_return())
597 def test_float_in(self):
598 GIMarshallingTests.float_in(Number(self.MAX))
600 self.assertRaises(TypeError, GIMarshallingTests.float_in, "self.MAX")
602 def test_float_out(self):
603 self.assertAlmostEqual(self.MAX, GIMarshallingTests.float_out())
605 def test_float_inout(self):
606 self.assertAlmostEqual(self.MIN, GIMarshallingTests.float_inout(Number(self.MAX)))
609 class TestDouble(unittest.TestCase):
611 MAX = GObject.G_MAXDOUBLE
612 MIN = GObject.G_MINDOUBLE
614 def test_double_return(self):
615 self.assertAlmostEqual(self.MAX, GIMarshallingTests.double_return())
617 def test_double_in(self):
618 GIMarshallingTests.double_in(Number(self.MAX))
620 self.assertRaises(TypeError, GIMarshallingTests.double_in, "self.MAX")
622 def test_double_out(self):
623 self.assertAlmostEqual(self.MAX, GIMarshallingTests.double_out())
625 def test_double_inout(self):
626 self.assertAlmostEqual(self.MIN, GIMarshallingTests.double_inout(Number(self.MAX)))
629 class TestGType(unittest.TestCase):
631 def test_gtype_name(self):
632 self.assertEqual("void", GObject.TYPE_NONE.name)
633 self.assertEqual("gchararray", GObject.TYPE_STRING.name)
635 def check_readonly(gtype):
638 self.assertRaises(AttributeError, check_readonly, GObject.TYPE_NONE)
639 self.assertRaises(AttributeError, check_readonly, GObject.TYPE_STRING)
641 def test_gtype_return(self):
642 self.assertEqual(GObject.TYPE_NONE, GIMarshallingTests.gtype_return())
643 self.assertEqual(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_return())
645 def test_gtype_in(self):
646 GIMarshallingTests.gtype_in(GObject.TYPE_NONE)
647 GIMarshallingTests.gtype_string_in(GObject.TYPE_STRING)
648 self.assertRaises(TypeError, GIMarshallingTests.gtype_in, "foo")
649 self.assertRaises(TypeError, GIMarshallingTests.gtype_string_in, "foo")
651 def test_gtype_out(self):
652 self.assertEqual(GObject.TYPE_NONE, GIMarshallingTests.gtype_out())
653 self.assertEqual(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_out())
655 def test_gtype_inout(self):
656 self.assertEqual(GObject.TYPE_INT, GIMarshallingTests.gtype_inout(GObject.TYPE_NONE))
659 class TestUtf8(unittest.TestCase):
661 def test_utf8_none_return(self):
662 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_none_return())
664 def test_utf8_full_return(self):
665 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_full_return())
667 def test_utf8_none_in(self):
668 GIMarshallingTests.utf8_none_in(CONSTANT_UTF8)
669 if sys.version_info < (3, 0):
670 GIMarshallingTests.utf8_none_in(PY2_UNICODE_UTF8)
672 self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, CONSTANT_NUMBER)
673 self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, None)
675 def test_utf8_none_out(self):
676 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_none_out())
678 def test_utf8_full_out(self):
679 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_full_out())
681 def test_utf8_dangling_out(self):
682 GIMarshallingTests.utf8_dangling_out()
684 def test_utf8_none_inout(self):
685 self.assertEqual("", GIMarshallingTests.utf8_none_inout(CONSTANT_UTF8))
687 def test_utf8_full_inout(self):
688 self.assertEqual("", GIMarshallingTests.utf8_full_inout(CONSTANT_UTF8))
691 class TestFilename(unittest.TestCase):
693 self.workdir = tempfile.mkdtemp()
696 shutil.rmtree(self.workdir)
698 def test_filename_in(self):
699 fname = os.path.join(self.workdir, _unicode('testäø.txt'))
700 self.assertRaises(GLib.GError, GLib.file_get_contents, fname)
702 with open(fname.encode('UTF-8'), 'wb') as f:
703 f.write(b'hello world!\n\x01\x02')
705 (result, contents) = GLib.file_get_contents(fname)
706 self.assertEqual(result, True)
707 self.assertEqual(contents, b'hello world!\n\x01\x02')
709 def test_filename_out(self):
710 self.assertRaises(GLib.GError, GLib.Dir.make_tmp, 'test')
712 dirname = GLib.Dir.make_tmp('testäø.XXXXXX')
713 self.assertTrue('/testäø.' in dirname, dirname)
714 dirname = _bytes(dirname)
715 self.assertTrue(os.path.isdir(dirname))
718 def test_filename_type_error(self):
719 self.assertRaises(TypeError, GLib.file_get_contents, 23)
722 class TestArray(unittest.TestCase):
724 def test_array_fixed_int_return(self):
725 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_int_return())
727 def test_array_fixed_short_return(self):
728 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_short_return())
730 def test_array_fixed_int_in(self):
731 GIMarshallingTests.array_fixed_int_in(Sequence([-1, 0, 1, 2]))
733 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, Sequence([-1, '0', 1, 2]))
735 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, 42)
736 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, None)
738 def test_array_fixed_short_in(self):
739 GIMarshallingTests.array_fixed_short_in(Sequence([-1, 0, 1, 2]))
741 def test_array_fixed_out(self):
742 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_out())
744 def test_array_fixed_inout(self):
745 self.assertEqual([2, 1, 0, -1], GIMarshallingTests.array_fixed_inout([-1, 0, 1, 2]))
747 def test_array_return(self):
748 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_return())
750 def test_array_return_etc(self):
751 self.assertEqual(([5, 0, 1, 9], 14), GIMarshallingTests.array_return_etc(5, 9))
753 def test_array_in(self):
754 GIMarshallingTests.array_in(Sequence([-1, 0, 1, 2]))
755 GIMarshallingTests.array_in_guint64_len(Sequence([-1, 0, 1, 2]))
756 GIMarshallingTests.array_in_guint8_len(Sequence([-1, 0, 1, 2]))
758 def test_array_in_len_before(self):
759 GIMarshallingTests.array_in_len_before(Sequence([-1, 0, 1, 2]))
761 def test_array_in_len_zero_terminated(self):
762 GIMarshallingTests.array_in_len_zero_terminated(Sequence([-1, 0, 1, 2]))
764 def test_array_uint8_in(self):
765 GIMarshallingTests.array_uint8_in(Sequence([97, 98, 99, 100]))
766 GIMarshallingTests.array_uint8_in(_bytes("abcd"))
768 def test_array_string_in(self):
769 GIMarshallingTests.array_string_in(['foo', 'bar'])
771 def test_array_out(self):
772 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_out())
774 def test_array_out_etc(self):
775 self.assertEqual(([-5, 0, 1, 9], 4), GIMarshallingTests.array_out_etc(-5, 9))
777 def test_array_inout(self):
778 self.assertEqual([-2, -1, 0, 1, 2], GIMarshallingTests.array_inout(Sequence([-1, 0, 1, 2])))
780 def test_array_inout_etc(self):
781 self.assertEqual(([-5, -1, 0, 1, 9], 4),
782 GIMarshallingTests.array_inout_etc(-5, Sequence([-1, 0, 1, 2]), 9))
784 def test_method_array_in(self):
785 object_ = GIMarshallingTests.Object()
786 object_.method_array_in(Sequence([-1, 0, 1, 2]))
788 def test_method_array_out(self):
789 object_ = GIMarshallingTests.Object()
790 self.assertEqual([-1, 0, 1, 2], object_.method_array_out())
792 def test_method_array_inout(self):
793 object_ = GIMarshallingTests.Object()
794 self.assertEqual([-2, -1, 0, 1, 2], object_.method_array_inout(Sequence([-1, 0, 1, 2])))
796 def test_method_array_return(self):
797 object_ = GIMarshallingTests.Object()
798 self.assertEqual([-1, 0, 1, 2], object_.method_array_return())
800 def test_array_enum_in(self):
801 GIMarshallingTests.array_enum_in([GIMarshallingTests.Enum.VALUE1,
802 GIMarshallingTests.Enum.VALUE2,
803 GIMarshallingTests.Enum.VALUE3])
805 def test_array_boxed_struct_in(self):
806 struct1 = GIMarshallingTests.BoxedStruct()
808 struct2 = GIMarshallingTests.BoxedStruct()
810 struct3 = GIMarshallingTests.BoxedStruct()
813 GIMarshallingTests.array_struct_in([struct1, struct2, struct3])
815 @unittest.skipUnless(hasattr(GIMarshallingTests, 'array_struct_value_in'),
816 'too old gobject-introspection')
817 def test_array_boxed_struct_value_in(self):
818 struct1 = GIMarshallingTests.BoxedStruct()
820 struct2 = GIMarshallingTests.BoxedStruct()
822 struct3 = GIMarshallingTests.BoxedStruct()
825 GIMarshallingTests.array_struct_value_in([struct1, struct2, struct3])
827 def test_array_boxed_struct_take_in(self):
828 struct1 = GIMarshallingTests.BoxedStruct()
830 struct2 = GIMarshallingTests.BoxedStruct()
832 struct3 = GIMarshallingTests.BoxedStruct()
835 GIMarshallingTests.array_struct_take_in([struct1, struct2, struct3])
837 self.assertEqual(1, struct1.long_)
839 def test_array_boxed_struct_return(self):
840 (struct1, struct2, struct3) = GIMarshallingTests.array_zero_terminated_return_struct()
841 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct1))
842 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct2))
843 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct3))
844 self.assertEqual(42, struct1.long_)
845 self.assertEqual(43, struct2.long_)
846 self.assertEqual(44, struct3.long_)
848 def test_array_simple_struct_in(self):
849 struct1 = GIMarshallingTests.SimpleStruct()
851 struct2 = GIMarshallingTests.SimpleStruct()
853 struct3 = GIMarshallingTests.SimpleStruct()
856 GIMarshallingTests.array_simple_struct_in([struct1, struct2, struct3])
858 def test_array_multi_array_key_value_in(self):
859 GIMarshallingTests.multi_array_key_value_in(["one", "two", "three"],
862 def test_array_in_nonzero_nonlen(self):
863 GIMarshallingTests.array_in_nonzero_nonlen(1, b'abcd')
865 def test_array_fixed_out_struct(self):
866 struct1, struct2 = GIMarshallingTests.array_fixed_out_struct()
868 self.assertEqual(7, struct1.long_)
869 self.assertEqual(6, struct1.int8)
870 self.assertEqual(6, struct2.long_)
871 self.assertEqual(7, struct2.int8)
873 def test_array_zero_terminated_return(self):
874 self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_return())
876 def test_array_zero_terminated_return_null(self):
877 self.assertEqual([], GIMarshallingTests.array_zero_terminated_return_null())
879 def test_array_zero_terminated_in(self):
880 GIMarshallingTests.array_zero_terminated_in(Sequence(['0', '1', '2']))
882 def test_array_zero_terminated_out(self):
883 self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
885 def test_array_zero_terminated_inout(self):
886 self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.array_zero_terminated_inout(['0', '1', '2']))
888 def test_init_function(self):
889 self.assertEqual((True, []), GIMarshallingTests.init_function([]))
890 self.assertEqual((True, []), GIMarshallingTests.init_function(['hello']))
891 self.assertEqual((True, ['hello']),
892 GIMarshallingTests.init_function(['hello', 'world']))
895 class TestGStrv(unittest.TestCase):
897 def test_gstrv_return(self):
898 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_return())
900 def test_gstrv_in(self):
901 GIMarshallingTests.gstrv_in(Sequence(['0', '1', '2']))
903 def test_gstrv_out(self):
904 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_out())
906 def test_gstrv_inout(self):
907 self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.gstrv_inout(['0', '1', '2']))
910 class TestArrayGVariant(unittest.TestCase):
912 def test_array_gvariant_none_in(self):
913 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
914 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_none_in(v)]
915 self.assertEqual([27, "Hello"], returned)
917 def test_array_gvariant_container_in(self):
918 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
919 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_container_in(v)]
920 self.assertEqual([27, "Hello"], returned)
922 def test_array_gvariant_full_in(self):
923 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
924 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_full_in(v)]
925 self.assertEqual([27, "Hello"], returned)
927 def test_bytearray_gvariant(self):
928 v = GLib.Variant.new_bytestring(b"foo")
929 self.assertEqual(v.get_bytestring(), b"foo")
932 class TestGArray(unittest.TestCase):
934 def test_garray_int_none_return(self):
935 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.garray_int_none_return())
937 def test_garray_uint64_none_return(self):
938 self.assertEqual([0, GObject.G_MAXUINT64], GIMarshallingTests.garray_uint64_none_return())
940 def test_garray_utf8_none_return(self):
941 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_return())
943 def test_garray_utf8_container_return(self):
944 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_return())
946 def test_garray_utf8_full_return(self):
947 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_return())
949 def test_garray_int_none_in(self):
950 GIMarshallingTests.garray_int_none_in(Sequence([-1, 0, 1, 2]))
952 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, Sequence([-1, '0', 1, 2]))
954 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, 42)
955 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, None)
957 def test_garray_uint64_none_in(self):
958 GIMarshallingTests.garray_uint64_none_in(Sequence([0, GObject.G_MAXUINT64]))
960 def test_garray_utf8_none_in(self):
961 GIMarshallingTests.garray_utf8_none_in(Sequence(['0', '1', '2']))
963 def test_garray_utf8_none_out(self):
964 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_out())
966 def test_garray_utf8_container_out(self):
967 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_out())
969 def test_garray_utf8_full_out(self):
970 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out())
972 @unittest.skipUnless(hasattr(GIMarshallingTests, 'garray_utf8_full_out_caller_allocated'),
973 'too old gobject-introspection')
974 def test_garray_utf8_full_out_caller_allocated(self):
975 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out_caller_allocated())
977 def test_garray_utf8_none_inout(self):
978 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_none_inout(Sequence(('0', '1', '2'))))
980 def test_garray_utf8_container_inout(self):
981 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_container_inout(['0', '1', '2']))
983 def test_garray_utf8_full_inout(self):
984 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_full_inout(['0', '1', '2']))
987 class TestGPtrArray(unittest.TestCase):
989 def test_gptrarray_utf8_none_return(self):
990 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_return())
992 def test_gptrarray_utf8_container_return(self):
993 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_return())
995 def test_gptrarray_utf8_full_return(self):
996 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_return())
998 def test_gptrarray_utf8_none_in(self):
999 GIMarshallingTests.gptrarray_utf8_none_in(Sequence(['0', '1', '2']))
1001 def test_gptrarray_utf8_none_out(self):
1002 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_out())
1004 def test_gptrarray_utf8_container_out(self):
1005 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_out())
1007 def test_gptrarray_utf8_full_out(self):
1008 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_out())
1010 def test_gptrarray_utf8_none_inout(self):
1011 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_none_inout(Sequence(('0', '1', '2'))))
1013 def test_gptrarray_utf8_container_inout(self):
1014 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_container_inout(['0', '1', '2']))
1016 def test_gptrarray_utf8_full_inout(self):
1017 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_full_inout(['0', '1', '2']))
1020 class TestGBytes(unittest.TestCase):
1021 def test_gbytes_create(self):
1022 b = GLib.Bytes.new(b'\x00\x01\xFF')
1023 self.assertEqual(3, b.get_size())
1024 self.assertEqual(b'\x00\x01\xFF', b.get_data())
1026 def test_gbytes_create_take(self):
1027 b = GLib.Bytes.new_take(b'\x00\x01\xFF')
1028 self.assertEqual(3, b.get_size())
1029 self.assertEqual(b'\x00\x01\xFF', b.get_data())
1031 @unittest.skipUnless(hasattr(GIMarshallingTests, 'gbytes_full_return'),
1032 'too old gobject-introspection')
1033 def test_gbytes_full_return(self):
1034 b = GIMarshallingTests.gbytes_full_return()
1035 self.assertEqual(4, b.get_size())
1036 self.assertEqual(b'\x00\x31\xFF\x33', b.get_data())
1038 @unittest.skipUnless(hasattr(GIMarshallingTests, 'gbytes_full_return'),
1039 'too old gobject-introspection')
1040 def test_gbytes_none_in(self):
1041 b = GIMarshallingTests.gbytes_full_return()
1042 GIMarshallingTests.gbytes_none_in(b)
1044 def test_compare(self):
1045 a1 = GLib.Bytes.new(b'\x00\x01\xFF')
1046 a2 = GLib.Bytes.new(b'\x00\x01\xFF')
1047 b = GLib.Bytes.new(b'\x00\x01\xFE')
1049 self.assertTrue(a1.equal(a2))
1050 self.assertTrue(a2.equal(a1))
1051 self.assertFalse(a1.equal(b))
1052 self.assertFalse(b.equal(a2))
1054 self.assertEqual(0, a1.compare(a2))
1055 self.assertLess(0, a1.compare(b))
1056 self.assertGreater(0, b.compare(a1))
1059 class TestGByteArray(unittest.TestCase):
1061 ba = GLib.ByteArray.new()
1062 self.assertEqual(b'', ba)
1064 ba = GLib.ByteArray.new_take(b'\x01\x02\xFF')
1065 self.assertEqual(b'\x01\x02\xFF', ba)
1067 def test_bytearray_full_return(self):
1068 self.assertEqual(b'\x001\xFF3', GIMarshallingTests.bytearray_full_return())
1070 def test_bytearray_none_in(self):
1071 GIMarshallingTests.bytearray_none_in(b'\x00\x31\xFF\x33')
1074 class TestGList(unittest.TestCase):
1076 def test_glist_int_none_return(self):
1077 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.glist_int_none_return())
1079 def test_glist_uint32_none_return(self):
1080 self.assertEqual([0, GObject.G_MAXUINT32], GIMarshallingTests.glist_uint32_none_return())
1082 def test_glist_utf8_none_return(self):
1083 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_return())
1085 def test_glist_utf8_container_return(self):
1086 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_return())
1088 def test_glist_utf8_full_return(self):
1089 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_return())
1091 def test_glist_int_none_in(self):
1092 GIMarshallingTests.glist_int_none_in(Sequence((-1, 0, 1, 2)))
1094 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, Sequence((-1, '0', 1, 2)))
1096 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, 42)
1097 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, None)
1099 def test_glist_uint32_none_in(self):
1100 GIMarshallingTests.glist_uint32_none_in(Sequence((0, GObject.G_MAXUINT32)))
1102 def test_glist_utf8_none_in(self):
1103 GIMarshallingTests.glist_utf8_none_in(Sequence(('0', '1', '2')))
1105 def test_glist_utf8_none_out(self):
1106 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_out())
1108 def test_glist_utf8_container_out(self):
1109 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_out())
1111 def test_glist_utf8_full_out(self):
1112 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_out())
1114 def test_glist_utf8_none_inout(self):
1115 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_none_inout(Sequence(('0', '1', '2'))))
1117 def test_glist_utf8_container_inout(self):
1118 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_container_inout(('0', '1', '2')))
1120 def test_glist_utf8_full_inout(self):
1121 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_full_inout(('0', '1', '2')))
1124 class TestGSList(unittest.TestCase):
1126 def test_gslist_int_none_return(self):
1127 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.gslist_int_none_return())
1129 def test_gslist_utf8_none_return(self):
1130 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_return())
1132 def test_gslist_utf8_container_return(self):
1133 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_return())
1135 def test_gslist_utf8_full_return(self):
1136 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_return())
1138 def test_gslist_int_none_in(self):
1139 GIMarshallingTests.gslist_int_none_in(Sequence((-1, 0, 1, 2)))
1141 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, Sequence((-1, '0', 1, 2)))
1143 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, 42)
1144 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, None)
1146 def test_gslist_utf8_none_in(self):
1147 GIMarshallingTests.gslist_utf8_none_in(Sequence(('0', '1', '2')))
1149 def test_gslist_utf8_none_out(self):
1150 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_out())
1152 def test_gslist_utf8_container_out(self):
1153 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_out())
1155 def test_gslist_utf8_full_out(self):
1156 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_out())
1158 def test_gslist_utf8_none_inout(self):
1159 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_none_inout(Sequence(('0', '1', '2'))))
1161 def test_gslist_utf8_container_inout(self):
1162 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_container_inout(('0', '1', '2')))
1164 def test_gslist_utf8_full_inout(self):
1165 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_full_inout(('0', '1', '2')))
1168 class TestGHashTable(unittest.TestCase):
1170 def test_ghashtable_int_none_return(self):
1171 self.assertEqual({-1: 1, 0: 0, 1: -1, 2: -2}, GIMarshallingTests.ghashtable_int_none_return())
1173 def test_ghashtable_int_none_return2(self):
1174 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_return())
1176 def test_ghashtable_int_container_return(self):
1177 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_return())
1179 def test_ghashtable_int_full_return(self):
1180 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_return())
1182 def test_ghashtable_int_none_in(self):
1183 GIMarshallingTests.ghashtable_int_none_in({-1: 1, 0: 0, 1: -1, 2: -2})
1185 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, '0': 0, 1: -1, 2: -2})
1186 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, 0: '0', 1: -1, 2: -2})
1188 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, '{-1: 1, 0: 0, 1: -1, 2: -2}')
1189 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, None)
1191 def test_ghashtable_utf8_none_in(self):
1192 GIMarshallingTests.ghashtable_utf8_none_in({'-1': '1', '0': '0', '1': '-1', '2': '-2'})
1194 def test_ghashtable_utf8_none_out(self):
1195 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_out())
1197 def test_ghashtable_utf8_container_out(self):
1198 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_out())
1200 def test_ghashtable_utf8_full_out(self):
1201 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_out())
1203 def test_ghashtable_utf8_none_inout(self):
1204 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1205 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1206 GIMarshallingTests.ghashtable_utf8_none_inout(i))
1208 def test_ghashtable_utf8_container_inout(self):
1209 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1210 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1211 GIMarshallingTests.ghashtable_utf8_container_inout(i))
1213 def test_ghashtable_utf8_full_inout(self):
1214 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1215 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1216 GIMarshallingTests.ghashtable_utf8_full_inout(i))
1219 class TestGValue(unittest.TestCase):
1221 def test_gvalue_return(self):
1222 self.assertEqual(42, GIMarshallingTests.gvalue_return())
1224 def test_gvalue_in(self):
1225 GIMarshallingTests.gvalue_in(42)
1226 value = GObject.Value(GObject.TYPE_INT, 42)
1227 GIMarshallingTests.gvalue_in(value)
1229 def test_gvalue_int64_in(self):
1230 value = GObject.Value(GObject.TYPE_INT64, GObject.G_MAXINT64)
1231 GIMarshallingTests.gvalue_int64_in(value)
1233 def test_gvalue_in_with_type(self):
1234 value = GObject.Value(GObject.TYPE_STRING, 'foo')
1235 GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_STRING)
1237 value = GObject.Value(GIMarshallingTests.Flags.__gtype__,
1238 GIMarshallingTests.Flags.VALUE1)
1239 GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_FLAGS)
1241 def test_gvalue_in_enum(self):
1242 value = GObject.Value(GIMarshallingTests.Enum.__gtype__,
1243 GIMarshallingTests.Enum.VALUE3)
1244 GIMarshallingTests.gvalue_in_enum(value)
1246 def test_gvalue_out(self):
1247 self.assertEqual(42, GIMarshallingTests.gvalue_out())
1249 def test_gvalue_int64_out(self):
1250 self.assertEqual(GObject.G_MAXINT64, GIMarshallingTests.gvalue_int64_out())
1252 def test_gvalue_out_caller_allocates(self):
1253 self.assertEqual(42, GIMarshallingTests.gvalue_out_caller_allocates())
1255 def test_gvalue_inout(self):
1256 self.assertEqual('42', GIMarshallingTests.gvalue_inout(42))
1257 value = GObject.Value(int, 42)
1258 self.assertEqual('42', GIMarshallingTests.gvalue_inout(value))
1260 def test_gvalue_flat_array_in(self):
1261 # the function already asserts the correct values
1262 GIMarshallingTests.gvalue_flat_array([42, "42", True])
1264 def test_gvalue_flat_array_out(self):
1265 values = GIMarshallingTests.return_gvalue_flat_array()
1266 self.assertEqual(values, [42, '42', True])
1268 def test_gvalue_gobject_ref_counts(self):
1269 # Tests a GObject held by a GValue
1270 obj = GObject.Object()
1271 ref = weakref.ref(obj)
1272 grefcount = obj.__grefcount__
1274 value = GObject.Value()
1275 value.init(GObject.TYPE_OBJECT)
1277 # TYPE_OBJECT will inc ref count as it should
1278 value.set_object(obj)
1279 self.assertEqual(obj.__grefcount__, grefcount + 1)
1281 # multiple set_object should not inc ref count
1282 value.set_object(obj)
1283 self.assertEqual(obj.__grefcount__, grefcount + 1)
1285 # get_object will re-use the same wrapper as obj
1286 res = value.get_object()
1287 self.assertEqual(obj, res)
1288 self.assertEqual(obj.__grefcount__, grefcount + 1)
1290 # multiple get_object should not inc ref count
1291 res = value.get_object()
1292 self.assertEqual(obj.__grefcount__, grefcount + 1)
1294 # deletion of the result and value holder should bring the
1295 # refcount back to where we started
1299 self.assertEqual(obj.__grefcount__, grefcount)
1303 self.assertEqual(ref(), None)
1305 def test_gvalue_boxed_ref_counts(self):
1306 # Tests a boxed type wrapping a python object pointer (TYPE_PYOBJECT)
1312 ref = weakref.ref(obj)
1313 refcount = sys.getrefcount(obj)
1315 value = GObject.Value()
1316 value.init(GObject.TYPE_PYOBJECT)
1318 # boxed TYPE_PYOBJECT will inc ref count as it should
1319 value.set_boxed(obj)
1320 self.assertEqual(sys.getrefcount(obj), refcount + 1)
1322 # multiple set_boxed should not inc ref count
1323 value.set_boxed(obj)
1324 self.assertEqual(sys.getrefcount(obj), refcount + 1)
1326 res = value.get_boxed()
1327 self.assertEqual(obj, res)
1328 self.assertEqual(sys.getrefcount(obj), refcount + 2)
1330 # multiple get_boxed should not inc ref count
1331 res = value.get_boxed()
1332 self.assertEqual(sys.getrefcount(obj), refcount + 2)
1334 # deletion of the result and value holder should bring the
1335 # refcount back to where we started
1339 self.assertEqual(sys.getrefcount(obj), refcount)
1343 self.assertEqual(ref(), None)
1346 def disabled_test_gvalue_flat_array_round_trip(self):
1347 self.assertEqual([42, '42', True],
1348 GIMarshallingTests.gvalue_flat_array_round_trip(42, '42', True))
1351 class TestGClosure(unittest.TestCase):
1353 def test_gclosure_in(self):
1354 GIMarshallingTests.gclosure_in(lambda: 42)
1356 # test passing a closure between two C calls
1357 closure = GIMarshallingTests.gclosure_return()
1358 GIMarshallingTests.gclosure_in(closure)
1360 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, 42)
1361 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, None)
1364 class TestCallbacks(unittest.TestCase):
1365 def test_return_value_only(self):
1368 self.assertEqual(GIMarshallingTests.callback_return_value_only(cb), 5)
1370 def test_one_out_arg(self):
1373 self.assertAlmostEqual(GIMarshallingTests.callback_one_out_parameter(cb), 5.5)
1375 def test_multiple_out_args(self):
1378 res = GIMarshallingTests.callback_multiple_out_parameters(cb)
1379 self.assertAlmostEqual(res[0], 5.5)
1380 self.assertAlmostEqual(res[1], 42.0)
1382 def test_return_and_one_out_arg(self):
1385 res = GIMarshallingTests.callback_return_value_and_one_out_parameter(cb)
1386 self.assertEqual(res[0], 5)
1387 self.assertAlmostEqual(res[1], 42.0)
1389 def test_return_and_multiple_out_arg(self):
1391 return (5, 42, -1000)
1392 self.assertEqual(GIMarshallingTests.callback_return_value_and_multiple_out_parameters(cb),
1396 class TestPointer(unittest.TestCase):
1397 def test_pointer_in_return(self):
1398 self.assertEqual(GIMarshallingTests.pointer_in_return(42), 42)
1401 class TestEnum(unittest.TestCase):
1404 def setUpClass(cls):
1405 '''Run tests under a test locale.
1407 Upper case conversion of member names should not be locale specific
1408 e. g. in Turkish, "i".upper() == "i", which gives results like "iNVALiD"
1410 Run test under a locale which defines toupper('a') == 'a'
1412 cls.locale_dir = tempfile.mkdtemp()
1413 src = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'te_ST@nouppera')
1414 dest = os.path.join(cls.locale_dir, 'te_ST.UTF-8@nouppera')
1415 subprocess.check_call(['localedef', '-i', src, '-c', '-f', 'UTF-8', dest])
1416 os.environ['LOCPATH'] = cls.locale_dir
1417 locale.setlocale(locale.LC_ALL, 'te_ST.UTF-8@nouppera')
1420 def tearDownClass(cls):
1421 locale.setlocale(locale.LC_ALL, 'C')
1422 shutil.rmtree(cls.locale_dir)
1424 del os.environ['LOCPATH']
1428 def test_enum(self):
1429 self.assertTrue(issubclass(GIMarshallingTests.Enum, int))
1430 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE1, GIMarshallingTests.Enum))
1431 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE2, GIMarshallingTests.Enum))
1432 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE3, GIMarshallingTests.Enum))
1433 self.assertEqual(42, GIMarshallingTests.Enum.VALUE3)
1435 def test_value_nick_and_name(self):
1436 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_nick, 'value1')
1437 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_nick, 'value2')
1438 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_nick, 'value3')
1440 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE1')
1441 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE2')
1442 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE3')
1444 def test_enum_in(self):
1445 GIMarshallingTests.enum_in(GIMarshallingTests.Enum.VALUE3)
1446 GIMarshallingTests.enum_in(42)
1448 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 43)
1449 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 'GIMarshallingTests.Enum.VALUE3')
1451 def test_enum_return(self):
1452 enum = GIMarshallingTests.enum_returnv()
1453 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1454 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1456 def test_enum_out(self):
1457 enum = GIMarshallingTests.enum_out()
1458 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1459 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1461 def test_enum_inout(self):
1462 enum = GIMarshallingTests.enum_inout(GIMarshallingTests.Enum.VALUE3)
1463 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1464 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE1)
1466 def test_enum_second(self):
1467 # check for the bug where different non-gtype enums share the same class
1468 self.assertNotEqual(GIMarshallingTests.Enum, GIMarshallingTests.SecondEnum)
1470 # check that values are not being shared between different enums
1471 self.assertTrue(hasattr(GIMarshallingTests.SecondEnum, "SECONDVALUE1"))
1472 self.assertRaises(AttributeError, getattr, GIMarshallingTests.Enum, "SECONDVALUE1")
1473 self.assertTrue(hasattr(GIMarshallingTests.Enum, "VALUE1"))
1474 self.assertRaises(AttributeError, getattr, GIMarshallingTests.SecondEnum, "VALUE1")
1476 def test_enum_gtype_name_is_namespaced(self):
1477 self.assertEqual(GIMarshallingTests.Enum.__gtype__.name,
1478 'PyGIMarshallingTestsEnum')
1480 def test_enum_double_registration_error(self):
1481 # a warning is printed for double registration and pygobject will
1482 # also raise a RuntimeError.
1483 old_mask = GLib.log_set_always_fatal(GLib.LogLevelFlags.LEVEL_ERROR)
1485 self.assertRaises(RuntimeError,
1486 gi._gi.enum_register_new_gtype_and_add,
1487 GIMarshallingTests.Enum.__info__)
1489 GLib.log_set_always_fatal(old_mask)
1491 def test_enum_add_type_error(self):
1492 self.assertRaises(TypeError,
1494 GIMarshallingTests.NoTypeFlags.__gtype__)
1497 class TestGEnum(unittest.TestCase):
1499 def test_genum(self):
1500 self.assertTrue(issubclass(GIMarshallingTests.GEnum, GObject.GEnum))
1501 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE1, GIMarshallingTests.GEnum))
1502 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE2, GIMarshallingTests.GEnum))
1503 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE3, GIMarshallingTests.GEnum))
1504 self.assertEqual(42, GIMarshallingTests.GEnum.VALUE3)
1506 def test_value_nick_and_name(self):
1507 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_nick, 'value1')
1508 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_nick, 'value2')
1509 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_nick, 'value3')
1511 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE1')
1512 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE2')
1513 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE3')
1515 def test_genum_in(self):
1516 GIMarshallingTests.genum_in(GIMarshallingTests.GEnum.VALUE3)
1517 GIMarshallingTests.genum_in(42)
1519 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 43)
1520 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 'GIMarshallingTests.GEnum.VALUE3')
1522 def test_genum_return(self):
1523 genum = GIMarshallingTests.genum_returnv()
1524 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1525 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1527 def test_genum_out(self):
1528 genum = GIMarshallingTests.genum_out()
1529 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1530 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1532 def test_genum_inout(self):
1533 genum = GIMarshallingTests.genum_inout(GIMarshallingTests.GEnum.VALUE3)
1534 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1535 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE1)
1538 class TestGFlags(unittest.TestCase):
1540 def test_flags(self):
1541 self.assertTrue(issubclass(GIMarshallingTests.Flags, GObject.GFlags))
1542 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1, GIMarshallingTests.Flags))
1543 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE2, GIMarshallingTests.Flags))
1544 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE3, GIMarshallingTests.Flags))
1545 # __or__() operation should still return an instance, not an int.
1546 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1 | GIMarshallingTests.Flags.VALUE2,
1547 GIMarshallingTests.Flags))
1548 self.assertEqual(1 << 1, GIMarshallingTests.Flags.VALUE2)
1550 def test_value_nick_and_name(self):
1551 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_nick, 'value1')
1552 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_nick, 'value2')
1553 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_nick, 'value3')
1555 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE1')
1556 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE2')
1557 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE3')
1559 def test_flags_in(self):
1560 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2)
1561 # result of __or__() operation should still be valid instance, not an int.
1562 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE2)
1563 GIMarshallingTests.flags_in_zero(Number(0))
1565 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 1 << 1)
1566 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 'GIMarshallingTests.Flags.VALUE2')
1568 def test_flags_return(self):
1569 flags = GIMarshallingTests.flags_returnv()
1570 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1571 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1573 def test_flags_out(self):
1574 flags = GIMarshallingTests.flags_out()
1575 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1576 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1578 def test_flags_inout(self):
1579 flags = GIMarshallingTests.flags_inout(GIMarshallingTests.Flags.VALUE2)
1580 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1581 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE1)
1584 class TestNoTypeFlags(unittest.TestCase):
1586 def test_flags(self):
1587 self.assertTrue(issubclass(GIMarshallingTests.NoTypeFlags, GObject.GFlags))
1588 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1, GIMarshallingTests.NoTypeFlags))
1589 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE2, GIMarshallingTests.NoTypeFlags))
1590 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE3, GIMarshallingTests.NoTypeFlags))
1591 # __or__() operation should still return an instance, not an int.
1592 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1 | GIMarshallingTests.NoTypeFlags.VALUE2,
1593 GIMarshallingTests.NoTypeFlags))
1594 self.assertEqual(1 << 1, GIMarshallingTests.NoTypeFlags.VALUE2)
1596 def test_value_nick_and_name(self):
1597 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_nick, 'value1')
1598 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_nick, 'value2')
1599 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_nick, 'value3')
1601 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE1')
1602 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2')
1603 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE3')
1605 def test_flags_in(self):
1606 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2)
1607 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2 | GIMarshallingTests.NoTypeFlags.VALUE2)
1608 GIMarshallingTests.no_type_flags_in_zero(Number(0))
1610 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 1 << 1)
1611 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 'GIMarshallingTests.NoTypeFlags.VALUE2')
1613 def test_flags_return(self):
1614 flags = GIMarshallingTests.no_type_flags_returnv()
1615 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1616 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1618 def test_flags_out(self):
1619 flags = GIMarshallingTests.no_type_flags_out()
1620 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1621 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1623 def test_flags_inout(self):
1624 flags = GIMarshallingTests.no_type_flags_inout(GIMarshallingTests.NoTypeFlags.VALUE2)
1625 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1626 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE1)
1628 def test_flags_gtype_name_is_namespaced(self):
1629 self.assertEqual(GIMarshallingTests.NoTypeFlags.__gtype__.name,
1630 'PyGIMarshallingTestsNoTypeFlags')
1632 def test_flags_double_registration_error(self):
1633 # a warning is printed for double registration and pygobject will
1634 # also raise a RuntimeError.
1635 old_mask = GLib.log_set_always_fatal(GLib.LogLevelFlags.LEVEL_ERROR)
1637 self.assertRaises(RuntimeError,
1638 gi._gi.flags_register_new_gtype_and_add,
1639 GIMarshallingTests.NoTypeFlags.__info__)
1641 GLib.log_set_always_fatal(old_mask)
1644 class TestStructure(unittest.TestCase):
1646 def test_simple_struct(self):
1647 self.assertTrue(issubclass(GIMarshallingTests.SimpleStruct, GObject.GPointer))
1649 struct = GIMarshallingTests.SimpleStruct()
1650 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1652 self.assertEqual(0, struct.long_)
1653 self.assertEqual(0, struct.int8)
1658 self.assertEqual(6, struct.long_)
1659 self.assertEqual(7, struct.int8)
1663 def test_nested_struct(self):
1664 struct = GIMarshallingTests.NestedStruct()
1666 self.assertTrue(isinstance(struct.simple_struct, GIMarshallingTests.SimpleStruct))
1668 struct.simple_struct.long_ = 42
1669 self.assertEqual(42, struct.simple_struct.long_)
1673 def test_not_simple_struct(self):
1674 struct = GIMarshallingTests.NotSimpleStruct()
1675 self.assertEqual(None, struct.pointer)
1677 def test_simple_struct_return(self):
1678 struct = GIMarshallingTests.simple_struct_returnv()
1680 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1681 self.assertEqual(6, struct.long_)
1682 self.assertEqual(7, struct.int8)
1686 def test_simple_struct_in(self):
1687 struct = GIMarshallingTests.SimpleStruct()
1691 GIMarshallingTests.SimpleStruct.inv(struct)
1695 struct = GIMarshallingTests.NestedStruct()
1697 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, struct)
1701 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, None)
1703 def test_simple_struct_method(self):
1704 struct = GIMarshallingTests.SimpleStruct()
1712 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.method)
1714 def test_pointer_struct(self):
1715 self.assertTrue(issubclass(GIMarshallingTests.PointerStruct, GObject.GPointer))
1717 struct = GIMarshallingTests.PointerStruct()
1718 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1722 def test_pointer_struct_return(self):
1723 struct = GIMarshallingTests.pointer_struct_returnv()
1725 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1726 self.assertEqual(42, struct.long_)
1730 def test_pointer_struct_in(self):
1731 struct = GIMarshallingTests.PointerStruct()
1738 @unittest.skipUnless(hasattr(GIMarshallingTests.BoxedStruct, 'string_'),
1739 'too old gobject-introspection')
1740 def test_boxed_struct(self):
1741 self.assertTrue(issubclass(GIMarshallingTests.BoxedStruct, GObject.GBoxed))
1743 struct = GIMarshallingTests.BoxedStruct()
1744 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1746 self.assertEqual(0, struct.long_)
1747 self.assertEqual(None, struct.string_)
1748 self.assertEqual([], struct.g_strv)
1752 @unittest.skipUnless(hasattr(GIMarshallingTests.BoxedStruct, 'string_'),
1753 'too old gobject-introspection')
1754 def test_boxed_struct_new(self):
1755 struct = GIMarshallingTests.BoxedStruct.new()
1756 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1757 self.assertEqual(struct.long_, 0)
1758 self.assertEqual(struct.string_, None)
1762 @unittest.skipUnless(hasattr(GIMarshallingTests.BoxedStruct, 'string_'),
1763 'too old gobject-introspection')
1764 def test_boxed_struct_copy(self):
1765 struct = GIMarshallingTests.BoxedStruct()
1767 struct.string_ = 'hello'
1769 new_struct = struct.copy()
1770 self.assertTrue(isinstance(new_struct, GIMarshallingTests.BoxedStruct))
1771 self.assertEqual(new_struct.long_, 42)
1772 self.assertEqual(new_struct.string_, 'hello')
1777 @unittest.skipUnless(hasattr(GIMarshallingTests.BoxedStruct, 'string_'),
1778 'too old gobject-introspection')
1779 def test_boxed_struct_return(self):
1780 struct = GIMarshallingTests.boxed_struct_returnv()
1782 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1783 self.assertEqual(42, struct.long_)
1784 self.assertEqual('hello', struct.string_)
1785 self.assertEqual(['0', '1', '2'], struct.g_strv)
1789 def test_boxed_struct_in(self):
1790 struct = GIMarshallingTests.BoxedStruct()
1797 def test_boxed_struct_out(self):
1798 struct = GIMarshallingTests.boxed_struct_out()
1800 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1801 self.assertEqual(42, struct.long_)
1805 def test_boxed_struct_inout(self):
1806 in_struct = GIMarshallingTests.BoxedStruct()
1807 in_struct.long_ = 42
1809 out_struct = GIMarshallingTests.boxed_struct_inout(in_struct)
1811 self.assertTrue(isinstance(out_struct, GIMarshallingTests.BoxedStruct))
1812 self.assertEqual(0, out_struct.long_)
1817 def test_struct_field_assignment(self):
1818 struct = GIMarshallingTests.BoxedStruct()
1821 struct.string_ = 'hello'
1822 self.assertEqual(struct.long_, 42)
1823 self.assertEqual(struct.string_, 'hello')
1825 def test_union(self):
1826 union = GIMarshallingTests.Union()
1828 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1830 new_union = union.copy()
1831 self.assertTrue(isinstance(new_union, GIMarshallingTests.Union))
1836 def test_union_return(self):
1837 union = GIMarshallingTests.union_returnv()
1839 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1840 self.assertEqual(42, union.long_)
1844 def test_union_in(self):
1845 union = GIMarshallingTests.Union()
1852 def test_union_method(self):
1853 union = GIMarshallingTests.Union()
1860 self.assertRaises(TypeError, GIMarshallingTests.Union.method)
1863 class TestGObject(unittest.TestCase):
1865 def test_object(self):
1866 self.assertTrue(issubclass(GIMarshallingTests.Object, GObject.GObject))
1868 object_ = GIMarshallingTests.Object()
1869 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1870 self.assertEqual(object_.__grefcount__, 1)
1872 def test_object_new(self):
1873 object_ = GIMarshallingTests.Object.new(42)
1874 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1875 self.assertEqual(object_.__grefcount__, 1)
1877 def test_object_int(self):
1878 object_ = GIMarshallingTests.Object(int=42)
1879 self.assertEqual(object_.int_, 42)
1880 # FIXME: Don't work yet.
1882 # self.assertEqual(object_.int_, 0)
1884 def test_object_static_method(self):
1885 GIMarshallingTests.Object.static_method()
1887 def test_object_method(self):
1888 GIMarshallingTests.Object(int=42).method()
1889 self.assertRaises(TypeError, GIMarshallingTests.Object.method, GObject.GObject())
1890 self.assertRaises(TypeError, GIMarshallingTests.Object.method)
1892 def test_sub_object(self):
1893 self.assertTrue(issubclass(GIMarshallingTests.SubObject, GIMarshallingTests.Object))
1895 object_ = GIMarshallingTests.SubObject()
1896 self.assertTrue(isinstance(object_, GIMarshallingTests.SubObject))
1898 def test_sub_object_new(self):
1899 self.assertRaises(TypeError, GIMarshallingTests.SubObject.new, 42)
1901 def test_sub_object_static_method(self):
1902 object_ = GIMarshallingTests.SubObject()
1903 object_.static_method()
1905 def test_sub_object_method(self):
1906 object_ = GIMarshallingTests.SubObject(int=42)
1909 def test_sub_object_sub_method(self):
1910 object_ = GIMarshallingTests.SubObject()
1911 object_.sub_method()
1913 def test_sub_object_overwritten_method(self):
1914 object_ = GIMarshallingTests.SubObject()
1915 object_.overwritten_method()
1917 self.assertRaises(TypeError, GIMarshallingTests.SubObject.overwritten_method, GIMarshallingTests.Object())
1919 def test_sub_object_int(self):
1920 object_ = GIMarshallingTests.SubObject()
1921 self.assertEqual(object_.int_, 0)
1922 # FIXME: Don't work yet.
1924 # self.assertEqual(object_.int_, 42)
1926 def test_object_none_return(self):
1927 object_ = GIMarshallingTests.Object.none_return()
1928 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1929 self.assertEqual(object_.__grefcount__, 2)
1931 def test_object_full_return(self):
1932 object_ = GIMarshallingTests.Object.full_return()
1933 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1934 self.assertEqual(object_.__grefcount__, 1)
1936 def test_object_none_in(self):
1937 object_ = GIMarshallingTests.Object(int=42)
1938 GIMarshallingTests.Object.none_in(object_)
1939 self.assertEqual(object_.__grefcount__, 1)
1941 object_ = GIMarshallingTests.SubObject(int=42)
1942 GIMarshallingTests.Object.none_in(object_)
1944 object_ = GObject.GObject()
1945 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, object_)
1947 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, None)
1949 def test_object_none_out(self):
1950 object_ = GIMarshallingTests.Object.none_out()
1951 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1952 self.assertEqual(object_.__grefcount__, 2)
1954 new_object = GIMarshallingTests.Object.none_out()
1955 self.assertTrue(new_object is object_)
1957 def test_object_full_out(self):
1958 object_ = GIMarshallingTests.Object.full_out()
1959 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1960 self.assertEqual(object_.__grefcount__, 1)
1962 def test_object_none_inout(self):
1963 object_ = GIMarshallingTests.Object(int=42)
1964 new_object = GIMarshallingTests.Object.none_inout(object_)
1966 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1968 self.assertFalse(object_ is new_object)
1970 self.assertEqual(object_.__grefcount__, 1)
1971 self.assertEqual(new_object.__grefcount__, 2)
1973 new_new_object = GIMarshallingTests.Object.none_inout(object_)
1974 self.assertTrue(new_new_object is new_object)
1976 GIMarshallingTests.Object.none_inout(GIMarshallingTests.SubObject(int=42))
1978 def test_object_full_inout(self):
1979 object_ = GIMarshallingTests.Object(int=42)
1980 new_object = GIMarshallingTests.Object.full_inout(object_)
1982 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1984 self.assertFalse(object_ is new_object)
1986 self.assertEqual(object_.__grefcount__, 2)
1987 self.assertEqual(new_object.__grefcount__, 1)
1989 # FIXME: Doesn't actually return the same object.
1990 # def test_object_inout_same(self):
1991 # object_ = GIMarshallingTests.Object()
1992 # new_object = GIMarshallingTests.object_full_inout(object_)
1993 # self.assertTrue(object_ is new_object)
1994 # self.assertEqual(object_.__grefcount__, 1)
1997 class TestPythonGObject(unittest.TestCase):
1999 class Object(GIMarshallingTests.Object):
2000 return_for_caller_allocated_out_parameter = 'test caller alloc return'
2002 def __init__(self, int):
2003 GIMarshallingTests.Object.__init__(self)
2007 # Don't call super, which asserts that self.int == 42.
2010 def do_method_int8_in(self, int8):
2013 def do_method_int8_out(self):
2016 def do_method_int8_arg_and_out_caller(self, arg):
2019 def do_method_int8_arg_and_out_callee(self, arg):
2022 def do_method_str_arg_out_ret(self, arg):
2023 return (arg.upper(), len(arg))
2025 def do_method_with_default_implementation(self, int8):
2026 GIMarshallingTests.Object.do_method_with_default_implementation(self, int8)
2027 self.props.int += int8
2029 def do_vfunc_return_value_only(self):
2032 def do_vfunc_one_out_parameter(self):
2035 def do_vfunc_multiple_out_parameters(self):
2036 return (42.42, 3.14)
2038 def do_vfunc_return_value_and_one_out_parameter(self):
2041 def do_vfunc_return_value_and_multiple_out_parameters(self):
2044 def do_vfunc_caller_allocated_out_parameter(self):
2045 return self.return_for_caller_allocated_out_parameter
2047 class SubObject(GIMarshallingTests.SubObject):
2048 def __init__(self, int):
2049 GIMarshallingTests.SubObject.__init__(self)
2052 def do_method_with_default_implementation(self, int8):
2055 class Interface3Impl(GObject.Object, GIMarshallingTests.Interface3):
2057 GObject.Object.__init__(self)
2058 self.variants = None
2059 self.n_variants = None
2061 def do_test_variant_array_in(self, variants, n_variants):
2062 self.variants = variants
2063 self.n_variants = n_variants
2065 def test_object(self):
2066 self.assertTrue(issubclass(self.Object, GIMarshallingTests.Object))
2068 object_ = self.Object(int=42)
2069 self.assertTrue(isinstance(object_, self.Object))
2071 def test_object_method(self):
2072 self.Object(int=0).method()
2074 def test_object_vfuncs(self):
2075 object_ = self.Object(int=42)
2076 object_.method_int8_in(84)
2077 self.assertEqual(object_.val, 84)
2078 self.assertEqual(object_.method_int8_out(), 42)
2080 # can be dropped when bumping g-i dependencies to >= 1.35.2
2081 if hasattr(object_, 'method_int8_arg_and_out_caller'):
2082 self.assertEqual(object_.method_int8_arg_and_out_caller(42), 43)
2083 self.assertEqual(object_.method_int8_arg_and_out_callee(42), 43)
2084 self.assertEqual(object_.method_str_arg_out_ret('hello'), ('HELLO', 5))
2086 object_.method_with_default_implementation(42)
2087 self.assertEqual(object_.props.int, 84)
2089 self.assertEqual(object_.vfunc_return_value_only(), 4242)
2090 self.assertAlmostEqual(object_.vfunc_one_out_parameter(), 42.42, places=5)
2092 (a, b) = object_.vfunc_multiple_out_parameters()
2093 self.assertAlmostEqual(a, 42.42, places=5)
2094 self.assertAlmostEqual(b, 3.14, places=5)
2096 self.assertEqual(object_.vfunc_return_value_and_one_out_parameter(), (5, 42))
2097 self.assertEqual(object_.vfunc_return_value_and_multiple_out_parameters(), (5, 42, 99))
2099 self.assertEqual(object_.vfunc_caller_allocated_out_parameter(),
2100 object_.return_for_caller_allocated_out_parameter)
2102 class ObjectWithoutVFunc(GIMarshallingTests.Object):
2103 def __init__(self, int):
2104 GIMarshallingTests.Object.__init__(self)
2106 object_ = ObjectWithoutVFunc(int=42)
2107 object_.method_with_default_implementation(84)
2108 self.assertEqual(object_.props.int, 84)
2110 def test_vfunc_return_ref_count(self):
2111 obj = self.Object(int=42)
2112 ref_count = sys.getrefcount(obj.return_for_caller_allocated_out_parameter)
2113 ret = obj.vfunc_caller_allocated_out_parameter()
2116 # Make sure the return and what the vfunc returned
2117 # are equal but not the same object.
2118 self.assertEqual(ret, obj.return_for_caller_allocated_out_parameter)
2119 self.assertFalse(ret is obj.return_for_caller_allocated_out_parameter)
2120 self.assertEqual(sys.getrefcount(obj.return_for_caller_allocated_out_parameter),
2123 def test_subobject_parent_vfunc(self):
2124 object_ = self.SubObject(int=81)
2125 object_.method_with_default_implementation(87)
2126 self.assertEqual(object_.val, 87)
2128 def test_dynamic_module(self):
2129 from gi.module import DynamicModule
2130 self.assertTrue(isinstance(GObject, DynamicModule))
2132 def test_subobject_non_vfunc_do_method(self):
2133 class PythonObjectWithNonVFuncDoMethod:
2134 def do_not_a_vfunc(self):
2137 class ObjectOverrideNonVFuncDoMethod(GIMarshallingTests.Object, PythonObjectWithNonVFuncDoMethod):
2138 def do_not_a_vfunc(self):
2139 value = super(ObjectOverrideNonVFuncDoMethod, self).do_not_a_vfunc()
2142 object_ = ObjectOverrideNonVFuncDoMethod()
2143 self.assertEqual(18, object_.do_not_a_vfunc())
2145 def test_native_function_not_set_in_subclass_dict(self):
2146 # Previously, GI was setting virtual functions on the class as well
2147 # as any *native* class that subclasses it. Here we check that it is only
2148 # set on the class that the method is originally from.
2149 self.assertTrue('do_method_with_default_implementation' in GIMarshallingTests.Object.__dict__)
2150 self.assertTrue('do_method_with_default_implementation' not in GIMarshallingTests.SubObject.__dict__)
2152 def test_subobject_with_interface_and_non_vfunc_do_method(self):
2153 # There was a bug for searching for vfuncs in interfaces. It was
2154 # triggered by having a do_* method that wasn't overriding
2155 # a native vfunc, as well as inheriting from an interface.
2156 class GObjectSubclassWithInterface(GObject.GObject, GIMarshallingTests.Interface):
2157 def do_method_not_a_vfunc(self):
2160 def test_subsubobject(self):
2161 class SubSubSubObject(GIMarshallingTests.SubSubObject):
2162 def do_method_deep_hierarchy(self, num):
2163 self.props.int = num * 2
2165 sub_sub_sub_object = SubSubSubObject()
2166 GIMarshallingTests.SubSubObject.do_method_deep_hierarchy(sub_sub_sub_object, 5)
2167 self.assertEqual(sub_sub_sub_object.props.int, 5)
2169 def test_interface3impl(self):
2170 iface3 = self.Interface3Impl()
2171 variants = [GLib.Variant('i', 27), GLib.Variant('s', 'Hello')]
2172 iface3.test_variant_array_in(variants)
2173 self.assertEqual(iface3.n_variants, 2)
2174 self.assertEqual(iface3.variants[0].unpack(), 27)
2175 self.assertEqual(iface3.variants[1].unpack(), 'Hello')
2177 def test_python_subsubobject_vfunc(self):
2178 class PySubObject(GIMarshallingTests.Object):
2180 GIMarshallingTests.Object.__init__(self)
2181 self.sub_method_int8_called = 0
2183 def do_method_int8_in(self, int8):
2184 self.sub_method_int8_called += 1
2186 class PySubSubObject(PySubObject):
2188 PySubObject.__init__(self)
2189 self.subsub_method_int8_called = 0
2191 def do_method_int8_in(self, int8):
2192 self.subsub_method_int8_called += 1
2195 so.method_int8_in(1)
2196 self.assertEqual(so.sub_method_int8_called, 1)
2198 # it should call the method on the SubSub object only
2199 sso = PySubSubObject()
2200 sso.method_int8_in(1)
2201 self.assertEqual(sso.subsub_method_int8_called, 1)
2202 self.assertEqual(sso.sub_method_int8_called, 0)
2204 def test_callback_in_vfunc(self):
2205 class SubObject(GIMarshallingTests.Object):
2207 GObject.GObject.__init__(self)
2210 def do_vfunc_with_callback(self, callback):
2211 self.worked = callback(42) == 42
2213 _object = SubObject()
2214 _object.call_vfunc_with_callback()
2215 self.assertTrue(_object.worked)
2216 _object.worked = False
2217 _object.call_vfunc_with_callback()
2218 self.assertTrue(_object.worked)
2221 class TestMultiOutputArgs(unittest.TestCase):
2223 def test_int_out_out(self):
2224 self.assertEqual((6, 7), GIMarshallingTests.int_out_out())
2226 def test_int_return_out(self):
2227 self.assertEqual((6, 7), GIMarshallingTests.int_return_out())
2230 class TestGErrorException(unittest.TestCase):
2231 def test_gerror_exception(self):
2232 self.assertRaises(GObject.GError, GIMarshallingTests.gerror)
2234 GIMarshallingTests.gerror()
2236 etype, e = sys.exc_info()[:2]
2237 self.assertEqual(e.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2238 self.assertEqual(e.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2239 self.assertEqual(e.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2245 class TestInterfaces(unittest.TestCase):
2247 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2249 GObject.GObject.__init__(self)
2252 def do_test_int8_in(self, int8):
2256 self.instance = self.TestInterfaceImpl()
2258 def test_wrapper(self):
2259 self.assertTrue(issubclass(GIMarshallingTests.Interface, GObject.GInterface))
2260 self.assertEqual(GIMarshallingTests.Interface.__gtype__.name, 'GIMarshallingTestsInterface')
2261 self.assertRaises(NotImplementedError, GIMarshallingTests.Interface)
2263 def test_implementation(self):
2264 self.assertTrue(issubclass(self.TestInterfaceImpl, GIMarshallingTests.Interface))
2265 self.assertTrue(isinstance(self.instance, GIMarshallingTests.Interface))
2267 def test_int8_int(self):
2268 GIMarshallingTests.test_interface_test_int8_in(self.instance, 42)
2269 self.assertEqual(self.instance.val, 42)
2271 def test_subclass(self):
2272 class TestInterfaceImplA(self.TestInterfaceImpl):
2275 class TestInterfaceImplB(TestInterfaceImplA):
2278 instance = TestInterfaceImplA()
2279 GIMarshallingTests.test_interface_test_int8_in(instance, 42)
2280 self.assertEqual(instance.val, 42)
2283 # there was a problem with Python bailing out because of
2284 # http://en.wikipedia.org/wiki/Diamond_problem with interfaces,
2285 # which shouldn't really be a problem.
2287 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2290 class TestInterfaceImpl2(GIMarshallingTests.Interface,
2294 class TestInterfaceImpl3(self.TestInterfaceImpl,
2295 GIMarshallingTests.Interface2):
2298 def test_type_mismatch(self):
2299 obj = GIMarshallingTests.Object()
2301 # wrong type for first argument: interface
2302 enum = Gio.File.new_for_path('.').enumerate_children(
2303 '', Gio.FileQueryInfoFlags.NONE, None)
2306 self.fail('call with wrong type argument unexpectedly succeeded')
2307 except TypeError as e:
2308 # should have argument name
2309 self.assertTrue('cancellable' in str(e), e)
2310 # should have expected type
2311 self.assertTrue('xpected Gio.Cancellable' in str(e), e)
2312 # should have actual type
2313 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2315 # wrong type for self argument: interface
2317 Gio.FileEnumerator.next_file(obj, None)
2318 self.fail('call with wrong type argument unexpectedly succeeded')
2319 except TypeError as e:
2320 if sys.version_info < (3, 0):
2321 self.assertTrue('FileEnumerator' in str(e), e)
2322 self.assertTrue('Object' in str(e), e)
2324 # should have argument name
2325 self.assertTrue('self' in str(e), e)
2326 # should have expected type
2327 self.assertTrue('xpected Gio.FileEnumerator' in str(e), e)
2328 # should have actual type
2329 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2331 # wrong type for first argument: GObject
2332 var = GLib.Variant('s', 'mystring')
2333 action = Gio.SimpleAction.new('foo', var.get_type())
2335 action.activate(obj)
2336 self.fail('call with wrong type argument unexpectedly succeeded')
2337 except TypeError as e:
2338 # should have argument name
2339 self.assertTrue('parameter' in str(e), e)
2340 # should have expected type
2341 self.assertTrue('xpected GLib.Variant' in str(e), e)
2342 # should have actual type
2343 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2345 # wrong type for self argument: GObject
2347 Gio.SimpleAction.activate(obj, obj)
2348 self.fail('call with wrong type argument unexpectedly succeeded')
2349 except TypeError as e:
2350 if sys.version_info < (3, 0):
2351 self.assertTrue('SimpleAction' in str(e), e)
2352 self.assertTrue('Object' in str(e), e)
2354 # should have argument name
2355 self.assertTrue('self' in str(e), e)
2356 # should have expected type
2357 self.assertTrue('xpected Gio.Action' in str(e), e)
2358 # should have actual type
2359 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2362 class TestInterfaceClash(unittest.TestCase):
2364 def test_clash(self):
2366 class TestClash(GObject.GObject, GIMarshallingTests.Interface, GIMarshallingTests.Interface2):
2367 def do_test_int8_in(self, int8):
2371 self.assertRaises(TypeError, create_clash)
2374 class TestOverrides(unittest.TestCase):
2376 def test_constant(self):
2377 self.assertEqual(GIMarshallingTests.OVERRIDES_CONSTANT, 7)
2379 def test_struct(self):
2380 # Test that the constructor has been overridden.
2381 struct = GIMarshallingTests.OverridesStruct(42)
2383 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2385 # Test that the method has been overridden.
2386 self.assertEqual(6, struct.method())
2390 # Test that the overrides wrapper has been registered.
2391 struct = GIMarshallingTests.overrides_struct_returnv()
2393 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2397 def test_object(self):
2398 # Test that the constructor has been overridden.
2399 object_ = GIMarshallingTests.OverridesObject(42)
2401 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2403 # Test that the alternate constructor has been overridden.
2404 object_ = GIMarshallingTests.OverridesObject.new(42)
2406 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2408 # Test that the method has been overridden.
2409 self.assertEqual(6, object_.method())
2411 # Test that the overrides wrapper has been registered.
2412 object_ = GIMarshallingTests.OverridesObject.returnv()
2414 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2416 def test_module_name(self):
2418 self.assertEqual(GIMarshallingTests.OverridesStruct.__module__, 'gi.overrides.GIMarshallingTests')
2419 self.assertEqual(GIMarshallingTests.OverridesObject.__module__, 'gi.overrides.GIMarshallingTests')
2420 self.assertEqual(GObject.Object.__module__, 'gi.overrides.GObject')
2423 self.assertEqual(GIMarshallingTests.SubObject.__module__, 'gi.repository.GIMarshallingTests')
2424 # FIXME: does not work with TEST_NAMES='test_thread test_gi.TestOverrides',
2425 # it is importlib._bootstrap then
2426 #self.assertEqual(GObject.InitiallyUnowned.__module__, 'gi.repository.GObject')
2429 class TestDir(unittest.TestCase):
2430 def test_members_list(self):
2431 list = dir(GIMarshallingTests)
2432 self.assertTrue('OverridesStruct' in list)
2433 self.assertTrue('BoxedStruct' in list)
2434 self.assertTrue('OVERRIDES_CONSTANT' in list)
2435 self.assertTrue('GEnum' in list)
2436 self.assertTrue('int32_return_max' in list)
2438 def test_modules_list(self):
2439 import gi.repository
2440 list = dir(gi.repository)
2441 self.assertTrue('GIMarshallingTests' in list)
2443 # FIXME: test to see if a module which was not imported is in the list
2444 # we should be listing every typelib we find, not just the ones
2445 # which are imported
2447 # to test this I recommend we compile a fake module which
2448 # our tests would never import and check to see if it is
2451 # self.assertTrue('DoNotImportDummyTests' in list)
2454 class TestGErrorArrayInCrash(unittest.TestCase):
2455 # Previously there was a bug in invoke, in which C arrays were unwrapped
2456 # from inside GArrays to be passed to the C function. But when a GError was
2457 # set, invoke would attempt to free the C array as if it were a GArray.
2458 # This crash is only for C arrays. It does not happen for C functions which
2459 # take in GArrays. See https://bugzilla.gnome.org/show_bug.cgi?id=642708
2460 def test_gerror_array_in_crash(self):
2461 self.assertRaises(GObject.GError, GIMarshallingTests.gerror_array_in, [1, 2, 3])
2464 class TestGErrorOut(unittest.TestCase):
2465 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2466 def test_gerror_out(self):
2467 error, debug = GIMarshallingTests.gerror_out()
2469 self.assertIsInstance(error, GObject.GError)
2470 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2471 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2472 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2473 self.assertEqual(debug, GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE)
2476 class TestGErrorOutTransferNone(unittest.TestCase):
2477 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2478 def test_gerror_out_transfer_none(self):
2479 error, debug = GIMarshallingTests.gerror_out_transfer_none()
2481 self.assertIsInstance(error, GObject.GError)
2482 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2483 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2484 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2485 self.assertEqual(GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE, debug)
2488 class TestGErrorReturn(unittest.TestCase):
2489 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2490 def test_return_gerror(self):
2491 error = GIMarshallingTests.gerror_return()
2493 self.assertIsInstance(error, GObject.GError)
2494 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2495 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2496 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2499 class TestParamSpec(unittest.TestCase):
2500 # https://bugzilla.gnome.org/show_bug.cgi?id=682355
2501 @unittest.skipUnless(hasattr(GIMarshallingTests, 'param_spec_in_bool'),
2502 'too old gobject-introspection')
2503 @unittest.expectedFailure
2504 def test_param_spec_in_bool(self):
2505 ps = GObject.param_spec_boolean('mybool', 'test-bool', 'boolblurb',
2506 True, GObject.ParamFlags.READABLE)
2507 GIMarshallingTests.param_spec_in_bool(ps)
2509 def test_param_spec_return(self):
2510 obj = GIMarshallingTests.param_spec_return()
2511 self.assertEqual(obj.name, 'test-param')
2512 self.assertEqual(obj.nick, 'test')
2513 self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2515 def test_param_spec_out(self):
2516 obj = GIMarshallingTests.param_spec_out()
2517 self.assertEqual(obj.name, 'test-param')
2518 self.assertEqual(obj.nick, 'test')
2519 self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2522 class TestKeywordArgs(unittest.TestCase):
2524 def test_calling(self):
2525 kw_func = GIMarshallingTests.int_three_in_three_out
2527 self.assertEqual(kw_func(1, 2, 3), (1, 2, 3))
2528 self.assertEqual(kw_func(**{'a': 4, 'b': 5, 'c': 6}), (4, 5, 6))
2529 self.assertEqual(kw_func(1, **{'b': 7, 'c': 8}), (1, 7, 8))
2530 self.assertEqual(kw_func(1, 7, **{'c': 8}), (1, 7, 8))
2531 self.assertEqual(kw_func(1, c=8, **{'b': 7}), (1, 7, 8))
2532 self.assertEqual(kw_func(2, c=4, b=3), (2, 3, 4))
2533 self.assertEqual(kw_func(a=2, c=4, b=3), (2, 3, 4))
2535 def assertRaisesMessage(self, exception, message, func, *args, **kwargs):
2537 func(*args, **kwargs)
2539 (e_type, e) = sys.exc_info()[:2]
2540 if message is not None:
2541 self.assertEqual(str(e), message)
2545 msg = "%s() did not raise %s" % (func.__name__, exception.__name__)
2546 raise AssertionError(msg)
2548 def test_type_errors(self):
2550 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2551 GIMarshallingTests.int_three_in_three_out)
2552 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (1 given)",
2553 GIMarshallingTests.int_three_in_three_out, 1)
2554 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2555 GIMarshallingTests.int_three_in_three_out, *())
2556 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2557 GIMarshallingTests.int_three_in_three_out, *(), **{})
2558 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (0 given)",
2559 GIMarshallingTests.int_three_in_three_out, *(), **{'c': 4})
2561 # test too many args
2562 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (4 given)",
2563 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4))
2564 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (4 given)",
2565 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4), c=6)
2567 # test too many keyword args
2568 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got multiple values for keyword argument 'a'",
2569 GIMarshallingTests.int_three_in_three_out, 1, 2, 3, **{'a': 4, 'b': 5})
2570 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'd'",
2571 GIMarshallingTests.int_three_in_three_out, d=4)
2572 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'e'",
2573 GIMarshallingTests.int_three_in_three_out, **{'e': 2})
2575 def test_kwargs_are_not_modified(self):
2578 GIMarshallingTests.int_three_in_three_out(1, c=4, **d)
2579 self.assertEqual(d, d2)
2582 class TestPropertiesObject(unittest.TestCase):
2585 self.obj = GIMarshallingTests.PropertiesObject()
2587 def test_boolean(self):
2588 self.assertEqual(self.obj.props.some_boolean, False)
2589 self.obj.props.some_boolean = True
2590 self.assertEqual(self.obj.props.some_boolean, True)
2592 obj = GIMarshallingTests.PropertiesObject(some_boolean=True)
2593 self.assertEqual(obj.props.some_boolean, True)
2595 def test_char(self):
2596 self.assertEqual(self.obj.props.some_char, 0)
2597 self.obj.props.some_char = GObject.G_MAXINT8
2598 self.assertEqual(self.obj.props.some_char, GObject.G_MAXINT8)
2600 obj = GIMarshallingTests.PropertiesObject(some_char=-42)
2601 self.assertEqual(obj.props.some_char, -42)
2603 def test_uchar(self):
2604 self.assertEqual(self.obj.props.some_uchar, 0)
2605 self.obj.props.some_uchar = GObject.G_MAXUINT8
2606 self.assertEqual(self.obj.props.some_uchar, GObject.G_MAXUINT8)
2608 obj = GIMarshallingTests.PropertiesObject(some_uchar=42)
2609 self.assertEqual(obj.props.some_uchar, 42)
2612 self.assertEqual(self.obj.props.some_int, 0)
2613 self.obj.props.some_int = GObject.G_MAXINT
2614 self.assertEqual(self.obj.props.some_int, GObject.G_MAXINT)
2616 obj = GIMarshallingTests.PropertiesObject(some_int=-42)
2617 self.assertEqual(obj.props.some_int, -42)
2619 self.assertRaises(TypeError, setattr, self.obj.props, 'some_int', 'foo')
2620 self.assertRaises(TypeError, setattr, self.obj.props, 'some_int', None)
2622 self.assertEqual(obj.props.some_int, -42)
2624 def test_uint(self):
2625 self.assertEqual(self.obj.props.some_uint, 0)
2626 self.obj.props.some_uint = GObject.G_MAXUINT
2627 self.assertEqual(self.obj.props.some_uint, GObject.G_MAXUINT)
2629 obj = GIMarshallingTests.PropertiesObject(some_uint=42)
2630 self.assertEqual(obj.props.some_uint, 42)
2632 self.assertRaises(TypeError, setattr, self.obj.props, 'some_uint', 'foo')
2633 self.assertRaises(TypeError, setattr, self.obj.props, 'some_uint', None)
2635 self.assertEqual(obj.props.some_uint, 42)
2637 def test_long(self):
2638 self.assertEqual(self.obj.props.some_long, 0)
2639 self.obj.props.some_long = GObject.G_MAXLONG
2640 self.assertEqual(self.obj.props.some_long, GObject.G_MAXLONG)
2642 obj = GIMarshallingTests.PropertiesObject(some_long=-42)
2643 self.assertEqual(obj.props.some_long, -42)
2645 self.assertRaises(TypeError, setattr, self.obj.props, 'some_long', 'foo')
2646 self.assertRaises(TypeError, setattr, self.obj.props, 'some_long', None)
2648 self.assertEqual(obj.props.some_long, -42)
2650 def test_ulong(self):
2651 self.assertEqual(self.obj.props.some_ulong, 0)
2652 self.obj.props.some_ulong = GObject.G_MAXULONG
2653 self.assertEqual(self.obj.props.some_ulong, GObject.G_MAXULONG)
2655 obj = GIMarshallingTests.PropertiesObject(some_ulong=42)
2656 self.assertEqual(obj.props.some_ulong, 42)
2658 self.assertRaises(TypeError, setattr, self.obj.props, 'some_ulong', 'foo')
2659 self.assertRaises(TypeError, setattr, self.obj.props, 'some_ulong', None)
2661 self.assertEqual(obj.props.some_ulong, 42)
2663 def test_int64(self):
2664 self.assertEqual(self.obj.props.some_int64, 0)
2665 self.obj.props.some_int64 = GObject.G_MAXINT64
2666 self.assertEqual(self.obj.props.some_int64, GObject.G_MAXINT64)
2668 obj = GIMarshallingTests.PropertiesObject(some_int64=-4200000000000000)
2669 self.assertEqual(obj.props.some_int64, -4200000000000000)
2671 def test_uint64(self):
2672 self.assertEqual(self.obj.props.some_uint64, 0)
2673 self.obj.props.some_uint64 = GObject.G_MAXUINT64
2674 self.assertEqual(self.obj.props.some_uint64, GObject.G_MAXUINT64)
2676 obj = GIMarshallingTests.PropertiesObject(some_uint64=4200000000000000)
2677 self.assertEqual(obj.props.some_uint64, 4200000000000000)
2679 def test_float(self):
2680 self.assertEqual(self.obj.props.some_float, 0)
2681 self.obj.props.some_float = GObject.G_MAXFLOAT
2682 self.assertEqual(self.obj.props.some_float, GObject.G_MAXFLOAT)
2684 obj = GIMarshallingTests.PropertiesObject(some_float=42.42)
2685 self.assertAlmostEqual(obj.props.some_float, 42.42, 4)
2687 obj = GIMarshallingTests.PropertiesObject(some_float=42)
2688 self.assertAlmostEqual(obj.props.some_float, 42.0, 4)
2690 self.assertRaises(TypeError, setattr, self.obj.props, 'some_float', 'foo')
2691 self.assertRaises(TypeError, setattr, self.obj.props, 'some_float', None)
2693 self.assertAlmostEqual(obj.props.some_float, 42.0, 4)
2695 def test_double(self):
2696 self.assertEqual(self.obj.props.some_double, 0)
2697 self.obj.props.some_double = GObject.G_MAXDOUBLE
2698 self.assertEqual(self.obj.props.some_double, GObject.G_MAXDOUBLE)
2700 obj = GIMarshallingTests.PropertiesObject(some_double=42.42)
2701 self.assertAlmostEqual(obj.props.some_double, 42.42)
2703 obj = GIMarshallingTests.PropertiesObject(some_double=42)
2704 self.assertAlmostEqual(obj.props.some_double, 42.0)
2706 self.assertRaises(TypeError, setattr, self.obj.props, 'some_double', 'foo')
2707 self.assertRaises(TypeError, setattr, self.obj.props, 'some_double', None)
2709 self.assertAlmostEqual(obj.props.some_double, 42.0)
2711 def test_strv(self):
2712 self.assertEqual(self.obj.props.some_strv, [])
2713 self.obj.props.some_strv = ['hello', 'world']
2714 self.assertEqual(self.obj.props.some_strv, ['hello', 'world'])
2716 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', 1)
2717 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', 'foo')
2718 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', [1, 2])
2719 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', ['foo', 1])
2721 self.assertEqual(self.obj.props.some_strv, ['hello', 'world'])
2723 obj = GIMarshallingTests.PropertiesObject(some_strv=['hello', 'world'])
2724 self.assertEqual(obj.props.some_strv, ['hello', 'world'])
2726 def test_boxed_struct(self):
2727 self.assertEqual(self.obj.props.some_boxed_struct, None)
2730 __gtype__ = GObject.TYPE_STRV
2732 struct1 = GIMarshallingTests.BoxedStruct()
2735 self.obj.props.some_boxed_struct = struct1
2736 self.assertEqual(self.obj.props.some_boxed_struct.long_, 1)
2737 self.assertEqual(self.obj.some_boxed_struct.long_, 1)
2739 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_struct', 1)
2740 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_struct', 'foo')
2742 obj = GIMarshallingTests.PropertiesObject(some_boxed_struct=struct1)
2743 self.assertEqual(obj.props.some_boxed_struct.long_, 1)
2745 @unittest.skipUnless(hasattr(GIMarshallingTests.PropertiesObject, 'some_boxed_glist'),
2746 'too old gobject-introspection')
2747 def test_boxed_glist(self):
2748 self.assertEqual(self.obj.props.some_boxed_glist, [])
2750 l = [GObject.G_MININT, 42, GObject.G_MAXINT]
2751 self.obj.props.some_boxed_glist = l
2752 self.assertEqual(self.obj.props.some_boxed_glist, l)
2753 self.obj.props.some_boxed_glist = []
2754 self.assertEqual(self.obj.props.some_boxed_glist, [])
2756 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_glist', 1)
2757 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_glist', 'foo')
2758 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_glist', ['a'])
2760 @unittest.expectedFailure
2761 def test_boxed_glist_ctor(self):
2762 l = [GObject.G_MININT, 42, GObject.G_MAXINT]
2763 obj = GIMarshallingTests.PropertiesObject(some_boxed_glist=l)
2764 self.assertEqual(obj.props.some_boxed_glist, l)
2766 @unittest.skipUnless(hasattr(GIMarshallingTests.PropertiesObject, 'some_variant'),
2767 'too old gobject-introspection')
2768 def test_variant(self):
2769 self.assertEqual(self.obj.props.some_variant, None)
2771 self.obj.props.some_variant = GLib.Variant('o', '/myobj')
2772 self.assertEqual(self.obj.props.some_variant.get_type_string(), 'o')
2773 self.assertEqual(self.obj.props.some_variant.print_(False), "'/myobj'")
2775 self.obj.props.some_variant = None
2776 self.assertEqual(self.obj.props.some_variant, None)
2778 obj = GIMarshallingTests.PropertiesObject(some_variant=GLib.Variant('b', True))
2779 self.assertEqual(obj.props.some_variant.get_type_string(), 'b')
2780 self.assertEqual(obj.props.some_variant.get_boolean(), True)
2782 self.assertRaises(TypeError, setattr, self.obj.props, 'some_variant', 'foo')
2783 self.assertRaises(TypeError, setattr, self.obj.props, 'some_variant', 23)
2785 self.assertEqual(obj.props.some_variant.get_type_string(), 'b')
2786 self.assertEqual(obj.props.some_variant.get_boolean(), True)
2789 class TestKeywords(unittest.TestCase):
2790 def test_method(self):
2792 v = GLib.Variant('i', 1)
2793 self.assertEqual(v.print_(False), '1')
2795 def test_function(self):
2797 self.assertEqual(GLib.Thread.yield_(), None)
2799 def test_struct_method(self):
2800 # g_timer_continue()
2801 # we cannot currently instantiate GLib.Timer objects, so just ensure
2803 self.assertTrue(callable(GLib.Timer.continue_))
2805 def test_uppercase(self):
2806 self.assertEqual(GLib.IOCondition.IN.value_nicks, ['in'])
2809 class TestModule(unittest.TestCase):
2810 def test_path(self):
2811 self.assertTrue(GIMarshallingTests.__path__.endswith('GIMarshallingTests-1.0.typelib'),
2812 GIMarshallingTests.__path__)
2815 self.assertTrue("'GIMarshallingTests' from '" in str(GIMarshallingTests),
2816 str(GIMarshallingTests))
2819 _dir = dir(GIMarshallingTests)
2820 self.assertGreater(len(_dir), 10)
2822 self.assertTrue('SimpleStruct' in _dir)
2823 self.assertTrue('Interface2' in _dir)
2824 self.assertTrue('CONSTANT_GERROR_CODE' in _dir)
2825 self.assertTrue('array_zero_terminated_inout' in _dir)
2827 # assert that dir() does not contain garbage
2828 for item_name in _dir:
2829 item = getattr(GIMarshallingTests, item_name)
2830 self.assertTrue(hasattr(item, '__class__'))
2832 def test_help(self):
2833 orig_stdout = sys.stdout
2835 if sys.version_info < (3, 0):
2836 sys.stdout = BytesIO()
2838 sys.stdout = StringIO()
2839 help(GIMarshallingTests)
2840 output = sys.stdout.getvalue()
2842 sys.stdout = orig_stdout
2844 self.assertTrue('SimpleStruct' in output, output)
2845 self.assertTrue('Interface2' in output, output)
2846 self.assertTrue('method_array_inout' in output, output)
2849 class TestProjectVersion(unittest.TestCase):
2850 def test_version_str(self):
2851 self.assertGreaterEqual(gi.__version__, "3.3.5")
2853 def test_version_info(self):
2854 self.assertEqual(len(gi.version_info), 3)
2855 self.assertGreaterEqual(gi.version_info, (3, 3, 5))
2857 def test_check_version(self):
2858 self.assertRaises(ValueError, gi.check_version, (99, 0, 0))
2859 self.assertRaises(ValueError, gi.check_version, "99.0.0")
2860 gi.check_version((3, 3, 5))
2861 gi.check_version("3.3.5")
2864 class TestObjectInfo(unittest.TestCase):
2865 def test_get_abstract_with_abstract(self):
2866 repo = gi.gi.Repository.get_default()
2867 info = repo.find_by_name('GObject', 'TypeModule')
2868 self.assertTrue(info.get_abstract())
2870 def test_get_abstract_with_concrete(self):
2871 repo = gi.gi.Repository.get_default()
2872 info = repo.find_by_name('GObject', 'Object')
2873 self.assertFalse(info.get_abstract())
2876 class TestSignatureArgs(unittest.TestCase):
2877 def test_split_args_multi_out(self):
2878 in_args, out_args = gi.types.split_function_info_args(GIMarshallingTests.int_out_out.__info__)
2879 self.assertEqual(len(in_args), 0)
2880 self.assertEqual(len(out_args), 2)
2881 self.assertEqual(out_args[0].get_pytype_hint(), 'int')
2882 self.assertEqual(out_args[1].get_pytype_hint(), 'int')
2884 def test_split_args_inout(self):
2885 in_args, out_args = gi.types.split_function_info_args(GIMarshallingTests.long_inout_max_min.__info__)
2886 self.assertEqual(len(in_args), 1)
2887 self.assertEqual(len(out_args), 1)
2888 self.assertEqual(in_args[0].get_name(), out_args[0].get_name())
2889 self.assertEqual(in_args[0].get_pytype_hint(), out_args[0].get_pytype_hint())
2891 def test_split_args_none(self):
2892 obj = GIMarshallingTests.Object(int=33)
2893 in_args, out_args = gi.types.split_function_info_args(obj.none_inout.__info__)
2894 self.assertEqual(len(in_args), 1)
2895 self.assertEqual(len(out_args), 1)
2897 def test_final_signature_with_full_inout(self):
2898 self.assertEqual(GIMarshallingTests.Object.full_inout.__doc__,
2899 'full_inout(object:GIMarshallingTests.Object) -> object:GIMarshallingTests.Object')
2901 def test_overridden_doc_is_not_clobbered(self):
2902 self.assertEqual(GIMarshallingTests.OverridesObject.method.__doc__,
2903 'Overridden doc string.')