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 def test_array_boxed_struct_take_in(self):
816 struct1 = GIMarshallingTests.BoxedStruct()
818 struct2 = GIMarshallingTests.BoxedStruct()
820 struct3 = GIMarshallingTests.BoxedStruct()
823 GIMarshallingTests.array_struct_take_in([struct1, struct2, struct3])
825 self.assertEqual(1, struct1.long_)
827 @unittest.expectedFailure
828 def test_array_boxed_struct_return(self):
829 (struct1, struct2, struct3) = GIMarshallingTests.array_zero_terminated_return_struct()
830 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct1))
831 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct2))
832 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct3))
833 # FIXME: gets bogus values
834 self.assertEqual(42, struct1.long_)
835 self.assertEqual(43, struct2.long_)
836 self.assertEqual(44, struct3.long_)
838 def test_array_simple_struct_in(self):
839 struct1 = GIMarshallingTests.SimpleStruct()
841 struct2 = GIMarshallingTests.SimpleStruct()
843 struct3 = GIMarshallingTests.SimpleStruct()
846 GIMarshallingTests.array_simple_struct_in([struct1, struct2, struct3])
848 def test_array_multi_array_key_value_in(self):
849 GIMarshallingTests.multi_array_key_value_in(["one", "two", "three"],
852 def test_array_in_nonzero_nonlen(self):
853 GIMarshallingTests.array_in_nonzero_nonlen(1, b'abcd')
855 def test_array_fixed_out_struct(self):
856 struct1, struct2 = GIMarshallingTests.array_fixed_out_struct()
858 self.assertEqual(7, struct1.long_)
859 self.assertEqual(6, struct1.int8)
860 self.assertEqual(6, struct2.long_)
861 self.assertEqual(7, struct2.int8)
863 def test_array_zero_terminated_return(self):
864 self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_return())
866 def test_array_zero_terminated_return_null(self):
867 self.assertEqual([], GIMarshallingTests.array_zero_terminated_return_null())
869 def test_array_zero_terminated_in(self):
870 GIMarshallingTests.array_zero_terminated_in(Sequence(['0', '1', '2']))
872 def test_array_zero_terminated_out(self):
873 self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
875 def test_array_zero_terminated_inout(self):
876 self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.array_zero_terminated_inout(['0', '1', '2']))
878 def test_init_function(self):
879 self.assertEqual((True, []), GIMarshallingTests.init_function([]))
880 self.assertEqual((True, []), GIMarshallingTests.init_function(['hello']))
881 self.assertEqual((True, ['hello']),
882 GIMarshallingTests.init_function(['hello', 'world']))
885 class TestGStrv(unittest.TestCase):
887 def test_gstrv_return(self):
888 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_return())
890 def test_gstrv_in(self):
891 GIMarshallingTests.gstrv_in(Sequence(['0', '1', '2']))
893 def test_gstrv_out(self):
894 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_out())
896 def test_gstrv_inout(self):
897 self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.gstrv_inout(['0', '1', '2']))
900 class TestArrayGVariant(unittest.TestCase):
902 def test_array_gvariant_none_in(self):
903 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
904 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_none_in(v)]
905 self.assertEqual([27, "Hello"], returned)
907 def test_array_gvariant_container_in(self):
908 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
909 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_container_in(v)]
910 self.assertEqual([27, "Hello"], returned)
912 def test_array_gvariant_full_in(self):
913 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
914 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_full_in(v)]
915 self.assertEqual([27, "Hello"], returned)
917 def test_bytearray_gvariant(self):
918 v = GLib.Variant.new_bytestring(b"foo")
919 self.assertEqual(v.get_bytestring(), b"foo")
922 class TestGArray(unittest.TestCase):
924 def test_garray_int_none_return(self):
925 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.garray_int_none_return())
927 def test_garray_uint64_none_return(self):
928 self.assertEqual([0, GObject.G_MAXUINT64], GIMarshallingTests.garray_uint64_none_return())
930 def test_garray_utf8_none_return(self):
931 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_return())
933 def test_garray_utf8_container_return(self):
934 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_return())
936 def test_garray_utf8_full_return(self):
937 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_return())
939 def test_garray_int_none_in(self):
940 GIMarshallingTests.garray_int_none_in(Sequence([-1, 0, 1, 2]))
942 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, Sequence([-1, '0', 1, 2]))
944 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, 42)
945 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, None)
947 def test_garray_uint64_none_in(self):
948 GIMarshallingTests.garray_uint64_none_in(Sequence([0, GObject.G_MAXUINT64]))
950 def test_garray_utf8_none_in(self):
951 GIMarshallingTests.garray_utf8_none_in(Sequence(['0', '1', '2']))
953 def test_garray_utf8_none_out(self):
954 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_out())
956 def test_garray_utf8_container_out(self):
957 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_out())
959 def test_garray_utf8_full_out(self):
960 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out())
962 @unittest.skipUnless(hasattr(GIMarshallingTests, 'garray_utf8_full_out_caller_allocated'),
963 'too old gobject-introspection')
964 def test_garray_utf8_full_out_caller_allocated(self):
965 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out_caller_allocated())
967 def test_garray_utf8_none_inout(self):
968 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_none_inout(Sequence(('0', '1', '2'))))
970 def test_garray_utf8_container_inout(self):
971 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_container_inout(['0', '1', '2']))
973 def test_garray_utf8_full_inout(self):
974 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_full_inout(['0', '1', '2']))
977 class TestGPtrArray(unittest.TestCase):
979 def test_gptrarray_utf8_none_return(self):
980 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_return())
982 def test_gptrarray_utf8_container_return(self):
983 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_return())
985 def test_gptrarray_utf8_full_return(self):
986 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_return())
988 def test_gptrarray_utf8_none_in(self):
989 GIMarshallingTests.gptrarray_utf8_none_in(Sequence(['0', '1', '2']))
991 def test_gptrarray_utf8_none_out(self):
992 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_out())
994 def test_gptrarray_utf8_container_out(self):
995 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_out())
997 def test_gptrarray_utf8_full_out(self):
998 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_out())
1000 def test_gptrarray_utf8_none_inout(self):
1001 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_none_inout(Sequence(('0', '1', '2'))))
1003 def test_gptrarray_utf8_container_inout(self):
1004 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_container_inout(['0', '1', '2']))
1006 def test_gptrarray_utf8_full_inout(self):
1007 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_full_inout(['0', '1', '2']))
1010 class TestGBytes(unittest.TestCase):
1011 def test_gbytes_create(self):
1012 b = GLib.Bytes.new(b'\x00\x01\xFF')
1013 self.assertEqual(3, b.get_size())
1014 self.assertEqual(b'\x00\x01\xFF', b.get_data())
1016 def test_gbytes_create_take(self):
1017 b = GLib.Bytes.new_take(b'\x00\x01\xFF')
1018 self.assertEqual(3, b.get_size())
1019 self.assertEqual(b'\x00\x01\xFF', b.get_data())
1021 @unittest.skipUnless(hasattr(GIMarshallingTests, 'gbytes_full_return'),
1022 'too old gobject-introspection')
1023 def test_gbytes_full_return(self):
1024 b = GIMarshallingTests.gbytes_full_return()
1025 self.assertEqual(4, b.get_size())
1026 self.assertEqual(b'\x00\x31\xFF\x33', b.get_data())
1028 @unittest.skipUnless(hasattr(GIMarshallingTests, 'gbytes_full_return'),
1029 'too old gobject-introspection')
1030 def test_gbytes_none_in(self):
1031 b = GIMarshallingTests.gbytes_full_return()
1032 GIMarshallingTests.gbytes_none_in(b)
1034 def test_compare(self):
1035 a1 = GLib.Bytes.new(b'\x00\x01\xFF')
1036 a2 = GLib.Bytes.new(b'\x00\x01\xFF')
1037 b = GLib.Bytes.new(b'\x00\x01\xFE')
1039 self.assertTrue(a1.equal(a2))
1040 self.assertTrue(a2.equal(a1))
1041 self.assertFalse(a1.equal(b))
1042 self.assertFalse(b.equal(a2))
1044 self.assertEqual(0, a1.compare(a2))
1045 self.assertEqual(1, a1.compare(b))
1046 self.assertEqual(-1, b.compare(a1))
1049 class TestGByteArray(unittest.TestCase):
1051 ba = GLib.ByteArray.new()
1052 self.assertEqual(b'', ba)
1054 ba = GLib.ByteArray.new_take(b'\x01\x02\xFF')
1055 self.assertEqual(b'\x01\x02\xFF', ba)
1057 def test_bytearray_full_return(self):
1058 self.assertEqual(b'\x001\xFF3', GIMarshallingTests.bytearray_full_return())
1060 def test_bytearray_none_in(self):
1061 GIMarshallingTests.bytearray_none_in(b'\x00\x31\xFF\x33')
1064 class TestGList(unittest.TestCase):
1066 def test_glist_int_none_return(self):
1067 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.glist_int_none_return())
1069 def test_glist_uint32_none_return(self):
1070 self.assertEqual([0, GObject.G_MAXUINT32], GIMarshallingTests.glist_uint32_none_return())
1072 def test_glist_utf8_none_return(self):
1073 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_return())
1075 def test_glist_utf8_container_return(self):
1076 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_return())
1078 def test_glist_utf8_full_return(self):
1079 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_return())
1081 def test_glist_int_none_in(self):
1082 GIMarshallingTests.glist_int_none_in(Sequence((-1, 0, 1, 2)))
1084 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, Sequence((-1, '0', 1, 2)))
1086 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, 42)
1087 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, None)
1089 def test_glist_uint32_none_in(self):
1090 GIMarshallingTests.glist_uint32_none_in(Sequence((0, GObject.G_MAXUINT32)))
1092 def test_glist_utf8_none_in(self):
1093 GIMarshallingTests.glist_utf8_none_in(Sequence(('0', '1', '2')))
1095 def test_glist_utf8_none_out(self):
1096 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_out())
1098 def test_glist_utf8_container_out(self):
1099 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_out())
1101 def test_glist_utf8_full_out(self):
1102 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_out())
1104 def test_glist_utf8_none_inout(self):
1105 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_none_inout(Sequence(('0', '1', '2'))))
1107 def test_glist_utf8_container_inout(self):
1108 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_container_inout(('0', '1', '2')))
1110 def test_glist_utf8_full_inout(self):
1111 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_full_inout(('0', '1', '2')))
1114 class TestGSList(unittest.TestCase):
1116 def test_gslist_int_none_return(self):
1117 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.gslist_int_none_return())
1119 def test_gslist_utf8_none_return(self):
1120 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_return())
1122 def test_gslist_utf8_container_return(self):
1123 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_return())
1125 def test_gslist_utf8_full_return(self):
1126 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_return())
1128 def test_gslist_int_none_in(self):
1129 GIMarshallingTests.gslist_int_none_in(Sequence((-1, 0, 1, 2)))
1131 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, Sequence((-1, '0', 1, 2)))
1133 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, 42)
1134 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, None)
1136 def test_gslist_utf8_none_in(self):
1137 GIMarshallingTests.gslist_utf8_none_in(Sequence(('0', '1', '2')))
1139 def test_gslist_utf8_none_out(self):
1140 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_out())
1142 def test_gslist_utf8_container_out(self):
1143 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_out())
1145 def test_gslist_utf8_full_out(self):
1146 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_out())
1148 def test_gslist_utf8_none_inout(self):
1149 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_none_inout(Sequence(('0', '1', '2'))))
1151 def test_gslist_utf8_container_inout(self):
1152 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_container_inout(('0', '1', '2')))
1154 def test_gslist_utf8_full_inout(self):
1155 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_full_inout(('0', '1', '2')))
1158 class TestGHashTable(unittest.TestCase):
1160 def test_ghashtable_int_none_return(self):
1161 self.assertEqual({-1: 1, 0: 0, 1: -1, 2: -2}, GIMarshallingTests.ghashtable_int_none_return())
1163 def test_ghashtable_int_none_return2(self):
1164 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_return())
1166 def test_ghashtable_int_container_return(self):
1167 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_return())
1169 def test_ghashtable_int_full_return(self):
1170 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_return())
1172 def test_ghashtable_int_none_in(self):
1173 GIMarshallingTests.ghashtable_int_none_in({-1: 1, 0: 0, 1: -1, 2: -2})
1175 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, '0': 0, 1: -1, 2: -2})
1176 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, 0: '0', 1: -1, 2: -2})
1178 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, '{-1: 1, 0: 0, 1: -1, 2: -2}')
1179 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, None)
1181 def test_ghashtable_utf8_none_in(self):
1182 GIMarshallingTests.ghashtable_utf8_none_in({'-1': '1', '0': '0', '1': '-1', '2': '-2'})
1184 def test_ghashtable_utf8_none_out(self):
1185 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_out())
1187 def test_ghashtable_utf8_container_out(self):
1188 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_out())
1190 def test_ghashtable_utf8_full_out(self):
1191 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_out())
1193 def test_ghashtable_utf8_none_inout(self):
1194 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1195 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1196 GIMarshallingTests.ghashtable_utf8_none_inout(i))
1198 def test_ghashtable_utf8_container_inout(self):
1199 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1200 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1201 GIMarshallingTests.ghashtable_utf8_container_inout(i))
1203 def test_ghashtable_utf8_full_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_full_inout(i))
1209 class TestGValue(unittest.TestCase):
1211 def test_gvalue_return(self):
1212 self.assertEqual(42, GIMarshallingTests.gvalue_return())
1214 def test_gvalue_in(self):
1215 GIMarshallingTests.gvalue_in(42)
1216 value = GObject.Value()
1217 value.init(GObject.TYPE_INT)
1219 GIMarshallingTests.gvalue_in(value)
1221 def test_gvalue_int64_in(self):
1222 value = GObject.Value()
1223 value.init(GObject.TYPE_INT64)
1224 value.set_int64(GObject.G_MAXINT64)
1225 GIMarshallingTests.gvalue_int64_in(value)
1227 def test_gvalue_in_with_type(self):
1228 value = GObject.Value()
1229 value.init(GObject.TYPE_STRING)
1230 value.set_string('foo')
1231 GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_STRING)
1233 value = GObject.Value()
1234 value.init(GIMarshallingTests.Flags.__gtype__)
1235 value.set_flags(GIMarshallingTests.Flags.VALUE1)
1236 GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_FLAGS)
1238 def test_gvalue_in_enum(self):
1239 value = GObject.Value()
1240 value.init(GIMarshallingTests.Enum.__gtype__)
1241 value.set_enum(GIMarshallingTests.Enum.VALUE3)
1242 GIMarshallingTests.gvalue_in_enum(value)
1244 def test_gvalue_out(self):
1245 self.assertEqual(42, GIMarshallingTests.gvalue_out())
1247 def test_gvalue_int64_out(self):
1248 self.assertEqual(GObject.G_MAXINT64, GIMarshallingTests.gvalue_int64_out())
1250 def test_gvalue_out_caller_allocates(self):
1251 self.assertEqual(42, GIMarshallingTests.gvalue_out_caller_allocates())
1253 def test_gvalue_inout(self):
1254 self.assertEqual('42', GIMarshallingTests.gvalue_inout(42))
1255 value = GObject.Value()
1256 value.init(GObject.TYPE_INT)
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 @unittest.expectedFailure
1371 def test_one_out_arg(self):
1374 self.assertAlmostEqual(GIMarshallingTests.callback_one_out_parameter(cb), 5.5)
1376 @unittest.expectedFailure
1377 def test_multiple_out_args(self):
1380 res = GIMarshallingTests.callback_multiple_out_parameters(cb)
1381 self.assertAlmostEqual(res[0], 5.5)
1382 self.assertAlmostEqual(res[1], 42.0)
1384 @unittest.expectedFailure
1385 def test_return_and_one_out_arg(self):
1388 res = GIMarshallingTests.callback_return_value_and_one_out_parameter(cb)
1389 self.assertEqual(res[0], 5)
1390 self.assertAlmostEqual(res[1], 42.0)
1392 @unittest.expectedFailure
1393 def test_return_and_multiple_out_arg(self):
1395 return (5, 42, -1000)
1396 self.assertEqual(GIMarshallingTests.callback_return_value_and_multiple_out_parameters(cb),
1400 class TestPointer(unittest.TestCase):
1401 def test_pointer_in_return(self):
1402 self.assertEqual(GIMarshallingTests.pointer_in_return(42), 42)
1405 class TestEnum(unittest.TestCase):
1408 def setUpClass(cls):
1409 '''Run tests under a test locale.
1411 Upper case conversion of member names should not be locale specific
1412 e. g. in Turkish, "i".upper() == "i", which gives results like "iNVALiD"
1414 Run test under a locale which defines toupper('a') == 'a'
1416 cls.locale_dir = tempfile.mkdtemp()
1417 src = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'te_ST@nouppera')
1418 dest = os.path.join(cls.locale_dir, 'te_ST.UTF-8@nouppera')
1419 subprocess.check_call(['localedef', '-i', src, '-c', '-f', 'UTF-8', dest])
1420 os.environ['LOCPATH'] = cls.locale_dir
1421 locale.setlocale(locale.LC_ALL, 'te_ST.UTF-8@nouppera')
1424 def tearDownClass(cls):
1425 locale.setlocale(locale.LC_ALL, 'C')
1426 shutil.rmtree(cls.locale_dir)
1428 del os.environ['LOCPATH']
1432 def test_enum(self):
1433 self.assertTrue(issubclass(GIMarshallingTests.Enum, int))
1434 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE1, GIMarshallingTests.Enum))
1435 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE2, GIMarshallingTests.Enum))
1436 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE3, GIMarshallingTests.Enum))
1437 self.assertEqual(42, GIMarshallingTests.Enum.VALUE3)
1439 def test_value_nick_and_name(self):
1440 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_nick, 'value1')
1441 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_nick, 'value2')
1442 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_nick, 'value3')
1444 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE1')
1445 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE2')
1446 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE3')
1448 def test_enum_in(self):
1449 GIMarshallingTests.enum_in(GIMarshallingTests.Enum.VALUE3)
1450 GIMarshallingTests.enum_in(42)
1452 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 43)
1453 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 'GIMarshallingTests.Enum.VALUE3')
1455 def test_enum_return(self):
1456 enum = GIMarshallingTests.enum_returnv()
1457 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1458 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1460 def test_enum_out(self):
1461 enum = GIMarshallingTests.enum_out()
1462 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1463 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1465 def test_enum_inout(self):
1466 enum = GIMarshallingTests.enum_inout(GIMarshallingTests.Enum.VALUE3)
1467 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1468 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE1)
1470 def test_enum_second(self):
1471 # check for the bug where different non-gtype enums share the same class
1472 self.assertNotEqual(GIMarshallingTests.Enum, GIMarshallingTests.SecondEnum)
1474 # check that values are not being shared between different enums
1475 self.assertTrue(hasattr(GIMarshallingTests.SecondEnum, "SECONDVALUE1"))
1476 self.assertRaises(AttributeError, getattr, GIMarshallingTests.Enum, "SECONDVALUE1")
1477 self.assertTrue(hasattr(GIMarshallingTests.Enum, "VALUE1"))
1478 self.assertRaises(AttributeError, getattr, GIMarshallingTests.SecondEnum, "VALUE1")
1481 class TestGEnum(unittest.TestCase):
1483 def test_genum(self):
1484 self.assertTrue(issubclass(GIMarshallingTests.GEnum, GObject.GEnum))
1485 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE1, GIMarshallingTests.GEnum))
1486 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE2, GIMarshallingTests.GEnum))
1487 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE3, GIMarshallingTests.GEnum))
1488 self.assertEqual(42, GIMarshallingTests.GEnum.VALUE3)
1490 def test_value_nick_and_name(self):
1491 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_nick, 'value1')
1492 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_nick, 'value2')
1493 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_nick, 'value3')
1495 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE1')
1496 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE2')
1497 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE3')
1499 def test_genum_in(self):
1500 GIMarshallingTests.genum_in(GIMarshallingTests.GEnum.VALUE3)
1501 GIMarshallingTests.genum_in(42)
1503 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 43)
1504 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 'GIMarshallingTests.GEnum.VALUE3')
1506 def test_genum_return(self):
1507 genum = GIMarshallingTests.genum_returnv()
1508 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1509 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1511 def test_genum_out(self):
1512 genum = GIMarshallingTests.genum_out()
1513 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1514 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1516 def test_genum_inout(self):
1517 genum = GIMarshallingTests.genum_inout(GIMarshallingTests.GEnum.VALUE3)
1518 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1519 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE1)
1522 class TestGFlags(unittest.TestCase):
1524 def test_flags(self):
1525 self.assertTrue(issubclass(GIMarshallingTests.Flags, GObject.GFlags))
1526 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1, GIMarshallingTests.Flags))
1527 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE2, GIMarshallingTests.Flags))
1528 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE3, GIMarshallingTests.Flags))
1529 # __or__() operation should still return an instance, not an int.
1530 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1 | GIMarshallingTests.Flags.VALUE2,
1531 GIMarshallingTests.Flags))
1532 self.assertEqual(1 << 1, GIMarshallingTests.Flags.VALUE2)
1534 def test_value_nick_and_name(self):
1535 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_nick, 'value1')
1536 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_nick, 'value2')
1537 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_nick, 'value3')
1539 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE1')
1540 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE2')
1541 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE3')
1543 def test_flags_in(self):
1544 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2)
1545 # result of __or__() operation should still be valid instance, not an int.
1546 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE2)
1547 GIMarshallingTests.flags_in_zero(Number(0))
1549 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 1 << 1)
1550 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 'GIMarshallingTests.Flags.VALUE2')
1552 def test_flags_return(self):
1553 flags = GIMarshallingTests.flags_returnv()
1554 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1555 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1557 def test_flags_out(self):
1558 flags = GIMarshallingTests.flags_out()
1559 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1560 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1562 def test_flags_inout(self):
1563 flags = GIMarshallingTests.flags_inout(GIMarshallingTests.Flags.VALUE2)
1564 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1565 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE1)
1568 class TestNoTypeFlags(unittest.TestCase):
1570 def test_flags(self):
1571 self.assertTrue(issubclass(GIMarshallingTests.NoTypeFlags, GObject.GFlags))
1572 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1, GIMarshallingTests.NoTypeFlags))
1573 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE2, GIMarshallingTests.NoTypeFlags))
1574 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE3, GIMarshallingTests.NoTypeFlags))
1575 # __or__() operation should still return an instance, not an int.
1576 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1 | GIMarshallingTests.NoTypeFlags.VALUE2,
1577 GIMarshallingTests.NoTypeFlags))
1578 self.assertEqual(1 << 1, GIMarshallingTests.NoTypeFlags.VALUE2)
1580 def test_value_nick_and_name(self):
1581 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_nick, 'value1')
1582 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_nick, 'value2')
1583 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_nick, 'value3')
1585 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE1')
1586 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2')
1587 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE3')
1589 def test_flags_in(self):
1590 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2)
1591 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2 | GIMarshallingTests.NoTypeFlags.VALUE2)
1592 GIMarshallingTests.no_type_flags_in_zero(Number(0))
1594 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 1 << 1)
1595 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 'GIMarshallingTests.NoTypeFlags.VALUE2')
1597 def test_flags_return(self):
1598 flags = GIMarshallingTests.no_type_flags_returnv()
1599 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1600 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1602 def test_flags_out(self):
1603 flags = GIMarshallingTests.no_type_flags_out()
1604 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1605 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1607 def test_flags_inout(self):
1608 flags = GIMarshallingTests.no_type_flags_inout(GIMarshallingTests.NoTypeFlags.VALUE2)
1609 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1610 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE1)
1613 class TestStructure(unittest.TestCase):
1615 def test_simple_struct(self):
1616 self.assertTrue(issubclass(GIMarshallingTests.SimpleStruct, GObject.GPointer))
1618 struct = GIMarshallingTests.SimpleStruct()
1619 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1621 self.assertEqual(0, struct.long_)
1622 self.assertEqual(0, struct.int8)
1627 self.assertEqual(6, struct.long_)
1628 self.assertEqual(7, struct.int8)
1632 def test_nested_struct(self):
1633 struct = GIMarshallingTests.NestedStruct()
1635 self.assertTrue(isinstance(struct.simple_struct, GIMarshallingTests.SimpleStruct))
1637 struct.simple_struct.long_ = 42
1638 self.assertEqual(42, struct.simple_struct.long_)
1642 def test_not_simple_struct(self):
1643 struct = GIMarshallingTests.NotSimpleStruct()
1644 self.assertEqual(None, struct.pointer)
1646 def test_simple_struct_return(self):
1647 struct = GIMarshallingTests.simple_struct_returnv()
1649 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1650 self.assertEqual(6, struct.long_)
1651 self.assertEqual(7, struct.int8)
1655 def test_simple_struct_in(self):
1656 struct = GIMarshallingTests.SimpleStruct()
1660 GIMarshallingTests.SimpleStruct.inv(struct)
1664 struct = GIMarshallingTests.NestedStruct()
1666 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, struct)
1670 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, None)
1672 def test_simple_struct_method(self):
1673 struct = GIMarshallingTests.SimpleStruct()
1681 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.method)
1683 def test_pointer_struct(self):
1684 self.assertTrue(issubclass(GIMarshallingTests.PointerStruct, GObject.GPointer))
1686 struct = GIMarshallingTests.PointerStruct()
1687 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1691 def test_pointer_struct_return(self):
1692 struct = GIMarshallingTests.pointer_struct_returnv()
1694 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1695 self.assertEqual(42, struct.long_)
1699 def test_pointer_struct_in(self):
1700 struct = GIMarshallingTests.PointerStruct()
1707 def test_boxed_struct(self):
1708 self.assertTrue(issubclass(GIMarshallingTests.BoxedStruct, GObject.GBoxed))
1710 struct = GIMarshallingTests.BoxedStruct()
1711 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1713 self.assertEqual(0, struct.long_)
1714 self.assertEqual([], struct.g_strv)
1718 def test_boxed_struct_new(self):
1719 struct = GIMarshallingTests.BoxedStruct.new()
1720 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1724 def test_boxed_struct_copy(self):
1725 struct = GIMarshallingTests.BoxedStruct()
1727 new_struct = struct.copy()
1728 self.assertTrue(isinstance(new_struct, GIMarshallingTests.BoxedStruct))
1733 def test_boxed_struct_return(self):
1734 struct = GIMarshallingTests.boxed_struct_returnv()
1736 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1737 self.assertEqual(42, struct.long_)
1738 self.assertEqual(['0', '1', '2'], struct.g_strv)
1742 def test_boxed_struct_in(self):
1743 struct = GIMarshallingTests.BoxedStruct()
1750 def test_boxed_struct_out(self):
1751 struct = GIMarshallingTests.boxed_struct_out()
1753 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1754 self.assertEqual(42, struct.long_)
1758 def test_boxed_struct_inout(self):
1759 in_struct = GIMarshallingTests.BoxedStruct()
1760 in_struct.long_ = 42
1762 out_struct = GIMarshallingTests.boxed_struct_inout(in_struct)
1764 self.assertTrue(isinstance(out_struct, GIMarshallingTests.BoxedStruct))
1765 self.assertEqual(0, out_struct.long_)
1770 def test_union(self):
1771 union = GIMarshallingTests.Union()
1773 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1775 new_union = union.copy()
1776 self.assertTrue(isinstance(new_union, GIMarshallingTests.Union))
1781 def test_union_return(self):
1782 union = GIMarshallingTests.union_returnv()
1784 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1785 self.assertEqual(42, union.long_)
1789 def test_union_in(self):
1790 union = GIMarshallingTests.Union()
1797 def test_union_method(self):
1798 union = GIMarshallingTests.Union()
1805 self.assertRaises(TypeError, GIMarshallingTests.Union.method)
1808 class TestGObject(unittest.TestCase):
1810 def test_object(self):
1811 self.assertTrue(issubclass(GIMarshallingTests.Object, GObject.GObject))
1813 object_ = GIMarshallingTests.Object()
1814 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1815 self.assertEqual(object_.__grefcount__, 1)
1817 def test_object_new(self):
1818 object_ = GIMarshallingTests.Object.new(42)
1819 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1820 self.assertEqual(object_.__grefcount__, 1)
1822 def test_object_int(self):
1823 object_ = GIMarshallingTests.Object(int=42)
1824 self.assertEqual(object_.int_, 42)
1825 # FIXME: Don't work yet.
1827 # self.assertEqual(object_.int_, 0)
1829 def test_object_static_method(self):
1830 GIMarshallingTests.Object.static_method()
1832 def test_object_method(self):
1833 GIMarshallingTests.Object(int=42).method()
1834 self.assertRaises(TypeError, GIMarshallingTests.Object.method, GObject.GObject())
1835 self.assertRaises(TypeError, GIMarshallingTests.Object.method)
1837 def test_sub_object(self):
1838 self.assertTrue(issubclass(GIMarshallingTests.SubObject, GIMarshallingTests.Object))
1840 object_ = GIMarshallingTests.SubObject()
1841 self.assertTrue(isinstance(object_, GIMarshallingTests.SubObject))
1843 def test_sub_object_new(self):
1844 self.assertRaises(TypeError, GIMarshallingTests.SubObject.new, 42)
1846 def test_sub_object_static_method(self):
1847 object_ = GIMarshallingTests.SubObject()
1848 object_.static_method()
1850 def test_sub_object_method(self):
1851 object_ = GIMarshallingTests.SubObject(int=42)
1854 def test_sub_object_sub_method(self):
1855 object_ = GIMarshallingTests.SubObject()
1856 object_.sub_method()
1858 def test_sub_object_overwritten_method(self):
1859 object_ = GIMarshallingTests.SubObject()
1860 object_.overwritten_method()
1862 self.assertRaises(TypeError, GIMarshallingTests.SubObject.overwritten_method, GIMarshallingTests.Object())
1864 def test_sub_object_int(self):
1865 object_ = GIMarshallingTests.SubObject()
1866 self.assertEqual(object_.int_, 0)
1867 # FIXME: Don't work yet.
1869 # self.assertEqual(object_.int_, 42)
1871 def test_object_none_return(self):
1872 object_ = GIMarshallingTests.Object.none_return()
1873 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1874 self.assertEqual(object_.__grefcount__, 2)
1876 def test_object_full_return(self):
1877 object_ = GIMarshallingTests.Object.full_return()
1878 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1879 self.assertEqual(object_.__grefcount__, 1)
1881 def test_object_none_in(self):
1882 object_ = GIMarshallingTests.Object(int=42)
1883 GIMarshallingTests.Object.none_in(object_)
1884 self.assertEqual(object_.__grefcount__, 1)
1886 object_ = GIMarshallingTests.SubObject(int=42)
1887 GIMarshallingTests.Object.none_in(object_)
1889 object_ = GObject.GObject()
1890 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, object_)
1892 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, None)
1894 def test_object_none_out(self):
1895 object_ = GIMarshallingTests.Object.none_out()
1896 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1897 self.assertEqual(object_.__grefcount__, 2)
1899 new_object = GIMarshallingTests.Object.none_out()
1900 self.assertTrue(new_object is object_)
1902 def test_object_full_out(self):
1903 object_ = GIMarshallingTests.Object.full_out()
1904 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1905 self.assertEqual(object_.__grefcount__, 1)
1907 def test_object_none_inout(self):
1908 object_ = GIMarshallingTests.Object(int=42)
1909 new_object = GIMarshallingTests.Object.none_inout(object_)
1911 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1913 self.assertFalse(object_ is new_object)
1915 self.assertEqual(object_.__grefcount__, 1)
1916 self.assertEqual(new_object.__grefcount__, 2)
1918 new_new_object = GIMarshallingTests.Object.none_inout(object_)
1919 self.assertTrue(new_new_object is new_object)
1921 GIMarshallingTests.Object.none_inout(GIMarshallingTests.SubObject(int=42))
1923 def test_object_full_inout(self):
1924 object_ = GIMarshallingTests.Object(int=42)
1925 new_object = GIMarshallingTests.Object.full_inout(object_)
1927 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1929 self.assertFalse(object_ is new_object)
1931 self.assertEqual(object_.__grefcount__, 2)
1932 self.assertEqual(new_object.__grefcount__, 1)
1934 # FIXME: Doesn't actually return the same object.
1935 # def test_object_inout_same(self):
1936 # object_ = GIMarshallingTests.Object()
1937 # new_object = GIMarshallingTests.object_full_inout(object_)
1938 # self.assertTrue(object_ is new_object)
1939 # self.assertEqual(object_.__grefcount__, 1)
1942 class TestPythonGObject(unittest.TestCase):
1944 class Object(GIMarshallingTests.Object):
1945 return_for_caller_allocated_out_parameter = 'test caller alloc return'
1947 def __init__(self, int):
1948 GIMarshallingTests.Object.__init__(self)
1952 # Don't call super, which asserts that self.int == 42.
1955 def do_method_int8_in(self, int8):
1958 def do_method_int8_out(self):
1961 def do_method_int8_arg_and_out_caller(self, arg):
1964 def do_method_int8_arg_and_out_callee(self, arg):
1967 def do_method_str_arg_out_ret(self, arg):
1968 return (arg.upper(), len(arg))
1970 def do_method_with_default_implementation(self, int8):
1971 GIMarshallingTests.Object.do_method_with_default_implementation(self, int8)
1972 self.props.int += int8
1974 def do_vfunc_return_value_only(self):
1977 def do_vfunc_one_out_parameter(self):
1980 def do_vfunc_multiple_out_parameters(self):
1981 return (42.42, 3.14)
1983 def do_vfunc_return_value_and_one_out_parameter(self):
1986 def do_vfunc_return_value_and_multiple_out_parameters(self):
1989 def do_vfunc_caller_allocated_out_parameter(self):
1990 return self.return_for_caller_allocated_out_parameter
1992 class SubObject(GIMarshallingTests.SubObject):
1993 def __init__(self, int):
1994 GIMarshallingTests.SubObject.__init__(self)
1997 def do_method_with_default_implementation(self, int8):
2000 class Interface3Impl(GObject.Object, GIMarshallingTests.Interface3):
2002 GObject.Object.__init__(self)
2003 self.variants = None
2004 self.n_variants = None
2006 def do_test_variant_array_in(self, variants, n_variants):
2007 self.variants = variants
2008 self.n_variants = n_variants
2010 def test_object(self):
2011 self.assertTrue(issubclass(self.Object, GIMarshallingTests.Object))
2013 object_ = self.Object(int=42)
2014 self.assertTrue(isinstance(object_, self.Object))
2016 def test_object_method(self):
2017 self.Object(int=0).method()
2019 def test_object_vfuncs(self):
2020 object_ = self.Object(int=42)
2021 object_.method_int8_in(84)
2022 self.assertEqual(object_.val, 84)
2023 self.assertEqual(object_.method_int8_out(), 42)
2025 # can be dropped when bumping g-i dependencies to >= 1.35.2
2026 if hasattr(object_, 'method_int8_arg_and_out_caller'):
2027 self.assertEqual(object_.method_int8_arg_and_out_caller(42), 43)
2028 self.assertEqual(object_.method_int8_arg_and_out_callee(42), 43)
2029 self.assertEqual(object_.method_str_arg_out_ret('hello'), ('HELLO', 5))
2031 object_.method_with_default_implementation(42)
2032 self.assertEqual(object_.props.int, 84)
2034 self.assertEqual(object_.vfunc_return_value_only(), 4242)
2035 self.assertAlmostEqual(object_.vfunc_one_out_parameter(), 42.42, places=5)
2037 (a, b) = object_.vfunc_multiple_out_parameters()
2038 self.assertAlmostEqual(a, 42.42, places=5)
2039 self.assertAlmostEqual(b, 3.14, places=5)
2041 self.assertEqual(object_.vfunc_return_value_and_one_out_parameter(), (5, 42))
2042 self.assertEqual(object_.vfunc_return_value_and_multiple_out_parameters(), (5, 42, 99))
2044 self.assertEqual(object_.vfunc_caller_allocated_out_parameter(),
2045 object_.return_for_caller_allocated_out_parameter)
2047 class ObjectWithoutVFunc(GIMarshallingTests.Object):
2048 def __init__(self, int):
2049 GIMarshallingTests.Object.__init__(self)
2051 object_ = ObjectWithoutVFunc(int=42)
2052 object_.method_with_default_implementation(84)
2053 self.assertEqual(object_.props.int, 84)
2055 def test_vfunc_return_ref_count(self):
2056 obj = self.Object(int=42)
2057 ref_count = sys.getrefcount(obj.return_for_caller_allocated_out_parameter)
2058 ret = obj.vfunc_caller_allocated_out_parameter()
2061 # Make sure the return and what the vfunc returned
2062 # are equal but not the same object.
2063 self.assertEqual(ret, obj.return_for_caller_allocated_out_parameter)
2064 self.assertFalse(ret is obj.return_for_caller_allocated_out_parameter)
2065 self.assertEqual(sys.getrefcount(obj.return_for_caller_allocated_out_parameter),
2068 def test_subobject_parent_vfunc(self):
2069 object_ = self.SubObject(int=81)
2070 object_.method_with_default_implementation(87)
2071 self.assertEqual(object_.val, 87)
2073 def test_dynamic_module(self):
2074 from gi.module import DynamicModule
2075 self.assertTrue(isinstance(GObject, DynamicModule))
2077 def test_subobject_non_vfunc_do_method(self):
2078 class PythonObjectWithNonVFuncDoMethod:
2079 def do_not_a_vfunc(self):
2082 class ObjectOverrideNonVFuncDoMethod(GIMarshallingTests.Object, PythonObjectWithNonVFuncDoMethod):
2083 def do_not_a_vfunc(self):
2084 value = super(ObjectOverrideNonVFuncDoMethod, self).do_not_a_vfunc()
2087 object_ = ObjectOverrideNonVFuncDoMethod()
2088 self.assertEqual(18, object_.do_not_a_vfunc())
2090 def test_native_function_not_set_in_subclass_dict(self):
2091 # Previously, GI was setting virtual functions on the class as well
2092 # as any *native* class that subclasses it. Here we check that it is only
2093 # set on the class that the method is originally from.
2094 self.assertTrue('do_method_with_default_implementation' in GIMarshallingTests.Object.__dict__)
2095 self.assertTrue('do_method_with_default_implementation' not in GIMarshallingTests.SubObject.__dict__)
2097 def test_subobject_with_interface_and_non_vfunc_do_method(self):
2098 # There was a bug for searching for vfuncs in interfaces. It was
2099 # triggered by having a do_* method that wasn't overriding
2100 # a native vfunc, as well as inheriting from an interface.
2101 class GObjectSubclassWithInterface(GObject.GObject, GIMarshallingTests.Interface):
2102 def do_method_not_a_vfunc(self):
2105 def test_subsubobject(self):
2106 class SubSubSubObject(GIMarshallingTests.SubSubObject):
2107 def do_method_deep_hierarchy(self, num):
2108 self.props.int = num * 2
2110 sub_sub_sub_object = SubSubSubObject()
2111 GIMarshallingTests.SubSubObject.do_method_deep_hierarchy(sub_sub_sub_object, 5)
2112 self.assertEqual(sub_sub_sub_object.props.int, 5)
2114 def test_interface3impl(self):
2115 iface3 = self.Interface3Impl()
2116 variants = [GLib.Variant('i', 27), GLib.Variant('s', 'Hello')]
2117 iface3.test_variant_array_in(variants)
2118 self.assertEqual(iface3.n_variants, 2)
2119 self.assertEqual(iface3.variants[0].unpack(), 27)
2120 self.assertEqual(iface3.variants[1].unpack(), 'Hello')
2122 def test_python_subsubobject_vfunc(self):
2123 class PySubObject(GIMarshallingTests.Object):
2125 GIMarshallingTests.Object.__init__(self)
2126 self.sub_method_int8_called = 0
2128 def do_method_int8_in(self, int8):
2129 self.sub_method_int8_called += 1
2131 class PySubSubObject(PySubObject):
2133 PySubObject.__init__(self)
2134 self.subsub_method_int8_called = 0
2136 def do_method_int8_in(self, int8):
2137 self.subsub_method_int8_called += 1
2140 so.method_int8_in(1)
2141 self.assertEqual(so.sub_method_int8_called, 1)
2143 # it should call the method on the SubSub object only
2144 sso = PySubSubObject()
2145 sso.method_int8_in(1)
2146 self.assertEqual(sso.subsub_method_int8_called, 1)
2147 self.assertEqual(sso.sub_method_int8_called, 0)
2149 def test_callback_in_vfunc(self):
2150 class SubObject(GIMarshallingTests.Object):
2152 GObject.GObject.__init__(self)
2155 def do_vfunc_with_callback(self, callback):
2156 self.worked = callback(42) == 42
2158 _object = SubObject()
2159 _object.call_vfunc_with_callback()
2160 self.assertTrue(_object.worked)
2161 _object.worked = False
2162 _object.call_vfunc_with_callback()
2163 self.assertTrue(_object.worked)
2166 class TestMultiOutputArgs(unittest.TestCase):
2168 def test_int_out_out(self):
2169 self.assertEqual((6, 7), GIMarshallingTests.int_out_out())
2171 def test_int_return_out(self):
2172 self.assertEqual((6, 7), GIMarshallingTests.int_return_out())
2175 class TestGErrorException(unittest.TestCase):
2176 def test_gerror_exception(self):
2177 self.assertRaises(GObject.GError, GIMarshallingTests.gerror)
2179 GIMarshallingTests.gerror()
2181 etype, e = sys.exc_info()[:2]
2182 self.assertEqual(e.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2183 self.assertEqual(e.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2184 self.assertEqual(e.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2190 class TestInterfaces(unittest.TestCase):
2192 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2194 GObject.GObject.__init__(self)
2197 def do_test_int8_in(self, int8):
2201 self.instance = self.TestInterfaceImpl()
2203 def test_wrapper(self):
2204 self.assertTrue(issubclass(GIMarshallingTests.Interface, GObject.GInterface))
2205 self.assertEqual(GIMarshallingTests.Interface.__gtype__.name, 'GIMarshallingTestsInterface')
2206 self.assertRaises(NotImplementedError, GIMarshallingTests.Interface)
2208 def test_implementation(self):
2209 self.assertTrue(issubclass(self.TestInterfaceImpl, GIMarshallingTests.Interface))
2210 self.assertTrue(isinstance(self.instance, GIMarshallingTests.Interface))
2212 def test_int8_int(self):
2213 GIMarshallingTests.test_interface_test_int8_in(self.instance, 42)
2214 self.assertEqual(self.instance.val, 42)
2216 def test_subclass(self):
2217 class TestInterfaceImplA(self.TestInterfaceImpl):
2220 class TestInterfaceImplB(TestInterfaceImplA):
2223 instance = TestInterfaceImplA()
2224 GIMarshallingTests.test_interface_test_int8_in(instance, 42)
2225 self.assertEqual(instance.val, 42)
2228 # there was a problem with Python bailing out because of
2229 # http://en.wikipedia.org/wiki/Diamond_problem with interfaces,
2230 # which shouldn't really be a problem.
2232 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2235 class TestInterfaceImpl2(GIMarshallingTests.Interface,
2239 class TestInterfaceImpl3(self.TestInterfaceImpl,
2240 GIMarshallingTests.Interface2):
2243 def test_type_mismatch(self):
2244 obj = GIMarshallingTests.Object()
2246 # wrong type for first argument: interface
2247 enum = Gio.File.new_for_path('.').enumerate_children(
2248 '', Gio.FileQueryInfoFlags.NONE, None)
2251 self.fail('call with wrong type argument unexpectedly succeeded')
2252 except TypeError as e:
2253 # should have argument name
2254 self.assertTrue('cancellable' in str(e), e)
2255 # should have expected type
2256 self.assertTrue('xpected Gio.Cancellable' in str(e), e)
2257 # should have actual type
2258 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2260 # wrong type for self argument: interface
2262 Gio.FileEnumerator.next_file(obj, None)
2263 self.fail('call with wrong type argument unexpectedly succeeded')
2264 except TypeError as e:
2265 if sys.version_info < (3, 0):
2266 self.assertTrue('FileEnumerator' in str(e), e)
2267 self.assertTrue('Object' in str(e), e)
2269 # should have argument name
2270 self.assertTrue('self' in str(e), e)
2271 # should have expected type
2272 self.assertTrue('xpected Gio.FileEnumerator' in str(e), e)
2273 # should have actual type
2274 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2276 # wrong type for first argument: GObject
2277 var = GLib.Variant('s', 'mystring')
2278 action = Gio.SimpleAction.new('foo', var.get_type())
2280 action.activate(obj)
2281 self.fail('call with wrong type argument unexpectedly succeeded')
2282 except TypeError as e:
2283 # should have argument name
2284 self.assertTrue('parameter' in str(e), e)
2285 # should have expected type
2286 self.assertTrue('xpected GLib.Variant' in str(e), e)
2287 # should have actual type
2288 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2290 # wrong type for self argument: GObject
2292 Gio.SimpleAction.activate(obj, obj)
2293 self.fail('call with wrong type argument unexpectedly succeeded')
2294 except TypeError as e:
2295 if sys.version_info < (3, 0):
2296 self.assertTrue('SimpleAction' in str(e), e)
2297 self.assertTrue('Object' in str(e), e)
2299 # should have argument name
2300 self.assertTrue('self' in str(e), e)
2301 # should have expected type
2302 self.assertTrue('xpected Gio.Action' in str(e), e)
2303 # should have actual type
2304 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2307 class TestInterfaceClash(unittest.TestCase):
2309 def test_clash(self):
2311 class TestClash(GObject.GObject, GIMarshallingTests.Interface, GIMarshallingTests.Interface2):
2312 def do_test_int8_in(self, int8):
2316 self.assertRaises(TypeError, create_clash)
2319 class TestOverrides(unittest.TestCase):
2321 def test_constant(self):
2322 self.assertEqual(GIMarshallingTests.OVERRIDES_CONSTANT, 7)
2324 def test_struct(self):
2325 # Test that the constructor has been overridden.
2326 struct = GIMarshallingTests.OverridesStruct(42)
2328 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2330 # Test that the method has been overridden.
2331 self.assertEqual(6, struct.method())
2335 # Test that the overrides wrapper has been registered.
2336 struct = GIMarshallingTests.overrides_struct_returnv()
2338 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2342 def test_object(self):
2343 # Test that the constructor has been overridden.
2344 object_ = GIMarshallingTests.OverridesObject(42)
2346 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2348 # Test that the alternate constructor has been overridden.
2349 object_ = GIMarshallingTests.OverridesObject.new(42)
2351 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2353 # Test that the method has been overridden.
2354 self.assertEqual(6, object_.method())
2356 # Test that the overrides wrapper has been registered.
2357 object_ = GIMarshallingTests.OverridesObject.returnv()
2359 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2361 def test_module_name(self):
2363 self.assertEqual(GIMarshallingTests.OverridesStruct.__module__, 'gi.overrides.GIMarshallingTests')
2364 self.assertEqual(GIMarshallingTests.OverridesObject.__module__, 'gi.overrides.GIMarshallingTests')
2365 self.assertEqual(GObject.Object.__module__, 'gi.overrides.GObject')
2368 self.assertEqual(GIMarshallingTests.SubObject.__module__, 'gi.repository.GIMarshallingTests')
2369 # FIXME: does not work with TEST_NAMES='test_thread test_gi.TestOverrides',
2370 # it is importlib._bootstrap then
2371 #self.assertEqual(GObject.InitiallyUnowned.__module__, 'gi.repository.GObject')
2374 class TestDir(unittest.TestCase):
2375 def test_members_list(self):
2376 list = dir(GIMarshallingTests)
2377 self.assertTrue('OverridesStruct' in list)
2378 self.assertTrue('BoxedStruct' in list)
2379 self.assertTrue('OVERRIDES_CONSTANT' in list)
2380 self.assertTrue('GEnum' in list)
2381 self.assertTrue('int32_return_max' in list)
2383 def test_modules_list(self):
2384 import gi.repository
2385 list = dir(gi.repository)
2386 self.assertTrue('GIMarshallingTests' in list)
2388 # FIXME: test to see if a module which was not imported is in the list
2389 # we should be listing every typelib we find, not just the ones
2390 # which are imported
2392 # to test this I recommend we compile a fake module which
2393 # our tests would never import and check to see if it is
2396 # self.assertTrue('DoNotImportDummyTests' in list)
2399 class TestGErrorArrayInCrash(unittest.TestCase):
2400 # Previously there was a bug in invoke, in which C arrays were unwrapped
2401 # from inside GArrays to be passed to the C function. But when a GError was
2402 # set, invoke would attempt to free the C array as if it were a GArray.
2403 # This crash is only for C arrays. It does not happen for C functions which
2404 # take in GArrays. See https://bugzilla.gnome.org/show_bug.cgi?id=642708
2405 def test_gerror_array_in_crash(self):
2406 self.assertRaises(GObject.GError, GIMarshallingTests.gerror_array_in, [1, 2, 3])
2409 class TestGErrorOut(unittest.TestCase):
2410 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2411 def test_gerror_out(self):
2412 error, debug = GIMarshallingTests.gerror_out()
2414 self.assertIsInstance(error, GObject.GError)
2415 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2416 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2417 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2418 self.assertEqual(debug, GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE)
2421 class TestGErrorOutTransferNone(unittest.TestCase):
2422 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2423 def test_gerror_out_transfer_none(self):
2424 error, debug = GIMarshallingTests.gerror_out_transfer_none()
2426 self.assertIsInstance(error, GObject.GError)
2427 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2428 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2429 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2430 self.assertEqual(GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE, debug)
2433 class TestGErrorReturn(unittest.TestCase):
2434 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2435 def test_return_gerror(self):
2436 error = GIMarshallingTests.gerror_return()
2438 self.assertIsInstance(error, GObject.GError)
2439 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2440 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2441 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2444 class TestParamSpec(unittest.TestCase):
2445 def test_param_spec_return(self):
2446 obj = GIMarshallingTests.param_spec_return()
2447 self.assertEqual(obj.name, 'test-param')
2448 self.assertEqual(obj.nick, 'test')
2449 self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2451 def test_param_spec_out(self):
2452 obj = GIMarshallingTests.param_spec_out()
2453 self.assertEqual(obj.name, 'test-param')
2454 self.assertEqual(obj.nick, 'test')
2455 self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2458 class TestKeywordArgs(unittest.TestCase):
2460 def test_calling(self):
2461 kw_func = GIMarshallingTests.int_three_in_three_out
2463 self.assertEqual(kw_func(1, 2, 3), (1, 2, 3))
2464 self.assertEqual(kw_func(**{'a': 4, 'b': 5, 'c': 6}), (4, 5, 6))
2465 self.assertEqual(kw_func(1, **{'b': 7, 'c': 8}), (1, 7, 8))
2466 self.assertEqual(kw_func(1, 7, **{'c': 8}), (1, 7, 8))
2467 self.assertEqual(kw_func(1, c=8, **{'b': 7}), (1, 7, 8))
2468 self.assertEqual(kw_func(2, c=4, b=3), (2, 3, 4))
2469 self.assertEqual(kw_func(a=2, c=4, b=3), (2, 3, 4))
2471 def assertRaisesMessage(self, exception, message, func, *args, **kwargs):
2473 func(*args, **kwargs)
2475 (e_type, e) = sys.exc_info()[:2]
2476 if message is not None:
2477 self.assertEqual(str(e), message)
2481 msg = "%s() did not raise %s" % (func.__name__, exception.__name__)
2482 raise AssertionError(msg)
2484 def test_type_errors(self):
2486 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2487 GIMarshallingTests.int_three_in_three_out)
2488 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (1 given)",
2489 GIMarshallingTests.int_three_in_three_out, 1)
2490 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2491 GIMarshallingTests.int_three_in_three_out, *())
2492 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2493 GIMarshallingTests.int_three_in_three_out, *(), **{})
2494 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (0 given)",
2495 GIMarshallingTests.int_three_in_three_out, *(), **{'c': 4})
2497 # test too many args
2498 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (4 given)",
2499 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4))
2500 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (4 given)",
2501 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4), c=6)
2503 # test too many keyword args
2504 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got multiple values for keyword argument 'a'",
2505 GIMarshallingTests.int_three_in_three_out, 1, 2, 3, **{'a': 4, 'b': 5})
2506 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'd'",
2507 GIMarshallingTests.int_three_in_three_out, d=4)
2508 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'e'",
2509 GIMarshallingTests.int_three_in_three_out, **{'e': 2})
2511 def test_kwargs_are_not_modified(self):
2514 GIMarshallingTests.int_three_in_three_out(1, c=4, **d)
2515 self.assertEqual(d, d2)
2518 class TestPropertiesObject(unittest.TestCase):
2521 self.obj = GIMarshallingTests.PropertiesObject()
2523 def test_boolean(self):
2524 self.assertEqual(self.obj.props.some_boolean, False)
2525 self.obj.props.some_boolean = True
2526 self.assertEqual(self.obj.props.some_boolean, True)
2528 obj = GIMarshallingTests.PropertiesObject(some_boolean=True)
2529 self.assertEqual(obj.props.some_boolean, True)
2531 @unittest.expectedFailure
2532 def test_char(self):
2533 # gobject-introspection thinks it has a guint8 type tag, which is
2534 # wrong; this will raise an assertion critical which we need to ignore
2535 old_mask = GLib.log_set_always_fatal(
2536 GLib.LogLevelFlags.LEVEL_WARNING | GLib.LogLevelFlags.LEVEL_ERROR)
2538 self.assertEqual(self.obj.props.some_char, 0)
2539 self.obj.props.some_char = GObject.G_MAXINT8
2540 self.assertEqual(self.obj.props.some_char, GObject.G_MAXINT8)
2542 GLib.log_set_always_fatal(old_mask)
2544 obj = GIMarshallingTests.PropertiesObject(some_char=-42)
2545 self.assertEqual(obj.props.some_char, -42)
2547 def test_uchar(self):
2548 self.assertEqual(self.obj.props.some_uchar, 0)
2549 self.obj.props.some_uchar = GObject.G_MAXUINT8
2550 self.assertEqual(self.obj.props.some_uchar, GObject.G_MAXUINT8)
2552 obj = GIMarshallingTests.PropertiesObject(some_uchar=42)
2553 self.assertEqual(obj.props.some_uchar, 42)
2556 self.assertEqual(self.obj.props.some_int, 0)
2557 self.obj.props.some_int = GObject.G_MAXINT
2558 self.assertEqual(self.obj.props.some_int, GObject.G_MAXINT)
2560 obj = GIMarshallingTests.PropertiesObject(some_int=-42)
2561 self.assertEqual(obj.props.some_int, -42)
2563 self.assertRaises(TypeError, setattr, self.obj.props, 'some_int', 'foo')
2564 self.assertRaises(TypeError, setattr, self.obj.props, 'some_int', None)
2566 self.assertEqual(obj.props.some_int, -42)
2568 def test_uint(self):
2569 self.assertEqual(self.obj.props.some_uint, 0)
2570 self.obj.props.some_uint = GObject.G_MAXUINT
2571 self.assertEqual(self.obj.props.some_uint, GObject.G_MAXUINT)
2573 obj = GIMarshallingTests.PropertiesObject(some_uint=42)
2574 self.assertEqual(obj.props.some_uint, 42)
2576 self.assertRaises(TypeError, setattr, self.obj.props, 'some_uint', 'foo')
2577 self.assertRaises(TypeError, setattr, self.obj.props, 'some_uint', None)
2579 self.assertEqual(obj.props.some_uint, 42)
2581 def test_long(self):
2582 self.assertEqual(self.obj.props.some_long, 0)
2583 self.obj.props.some_long = GObject.G_MAXLONG
2584 self.assertEqual(self.obj.props.some_long, GObject.G_MAXLONG)
2586 obj = GIMarshallingTests.PropertiesObject(some_long=-42)
2587 self.assertEqual(obj.props.some_long, -42)
2589 self.assertRaises(TypeError, setattr, self.obj.props, 'some_long', 'foo')
2590 self.assertRaises(TypeError, setattr, self.obj.props, 'some_long', None)
2592 self.assertEqual(obj.props.some_long, -42)
2594 def test_ulong(self):
2595 self.assertEqual(self.obj.props.some_ulong, 0)
2596 self.obj.props.some_ulong = GObject.G_MAXULONG
2597 self.assertEqual(self.obj.props.some_ulong, GObject.G_MAXULONG)
2599 obj = GIMarshallingTests.PropertiesObject(some_ulong=42)
2600 self.assertEqual(obj.props.some_ulong, 42)
2602 self.assertRaises(TypeError, setattr, self.obj.props, 'some_ulong', 'foo')
2603 self.assertRaises(TypeError, setattr, self.obj.props, 'some_ulong', None)
2605 self.assertEqual(obj.props.some_ulong, 42)
2607 def test_int64(self):
2608 self.assertEqual(self.obj.props.some_int64, 0)
2609 self.obj.props.some_int64 = GObject.G_MAXINT64
2610 self.assertEqual(self.obj.props.some_int64, GObject.G_MAXINT64)
2612 obj = GIMarshallingTests.PropertiesObject(some_int64=-4200000000000000)
2613 self.assertEqual(obj.props.some_int64, -4200000000000000)
2615 def test_uint64(self):
2616 self.assertEqual(self.obj.props.some_uint64, 0)
2617 self.obj.props.some_uint64 = GObject.G_MAXUINT64
2618 self.assertEqual(self.obj.props.some_uint64, GObject.G_MAXUINT64)
2620 obj = GIMarshallingTests.PropertiesObject(some_uint64=4200000000000000)
2621 self.assertEqual(obj.props.some_uint64, 4200000000000000)
2623 def test_float(self):
2624 self.assertEqual(self.obj.props.some_float, 0)
2625 self.obj.props.some_float = GObject.G_MAXFLOAT
2626 self.assertEqual(self.obj.props.some_float, GObject.G_MAXFLOAT)
2628 obj = GIMarshallingTests.PropertiesObject(some_float=42.42)
2629 self.assertAlmostEqual(obj.props.some_float, 42.42, 4)
2631 obj = GIMarshallingTests.PropertiesObject(some_float=42)
2632 self.assertAlmostEqual(obj.props.some_float, 42.0, 4)
2634 self.assertRaises(TypeError, setattr, self.obj.props, 'some_float', 'foo')
2635 self.assertRaises(TypeError, setattr, self.obj.props, 'some_float', None)
2637 self.assertAlmostEqual(obj.props.some_float, 42.0, 4)
2639 def test_double(self):
2640 self.assertEqual(self.obj.props.some_double, 0)
2641 self.obj.props.some_double = GObject.G_MAXDOUBLE
2642 self.assertEqual(self.obj.props.some_double, GObject.G_MAXDOUBLE)
2644 obj = GIMarshallingTests.PropertiesObject(some_double=42.42)
2645 self.assertAlmostEqual(obj.props.some_double, 42.42)
2647 obj = GIMarshallingTests.PropertiesObject(some_double=42)
2648 self.assertAlmostEqual(obj.props.some_double, 42.0)
2650 self.assertRaises(TypeError, setattr, self.obj.props, 'some_double', 'foo')
2651 self.assertRaises(TypeError, setattr, self.obj.props, 'some_double', None)
2653 self.assertAlmostEqual(obj.props.some_double, 42.0)
2655 def test_strv(self):
2656 self.assertEqual(self.obj.props.some_strv, [])
2657 self.obj.props.some_strv = ['hello', 'world']
2658 self.assertEqual(self.obj.props.some_strv, ['hello', 'world'])
2660 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', 1)
2661 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', 'foo')
2662 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', [1, 2])
2663 self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', ['foo', 1])
2665 self.assertEqual(self.obj.props.some_strv, ['hello', 'world'])
2667 obj = GIMarshallingTests.PropertiesObject(some_strv=['hello', 'world'])
2668 self.assertEqual(obj.props.some_strv, ['hello', 'world'])
2670 def test_boxed_struct(self):
2671 self.assertEqual(self.obj.props.some_boxed_struct, None)
2674 __gtype__ = GObject.TYPE_STRV
2676 struct1 = GIMarshallingTests.BoxedStruct()
2679 self.obj.props.some_boxed_struct = struct1
2680 self.assertEqual(self.obj.props.some_boxed_struct.long_, 1)
2681 self.assertEqual(self.obj.some_boxed_struct.long_, 1)
2683 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_struct', 1)
2684 self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_struct', 'foo')
2686 obj = GIMarshallingTests.PropertiesObject(some_boxed_struct=struct1)
2687 self.assertEqual(obj.props.some_boxed_struct.long_, 1)
2689 @unittest.skipUnless(hasattr(GIMarshallingTests.PropertiesObject, 'some_variant'),
2690 'too old gobject-introspection')
2691 def test_variant(self):
2692 self.assertEqual(self.obj.props.some_variant, None)
2694 self.obj.props.some_variant = GLib.Variant('o', '/myobj')
2695 self.assertEqual(self.obj.props.some_variant.get_type_string(), 'o')
2696 self.assertEqual(self.obj.props.some_variant.print_(False), "'/myobj'")
2698 self.obj.props.some_variant = None
2699 self.assertEqual(self.obj.props.some_variant, None)
2701 obj = GIMarshallingTests.PropertiesObject(some_variant=GLib.Variant('b', True))
2702 self.assertEqual(obj.props.some_variant.get_type_string(), 'b')
2703 self.assertEqual(obj.props.some_variant.get_boolean(), True)
2705 self.assertRaises(TypeError, setattr, self.obj.props, 'some_variant', 'foo')
2706 self.assertRaises(TypeError, setattr, self.obj.props, 'some_variant', 23)
2708 self.assertEqual(obj.props.some_variant.get_type_string(), 'b')
2709 self.assertEqual(obj.props.some_variant.get_boolean(), True)
2712 class TestKeywords(unittest.TestCase):
2713 def test_method(self):
2715 v = GLib.Variant('i', 1)
2716 self.assertEqual(v.print_(False), '1')
2718 def test_function(self):
2720 self.assertEqual(GLib.Thread.yield_(), None)
2722 def test_struct_method(self):
2723 # g_timer_continue()
2724 # we cannot currently instantiate GLib.Timer objects, so just ensure
2726 self.assertTrue(callable(GLib.Timer.continue_))
2728 def test_uppercase(self):
2729 self.assertEqual(GLib.IOCondition.IN.value_nicks, ['in'])
2732 class TestModule(unittest.TestCase):
2733 def test_path(self):
2734 self.assertTrue(GIMarshallingTests.__path__.endswith('GIMarshallingTests-1.0.typelib'),
2735 GIMarshallingTests.__path__)
2738 self.assertTrue("'GIMarshallingTests' from '" in str(GIMarshallingTests),
2739 str(GIMarshallingTests))
2742 _dir = dir(GIMarshallingTests)
2743 self.assertGreater(len(_dir), 10)
2745 self.assertTrue('SimpleStruct' in _dir)
2746 self.assertTrue('Interface2' in _dir)
2747 self.assertTrue('CONSTANT_GERROR_CODE' in _dir)
2748 self.assertTrue('array_zero_terminated_inout' in _dir)
2750 # assert that dir() does not contain garbage
2751 for item_name in _dir:
2752 item = getattr(GIMarshallingTests, item_name)
2753 self.assertTrue(hasattr(item, '__class__'))
2755 def test_help(self):
2756 orig_stdout = sys.stdout
2758 if sys.version_info < (3, 0):
2759 sys.stdout = BytesIO()
2761 sys.stdout = StringIO()
2762 help(GIMarshallingTests)
2763 output = sys.stdout.getvalue()
2765 sys.stdout = orig_stdout
2767 self.assertTrue('SimpleStruct' in output, output)
2768 self.assertTrue('Interface2' in output, output)
2769 self.assertTrue('method_array_inout' in output, output)
2772 class TestProjectVersion(unittest.TestCase):
2773 def test_version_str(self):
2774 self.assertGreaterEqual(gi.__version__, "3.3.5")
2776 def test_version_info(self):
2777 self.assertEqual(len(gi.version_info), 3)
2778 self.assertGreaterEqual(gi.version_info, (3, 3, 5))
2780 def test_check_version(self):
2781 self.assertRaises(ValueError, gi.check_version, (99, 0, 0))
2782 self.assertRaises(ValueError, gi.check_version, "99.0.0")
2783 gi.check_version((3, 3, 5))
2784 gi.check_version("3.3.5")
2787 class TestObjectInfo(unittest.TestCase):
2788 def test_get_abstract_with_abstract(self):
2789 repo = gi.gi.Repository.get_default()
2790 info = repo.find_by_name('GObject', 'TypeModule')
2791 self.assertTrue(info.get_abstract())
2793 def test_get_abstract_with_concrete(self):
2794 repo = gi.gi.Repository.get_default()
2795 info = repo.find_by_name('GObject', 'Object')
2796 self.assertFalse(info.get_abstract())
2799 class TestSignatureArgs(unittest.TestCase):
2800 def test_split_args_multi_out(self):
2801 in_args, out_args = gi.types.split_function_info_args(GIMarshallingTests.int_out_out.__info__)
2802 self.assertEqual(len(in_args), 0)
2803 self.assertEqual(len(out_args), 2)
2804 self.assertEqual(out_args[0].get_pytype_hint(), 'int')
2805 self.assertEqual(out_args[1].get_pytype_hint(), 'int')
2807 def test_split_args_inout(self):
2808 in_args, out_args = gi.types.split_function_info_args(GIMarshallingTests.long_inout_max_min.__info__)
2809 self.assertEqual(len(in_args), 1)
2810 self.assertEqual(len(out_args), 1)
2811 self.assertEqual(in_args[0].get_name(), out_args[0].get_name())
2812 self.assertEqual(in_args[0].get_pytype_hint(), out_args[0].get_pytype_hint())
2814 def test_split_args_none(self):
2815 obj = GIMarshallingTests.Object(int=33)
2816 in_args, out_args = gi.types.split_function_info_args(obj.none_inout.__info__)
2817 self.assertEqual(len(in_args), 1)
2818 self.assertEqual(len(out_args), 1)
2820 def test_final_signature_with_full_inout(self):
2821 self.assertEqual(GIMarshallingTests.Object.full_inout.__doc__,
2822 'full_inout(object:GIMarshallingTests.Object) -> object:GIMarshallingTests.Object')
2824 def test_overridden_doc_is_not_clobbered(self):
2825 self.assertEqual(GIMarshallingTests.OverridesObject.method.__doc__,
2826 'Overridden doc string.')