1 # -*- Mode: Python; py-indent-offset: 4 -*-
3 # vim: tabstop=4 shiftwidth=4 expandtab
13 from gi.repository import GObject, GLib
15 from gi.repository import GIMarshallingTests
17 from compathelper import _bytes
19 if sys.version_info < (3, 0):
20 CONSTANT_UTF8 = "const \xe2\x99\xa5 utf8"
21 PY2_UNICODE_UTF8 = unicode(CONSTANT_UTF8, 'UTF-8')
24 CONSTANT_UTF8 = "const ♥ utf8"
25 CHAR_255 = bytes([255])
32 def __init__(self, value):
36 return int(self.value)
39 return float(self.value)
42 class Sequence(object):
44 def __init__(self, sequence):
45 self.sequence = sequence
48 return len(self.sequence)
50 def __getitem__(self, key):
51 return self.sequence[key]
54 class TestConstant(unittest.TestCase):
56 # Blocked by https://bugzilla.gnome.org/show_bug.cgi?id=595773
57 # def test_constant_utf8(self):
58 # self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.CONSTANT_UTF8)
60 def test_constant_number(self):
61 self.assertEqual(CONSTANT_NUMBER, GIMarshallingTests.CONSTANT_NUMBER)
64 class TestBoolean(unittest.TestCase):
66 def test_boolean_return(self):
67 self.assertEqual(True, GIMarshallingTests.boolean_return_true())
68 self.assertEqual(False, GIMarshallingTests.boolean_return_false())
70 def test_boolean_in(self):
71 GIMarshallingTests.boolean_in_true(True)
72 GIMarshallingTests.boolean_in_false(False)
74 GIMarshallingTests.boolean_in_true(1)
75 GIMarshallingTests.boolean_in_false(0)
77 def test_boolean_out(self):
78 self.assertEqual(True, GIMarshallingTests.boolean_out_true())
79 self.assertEqual(False, GIMarshallingTests.boolean_out_false())
81 def test_boolean_inout(self):
82 self.assertEqual(False, GIMarshallingTests.boolean_inout_true_false(True))
83 self.assertEqual(True, GIMarshallingTests.boolean_inout_false_true(False))
86 class TestInt8(unittest.TestCase):
88 MAX = GObject.G_MAXINT8
89 MIN = GObject.G_MININT8
91 def test_int8_return(self):
92 self.assertEqual(self.MAX, GIMarshallingTests.int8_return_max())
93 self.assertEqual(self.MIN, GIMarshallingTests.int8_return_min())
95 def test_int8_in(self):
96 max = Number(self.MAX)
97 min = Number(self.MIN)
99 GIMarshallingTests.int8_in_max(max)
100 GIMarshallingTests.int8_in_min(min)
105 self.assertRaises(ValueError, GIMarshallingTests.int8_in_max, max)
106 self.assertRaises(ValueError, GIMarshallingTests.int8_in_min, min)
108 self.assertRaises(TypeError, GIMarshallingTests.int8_in_max, "self.MAX")
110 def test_int8_out(self):
111 self.assertEqual(self.MAX, GIMarshallingTests.int8_out_max())
112 self.assertEqual(self.MIN, GIMarshallingTests.int8_out_min())
114 def test_int8_inout(self):
115 self.assertEqual(self.MIN, GIMarshallingTests.int8_inout_max_min(Number(self.MAX)))
116 self.assertEqual(self.MAX, GIMarshallingTests.int8_inout_min_max(Number(self.MIN)))
119 class TestUInt8(unittest.TestCase):
121 MAX = GObject.G_MAXUINT8
123 def test_uint8_return(self):
124 self.assertEqual(self.MAX, GIMarshallingTests.uint8_return())
126 def test_uint8_in(self):
127 number = Number(self.MAX)
129 GIMarshallingTests.uint8_in(number)
130 GIMarshallingTests.uint8_in(CHAR_255)
133 self.assertRaises(ValueError, GIMarshallingTests.uint8_in, number)
134 self.assertRaises(ValueError, GIMarshallingTests.uint8_in, Number(-1))
136 self.assertRaises(TypeError, GIMarshallingTests.uint8_in, "self.MAX")
138 def test_uint8_out(self):
139 self.assertEqual(self.MAX, GIMarshallingTests.uint8_out())
141 def test_uint8_inout(self):
142 self.assertEqual(0, GIMarshallingTests.uint8_inout(Number(self.MAX)))
145 class TestInt16(unittest.TestCase):
147 MAX = GObject.G_MAXINT16
148 MIN = GObject.G_MININT16
150 def test_int16_return(self):
151 self.assertEqual(self.MAX, GIMarshallingTests.int16_return_max())
152 self.assertEqual(self.MIN, GIMarshallingTests.int16_return_min())
154 def test_int16_in(self):
155 max = Number(self.MAX)
156 min = Number(self.MIN)
158 GIMarshallingTests.int16_in_max(max)
159 GIMarshallingTests.int16_in_min(min)
164 self.assertRaises(ValueError, GIMarshallingTests.int16_in_max, max)
165 self.assertRaises(ValueError, GIMarshallingTests.int16_in_min, min)
167 self.assertRaises(TypeError, GIMarshallingTests.int16_in_max, "self.MAX")
169 def test_int16_out(self):
170 self.assertEqual(self.MAX, GIMarshallingTests.int16_out_max())
171 self.assertEqual(self.MIN, GIMarshallingTests.int16_out_min())
173 def test_int16_inout(self):
174 self.assertEqual(self.MIN, GIMarshallingTests.int16_inout_max_min(Number(self.MAX)))
175 self.assertEqual(self.MAX, GIMarshallingTests.int16_inout_min_max(Number(self.MIN)))
178 class TestUInt16(unittest.TestCase):
180 MAX = GObject.G_MAXUINT16
182 def test_uint16_return(self):
183 self.assertEqual(self.MAX, GIMarshallingTests.uint16_return())
185 def test_uint16_in(self):
186 number = Number(self.MAX)
188 GIMarshallingTests.uint16_in(number)
192 self.assertRaises(ValueError, GIMarshallingTests.uint16_in, number)
193 self.assertRaises(ValueError, GIMarshallingTests.uint16_in, Number(-1))
195 self.assertRaises(TypeError, GIMarshallingTests.uint16_in, "self.MAX")
197 def test_uint16_out(self):
198 self.assertEqual(self.MAX, GIMarshallingTests.uint16_out())
200 def test_uint16_inout(self):
201 self.assertEqual(0, GIMarshallingTests.uint16_inout(Number(self.MAX)))
204 class TestInt32(unittest.TestCase):
206 MAX = GObject.G_MAXINT32
207 MIN = GObject.G_MININT32
209 def test_int32_return(self):
210 self.assertEqual(self.MAX, GIMarshallingTests.int32_return_max())
211 self.assertEqual(self.MIN, GIMarshallingTests.int32_return_min())
213 def test_int32_in(self):
214 max = Number(self.MAX)
215 min = Number(self.MIN)
217 GIMarshallingTests.int32_in_max(max)
218 GIMarshallingTests.int32_in_min(min)
223 self.assertRaises(ValueError, GIMarshallingTests.int32_in_max, max)
224 self.assertRaises(ValueError, GIMarshallingTests.int32_in_min, min)
226 self.assertRaises(TypeError, GIMarshallingTests.int32_in_max, "self.MAX")
228 def test_int32_out(self):
229 self.assertEqual(self.MAX, GIMarshallingTests.int32_out_max())
230 self.assertEqual(self.MIN, GIMarshallingTests.int32_out_min())
232 def test_int32_inout(self):
233 self.assertEqual(self.MIN, GIMarshallingTests.int32_inout_max_min(Number(self.MAX)))
234 self.assertEqual(self.MAX, GIMarshallingTests.int32_inout_min_max(Number(self.MIN)))
237 class TestUInt32(unittest.TestCase):
239 MAX = GObject.G_MAXUINT32
241 def test_uint32_return(self):
242 self.assertEqual(self.MAX, GIMarshallingTests.uint32_return())
244 def test_uint32_in(self):
245 number = Number(self.MAX)
247 GIMarshallingTests.uint32_in(number)
251 self.assertRaises(ValueError, GIMarshallingTests.uint32_in, number)
252 self.assertRaises(ValueError, GIMarshallingTests.uint32_in, Number(-1))
254 self.assertRaises(TypeError, GIMarshallingTests.uint32_in, "self.MAX")
256 def test_uint32_out(self):
257 self.assertEqual(self.MAX, GIMarshallingTests.uint32_out())
259 def test_uint32_inout(self):
260 self.assertEqual(0, GIMarshallingTests.uint32_inout(Number(self.MAX)))
263 class TestInt64(unittest.TestCase):
268 def test_int64_return(self):
269 self.assertEqual(self.MAX, GIMarshallingTests.int64_return_max())
270 self.assertEqual(self.MIN, GIMarshallingTests.int64_return_min())
272 def test_int64_in(self):
273 max = Number(self.MAX)
274 min = Number(self.MIN)
276 GIMarshallingTests.int64_in_max(max)
277 GIMarshallingTests.int64_in_min(min)
282 self.assertRaises(ValueError, GIMarshallingTests.int64_in_max, max)
283 self.assertRaises(ValueError, GIMarshallingTests.int64_in_min, min)
285 self.assertRaises(TypeError, GIMarshallingTests.int64_in_max, "self.MAX")
287 def test_int64_out(self):
288 self.assertEqual(self.MAX, GIMarshallingTests.int64_out_max())
289 self.assertEqual(self.MIN, GIMarshallingTests.int64_out_min())
291 def test_int64_inout(self):
292 self.assertEqual(self.MIN, GIMarshallingTests.int64_inout_max_min(Number(self.MAX)))
293 self.assertEqual(self.MAX, GIMarshallingTests.int64_inout_min_max(Number(self.MIN)))
296 class TestUInt64(unittest.TestCase):
300 def test_uint64_return(self):
301 self.assertEqual(self.MAX, GIMarshallingTests.uint64_return())
303 def test_uint64_in(self):
304 number = Number(self.MAX)
306 GIMarshallingTests.uint64_in(number)
310 self.assertRaises(ValueError, GIMarshallingTests.uint64_in, number)
311 self.assertRaises(ValueError, GIMarshallingTests.uint64_in, Number(-1))
313 self.assertRaises(TypeError, GIMarshallingTests.uint64_in, "self.MAX")
315 def test_uint64_out(self):
316 self.assertEqual(self.MAX, GIMarshallingTests.uint64_out())
318 def test_uint64_inout(self):
319 self.assertEqual(0, GIMarshallingTests.uint64_inout(Number(self.MAX)))
322 class TestShort(unittest.TestCase):
324 MAX = GObject.constants.G_MAXSHORT
325 MIN = GObject.constants.G_MINSHORT
327 def test_short_return(self):
328 self.assertEqual(self.MAX, GIMarshallingTests.short_return_max())
329 self.assertEqual(self.MIN, GIMarshallingTests.short_return_min())
331 def test_short_in(self):
332 max = Number(self.MAX)
333 min = Number(self.MIN)
335 GIMarshallingTests.short_in_max(max)
336 GIMarshallingTests.short_in_min(min)
341 self.assertRaises(ValueError, GIMarshallingTests.short_in_max, max)
342 self.assertRaises(ValueError, GIMarshallingTests.short_in_min, min)
344 self.assertRaises(TypeError, GIMarshallingTests.short_in_max, "self.MAX")
346 def test_short_out(self):
347 self.assertEqual(self.MAX, GIMarshallingTests.short_out_max())
348 self.assertEqual(self.MIN, GIMarshallingTests.short_out_min())
350 def test_short_inout(self):
351 self.assertEqual(self.MIN, GIMarshallingTests.short_inout_max_min(Number(self.MAX)))
352 self.assertEqual(self.MAX, GIMarshallingTests.short_inout_min_max(Number(self.MIN)))
355 class TestUShort(unittest.TestCase):
357 MAX = GObject.constants.G_MAXUSHORT
359 def test_ushort_return(self):
360 self.assertEqual(self.MAX, GIMarshallingTests.ushort_return())
362 def test_ushort_in(self):
363 number = Number(self.MAX)
365 GIMarshallingTests.ushort_in(number)
369 self.assertRaises(ValueError, GIMarshallingTests.ushort_in, number)
370 self.assertRaises(ValueError, GIMarshallingTests.ushort_in, Number(-1))
372 self.assertRaises(TypeError, GIMarshallingTests.ushort_in, "self.MAX")
374 def test_ushort_out(self):
375 self.assertEqual(self.MAX, GIMarshallingTests.ushort_out())
377 def test_ushort_inout(self):
378 self.assertEqual(0, GIMarshallingTests.ushort_inout(Number(self.MAX)))
381 class TestInt(unittest.TestCase):
383 MAX = GObject.constants.G_MAXINT
384 MIN = GObject.constants.G_MININT
386 def test_int_return(self):
387 self.assertEqual(self.MAX, GIMarshallingTests.int_return_max())
388 self.assertEqual(self.MIN, GIMarshallingTests.int_return_min())
390 def test_int_in(self):
391 max = Number(self.MAX)
392 min = Number(self.MIN)
394 GIMarshallingTests.int_in_max(max)
395 GIMarshallingTests.int_in_min(min)
400 self.assertRaises(ValueError, GIMarshallingTests.int_in_max, max)
401 self.assertRaises(ValueError, GIMarshallingTests.int_in_min, min)
403 self.assertRaises(TypeError, GIMarshallingTests.int_in_max, "self.MAX")
405 def test_int_out(self):
406 self.assertEqual(self.MAX, GIMarshallingTests.int_out_max())
407 self.assertEqual(self.MIN, GIMarshallingTests.int_out_min())
409 def test_int_inout(self):
410 self.assertEqual(self.MIN, GIMarshallingTests.int_inout_max_min(Number(self.MAX)))
411 self.assertEqual(self.MAX, GIMarshallingTests.int_inout_min_max(Number(self.MIN)))
412 self.assertRaises(TypeError, GIMarshallingTests.int_inout_min_max, Number(self.MIN), CONSTANT_NUMBER)
415 class TestUInt(unittest.TestCase):
417 MAX = GObject.constants.G_MAXUINT
419 def test_uint_return(self):
420 self.assertEqual(self.MAX, GIMarshallingTests.uint_return())
422 def test_uint_in(self):
423 number = Number(self.MAX)
425 GIMarshallingTests.uint_in(number)
429 self.assertRaises(ValueError, GIMarshallingTests.uint_in, number)
430 self.assertRaises(ValueError, GIMarshallingTests.uint_in, Number(-1))
432 self.assertRaises(TypeError, GIMarshallingTests.uint_in, "self.MAX")
434 def test_uint_out(self):
435 self.assertEqual(self.MAX, GIMarshallingTests.uint_out())
437 def test_uint_inout(self):
438 self.assertEqual(0, GIMarshallingTests.uint_inout(Number(self.MAX)))
441 class TestLong(unittest.TestCase):
443 MAX = GObject.constants.G_MAXLONG
444 MIN = GObject.constants.G_MINLONG
446 def test_long_return(self):
447 self.assertEqual(self.MAX, GIMarshallingTests.long_return_max())
448 self.assertEqual(self.MIN, GIMarshallingTests.long_return_min())
450 def test_long_in(self):
451 max = Number(self.MAX)
452 min = Number(self.MIN)
454 GIMarshallingTests.long_in_max(max)
455 GIMarshallingTests.long_in_min(min)
460 self.assertRaises(ValueError, GIMarshallingTests.long_in_max, max)
461 self.assertRaises(ValueError, GIMarshallingTests.long_in_min, min)
463 self.assertRaises(TypeError, GIMarshallingTests.long_in_max, "self.MAX")
465 def test_long_out(self):
466 self.assertEqual(self.MAX, GIMarshallingTests.long_out_max())
467 self.assertEqual(self.MIN, GIMarshallingTests.long_out_min())
469 def test_long_inout(self):
470 self.assertEqual(self.MIN, GIMarshallingTests.long_inout_max_min(Number(self.MAX)))
471 self.assertEqual(self.MAX, GIMarshallingTests.long_inout_min_max(Number(self.MIN)))
474 class TestULong(unittest.TestCase):
476 MAX = GObject.constants.G_MAXULONG
478 def test_ulong_return(self):
479 self.assertEqual(self.MAX, GIMarshallingTests.ulong_return())
481 def test_ulong_in(self):
482 number = Number(self.MAX)
484 GIMarshallingTests.ulong_in(number)
488 self.assertRaises(ValueError, GIMarshallingTests.ulong_in, number)
489 self.assertRaises(ValueError, GIMarshallingTests.ulong_in, Number(-1))
491 self.assertRaises(TypeError, GIMarshallingTests.ulong_in, "self.MAX")
493 def test_ulong_out(self):
494 self.assertEqual(self.MAX, GIMarshallingTests.ulong_out())
496 def test_ulong_inout(self):
497 self.assertEqual(0, GIMarshallingTests.ulong_inout(Number(self.MAX)))
500 class TestSSize(unittest.TestCase):
502 MAX = GObject.constants.G_MAXLONG
503 MIN = GObject.constants.G_MINLONG
505 def test_ssize_return(self):
506 self.assertEqual(self.MAX, GIMarshallingTests.ssize_return_max())
507 self.assertEqual(self.MIN, GIMarshallingTests.ssize_return_min())
509 def test_ssize_in(self):
510 max = Number(self.MAX)
511 min = Number(self.MIN)
513 GIMarshallingTests.ssize_in_max(max)
514 GIMarshallingTests.ssize_in_min(min)
519 self.assertRaises(ValueError, GIMarshallingTests.ssize_in_max, max)
520 self.assertRaises(ValueError, GIMarshallingTests.ssize_in_min, min)
522 self.assertRaises(TypeError, GIMarshallingTests.ssize_in_max, "self.MAX")
524 def test_ssize_out(self):
525 self.assertEqual(self.MAX, GIMarshallingTests.ssize_out_max())
526 self.assertEqual(self.MIN, GIMarshallingTests.ssize_out_min())
528 def test_ssize_inout(self):
529 self.assertEqual(self.MIN, GIMarshallingTests.ssize_inout_max_min(Number(self.MAX)))
530 self.assertEqual(self.MAX, GIMarshallingTests.ssize_inout_min_max(Number(self.MIN)))
533 class TestSize(unittest.TestCase):
535 MAX = GObject.constants.G_MAXULONG
537 def test_size_return(self):
538 self.assertEqual(self.MAX, GIMarshallingTests.size_return())
540 def test_size_in(self):
541 number = Number(self.MAX)
543 GIMarshallingTests.size_in(number)
547 self.assertRaises(ValueError, GIMarshallingTests.size_in, number)
548 self.assertRaises(ValueError, GIMarshallingTests.size_in, Number(-1))
550 self.assertRaises(TypeError, GIMarshallingTests.size_in, "self.MAX")
552 def test_size_out(self):
553 self.assertEqual(self.MAX, GIMarshallingTests.size_out())
555 def test_size_inout(self):
556 self.assertEqual(0, GIMarshallingTests.size_inout(Number(self.MAX)))
559 class TestFloat(unittest.TestCase):
561 MAX = GObject.constants.G_MAXFLOAT
562 MIN = GObject.constants.G_MINFLOAT
564 def test_float_return(self):
565 self.assertAlmostEquals(self.MAX, GIMarshallingTests.float_return())
567 def test_float_in(self):
568 GIMarshallingTests.float_in(Number(self.MAX))
570 self.assertRaises(TypeError, GIMarshallingTests.float_in, "self.MAX")
572 def test_float_out(self):
573 self.assertAlmostEquals(self.MAX, GIMarshallingTests.float_out())
575 def test_float_inout(self):
576 self.assertAlmostEquals(self.MIN, GIMarshallingTests.float_inout(Number(self.MAX)))
579 class TestDouble(unittest.TestCase):
581 MAX = GObject.constants.G_MAXDOUBLE
582 MIN = GObject.constants.G_MINDOUBLE
584 def test_double_return(self):
585 self.assertAlmostEquals(self.MAX, GIMarshallingTests.double_return())
587 def test_double_in(self):
588 GIMarshallingTests.double_in(Number(self.MAX))
590 self.assertRaises(TypeError, GIMarshallingTests.double_in, "self.MAX")
592 def test_double_out(self):
593 self.assertAlmostEquals(self.MAX, GIMarshallingTests.double_out())
595 def test_double_inout(self):
596 self.assertAlmostEquals(self.MIN, GIMarshallingTests.double_inout(Number(self.MAX)))
599 class TestGType(unittest.TestCase):
601 def test_gtype_name(self):
602 self.assertEqual("void", GObject.TYPE_NONE.name)
603 self.assertEqual("gchararray", GObject.TYPE_STRING.name)
605 def check_readonly(gtype):
608 self.assertRaises(AttributeError, check_readonly, GObject.TYPE_NONE)
609 self.assertRaises(AttributeError, check_readonly, GObject.TYPE_STRING)
611 def test_gtype_return(self):
612 self.assertEqual(GObject.TYPE_NONE, GIMarshallingTests.gtype_return())
613 self.assertEqual(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_return())
615 def test_gtype_in(self):
616 GIMarshallingTests.gtype_in(GObject.TYPE_NONE)
617 GIMarshallingTests.gtype_string_in(GObject.TYPE_STRING)
618 self.assertRaises(TypeError, GIMarshallingTests.gtype_in, "foo")
619 self.assertRaises(TypeError, GIMarshallingTests.gtype_string_in, "foo")
621 def test_gtype_out(self):
622 self.assertEqual(GObject.TYPE_NONE, GIMarshallingTests.gtype_out())
623 self.assertEqual(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_out())
625 def test_gtype_inout(self):
626 self.assertEqual(GObject.TYPE_INT, GIMarshallingTests.gtype_inout(GObject.TYPE_NONE))
629 class TestUtf8(unittest.TestCase):
631 def test_utf8_none_return(self):
632 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_none_return())
634 def test_utf8_full_return(self):
635 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_full_return())
637 def test_utf8_none_in(self):
638 GIMarshallingTests.utf8_none_in(CONSTANT_UTF8)
639 if sys.version_info < (3, 0):
640 GIMarshallingTests.utf8_none_in(PY2_UNICODE_UTF8)
642 self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, CONSTANT_NUMBER)
643 self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, None)
645 def test_utf8_none_out(self):
646 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_none_out())
648 def test_utf8_full_out(self):
649 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_full_out())
651 def test_utf8_dangling_out(self):
652 GIMarshallingTests.utf8_dangling_out()
654 def test_utf8_none_inout(self):
655 self.assertEqual("", GIMarshallingTests.utf8_none_inout(CONSTANT_UTF8))
657 def test_utf8_full_inout(self):
658 self.assertEqual("", GIMarshallingTests.utf8_full_inout(CONSTANT_UTF8))
661 class TestArray(unittest.TestCase):
663 def test_array_fixed_int_return(self):
664 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_int_return())
666 def test_array_fixed_short_return(self):
667 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_short_return())
669 def test_array_fixed_int_in(self):
670 GIMarshallingTests.array_fixed_int_in(Sequence([-1, 0, 1, 2]))
672 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, Sequence([-1, '0', 1, 2]))
674 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, 42)
675 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, None)
677 def test_array_fixed_short_in(self):
678 GIMarshallingTests.array_fixed_short_in(Sequence([-1, 0, 1, 2]))
680 def test_array_fixed_out(self):
681 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_out())
683 def test_array_fixed_inout(self):
684 self.assertEqual([2, 1, 0, -1], GIMarshallingTests.array_fixed_inout([-1, 0, 1, 2]))
686 def test_array_return(self):
687 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_return())
689 def test_array_in(self):
690 GIMarshallingTests.array_in(Sequence([-1, 0, 1, 2]))
692 def test_array_uint8_in(self):
693 GIMarshallingTests.array_uint8_in(Sequence([97, 98, 99, 100]))
694 GIMarshallingTests.array_uint8_in(_bytes("abcd"))
696 def test_array_out(self):
697 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_out())
699 def test_array_inout(self):
700 self.assertEqual([-2, -1, 0, 1, 2], GIMarshallingTests.array_inout(Sequence([-1, 0, 1, 2])))
702 def test_method_array_in(self):
703 object_ = GIMarshallingTests.Object()
704 object_.method_array_in(Sequence([-1, 0, 1, 2]))
706 def test_method_array_out(self):
707 object_ = GIMarshallingTests.Object()
708 self.assertEqual([-1, 0, 1, 2], object_.method_array_out())
710 def test_method_array_inout(self):
711 object_ = GIMarshallingTests.Object()
712 self.assertEqual([-2, -1, 0, 1, 2], object_.method_array_inout(Sequence([-1, 0, 1, 2])))
714 def test_method_array_return(self):
715 object_ = GIMarshallingTests.Object()
716 self.assertEqual([-1, 0, 1, 2], object_.method_array_return())
718 def test_array_enum_in(self):
719 GIMarshallingTests.array_enum_in([GIMarshallingTests.Enum.VALUE1,
720 GIMarshallingTests.Enum.VALUE2,
721 GIMarshallingTests.Enum.VALUE3])
723 def test_array_boxed_struct_in(self):
724 struct1 = GIMarshallingTests.BoxedStruct()
726 struct2 = GIMarshallingTests.BoxedStruct()
728 struct3 = GIMarshallingTests.BoxedStruct()
731 GIMarshallingTests.array_struct_in([struct1, struct2, struct3])
733 def test_array_simple_struct_in(self):
734 struct1 = GIMarshallingTests.SimpleStruct()
736 struct2 = GIMarshallingTests.SimpleStruct()
738 struct3 = GIMarshallingTests.SimpleStruct()
741 GIMarshallingTests.array_simple_struct_in([struct1, struct2, struct3])
743 def test_array_multi_array_key_value_in(self):
744 GIMarshallingTests.multi_array_key_value_in(["one", "two", "three"],
747 def test_array_in_nonzero_nonlen(self):
748 GIMarshallingTests.array_in_nonzero_nonlen(1, 'abcd')
750 def test_array_fixed_out_struct(self):
751 struct1, struct2 = GIMarshallingTests.array_fixed_out_struct()
753 self.assertEqual(7, struct1.long_)
754 self.assertEqual(6, struct1.int8)
755 self.assertEqual(6, struct2.long_)
756 self.assertEqual(7, struct2.int8)
758 def test_array_zero_terminated_return(self):
759 self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_return())
761 def test_array_zero_terminated_return_null(self):
762 self.assertEqual([], GIMarshallingTests.array_zero_terminated_return_null())
764 def test_array_zero_terminated_in(self):
765 GIMarshallingTests.array_zero_terminated_in(Sequence(['0', '1', '2']))
767 def test_array_zero_terminated_out(self):
768 self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
770 def test_array_zero_terminated_inout(self):
771 self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.array_zero_terminated_inout(['0', '1', '2']))
774 class TestGStrv(unittest.TestCase):
776 def test_gstrv_return(self):
777 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_return())
779 def test_gstrv_in(self):
780 GIMarshallingTests.gstrv_in(Sequence(['0', '1', '2']))
782 def test_gstrv_out(self):
783 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_out())
785 def test_gstrv_inout(self):
786 self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.gstrv_inout(['0', '1', '2']))
789 class TestArrayGVariant(unittest.TestCase):
791 def test_array_gvariant_none_in(self):
792 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
793 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_none_in(v)]
794 self.assertEqual([27, "Hello"], returned)
796 def test_array_gvariant_container_in(self):
797 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
798 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_none_in(v)]
799 self.assertEqual([27, "Hello"], returned)
801 def test_array_gvariant_full_in(self):
802 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
803 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_none_in(v)]
804 self.assertEqual([27, "Hello"], returned)
806 def test_bytearray_gvariant(self):
807 v = GLib.Variant.new_bytestring(b"foo")
808 self.assertEqual(v.get_bytestring(), b"foo")
811 class TestGArray(unittest.TestCase):
813 def test_garray_int_none_return(self):
814 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.garray_int_none_return())
816 def test_garray_utf8_none_return(self):
817 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_return())
819 def test_garray_utf8_container_return(self):
820 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_return())
822 def test_garray_utf8_full_return(self):
823 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_return())
825 def test_garray_int_none_in(self):
826 GIMarshallingTests.garray_int_none_in(Sequence([-1, 0, 1, 2]))
828 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, Sequence([-1, '0', 1, 2]))
830 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, 42)
831 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, None)
833 def test_garray_utf8_none_in(self):
834 GIMarshallingTests.garray_utf8_none_in(Sequence(['0', '1', '2']))
836 def test_garray_utf8_none_out(self):
837 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_out())
839 def test_garray_utf8_container_out(self):
840 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_out())
842 def test_garray_utf8_full_out(self):
843 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out())
845 def test_garray_utf8_none_inout(self):
846 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_none_inout(Sequence(('0', '1', '2'))))
848 def test_garray_utf8_container_inout(self):
849 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_container_inout(['0', '1', '2']))
851 def test_garray_utf8_full_inout(self):
852 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_full_inout(['0', '1', '2']))
855 class TestGPtrArray(unittest.TestCase):
857 def test_gptrarray_utf8_none_return(self):
858 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_return())
860 def test_gptrarray_utf8_container_return(self):
861 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_return())
863 def test_gptrarray_utf8_full_return(self):
864 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_return())
866 def test_gptrarray_utf8_none_in(self):
867 GIMarshallingTests.gptrarray_utf8_none_in(Sequence(['0', '1', '2']))
869 def test_gptrarray_utf8_none_out(self):
870 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_out())
872 def test_gptrarray_utf8_container_out(self):
873 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_out())
875 def test_gptrarray_utf8_full_out(self):
876 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_out())
878 def test_gptrarray_utf8_none_inout(self):
879 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_none_inout(Sequence(('0', '1', '2'))))
881 def test_gptrarray_utf8_container_inout(self):
882 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_container_inout(['0', '1', '2']))
884 def test_gptrarray_utf8_full_inout(self):
885 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_full_inout(['0', '1', '2']))
888 class TestGList(unittest.TestCase):
890 def test_glist_int_none_return(self):
891 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.glist_int_none_return())
893 def test_glist_utf8_none_return(self):
894 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_return())
896 def test_glist_utf8_container_return(self):
897 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_return())
899 def test_glist_utf8_full_return(self):
900 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_return())
902 def test_glist_int_none_in(self):
903 GIMarshallingTests.glist_int_none_in(Sequence((-1, 0, 1, 2)))
905 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, Sequence((-1, '0', 1, 2)))
907 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, 42)
908 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, None)
910 def test_glist_utf8_none_in(self):
911 GIMarshallingTests.glist_utf8_none_in(Sequence(('0', '1', '2')))
913 def test_glist_utf8_none_out(self):
914 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_out())
916 def test_glist_utf8_container_out(self):
917 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_out())
919 def test_glist_utf8_full_out(self):
920 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_out())
922 def test_glist_utf8_none_inout(self):
923 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_none_inout(Sequence(('0', '1', '2'))))
925 def test_glist_utf8_container_inout(self):
926 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_container_inout(('0', '1', '2')))
928 def test_glist_utf8_full_inout(self):
929 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_full_inout(('0', '1', '2')))
932 class TestGSList(unittest.TestCase):
934 def test_gslist_int_none_return(self):
935 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.gslist_int_none_return())
937 def test_gslist_utf8_none_return(self):
938 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_return())
940 def test_gslist_utf8_container_return(self):
941 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_return())
943 def test_gslist_utf8_full_return(self):
944 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_return())
946 def test_gslist_int_none_in(self):
947 GIMarshallingTests.gslist_int_none_in(Sequence((-1, 0, 1, 2)))
949 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, Sequence((-1, '0', 1, 2)))
951 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, 42)
952 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, None)
954 def test_gslist_utf8_none_in(self):
955 GIMarshallingTests.gslist_utf8_none_in(Sequence(('0', '1', '2')))
957 def test_gslist_utf8_none_out(self):
958 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_out())
960 def test_gslist_utf8_container_out(self):
961 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_out())
963 def test_gslist_utf8_full_out(self):
964 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_out())
966 def test_gslist_utf8_none_inout(self):
967 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_none_inout(Sequence(('0', '1', '2'))))
969 def test_gslist_utf8_container_inout(self):
970 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_container_inout(('0', '1', '2')))
972 def test_gslist_utf8_full_inout(self):
973 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_full_inout(('0', '1', '2')))
976 class TestGHashTable(unittest.TestCase):
978 def test_ghashtable_int_none_return(self):
979 self.assertEqual({-1: 1, 0: 0, 1: -1, 2: -2}, GIMarshallingTests.ghashtable_int_none_return())
981 def test_ghashtable_int_none_return2(self):
982 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_return())
984 def test_ghashtable_int_container_return(self):
985 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_return())
987 def test_ghashtable_int_full_return(self):
988 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_return())
990 def test_ghashtable_int_none_in(self):
991 GIMarshallingTests.ghashtable_int_none_in({-1: 1, 0: 0, 1: -1, 2: -2})
993 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, '0': 0, 1: -1, 2: -2})
994 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, 0: '0', 1: -1, 2: -2})
996 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, '{-1: 1, 0: 0, 1: -1, 2: -2}')
997 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, None)
999 def test_ghashtable_utf8_none_in(self):
1000 GIMarshallingTests.ghashtable_utf8_none_in({'-1': '1', '0': '0', '1': '-1', '2': '-2'})
1002 def test_ghashtable_utf8_none_out(self):
1003 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_out())
1005 def test_ghashtable_utf8_container_out(self):
1006 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_out())
1008 def test_ghashtable_utf8_full_out(self):
1009 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_out())
1011 def test_ghashtable_utf8_none_inout(self):
1012 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1013 GIMarshallingTests.ghashtable_utf8_none_inout({'-1': '1', '0': '0', '1': '-1', '2': '-2'}))
1015 def test_ghashtable_utf8_container_inout(self):
1016 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1017 GIMarshallingTests.ghashtable_utf8_container_inout({'-1': '1', '0': '0', '1': '-1', '2': '-2'}))
1019 def test_ghashtable_utf8_full_inout(self):
1020 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1021 GIMarshallingTests.ghashtable_utf8_full_inout({'-1': '1', '0': '0', '1': '-1', '2': '-2'}))
1024 class TestGValue(unittest.TestCase):
1026 def test_gvalue_return(self):
1027 self.assertEqual(42, GIMarshallingTests.gvalue_return())
1029 def test_gvalue_in(self):
1030 GIMarshallingTests.gvalue_in(42)
1031 value = GObject.Value()
1032 value.init(GObject.TYPE_INT)
1034 GIMarshallingTests.gvalue_in(value)
1036 def test_gvalue_out(self):
1037 self.assertEqual(42, GIMarshallingTests.gvalue_out())
1039 def test_gvalue_inout(self):
1040 self.assertEqual('42', GIMarshallingTests.gvalue_inout(42))
1041 value = GObject.Value()
1042 value.init(GObject.TYPE_INT)
1044 self.assertEqual('42', GIMarshallingTests.gvalue_inout(value))
1046 def test_gvalue_flat_array_in(self):
1047 # the function already asserts the correct values
1048 GIMarshallingTests.gvalue_flat_array([42, "42", True])
1050 def test_gvalue_flat_array_out(self):
1051 values = GIMarshallingTests.return_gvalue_flat_array()
1052 self.assertEqual(values, [42, '42', True])
1055 class TestGClosure(unittest.TestCase):
1057 def test_gclosure_in(self):
1058 GIMarshallingTests.gclosure_in(lambda: 42)
1060 # test passing a closure between two C calls
1061 closure = GIMarshallingTests.gclosure_return()
1062 GIMarshallingTests.gclosure_in(closure)
1064 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, 42)
1065 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, None)
1068 class TestPointer(unittest.TestCase):
1069 def test_pointer_in_return(self):
1070 self.assertEqual(GIMarshallingTests.pointer_in_return(42), 42)
1073 class TestEnum(unittest.TestCase):
1076 def setUpClass(cls):
1077 '''Run tests under a test locale.
1079 Upper case conversion of member names should not be locale specific
1080 e. g. in Turkish, "i".upper() == "i", which gives results like "iNVALiD"
1082 Run test under a locale which defines toupper('a') == 'a'
1084 cls.locale_dir = tempfile.mkdtemp()
1085 subprocess.check_call(['localedef', '-i',
1086 os.path.join(os.path.dirname(os.path.realpath(__file__)), 'te_ST@nouppera'),
1087 '-c', '-f', 'UTF-8', os.path.join(cls.locale_dir, 'te_ST.UTF-8@nouppera')])
1088 os.environ['LOCPATH'] = cls.locale_dir
1089 locale.setlocale(locale.LC_ALL, 'te_ST.UTF-8@nouppera')
1092 def tearDownClass(cls):
1093 locale.setlocale(locale.LC_ALL, 'C')
1094 shutil.rmtree(cls.locale_dir)
1096 del os.environ['LOCPATH']
1100 def test_enum(self):
1101 self.assertTrue(issubclass(GIMarshallingTests.Enum, int))
1102 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE1, GIMarshallingTests.Enum))
1103 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE2, GIMarshallingTests.Enum))
1104 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE3, GIMarshallingTests.Enum))
1105 self.assertEqual(42, GIMarshallingTests.Enum.VALUE3)
1107 def test_value_nick_and_name(self):
1108 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_nick, 'value1')
1109 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_nick, 'value2')
1110 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_nick, 'value3')
1112 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE1')
1113 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE2')
1114 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE3')
1116 def test_enum_in(self):
1117 GIMarshallingTests.enum_in(GIMarshallingTests.Enum.VALUE3)
1118 GIMarshallingTests.enum_in(42)
1120 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 43)
1121 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 'GIMarshallingTests.Enum.VALUE3')
1123 def test_enum_out(self):
1124 enum = GIMarshallingTests.enum_out()
1125 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1126 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1128 def test_enum_inout(self):
1129 enum = GIMarshallingTests.enum_inout(GIMarshallingTests.Enum.VALUE3)
1130 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1131 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE1)
1133 def test_enum_second(self):
1134 # check for the bug where different non-gtype enums share the same class
1135 self.assertNotEqual(GIMarshallingTests.Enum, GIMarshallingTests.SecondEnum)
1137 # check that values are not being shared between different enums
1138 self.assertTrue(hasattr(GIMarshallingTests.SecondEnum, "SECONDVALUE1"))
1139 self.assertRaises(AttributeError, getattr, GIMarshallingTests.Enum, "SECONDVALUE1")
1140 self.assertTrue(hasattr(GIMarshallingTests.Enum, "VALUE1"))
1141 self.assertRaises(AttributeError, getattr, GIMarshallingTests.SecondEnum, "VALUE1")
1144 class TestGEnum(unittest.TestCase):
1146 def test_genum(self):
1147 self.assertTrue(issubclass(GIMarshallingTests.GEnum, GObject.GEnum))
1148 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE1, GIMarshallingTests.GEnum))
1149 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE2, GIMarshallingTests.GEnum))
1150 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE3, GIMarshallingTests.GEnum))
1151 self.assertEqual(42, GIMarshallingTests.GEnum.VALUE3)
1153 def test_value_nick_and_name(self):
1154 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_nick, 'value1')
1155 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_nick, 'value2')
1156 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_nick, 'value3')
1158 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE1')
1159 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE2')
1160 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE3')
1162 def test_genum_in(self):
1163 GIMarshallingTests.genum_in(GIMarshallingTests.GEnum.VALUE3)
1164 GIMarshallingTests.genum_in(42)
1166 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 43)
1167 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 'GIMarshallingTests.GEnum.VALUE3')
1169 def test_genum_out(self):
1170 genum = GIMarshallingTests.genum_out()
1171 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1172 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1174 def test_genum_inout(self):
1175 genum = GIMarshallingTests.genum_inout(GIMarshallingTests.GEnum.VALUE3)
1176 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1177 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE1)
1180 class TestGFlags(unittest.TestCase):
1182 def test_flags(self):
1183 self.assertTrue(issubclass(GIMarshallingTests.Flags, GObject.GFlags))
1184 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1, GIMarshallingTests.Flags))
1185 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE2, GIMarshallingTests.Flags))
1186 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE3, GIMarshallingTests.Flags))
1187 # __or__() operation should still return an instance, not an int.
1188 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1 | GIMarshallingTests.Flags.VALUE2,
1189 GIMarshallingTests.Flags))
1190 self.assertEqual(1 << 1, GIMarshallingTests.Flags.VALUE2)
1192 def test_value_nick_and_name(self):
1193 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_nick, 'value1')
1194 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_nick, 'value2')
1195 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_nick, 'value3')
1197 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE1')
1198 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE2')
1199 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE3')
1201 def test_flags_in(self):
1202 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2)
1203 # result of __or__() operation should still be valid instance, not an int.
1204 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE2)
1205 GIMarshallingTests.flags_in_zero(Number(0))
1207 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 1 << 1)
1208 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 'GIMarshallingTests.Flags.VALUE2')
1210 def test_flags_out(self):
1211 flags = GIMarshallingTests.flags_out()
1212 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1213 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1215 def test_flags_inout(self):
1216 flags = GIMarshallingTests.flags_inout(GIMarshallingTests.Flags.VALUE2)
1217 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1218 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE1)
1221 class TestNoTypeFlags(unittest.TestCase):
1223 def test_flags(self):
1224 self.assertTrue(issubclass(GIMarshallingTests.NoTypeFlags, GObject.GFlags))
1225 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1, GIMarshallingTests.NoTypeFlags))
1226 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE2, GIMarshallingTests.NoTypeFlags))
1227 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE3, GIMarshallingTests.NoTypeFlags))
1228 # __or__() operation should still return an instance, not an int.
1229 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1 | GIMarshallingTests.NoTypeFlags.VALUE2,
1230 GIMarshallingTests.NoTypeFlags))
1231 self.assertEqual(1 << 1, GIMarshallingTests.NoTypeFlags.VALUE2)
1233 def test_value_nick_and_name(self):
1234 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_nick, 'value1')
1235 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_nick, 'value2')
1236 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_nick, 'value3')
1238 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE1')
1239 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2')
1240 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE3')
1242 def test_flags_in(self):
1243 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2)
1244 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2 | GIMarshallingTests.NoTypeFlags.VALUE2)
1245 GIMarshallingTests.no_type_flags_in_zero(Number(0))
1247 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 1 << 1)
1248 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 'GIMarshallingTests.NoTypeFlags.VALUE2')
1250 def test_flags_out(self):
1251 flags = GIMarshallingTests.no_type_flags_out()
1252 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1253 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1255 def test_flags_inout(self):
1256 flags = GIMarshallingTests.no_type_flags_inout(GIMarshallingTests.NoTypeFlags.VALUE2)
1257 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1258 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE1)
1261 class TestStructure(unittest.TestCase):
1263 def test_simple_struct(self):
1264 self.assertTrue(issubclass(GIMarshallingTests.SimpleStruct, GObject.GPointer))
1266 struct = GIMarshallingTests.SimpleStruct()
1267 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1269 self.assertEqual(0, struct.long_)
1270 self.assertEqual(0, struct.int8)
1275 self.assertEqual(6, struct.long_)
1276 self.assertEqual(7, struct.int8)
1280 def test_nested_struct(self):
1281 struct = GIMarshallingTests.NestedStruct()
1283 self.assertTrue(isinstance(struct.simple_struct, GIMarshallingTests.SimpleStruct))
1285 struct.simple_struct.long_ = 42
1286 self.assertEqual(42, struct.simple_struct.long_)
1290 def test_not_simple_struct(self):
1291 struct = GIMarshallingTests.NotSimpleStruct()
1292 self.assertEqual(None, struct.pointer)
1294 def test_simple_struct_return(self):
1295 struct = GIMarshallingTests.simple_struct_returnv()
1297 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1298 self.assertEqual(6, struct.long_)
1299 self.assertEqual(7, struct.int8)
1303 def test_simple_struct_in(self):
1304 struct = GIMarshallingTests.SimpleStruct()
1308 GIMarshallingTests.SimpleStruct.inv(struct)
1312 struct = GIMarshallingTests.NestedStruct()
1314 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, struct)
1318 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, None)
1320 def test_simple_struct_method(self):
1321 struct = GIMarshallingTests.SimpleStruct()
1329 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.method)
1331 def test_pointer_struct(self):
1332 self.assertTrue(issubclass(GIMarshallingTests.PointerStruct, GObject.GPointer))
1334 struct = GIMarshallingTests.PointerStruct()
1335 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1339 def test_pointer_struct_return(self):
1340 struct = GIMarshallingTests.pointer_struct_returnv()
1342 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1343 self.assertEqual(42, struct.long_)
1347 def test_pointer_struct_in(self):
1348 struct = GIMarshallingTests.PointerStruct()
1355 def test_boxed_struct(self):
1356 self.assertTrue(issubclass(GIMarshallingTests.BoxedStruct, GObject.GBoxed))
1358 struct = GIMarshallingTests.BoxedStruct()
1359 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1361 self.assertEqual(0, struct.long_)
1362 self.assertEqual([], struct.g_strv)
1366 def test_boxed_struct_new(self):
1367 struct = GIMarshallingTests.BoxedStruct.new()
1368 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1372 def test_boxed_struct_copy(self):
1373 struct = GIMarshallingTests.BoxedStruct()
1375 new_struct = struct.copy()
1376 self.assertTrue(isinstance(new_struct, GIMarshallingTests.BoxedStruct))
1381 def test_boxed_struct_return(self):
1382 struct = GIMarshallingTests.boxed_struct_returnv()
1384 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1385 self.assertEqual(42, struct.long_)
1386 self.assertEqual(['0', '1', '2'], struct.g_strv)
1390 def test_boxed_struct_in(self):
1391 struct = GIMarshallingTests.BoxedStruct()
1398 def test_boxed_struct_out(self):
1399 struct = GIMarshallingTests.boxed_struct_out()
1401 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1402 self.assertEqual(42, struct.long_)
1406 def test_boxed_struct_inout(self):
1407 in_struct = GIMarshallingTests.BoxedStruct()
1408 in_struct.long_ = 42
1410 out_struct = GIMarshallingTests.boxed_struct_inout(in_struct)
1412 self.assertTrue(isinstance(out_struct, GIMarshallingTests.BoxedStruct))
1413 self.assertEqual(0, out_struct.long_)
1418 def test_union(self):
1419 union = GIMarshallingTests.Union()
1421 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1423 new_union = union.copy()
1424 self.assertTrue(isinstance(new_union, GIMarshallingTests.Union))
1429 def test_union_return(self):
1430 union = GIMarshallingTests.union_returnv()
1432 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1433 self.assertEqual(42, union.long_)
1437 def test_union_in(self):
1438 union = GIMarshallingTests.Union()
1445 def test_union_method(self):
1446 union = GIMarshallingTests.Union()
1453 self.assertRaises(TypeError, GIMarshallingTests.Union.method)
1456 class TestGObject(unittest.TestCase):
1458 def test_object(self):
1459 self.assertTrue(issubclass(GIMarshallingTests.Object, GObject.GObject))
1461 object_ = GIMarshallingTests.Object()
1462 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1463 self.assertEqual(object_.__grefcount__, 1)
1465 def test_object_new(self):
1466 object_ = GIMarshallingTests.Object.new(42)
1467 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1468 self.assertEqual(object_.__grefcount__, 1)
1470 def test_object_int(self):
1471 object_ = GIMarshallingTests.Object(int=42)
1472 self.assertEqual(object_.int_, 42)
1473 # FIXME: Don't work yet.
1475 # self.assertEqual(object_.int_, 0)
1477 def test_object_static_method(self):
1478 GIMarshallingTests.Object.static_method()
1480 def test_object_method(self):
1481 GIMarshallingTests.Object(int=42).method()
1482 self.assertRaises(TypeError, GIMarshallingTests.Object.method, GObject.GObject())
1483 self.assertRaises(TypeError, GIMarshallingTests.Object.method)
1485 def test_sub_object(self):
1486 self.assertTrue(issubclass(GIMarshallingTests.SubObject, GIMarshallingTests.Object))
1488 object_ = GIMarshallingTests.SubObject()
1489 self.assertTrue(isinstance(object_, GIMarshallingTests.SubObject))
1491 def test_sub_object_new(self):
1492 self.assertRaises(TypeError, GIMarshallingTests.SubObject.new, 42)
1494 def test_sub_object_static_method(self):
1495 object_ = GIMarshallingTests.SubObject()
1496 object_.static_method()
1498 def test_sub_object_method(self):
1499 object_ = GIMarshallingTests.SubObject(int=42)
1502 def test_sub_object_sub_method(self):
1503 object_ = GIMarshallingTests.SubObject()
1504 object_.sub_method()
1506 def test_sub_object_overwritten_method(self):
1507 object_ = GIMarshallingTests.SubObject()
1508 object_.overwritten_method()
1510 self.assertRaises(TypeError, GIMarshallingTests.SubObject.overwritten_method, GIMarshallingTests.Object())
1512 def test_sub_object_int(self):
1513 object_ = GIMarshallingTests.SubObject()
1514 self.assertEqual(object_.int_, 0)
1515 # FIXME: Don't work yet.
1517 # self.assertEqual(object_.int_, 42)
1519 def test_object_none_return(self):
1520 object_ = GIMarshallingTests.Object.none_return()
1521 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1522 self.assertEqual(object_.__grefcount__, 2)
1524 def test_object_full_return(self):
1525 object_ = GIMarshallingTests.Object.full_return()
1526 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1527 self.assertEqual(object_.__grefcount__, 1)
1529 def test_object_none_in(self):
1530 object_ = GIMarshallingTests.Object(int=42)
1531 GIMarshallingTests.Object.none_in(object_)
1532 self.assertEqual(object_.__grefcount__, 1)
1534 object_ = GIMarshallingTests.SubObject(int=42)
1535 GIMarshallingTests.Object.none_in(object_)
1537 object_ = GObject.GObject()
1538 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, object_)
1540 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, None)
1542 def test_object_none_out(self):
1543 object_ = GIMarshallingTests.Object.none_out()
1544 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1545 self.assertEqual(object_.__grefcount__, 2)
1547 new_object = GIMarshallingTests.Object.none_out()
1548 self.assertTrue(new_object is object_)
1550 def test_object_full_out(self):
1551 object_ = GIMarshallingTests.Object.full_out()
1552 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1553 self.assertEqual(object_.__grefcount__, 1)
1555 def test_object_none_inout(self):
1556 object_ = GIMarshallingTests.Object(int=42)
1557 new_object = GIMarshallingTests.Object.none_inout(object_)
1559 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1561 self.assertFalse(object_ is new_object)
1563 self.assertEqual(object_.__grefcount__, 1)
1564 self.assertEqual(new_object.__grefcount__, 2)
1566 new_new_object = GIMarshallingTests.Object.none_inout(object_)
1567 self.assertTrue(new_new_object is new_object)
1569 GIMarshallingTests.Object.none_inout(GIMarshallingTests.SubObject(int=42))
1571 def test_object_full_inout(self):
1572 object_ = GIMarshallingTests.Object(int=42)
1573 new_object = GIMarshallingTests.Object.full_inout(object_)
1575 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1577 self.assertFalse(object_ is new_object)
1579 self.assertEqual(object_.__grefcount__, 2)
1580 self.assertEqual(new_object.__grefcount__, 1)
1582 # FIXME: Doesn't actually return the same object.
1583 # def test_object_inout_same(self):
1584 # object_ = GIMarshallingTests.Object()
1585 # new_object = GIMarshallingTests.object_full_inout(object_)
1586 # self.assertTrue(object_ is new_object)
1587 # self.assertEqual(object_.__grefcount__, 1)
1590 class TestPythonGObject(unittest.TestCase):
1592 class Object(GIMarshallingTests.Object):
1593 def __init__(self, int):
1594 GIMarshallingTests.Object.__init__(self)
1598 # Don't call super, which asserts that self.int == 42.
1601 def do_method_int8_in(self, int8):
1604 def do_method_int8_out(self):
1607 def do_method_with_default_implementation(self, int8):
1608 GIMarshallingTests.Object.do_method_with_default_implementation(self, int8)
1609 self.props.int += int8
1611 class SubObject(GIMarshallingTests.SubObject):
1612 def __init__(self, int):
1613 GIMarshallingTests.SubObject.__init__(self)
1616 def do_method_with_default_implementation(self, int8):
1619 def test_object(self):
1620 self.assertTrue(issubclass(self.Object, GIMarshallingTests.Object))
1622 object_ = self.Object(int=42)
1623 self.assertTrue(isinstance(object_, self.Object))
1625 def test_object_method(self):
1626 self.Object(int=0).method()
1628 def test_object_vfuncs(self):
1629 object_ = self.Object(int=42)
1630 object_.method_int8_in(84)
1631 self.assertEqual(object_.val, 84)
1632 self.assertEqual(object_.method_int8_out(), 42)
1634 object_.method_with_default_implementation(42)
1635 self.assertEqual(object_.props.int, 84)
1637 class ObjectWithoutVFunc(GIMarshallingTests.Object):
1638 def __init__(self, int):
1639 GIMarshallingTests.Object.__init__(self)
1641 object_ = ObjectWithoutVFunc(int=42)
1642 object_.method_with_default_implementation(84)
1643 self.assertEqual(object_.props.int, 84)
1645 def test_subobject_parent_vfunc(self):
1646 object_ = self.SubObject(int=81)
1647 object_.method_with_default_implementation(87)
1648 self.assertEqual(object_.val, 87)
1650 def test_dynamic_module(self):
1651 from gi.module import DynamicGObjectModule
1652 self.assertTrue(isinstance(GObject, DynamicGObjectModule))
1653 # compare the same enum from both the pygobject attrs and gi GObject attrs
1654 self.assertEqual(GObject.SIGNAL_ACTION, GObject.SignalFlags.ACTION)
1655 # compare a static gobject attr with a dynamic GObject attr
1657 self.assertEqual(GObject.GObject, gi._gobject.GObject)
1659 def test_subobject_non_vfunc_do_method(self):
1660 class PythonObjectWithNonVFuncDoMethod:
1661 def do_not_a_vfunc(self):
1664 class ObjectOverrideNonVFuncDoMethod(GIMarshallingTests.Object, PythonObjectWithNonVFuncDoMethod):
1665 def do_not_a_vfunc(self):
1666 value = super(ObjectOverrideNonVFuncDoMethod, self).do_not_a_vfunc()
1669 object_ = ObjectOverrideNonVFuncDoMethod()
1670 self.assertEqual(18, object_.do_not_a_vfunc())
1672 def test_native_function_not_set_in_subclass_dict(self):
1673 # Previously, GI was setting virtual functions on the class as well
1674 # as any *native* class that subclasses it. Here we check that it is only
1675 # set on the class that the method is originally from.
1676 self.assertTrue('do_method_with_default_implementation' in GIMarshallingTests.Object.__dict__)
1677 self.assertTrue('do_method_with_default_implementation' not in GIMarshallingTests.SubObject.__dict__)
1679 def test_subobject_with_interface_and_non_vfunc_do_method(self):
1680 # There was a bug for searching for vfuncs in interfaces. It was
1681 # triggered by having a do_* method that wasn't overriding
1682 # a native vfunc, as well as inheriting from an interface.
1683 class GObjectSubclassWithInterface(GObject.GObject, GIMarshallingTests.Interface):
1684 def do_method_not_a_vfunc(self):
1687 def test_subsubobject(self):
1688 class SubSubSubObject(GIMarshallingTests.SubSubObject):
1689 def do_method_deep_hierarchy(self, num):
1690 self.props.int = num * 2
1692 sub_sub_sub_object = SubSubSubObject()
1693 GIMarshallingTests.SubSubObject.do_method_deep_hierarchy(sub_sub_sub_object, 5)
1694 self.assertEqual(sub_sub_sub_object.props.int, 5)
1696 def test_callback_in_vfunc(self):
1697 class SubObject(GIMarshallingTests.Object):
1699 GObject.GObject.__init__(self)
1702 def do_vfunc_with_callback(self, callback):
1703 self.worked = callback(42) == 42
1705 _object = SubObject()
1706 _object.call_vfunc_with_callback()
1707 self.assertTrue(_object.worked == True)
1708 _object.worked = False
1709 _object.call_vfunc_with_callback()
1710 self.assertTrue(_object.worked == True)
1713 class TestMultiOutputArgs(unittest.TestCase):
1715 def test_int_out_out(self):
1716 self.assertEqual((6, 7), GIMarshallingTests.int_out_out())
1718 def test_int_return_out(self):
1719 self.assertEqual((6, 7), GIMarshallingTests.int_return_out())
1722 class TestGErrorException(unittest.TestCase):
1723 def test_gerror_exception(self):
1724 self.assertRaises(GObject.GError, GIMarshallingTests.gerror)
1726 GIMarshallingTests.gerror()
1728 etype, e = sys.exc_info()[:2]
1729 self.assertEqual(e.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
1730 self.assertEqual(e.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
1731 self.assertEqual(e.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
1737 class TestInterfaces(unittest.TestCase):
1739 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
1741 GObject.GObject.__init__(self)
1744 def do_test_int8_in(self, int8):
1748 self.instance = self.TestInterfaceImpl()
1750 def test_wrapper(self):
1751 self.assertTrue(issubclass(GIMarshallingTests.Interface, GObject.GInterface))
1752 self.assertEqual(GIMarshallingTests.Interface.__gtype__.name, 'GIMarshallingTestsInterface')
1753 self.assertRaises(NotImplementedError, GIMarshallingTests.Interface)
1755 def test_implementation(self):
1756 self.assertTrue(issubclass(self.TestInterfaceImpl, GIMarshallingTests.Interface))
1757 self.assertTrue(isinstance(self.instance, GIMarshallingTests.Interface))
1759 def test_int8_int(self):
1760 GIMarshallingTests.test_interface_test_int8_in(self.instance, 42)
1761 self.assertEqual(self.instance.val, 42)
1763 def test_subclass(self):
1764 class TestInterfaceImplA(self.TestInterfaceImpl):
1767 class TestInterfaceImplB(TestInterfaceImplA):
1770 instance = TestInterfaceImplA()
1771 GIMarshallingTests.test_interface_test_int8_in(instance, 42)
1772 self.assertEqual(instance.val, 42)
1775 # there was a problem with Python bailing out because of
1776 # http://en.wikipedia.org/wiki/Diamond_problem with interfaces,
1777 # which shouldn't really be a problem.
1779 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
1782 class TestInterfaceImpl2(GIMarshallingTests.Interface,
1786 class TestInterfaceImpl3(self.TestInterfaceImpl,
1787 GIMarshallingTests.Interface2):
1791 class TestInterfaceClash(unittest.TestCase):
1793 def test_clash(self):
1795 class TestClash(GObject.GObject, GIMarshallingTests.Interface, GIMarshallingTests.Interface2):
1796 def do_test_int8_in(self, int8):
1800 self.assertRaises(TypeError, create_clash)
1803 class TestOverrides(unittest.TestCase):
1805 def test_constant(self):
1806 self.assertEqual(GIMarshallingTests.OVERRIDES_CONSTANT, 7)
1808 def test_struct(self):
1809 # Test that the constructor has been overridden.
1810 struct = GIMarshallingTests.OverridesStruct(42)
1812 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
1814 # Test that the method has been overridden.
1815 self.assertEqual(6, struct.method())
1819 # Test that the overrides wrapper has been registered.
1820 struct = GIMarshallingTests.overrides_struct_returnv()
1822 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
1826 def test_object(self):
1827 # Test that the constructor has been overridden.
1828 object_ = GIMarshallingTests.OverridesObject(42)
1830 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
1832 # Test that the alternate constructor has been overridden.
1833 object_ = GIMarshallingTests.OverridesObject.new(42)
1835 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
1837 # Test that the method has been overridden.
1838 self.assertEqual(6, object_.method())
1840 # Test that the overrides wrapper has been registered.
1841 object_ = GIMarshallingTests.OverridesObject.returnv()
1843 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
1845 def test_module_name(self):
1846 self.assertEqual(GIMarshallingTests.OverridesStruct.__module__, 'gi.overrides.GIMarshallingTests')
1847 self.assertEqual(GObject.InitiallyUnowned.__module__, 'gi.repository.GObject')
1850 class TestDir(unittest.TestCase):
1851 def test_members_list(self):
1852 list = dir(GIMarshallingTests)
1853 self.assertTrue('OverridesStruct' in list)
1854 self.assertTrue('BoxedStruct' in list)
1855 self.assertTrue('OVERRIDES_CONSTANT' in list)
1856 self.assertTrue('GEnum' in list)
1857 self.assertTrue('int32_return_max' in list)
1859 def test_modules_list(self):
1860 import gi.repository
1861 list = dir(gi.repository)
1862 self.assertTrue('GIMarshallingTests' in list)
1864 # FIXME: test to see if a module which was not imported is in the list
1865 # we should be listing every typelib we find, not just the ones
1866 # which are imported
1868 # to test this I recommend we compile a fake module which
1869 # our tests would never import and check to see if it is
1872 # self.assertTrue('DoNotImportDummyTests' in list)
1875 class TestGErrorArrayInCrash(unittest.TestCase):
1876 # Previously there was a bug in invoke, in which C arrays were unwrapped
1877 # from inside GArrays to be passed to the C function. But when a GError was
1878 # set, invoke would attempt to free the C array as if it were a GArray.
1879 # This crash is only for C arrays. It does not happen for C functions which
1880 # take in GArrays. See https://bugzilla.gnome.org/show_bug.cgi?id=642708
1881 def test_gerror_array_in_crash(self):
1882 self.assertRaises(GObject.GError, GIMarshallingTests.gerror_array_in, [1, 2, 3])
1885 class TestGErrorOut(unittest.TestCase):
1886 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
1887 def test_gerror_out(self):
1888 error, debug = GIMarshallingTests.gerror_out()
1890 self.assertIsInstance(error, GObject.GError)
1891 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
1892 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
1893 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
1894 self.assertEqual(debug, GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE)
1897 class TestGErrorOutTransferNone(unittest.TestCase):
1898 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
1899 def test_gerror_out_transfer_none(self):
1900 error, debug = GIMarshallingTests.gerror_out_transfer_none()
1902 self.assertIsInstance(error, GObject.GError)
1903 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
1904 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
1905 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
1906 self.assertEqual(GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE, debug)
1909 class TestGErrorReturn(unittest.TestCase):
1910 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
1911 def test_return_gerror(self):
1912 error = GIMarshallingTests.gerror_return()
1914 self.assertIsInstance(error, GObject.GError)
1915 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
1916 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
1917 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
1920 class TestKeywordArgs(unittest.TestCase):
1922 def test_calling(self):
1923 kw_func = GIMarshallingTests.int_three_in_three_out
1925 self.assertEqual(kw_func(1, 2, 3), (1, 2, 3))
1926 self.assertEqual(kw_func(**{'a': 4, 'b': 5, 'c': 6}), (4, 5, 6))
1927 self.assertEqual(kw_func(1, **{'b': 7, 'c': 8}), (1, 7, 8))
1928 self.assertEqual(kw_func(1, 7, **{'c': 8}), (1, 7, 8))
1929 self.assertEqual(kw_func(1, c=8, **{'b': 7}), (1, 7, 8))
1930 self.assertEqual(kw_func(2, c=4, b=3), (2, 3, 4))
1931 self.assertEqual(kw_func(a=2, c=4, b=3), (2, 3, 4))
1933 def assertRaisesMessage(self, exception, message, func, *args, **kwargs):
1935 func(*args, **kwargs)
1937 (e_type, e) = sys.exc_info()[:2]
1938 if message is not None:
1939 self.assertEqual(str(e), message)
1943 msg = "%s() did not raise %s" % (func.__name__, exception.__name__)
1944 raise AssertionError(msg)
1946 def test_type_errors(self):
1948 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
1949 GIMarshallingTests.int_three_in_three_out)
1950 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (1 given)",
1951 GIMarshallingTests.int_three_in_three_out, 1)
1952 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
1953 GIMarshallingTests.int_three_in_three_out, *())
1954 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
1955 GIMarshallingTests.int_three_in_three_out, *(), **{})
1956 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (0 given)",
1957 GIMarshallingTests.int_three_in_three_out, *(), **{'c': 4})
1959 # test too many args
1960 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (4 given)",
1961 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4))
1962 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (4 given)",
1963 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4), c=6)
1965 # test too many keyword args
1966 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got multiple values for keyword argument 'a'",
1967 GIMarshallingTests.int_three_in_three_out, 1, 2, 3, **{'a': 4, 'b': 5})
1968 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'd'",
1969 GIMarshallingTests.int_three_in_three_out, d=4)
1970 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'e'",
1971 GIMarshallingTests.int_three_in_three_out, **{'e': 2})
1973 def test_kwargs_are_not_modified(self):
1976 GIMarshallingTests.int_three_in_three_out(1, c=4, **d)
1977 self.assertEqual(d, d2)
1980 class TestPropertiesObject(unittest.TestCase):
1983 self.obj = GIMarshallingTests.PropertiesObject()
1985 def test_boolean(self):
1986 self.assertEqual(self.obj.props.some_boolean, False)
1987 self.obj.props.some_boolean = True
1988 self.assertEqual(self.obj.props.some_boolean, True)
1990 @unittest.expectedFailure
1991 def test_char(self):
1992 # gobject-introspection thinks it has a guint8 type tag, which is
1993 # wrong; this will raise an assertion critical which we need to ignore
1994 old_mask = GLib.log_set_always_fatal(
1995 GLib.LogLevelFlags.LEVEL_WARNING | GLib.LogLevelFlags.LEVEL_ERROR)
1996 self.assertEqual(self.obj.props.some_char, 0)
1997 self.obj.props.some_char = GObject.G_MAXINT8
1998 self.assertEqual(self.obj.props.some_char, GObject.G_MAXINT8)
2000 GLib.log_set_always_fatal(old_mask)
2002 def test_uchar(self):
2003 self.assertEqual(self.obj.props.some_uchar, 0)
2004 self.obj.props.some_uchar = GObject.G_MAXUINT8
2005 self.assertEqual(self.obj.props.some_uchar, GObject.G_MAXUINT8)
2008 self.assertEqual(self.obj.props.some_int, 0)
2009 self.obj.props.some_int = GObject.G_MAXINT
2010 self.assertEqual(self.obj.props.some_int, GObject.G_MAXINT)
2012 def test_uint(self):
2013 self.assertEqual(self.obj.props.some_uint, 0)
2014 self.obj.props.some_uint = GObject.G_MAXUINT
2015 self.assertEqual(self.obj.props.some_uint, GObject.G_MAXUINT)
2017 def test_long(self):
2018 self.assertEqual(self.obj.props.some_long, 0)
2019 self.obj.props.some_long = GObject.G_MAXLONG
2020 self.assertEqual(self.obj.props.some_long, GObject.G_MAXLONG)
2022 def test_ulong(self):
2023 self.assertEqual(self.obj.props.some_ulong, 0)
2024 self.obj.props.some_ulong = GObject.G_MAXULONG
2025 self.assertEqual(self.obj.props.some_ulong, GObject.G_MAXULONG)
2027 def test_int64(self):
2028 self.assertEqual(self.obj.props.some_int64, 0)
2029 self.obj.props.some_int64 = GObject.G_MAXINT64
2030 self.assertEqual(self.obj.props.some_int64, GObject.G_MAXINT64)
2032 def test_uint64(self):
2033 self.assertEqual(self.obj.props.some_uint64, 0)
2034 self.obj.props.some_uint64 = GObject.G_MAXUINT64
2035 self.assertEqual(self.obj.props.some_uint64, GObject.G_MAXUINT64)
2037 def test_float(self):
2038 self.assertEqual(self.obj.props.some_float, 0)
2039 self.obj.props.some_float = GObject.G_MAXFLOAT
2040 self.assertEqual(self.obj.props.some_float, GObject.G_MAXFLOAT)
2042 def test_double(self):
2043 self.assertEqual(self.obj.props.some_double, 0)
2044 self.obj.props.some_double = GObject.G_MAXDOUBLE
2045 self.assertEqual(self.obj.props.some_double, GObject.G_MAXDOUBLE)