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"
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.assertEquals(CONSTANT_UTF8, GIMarshallingTests.CONSTANT_UTF8)
60 def test_constant_number(self):
61 self.assertEquals(CONSTANT_NUMBER, GIMarshallingTests.CONSTANT_NUMBER)
64 class TestBoolean(unittest.TestCase):
66 def test_boolean_return(self):
67 self.assertEquals(True, GIMarshallingTests.boolean_return_true())
68 self.assertEquals(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.assertEquals(True, GIMarshallingTests.boolean_out_true())
79 self.assertEquals(False, GIMarshallingTests.boolean_out_false())
81 def test_boolean_inout(self):
82 self.assertEquals(False, GIMarshallingTests.boolean_inout_true_false(True))
83 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.int8_return_max())
93 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.int8_out_max())
112 self.assertEquals(self.MIN, GIMarshallingTests.int8_out_min())
114 def test_int8_inout(self):
115 self.assertEquals(self.MIN, GIMarshallingTests.int8_inout_max_min(Number(self.MAX)))
116 self.assertEquals(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.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.uint8_out())
141 def test_uint8_inout(self):
142 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.int16_return_max())
152 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.int16_out_max())
171 self.assertEquals(self.MIN, GIMarshallingTests.int16_out_min())
173 def test_int16_inout(self):
174 self.assertEquals(self.MIN, GIMarshallingTests.int16_inout_max_min(Number(self.MAX)))
175 self.assertEquals(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.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.uint16_out())
200 def test_uint16_inout(self):
201 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.int32_return_max())
211 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.int32_out_max())
230 self.assertEquals(self.MIN, GIMarshallingTests.int32_out_min())
232 def test_int32_inout(self):
233 self.assertEquals(self.MIN, GIMarshallingTests.int32_inout_max_min(Number(self.MAX)))
234 self.assertEquals(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.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.uint32_out())
259 def test_uint32_inout(self):
260 self.assertEquals(0, GIMarshallingTests.uint32_inout(Number(self.MAX)))
263 class TestInt64(unittest.TestCase):
268 def test_int64_return(self):
269 self.assertEquals(self.MAX, GIMarshallingTests.int64_return_max())
270 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.int64_out_max())
289 self.assertEquals(self.MIN, GIMarshallingTests.int64_out_min())
291 def test_int64_inout(self):
292 self.assertEquals(self.MIN, GIMarshallingTests.int64_inout_max_min(Number(self.MAX)))
293 self.assertEquals(self.MAX, GIMarshallingTests.int64_inout_min_max(Number(self.MIN)))
296 class TestUInt64(unittest.TestCase):
300 def test_uint64_return(self):
301 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.uint64_out())
318 def test_uint64_inout(self):
319 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.short_return_max())
329 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.short_out_max())
348 self.assertEquals(self.MIN, GIMarshallingTests.short_out_min())
350 def test_short_inout(self):
351 self.assertEquals(self.MIN, GIMarshallingTests.short_inout_max_min(Number(self.MAX)))
352 self.assertEquals(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.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.ushort_out())
377 def test_ushort_inout(self):
378 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.int_return_max())
388 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.int_out_max())
407 self.assertEquals(self.MIN, GIMarshallingTests.int_out_min())
409 def test_int_inout(self):
410 self.assertEquals(self.MIN, GIMarshallingTests.int_inout_max_min(Number(self.MAX)))
411 self.assertEquals(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.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.uint_out())
437 def test_uint_inout(self):
438 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.long_return_max())
448 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.long_out_max())
467 self.assertEquals(self.MIN, GIMarshallingTests.long_out_min())
469 def test_long_inout(self):
470 self.assertEquals(self.MIN, GIMarshallingTests.long_inout_max_min(Number(self.MAX)))
471 self.assertEquals(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.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.ulong_out())
496 def test_ulong_inout(self):
497 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.ssize_return_max())
507 self.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.ssize_out_max())
526 self.assertEquals(self.MIN, GIMarshallingTests.ssize_out_min())
528 def test_ssize_inout(self):
529 self.assertEquals(self.MIN, GIMarshallingTests.ssize_inout_max_min(Number(self.MAX)))
530 self.assertEquals(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.assertEquals(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.assertEquals(self.MAX, GIMarshallingTests.size_out())
555 def test_size_inout(self):
556 self.assertEquals(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.assertEquals("void", GObject.TYPE_NONE.name)
603 self.assertEquals("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.assertEquals(GObject.TYPE_NONE, GIMarshallingTests.gtype_return())
613 self.assertEquals(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.assertEquals(GObject.TYPE_NONE, GIMarshallingTests.gtype_out())
623 self.assertEquals(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_out())
625 def test_gtype_inout(self):
626 self.assertEquals(GObject.TYPE_INT, GIMarshallingTests.gtype_inout(GObject.TYPE_NONE))
629 class TestUtf8(unittest.TestCase):
631 def test_utf8_none_return(self):
632 self.assertEquals(CONSTANT_UTF8, GIMarshallingTests.utf8_none_return())
634 def test_utf8_full_return(self):
635 self.assertEquals(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.assertEquals(CONSTANT_UTF8, GIMarshallingTests.utf8_none_out())
648 def test_utf8_full_out(self):
649 self.assertEquals(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.assertEquals("", GIMarshallingTests.utf8_none_inout(CONSTANT_UTF8))
657 def test_utf8_full_inout(self):
658 self.assertEquals("", GIMarshallingTests.utf8_full_inout(CONSTANT_UTF8))
661 class TestArray(unittest.TestCase):
663 def test_array_fixed_int_return(self):
664 self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.array_fixed_int_return())
666 def test_array_fixed_short_return(self):
667 self.assertEquals([-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.assertEquals([-1, 0, 1, 2], GIMarshallingTests.array_fixed_out())
683 def test_array_fixed_inout(self):
684 self.assertEquals([2, 1, 0, -1], GIMarshallingTests.array_fixed_inout([-1, 0, 1, 2]))
686 def test_array_return(self):
687 self.assertEquals([-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.assertEquals([-1, 0, 1, 2], GIMarshallingTests.array_out())
699 def test_array_inout(self):
700 self.assertEquals([-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.assertEquals([-1, 0, 1, 2], object_.method_array_out())
710 def test_method_array_inout(self):
711 object_ = GIMarshallingTests.Object()
712 self.assertEquals([-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.assertEquals([-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_fixed_out_struct(self):
748 struct1, struct2 = GIMarshallingTests.array_fixed_out_struct()
750 self.assertEquals(7, struct1.long_)
751 self.assertEquals(6, struct1.int8)
752 self.assertEquals(6, struct2.long_)
753 self.assertEquals(7, struct2.int8)
755 def test_array_zero_terminated_return(self):
756 self.assertEquals(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_return())
758 def test_array_zero_terminated_return_null(self):
759 self.assertEquals([], GIMarshallingTests.array_zero_terminated_return_null())
761 def test_array_zero_terminated_in(self):
762 GIMarshallingTests.array_zero_terminated_in(Sequence(['0', '1', '2']))
764 def test_array_zero_terminated_out(self):
765 self.assertEquals(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
767 def test_array_zero_terminated_out(self):
768 self.assertEquals(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
770 def test_array_zero_terminated_inout(self):
771 self.assertEquals(['-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.assertEquals(['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.assertEquals(['0', '1', '2'], GIMarshallingTests.gstrv_out())
785 def test_gstrv_out(self):
786 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gstrv_out())
788 def test_gstrv_inout(self):
789 self.assertEquals(['-1', '0', '1', '2'], GIMarshallingTests.gstrv_inout(['0', '1', '2']))
792 class TestArrayGVariant(unittest.TestCase):
794 def test_array_gvariant_none_in(self):
795 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
796 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_none_in(v)]
797 self.assertEquals([27, "Hello"], returned)
799 def test_array_gvariant_container_in(self):
800 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
801 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_none_in(v)]
802 self.assertEquals([27, "Hello"], returned)
804 def test_array_gvariant_full_in(self):
805 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
806 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_none_in(v)]
807 self.assertEquals([27, "Hello"], returned)
809 def test_bytearray_gvariant(self):
810 v = GLib.Variant.new_bytestring(b"foo")
811 self.assertEquals(v.get_bytestring(), b"foo")
814 class TestGArray(unittest.TestCase):
816 def test_garray_int_none_return(self):
817 self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.garray_int_none_return())
819 def test_garray_utf8_none_return(self):
820 self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_return())
822 def test_garray_utf8_container_return(self):
823 self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_return())
825 def test_garray_utf8_full_return(self):
826 self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_return())
828 def test_garray_int_none_in(self):
829 GIMarshallingTests.garray_int_none_in(Sequence([-1, 0, 1, 2]))
831 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, Sequence([-1, '0', 1, 2]))
833 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, 42)
834 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, None)
836 def test_garray_utf8_none_in(self):
837 GIMarshallingTests.garray_utf8_none_in(Sequence(['0', '1', '2']))
839 def test_garray_utf8_none_out(self):
840 self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_out())
842 def test_garray_utf8_container_out(self):
843 self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_out())
845 def test_garray_utf8_full_out(self):
846 self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out())
848 def test_garray_utf8_none_inout(self):
849 self.assertEquals(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_none_inout(Sequence(('0', '1', '2'))))
851 def test_garray_utf8_container_inout(self):
852 self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.garray_utf8_container_inout(['0', '1', '2']))
854 def test_garray_utf8_full_inout(self):
855 self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.garray_utf8_full_inout(['0', '1', '2']))
858 class TestGPtrArray(unittest.TestCase):
860 def test_gptrarray_utf8_none_return(self):
861 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_return())
863 def test_gptrarray_utf8_container_return(self):
864 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_return())
866 def test_gptrarray_utf8_full_return(self):
867 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_return())
869 def test_gptrarray_utf8_none_in(self):
870 GIMarshallingTests.gptrarray_utf8_none_in(Sequence(['0', '1', '2']))
872 def test_gptrarray_utf8_none_out(self):
873 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_out())
875 def test_gptrarray_utf8_container_out(self):
876 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_out())
878 def test_gptrarray_utf8_full_out(self):
879 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_out())
881 def test_gptrarray_utf8_none_inout(self):
882 self.assertEquals(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_none_inout(Sequence(('0', '1', '2'))))
884 def test_gptrarray_utf8_container_inout(self):
885 self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.gptrarray_utf8_container_inout(['0', '1', '2']))
887 def test_gptrarray_utf8_full_inout(self):
888 self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.gptrarray_utf8_full_inout(['0', '1', '2']))
891 class TestGList(unittest.TestCase):
893 def test_glist_int_none_return(self):
894 self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.glist_int_none_return())
896 def test_glist_utf8_none_return(self):
897 self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_return())
899 def test_glist_utf8_container_return(self):
900 self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_return())
902 def test_glist_utf8_full_return(self):
903 self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_return())
905 def test_glist_int_none_in(self):
906 GIMarshallingTests.glist_int_none_in(Sequence((-1, 0, 1, 2)))
908 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, Sequence((-1, '0', 1, 2)))
910 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, 42)
911 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, None)
913 def test_glist_utf8_none_in(self):
914 GIMarshallingTests.glist_utf8_none_in(Sequence(('0', '1', '2')))
916 def test_glist_utf8_none_out(self):
917 self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_out())
919 def test_glist_utf8_container_out(self):
920 self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_out())
922 def test_glist_utf8_full_out(self):
923 self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_out())
925 def test_glist_utf8_none_inout(self):
926 self.assertEquals(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_none_inout(Sequence(('0', '1', '2'))))
928 def test_glist_utf8_container_inout(self):
929 self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.glist_utf8_container_inout(('0', '1', '2')))
931 def test_glist_utf8_full_inout(self):
932 self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.glist_utf8_full_inout(('0', '1', '2')))
935 class TestGSList(unittest.TestCase):
937 def test_gslist_int_none_return(self):
938 self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.gslist_int_none_return())
940 def test_gslist_utf8_none_return(self):
941 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_return())
943 def test_gslist_utf8_container_return(self):
944 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_return())
946 def test_gslist_utf8_full_return(self):
947 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_return())
949 def test_gslist_int_none_in(self):
950 GIMarshallingTests.gslist_int_none_in(Sequence((-1, 0, 1, 2)))
952 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, Sequence((-1, '0', 1, 2)))
954 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, 42)
955 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, None)
957 def test_gslist_utf8_none_in(self):
958 GIMarshallingTests.gslist_utf8_none_in(Sequence(('0', '1', '2')))
960 def test_gslist_utf8_none_out(self):
961 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_out())
963 def test_gslist_utf8_container_out(self):
964 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_out())
966 def test_gslist_utf8_full_out(self):
967 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_out())
969 def test_gslist_utf8_none_inout(self):
970 self.assertEquals(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_none_inout(Sequence(('0', '1', '2'))))
972 def test_gslist_utf8_container_inout(self):
973 self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.gslist_utf8_container_inout(('0', '1', '2')))
975 def test_gslist_utf8_full_inout(self):
976 self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.gslist_utf8_full_inout(('0', '1', '2')))
979 class TestGHashTable(unittest.TestCase):
981 def test_ghashtable_int_none_return(self):
982 self.assertEquals({-1: 1, 0: 0, 1: -1, 2: -2}, GIMarshallingTests.ghashtable_int_none_return())
984 def test_ghashtable_int_none_return(self):
985 self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_return())
987 def test_ghashtable_int_container_return(self):
988 self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_return())
990 def test_ghashtable_int_full_return(self):
991 self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_return())
993 def test_ghashtable_int_none_in(self):
994 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, {-1: 1, 0: '0', 1: -1, 2: -2})
999 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, '{-1: 1, 0: 0, 1: -1, 2: -2}')
1000 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, None)
1002 def test_ghashtable_utf8_none_in(self):
1003 GIMarshallingTests.ghashtable_utf8_none_in({'-1': '1', '0': '0', '1': '-1', '2': '-2'})
1005 def test_ghashtable_utf8_none_out(self):
1006 self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_out())
1008 def test_ghashtable_utf8_container_out(self):
1009 self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_out())
1011 def test_ghashtable_utf8_full_out(self):
1012 self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_out())
1014 def test_ghashtable_utf8_none_inout(self):
1015 self.assertEquals({'-1': '1', '0': '0', '1': '1'},
1016 GIMarshallingTests.ghashtable_utf8_none_inout({'-1': '1', '0': '0', '1': '-1', '2': '-2'}))
1018 def test_ghashtable_utf8_container_inout(self):
1019 self.assertEquals({'-1': '1', '0': '0', '1': '1'},
1020 GIMarshallingTests.ghashtable_utf8_container_inout({'-1': '1', '0': '0', '1': '-1', '2': '-2'}))
1022 def test_ghashtable_utf8_full_inout(self):
1023 self.assertEquals({'-1': '1', '0': '0', '1': '1'},
1024 GIMarshallingTests.ghashtable_utf8_full_inout({'-1': '1', '0': '0', '1': '-1', '2': '-2'}))
1027 class TestGValue(unittest.TestCase):
1029 def test_gvalue_return(self):
1030 self.assertEquals(42, GIMarshallingTests.gvalue_return())
1032 def test_gvalue_in(self):
1033 GIMarshallingTests.gvalue_in(42)
1034 value = GObject.Value()
1035 value.init(GObject.TYPE_INT)
1037 GIMarshallingTests.gvalue_in(value)
1039 def test_gvalue_out(self):
1040 self.assertEquals(42, GIMarshallingTests.gvalue_out())
1042 def test_gvalue_inout(self):
1043 self.assertEquals('42', GIMarshallingTests.gvalue_inout(42))
1044 value = GObject.Value()
1045 value.init(GObject.TYPE_INT)
1047 self.assertEquals('42', GIMarshallingTests.gvalue_inout(value))
1049 def test_gvalue_flat_array_in(self):
1050 # the function already asserts the correct values
1051 GIMarshallingTests.gvalue_flat_array([42, "42", True])
1053 def test_gvalue_flat_array_out(self):
1054 values = GIMarshallingTests.return_gvalue_flat_array()
1055 self.assertEqual(values, [42, '42', True])
1058 class TestGClosure(unittest.TestCase):
1060 def test_gclosure_in(self):
1061 GIMarshallingTests.gclosure_in(lambda: 42)
1063 # test passing a closure between two C calls
1064 closure = GIMarshallingTests.gclosure_return()
1065 GIMarshallingTests.gclosure_in(closure)
1067 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, 42)
1068 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, None)
1071 class TestPointer(unittest.TestCase):
1072 def test_pointer_in_return(self):
1073 self.assertEquals(GIMarshallingTests.pointer_in_return(42), 42)
1076 class TestEnum(unittest.TestCase):
1079 def setUpClass(cls):
1080 '''Run tests under a test locale.
1082 Upper case conversion of member names should not be locale specific;
1083 e. g. in Turkish, "i".upper() == "i", which gives results like "iNVALiD"
1085 Run test under a locale which defines toupper('a') == 'a'
1087 cls.locale_dir = tempfile.mkdtemp()
1088 subprocess.check_call(['localedef', '-i',
1089 os.path.join(os.path.dirname(os.path.realpath(__file__)), 'te_ST@nouppera'),
1090 '-c', '-f', 'UTF-8', os.path.join(cls.locale_dir, 'te_ST.UTF-8@nouppera')])
1091 os.environ['LOCPATH'] = cls.locale_dir
1092 locale.setlocale(locale.LC_ALL, 'te_ST.UTF-8@nouppera')
1095 def tearDownClass(cls):
1096 locale.setlocale(locale.LC_ALL, 'C')
1097 shutil.rmtree(cls.locale_dir)
1099 del os.environ['LOCPATH']
1103 def test_enum(self):
1104 self.assertTrue(issubclass(GIMarshallingTests.Enum, int))
1105 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE1, GIMarshallingTests.Enum))
1106 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE2, GIMarshallingTests.Enum))
1107 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE3, GIMarshallingTests.Enum))
1108 self.assertEquals(42, GIMarshallingTests.Enum.VALUE3)
1110 def test_value_nick_and_name(self):
1111 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_nick, 'value1')
1112 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_nick, 'value2')
1113 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_nick, 'value3')
1115 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE1')
1116 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE2')
1117 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE3')
1119 def test_enum_in(self):
1120 GIMarshallingTests.enum_in(GIMarshallingTests.Enum.VALUE3)
1121 GIMarshallingTests.enum_in(42)
1123 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 43)
1124 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 'GIMarshallingTests.Enum.VALUE3')
1126 def test_enum_out(self):
1127 enum = GIMarshallingTests.enum_out()
1128 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1129 self.assertEquals(enum, GIMarshallingTests.Enum.VALUE3)
1131 def test_enum_inout(self):
1132 enum = GIMarshallingTests.enum_inout(GIMarshallingTests.Enum.VALUE3)
1133 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1134 self.assertEquals(enum, GIMarshallingTests.Enum.VALUE1)
1136 def test_enum_second(self):
1137 # check for the bug where different non-gtype enums share the same class
1138 self.assertNotEqual(GIMarshallingTests.Enum, GIMarshallingTests.SecondEnum)
1140 # check that values are not being shared between different enums
1141 self.assertTrue(hasattr(GIMarshallingTests.SecondEnum, "SECONDVALUE1"))
1142 self.assertRaises(AttributeError, getattr, GIMarshallingTests.Enum, "SECONDVALUE1")
1143 self.assertTrue(hasattr(GIMarshallingTests.Enum, "VALUE1"))
1144 self.assertRaises(AttributeError, getattr, GIMarshallingTests.SecondEnum, "VALUE1")
1147 class TestGEnum(unittest.TestCase):
1149 def test_genum(self):
1150 self.assertTrue(issubclass(GIMarshallingTests.GEnum, GObject.GEnum))
1151 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE1, GIMarshallingTests.GEnum))
1152 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE2, GIMarshallingTests.GEnum))
1153 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE3, GIMarshallingTests.GEnum))
1154 self.assertEquals(42, GIMarshallingTests.GEnum.VALUE3)
1156 def test_value_nick_and_name(self):
1157 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_nick, 'value1')
1158 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_nick, 'value2')
1159 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_nick, 'value3')
1161 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE1')
1162 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE2')
1163 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE3')
1165 def test_genum_in(self):
1166 GIMarshallingTests.genum_in(GIMarshallingTests.GEnum.VALUE3)
1167 GIMarshallingTests.genum_in(42)
1169 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 43)
1170 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 'GIMarshallingTests.GEnum.VALUE3')
1172 def test_genum_out(self):
1173 genum = GIMarshallingTests.genum_out()
1174 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1175 self.assertEquals(genum, GIMarshallingTests.GEnum.VALUE3)
1177 def test_genum_inout(self):
1178 genum = GIMarshallingTests.genum_inout(GIMarshallingTests.GEnum.VALUE3)
1179 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1180 self.assertEquals(genum, GIMarshallingTests.GEnum.VALUE1)
1183 class TestGFlags(unittest.TestCase):
1185 def test_flags(self):
1186 self.assertTrue(issubclass(GIMarshallingTests.Flags, GObject.GFlags))
1187 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1, GIMarshallingTests.Flags))
1188 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE2, GIMarshallingTests.Flags))
1189 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE3, GIMarshallingTests.Flags))
1190 # __or__() operation should still return an instance, not an int.
1191 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1 | GIMarshallingTests.Flags.VALUE2,
1192 GIMarshallingTests.Flags))
1193 self.assertEquals(1 << 1, GIMarshallingTests.Flags.VALUE2)
1195 def test_value_nick_and_name(self):
1196 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_nick, 'value1')
1197 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_nick, 'value2')
1198 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_nick, 'value3')
1200 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE1')
1201 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE2')
1202 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE3')
1204 def test_flags_in(self):
1205 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2)
1206 # result of __or__() operation should still be valid instance, not an int.
1207 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE2)
1208 GIMarshallingTests.flags_in_zero(Number(0))
1210 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 1 << 1)
1211 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 'GIMarshallingTests.Flags.VALUE2')
1213 def test_flags_out(self):
1214 flags = GIMarshallingTests.flags_out()
1215 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1216 self.assertEquals(flags, GIMarshallingTests.Flags.VALUE2)
1218 def test_flags_inout(self):
1219 flags = GIMarshallingTests.flags_inout(GIMarshallingTests.Flags.VALUE2)
1220 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1221 self.assertEquals(flags, GIMarshallingTests.Flags.VALUE1)
1224 class TestNoTypeFlags(unittest.TestCase):
1226 def test_flags(self):
1227 self.assertTrue(issubclass(GIMarshallingTests.NoTypeFlags, GObject.GFlags))
1228 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1, GIMarshallingTests.NoTypeFlags))
1229 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE2, GIMarshallingTests.NoTypeFlags))
1230 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE3, GIMarshallingTests.NoTypeFlags))
1231 # __or__() operation should still return an instance, not an int.
1232 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1 | GIMarshallingTests.NoTypeFlags.VALUE2,
1233 GIMarshallingTests.NoTypeFlags))
1234 self.assertEquals(1 << 1, GIMarshallingTests.NoTypeFlags.VALUE2)
1236 def test_value_nick_and_name(self):
1237 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_nick, 'value1')
1238 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_nick, 'value2')
1239 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_nick, 'value3')
1241 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE1')
1242 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2')
1243 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE3')
1245 def test_flags_in(self):
1246 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2)
1247 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2 | GIMarshallingTests.NoTypeFlags.VALUE2)
1248 GIMarshallingTests.no_type_flags_in_zero(Number(0))
1250 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 1 << 1)
1251 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 'GIMarshallingTests.NoTypeFlags.VALUE2')
1253 def test_flags_out(self):
1254 flags = GIMarshallingTests.no_type_flags_out()
1255 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1256 self.assertEquals(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1258 def test_flags_inout(self):
1259 flags = GIMarshallingTests.no_type_flags_inout(GIMarshallingTests.NoTypeFlags.VALUE2)
1260 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1261 self.assertEquals(flags, GIMarshallingTests.NoTypeFlags.VALUE1)
1264 class TestStructure(unittest.TestCase):
1266 def test_simple_struct(self):
1267 self.assertTrue(issubclass(GIMarshallingTests.SimpleStruct, GObject.GPointer))
1269 struct = GIMarshallingTests.SimpleStruct()
1270 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1272 self.assertEquals(0, struct.long_)
1273 self.assertEquals(0, struct.int8)
1278 self.assertEquals(6, struct.long_)
1279 self.assertEquals(7, struct.int8)
1283 def test_nested_struct(self):
1284 struct = GIMarshallingTests.NestedStruct()
1286 self.assertTrue(isinstance(struct.simple_struct, GIMarshallingTests.SimpleStruct))
1288 struct.simple_struct.long_ = 42
1289 self.assertEquals(42, struct.simple_struct.long_)
1293 def test_not_simple_struct(self):
1294 struct = GIMarshallingTests.NotSimpleStruct()
1295 self.assertEquals(None, struct.pointer)
1297 def test_simple_struct_return(self):
1298 struct = GIMarshallingTests.simple_struct_returnv()
1300 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1301 self.assertEquals(6, struct.long_)
1302 self.assertEquals(7, struct.int8)
1306 def test_simple_struct_in(self):
1307 struct = GIMarshallingTests.SimpleStruct()
1311 GIMarshallingTests.SimpleStruct.inv(struct)
1315 struct = GIMarshallingTests.NestedStruct()
1317 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, struct)
1321 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, None)
1323 def test_simple_struct_method(self):
1324 struct = GIMarshallingTests.SimpleStruct()
1332 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.method)
1334 def test_pointer_struct(self):
1335 self.assertTrue(issubclass(GIMarshallingTests.PointerStruct, GObject.GPointer))
1337 struct = GIMarshallingTests.PointerStruct()
1338 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1342 def test_pointer_struct_return(self):
1343 struct = GIMarshallingTests.pointer_struct_returnv()
1345 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1346 self.assertEquals(42, struct.long_)
1350 def test_pointer_struct_in(self):
1351 struct = GIMarshallingTests.PointerStruct()
1358 def test_boxed_struct(self):
1359 self.assertTrue(issubclass(GIMarshallingTests.BoxedStruct, GObject.GBoxed))
1361 struct = GIMarshallingTests.BoxedStruct()
1362 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1364 self.assertEquals(0, struct.long_)
1365 self.assertEquals([], struct.g_strv)
1369 def test_boxed_struct_new(self):
1370 struct = GIMarshallingTests.BoxedStruct.new()
1371 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1375 def test_boxed_struct_copy(self):
1376 struct = GIMarshallingTests.BoxedStruct()
1378 new_struct = struct.copy()
1379 self.assertTrue(isinstance(new_struct, GIMarshallingTests.BoxedStruct))
1384 def test_boxed_struct_return(self):
1385 struct = GIMarshallingTests.boxed_struct_returnv()
1387 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1388 self.assertEquals(42, struct.long_)
1389 self.assertEquals(['0', '1', '2'], struct.g_strv)
1393 def test_boxed_struct_in(self):
1394 struct = GIMarshallingTests.BoxedStruct()
1401 def test_boxed_struct_out(self):
1402 struct = GIMarshallingTests.boxed_struct_out()
1404 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1405 self.assertEquals(42, struct.long_)
1409 def test_boxed_struct_inout(self):
1410 in_struct = GIMarshallingTests.BoxedStruct()
1411 in_struct.long_ = 42
1413 out_struct = GIMarshallingTests.boxed_struct_inout(in_struct)
1415 self.assertTrue(isinstance(out_struct, GIMarshallingTests.BoxedStruct))
1416 self.assertEquals(0, out_struct.long_)
1421 def test_union(self):
1422 union = GIMarshallingTests.Union()
1424 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1426 new_union = union.copy()
1427 self.assertTrue(isinstance(new_union, GIMarshallingTests.Union))
1432 def test_union_return(self):
1433 union = GIMarshallingTests.union_returnv()
1435 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1436 self.assertEquals(42, union.long_)
1440 def test_union_in(self):
1441 union = GIMarshallingTests.Union()
1448 def test_union_method(self):
1449 union = GIMarshallingTests.Union()
1456 self.assertRaises(TypeError, GIMarshallingTests.Union.method)
1459 class TestGObject(unittest.TestCase):
1461 def test_object(self):
1462 self.assertTrue(issubclass(GIMarshallingTests.Object, GObject.GObject))
1464 object_ = GIMarshallingTests.Object()
1465 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1466 self.assertEquals(object_.__grefcount__, 1)
1468 def test_object_new(self):
1469 object_ = GIMarshallingTests.Object.new(42)
1470 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1471 self.assertEquals(object_.__grefcount__, 1)
1473 def test_object_int(self):
1474 object_ = GIMarshallingTests.Object(int = 42)
1475 self.assertEquals(object_.int_, 42)
1476 # FIXME: Don't work yet.
1478 # self.assertEquals(object_.int_, 0)
1480 def test_object_static_method(self):
1481 GIMarshallingTests.Object.static_method()
1483 def test_object_method(self):
1484 GIMarshallingTests.Object(int = 42).method()
1485 self.assertRaises(TypeError, GIMarshallingTests.Object.method, GObject.GObject())
1486 self.assertRaises(TypeError, GIMarshallingTests.Object.method)
1488 def test_sub_object(self):
1489 self.assertTrue(issubclass(GIMarshallingTests.SubObject, GIMarshallingTests.Object))
1491 object_ = GIMarshallingTests.SubObject()
1492 self.assertTrue(isinstance(object_, GIMarshallingTests.SubObject))
1494 def test_sub_object_new(self):
1495 self.assertRaises(TypeError, GIMarshallingTests.SubObject.new, 42)
1497 def test_sub_object_static_method(self):
1498 object_ = GIMarshallingTests.SubObject()
1499 object_.static_method()
1501 def test_sub_object_method(self):
1502 object_ = GIMarshallingTests.SubObject(int = 42)
1505 def test_sub_object_sub_method(self):
1506 object_ = GIMarshallingTests.SubObject()
1507 object_.sub_method()
1509 def test_sub_object_overwritten_method(self):
1510 object_ = GIMarshallingTests.SubObject()
1511 object_.overwritten_method()
1513 self.assertRaises(TypeError, GIMarshallingTests.SubObject.overwritten_method, GIMarshallingTests.Object())
1515 def test_sub_object_int(self):
1516 object_ = GIMarshallingTests.SubObject()
1517 self.assertEquals(object_.int_, 0)
1518 # FIXME: Don't work yet.
1520 # self.assertEquals(object_.int_, 42)
1522 def test_object_none_return(self):
1523 object_ = GIMarshallingTests.Object.none_return()
1524 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1525 self.assertEquals(object_.__grefcount__, 2)
1527 def test_object_full_return(self):
1528 object_ = GIMarshallingTests.Object.full_return()
1529 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1530 self.assertEquals(object_.__grefcount__, 1)
1532 def test_object_none_in(self):
1533 object_ = GIMarshallingTests.Object(int = 42)
1534 GIMarshallingTests.Object.none_in(object_)
1535 self.assertEquals(object_.__grefcount__, 1)
1537 object_ = GIMarshallingTests.SubObject(int = 42)
1538 GIMarshallingTests.Object.none_in(object_)
1540 object_ = GObject.GObject()
1541 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, object_)
1543 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, None)
1545 def test_object_none_out(self):
1546 object_ = GIMarshallingTests.Object.none_out()
1547 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1548 self.assertEquals(object_.__grefcount__, 2)
1550 new_object = GIMarshallingTests.Object.none_out()
1551 self.assertTrue(new_object is object_)
1553 def test_object_full_out(self):
1554 object_ = GIMarshallingTests.Object.full_out()
1555 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1556 self.assertEquals(object_.__grefcount__, 1)
1558 def test_object_none_inout(self):
1559 object_ = GIMarshallingTests.Object(int = 42)
1560 new_object = GIMarshallingTests.Object.none_inout(object_)
1562 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1564 self.assertFalse(object_ is new_object)
1566 self.assertEquals(object_.__grefcount__, 1)
1567 self.assertEquals(new_object.__grefcount__, 2)
1569 new_new_object = GIMarshallingTests.Object.none_inout(object_)
1570 self.assertTrue(new_new_object is new_object)
1572 GIMarshallingTests.Object.none_inout(GIMarshallingTests.SubObject(int = 42))
1574 def test_object_full_inout(self):
1575 object_ = GIMarshallingTests.Object(int = 42)
1576 new_object = GIMarshallingTests.Object.full_inout(object_)
1578 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1580 self.assertFalse(object_ is new_object)
1582 self.assertEquals(object_.__grefcount__, 2)
1583 self.assertEquals(new_object.__grefcount__, 1)
1585 # FIXME: Doesn't actually return the same object.
1586 # def test_object_inout_same(self):
1587 # object_ = GIMarshallingTests.Object()
1588 # new_object = GIMarshallingTests.object_full_inout(object_)
1589 # self.assertTrue(object_ is new_object)
1590 # self.assertEquals(object_.__grefcount__, 1)
1593 class TestPythonGObject(unittest.TestCase):
1595 class Object(GIMarshallingTests.Object):
1596 def __init__(self, int):
1597 GIMarshallingTests.Object.__init__(self)
1601 # Don't call super, which asserts that self.int == 42.
1604 def do_method_int8_in(self, int8):
1607 def do_method_int8_out(self):
1610 def do_method_with_default_implementation(self, int8):
1611 GIMarshallingTests.Object.do_method_with_default_implementation(self, int8)
1612 self.props.int += int8
1614 class SubObject(GIMarshallingTests.SubObject):
1615 def __init__(self, int):
1616 GIMarshallingTests.SubObject.__init__(self)
1619 def do_method_with_default_implementation(self, int8):
1622 def test_object(self):
1623 self.assertTrue(issubclass(self.Object, GIMarshallingTests.Object))
1625 object_ = self.Object(int = 42)
1626 self.assertTrue(isinstance(object_, self.Object))
1628 def test_object_method(self):
1629 self.Object(int = 0).method()
1631 def test_object_vfuncs(self):
1632 object_ = self.Object(int = 42)
1633 object_.method_int8_in(84)
1634 self.assertEqual(object_.val, 84)
1635 self.assertEqual(object_.method_int8_out(), 42)
1637 object_.method_with_default_implementation(42)
1638 self.assertEqual(object_.props.int, 84)
1640 class ObjectWithoutVFunc(GIMarshallingTests.Object):
1641 def __init__(self, int):
1642 GIMarshallingTests.Object.__init__(self)
1644 object_ = ObjectWithoutVFunc(int = 42)
1645 object_.method_with_default_implementation(84)
1646 self.assertEqual(object_.props.int, 84)
1648 def test_subobject_parent_vfunc(self):
1649 object_ = self.SubObject(int = 81)
1650 object_.method_with_default_implementation(87)
1651 self.assertEquals(object_.val, 87)
1653 def test_dynamic_module(self):
1654 from gi.module import DynamicGObjectModule
1655 self.assertTrue(isinstance(GObject, DynamicGObjectModule))
1656 # compare the same enum from both the pygobject attrs and gi GObject attrs
1657 self.assertEquals(GObject.SIGNAL_ACTION, GObject.SignalFlags.ACTION)
1658 # compare a static gobject attr with a dynamic GObject attr
1660 self.assertEquals(GObject.GObject, gi._gobject.GObject)
1662 def test_subobject_non_vfunc_do_method(self):
1663 class PythonObjectWithNonVFuncDoMethod:
1664 def do_not_a_vfunc(self):
1667 class ObjectOverrideNonVFuncDoMethod(GIMarshallingTests.Object, PythonObjectWithNonVFuncDoMethod):
1668 def do_not_a_vfunc(self):
1669 value = super(ObjectOverrideNonVFuncDoMethod, self).do_not_a_vfunc()
1672 object_ = ObjectOverrideNonVFuncDoMethod()
1673 self.assertEquals(18, object_.do_not_a_vfunc())
1675 def test_native_function_not_set_in_subclass_dict(self):
1676 # Previously, GI was setting virtual functions on the class as well
1677 # as any *native* class that subclasses it. Here we check that it is only
1678 # set on the class that the method is originally from.
1679 self.assertTrue('do_method_with_default_implementation' in GIMarshallingTests.Object.__dict__)
1680 self.assertTrue('do_method_with_default_implementation' not in GIMarshallingTests.SubObject.__dict__)
1682 def test_subobject_with_interface_and_non_vfunc_do_method(self):
1683 # There was a bug for searching for vfuncs in interfaces. It was
1684 # triggered by having a do_* method that wasn't overriding
1685 # a native vfunc, as well as inheriting from an interface.
1686 class GObjectSubclassWithInterface(GObject.GObject, GIMarshallingTests.Interface):
1687 def do_method_not_a_vfunc(self):
1690 def test_subsubobject(self):
1691 class SubSubSubObject(GIMarshallingTests.SubSubObject):
1692 def do_method_deep_hierarchy(self, num):
1693 self.props.int = num * 2
1695 sub_sub_sub_object = SubSubSubObject()
1696 GIMarshallingTests.SubSubObject.do_method_deep_hierarchy(sub_sub_sub_object, 5)
1697 self.assertEqual(sub_sub_sub_object.props.int, 5)
1700 class TestMultiOutputArgs(unittest.TestCase):
1702 def test_int_out_out(self):
1703 self.assertEquals((6, 7), GIMarshallingTests.int_out_out())
1705 def test_int_return_out(self):
1706 self.assertEquals((6, 7), GIMarshallingTests.int_return_out())
1708 class TestGErrorException(unittest.TestCase):
1709 def test_gerror_exception(self):
1710 self.assertRaises(GObject.GError, GIMarshallingTests.gerror)
1712 GIMarshallingTests.gerror()
1714 etype, e = sys.exc_info()[:2]
1715 self.assertEquals(e.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
1716 self.assertEquals(e.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
1717 self.assertEquals(e.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
1722 class TestInterfaces(unittest.TestCase):
1724 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
1726 GObject.GObject.__init__(self)
1729 def do_test_int8_in(self, int8):
1733 self.instance = self.TestInterfaceImpl()
1735 def test_wrapper(self):
1736 self.assertTrue(issubclass(GIMarshallingTests.Interface, GObject.GInterface))
1737 self.assertEquals(GIMarshallingTests.Interface.__gtype__.name, 'GIMarshallingTestsInterface')
1738 self.assertRaises(NotImplementedError, GIMarshallingTests.Interface)
1740 def test_implementation(self):
1741 self.assertTrue(issubclass(self.TestInterfaceImpl, GIMarshallingTests.Interface))
1742 self.assertTrue(isinstance(self.instance, GIMarshallingTests.Interface))
1744 def test_int8_int(self):
1745 GIMarshallingTests.test_interface_test_int8_in(self.instance, 42)
1746 self.assertEquals(self.instance.val, 42)
1748 def test_subclass(self):
1749 class TestInterfaceImplA(self.TestInterfaceImpl):
1752 class TestInterfaceImplB(TestInterfaceImplA):
1755 instance = TestInterfaceImplA()
1756 GIMarshallingTests.test_interface_test_int8_in(instance, 42)
1757 self.assertEquals(instance.val, 42)
1760 # there was a problem with Python bailing out because of
1761 # http://en.wikipedia.org/wiki/Diamond_problem with interfaces,
1762 # which shouldn't really be a problem.
1764 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
1767 class TestInterfaceImpl2(GIMarshallingTests.Interface,
1771 class TestInterfaceImpl3(self.TestInterfaceImpl,
1772 GIMarshallingTests.Interface2):
1776 class TestInterfaceClash(unittest.TestCase):
1778 def test_clash(self):
1780 class TestClash(GObject.GObject, GIMarshallingTests.Interface, GIMarshallingTests.Interface2):
1781 def do_test_int8_in(self, int8):
1785 self.assertRaises(TypeError, create_clash)
1788 class TestOverrides(unittest.TestCase):
1790 def test_constant(self):
1791 self.assertEquals(GIMarshallingTests.OVERRIDES_CONSTANT, 7)
1793 def test_struct(self):
1794 # Test that the constructor has been overridden.
1795 struct = GIMarshallingTests.OverridesStruct(42)
1797 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
1799 # Test that the method has been overridden.
1800 self.assertEquals(6, struct.method())
1804 # Test that the overrides wrapper has been registered.
1805 struct = GIMarshallingTests.overrides_struct_returnv()
1807 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
1811 def test_object(self):
1812 # Test that the constructor has been overridden.
1813 object_ = GIMarshallingTests.OverridesObject(42)
1815 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
1817 # Test that the alternate constructor has been overridden.
1818 object_ = GIMarshallingTests.OverridesObject.new(42)
1820 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
1822 # Test that the method has been overridden.
1823 self.assertEquals(6, object_.method())
1825 # Test that the overrides wrapper has been registered.
1826 object_ = GIMarshallingTests.OverridesObject.returnv()
1828 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
1830 def test_module_name(self):
1831 self.assertEquals(GIMarshallingTests.OverridesStruct.__module__, 'gi.overrides.GIMarshallingTests')
1832 self.assertEquals(GObject.InitiallyUnowned.__module__, 'gi.repository.GObject')
1835 class TestDir(unittest.TestCase):
1836 def test_members_list(self):
1837 list = dir(GIMarshallingTests)
1838 self.assertTrue('OverridesStruct' in list)
1839 self.assertTrue('BoxedStruct' in list)
1840 self.assertTrue('OVERRIDES_CONSTANT' in list)
1841 self.assertTrue('GEnum' in list)
1842 self.assertTrue('int32_return_max' in list)
1844 def test_modules_list(self):
1845 import gi.repository
1846 list = dir(gi.repository)
1847 self.assertTrue('GIMarshallingTests' in list)
1849 # FIXME: test to see if a module which was not imported is in the list
1850 # we should be listing every typelib we find, not just the ones
1851 # which are imported
1853 # to test this I recommend we compile a fake module which
1854 # our tests would never import and check to see if it is
1857 # self.assertTrue('DoNotImportDummyTests' in list)
1860 class TestGErrorArrayInCrash(unittest.TestCase):
1861 # Previously there was a bug in invoke, in which C arrays were unwrapped
1862 # from inside GArrays to be passed to the C function. But when a GError was
1863 # set, invoke would attempt to free the C array as if it were a GArray.
1864 # This crash is only for C arrays. It does not happen for C functions which
1865 # take in GArrays. See https://bugzilla.gnome.org/show_bug.cgi?id=642708
1866 def test_gerror_array_in_crash(self):
1867 self.assertRaises(GObject.GError, GIMarshallingTests.gerror_array_in, [1, 2, 3])
1870 class TestGErrorOut(unittest.TestCase):
1871 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
1872 def test_gerror_out(self):
1873 error, debug = GIMarshallingTests.gerror_out()
1875 self.assertIsInstance(error, GObject.GError)
1876 self.assertEquals(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
1877 self.assertEquals(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
1878 self.assertEquals(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
1879 self.assertEquals(debug, GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE)
1882 class TestGErrorOutTransferNone(unittest.TestCase):
1883 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
1884 def test_gerror_out_transfer_none(self):
1885 error, debug = GIMarshallingTests.gerror_out_transfer_none()
1887 self.assertIsInstance(error, GObject.GError)
1888 self.assertEquals(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
1889 self.assertEquals(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
1890 self.assertEquals(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
1891 self.assertEquals(GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE, debug)
1894 class TestGErrorReturn(unittest.TestCase):
1895 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
1896 def test_return_gerror(self):
1897 error = GIMarshallingTests.gerror_return()
1899 self.assertIsInstance(error, GObject.GError)
1900 self.assertEquals(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
1901 self.assertEquals(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
1902 self.assertEquals(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
1905 class TestKeywordArgs(unittest.TestCase):
1907 def test_calling(self):
1908 kw_func = GIMarshallingTests.int_three_in_three_out
1910 self.assertEquals(kw_func(1, 2, 3), (1, 2, 3))
1911 self.assertEquals(kw_func(**{'a':4, 'b':5, 'c':6}), (4, 5, 6))
1912 self.assertEquals(kw_func(1, **{'b':7, 'c':8}), (1, 7, 8))
1913 self.assertEquals(kw_func(1, 7, **{'c':8}), (1, 7, 8))
1914 self.assertEquals(kw_func(1, c=8, **{'b':7}), (1, 7, 8))
1915 self.assertEquals(kw_func(2, c=4, b=3), (2, 3, 4))
1916 self.assertEquals(kw_func(a=2, c=4, b=3), (2, 3, 4))
1918 def assertRaisesMessage(self, exception, message, func, *args, **kwargs):
1920 func(*args, **kwargs)
1922 (e_type, e) = sys.exc_info()[:2]
1923 if message is not None:
1924 self.assertEqual(str(e), message)
1928 msg = "%s() did not raise %s" % (func.__name__, exception.__name__)
1929 raise AssertionError(msg)
1931 def test_type_errors(self):
1933 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
1934 GIMarshallingTests.int_three_in_three_out)
1935 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (1 given)",
1936 GIMarshallingTests.int_three_in_three_out, 1)
1937 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
1938 GIMarshallingTests.int_three_in_three_out, *())
1939 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
1940 GIMarshallingTests.int_three_in_three_out, *(), **{})
1941 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (0 given)",
1942 GIMarshallingTests.int_three_in_three_out, *(), **{'c':4})
1944 # test too many args
1945 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (4 given)",
1946 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4))
1947 self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (4 given)",
1948 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4), c=6)
1950 # test too many keyword args
1951 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got multiple values for keyword argument 'a'",
1952 GIMarshallingTests.int_three_in_three_out, 1, 2, 3, **{'a': 4, 'b': 5})
1953 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'd'",
1954 GIMarshallingTests.int_three_in_three_out, d=4)
1955 self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'e'",
1956 GIMarshallingTests.int_three_in_three_out, **{'e': 2})
1958 def test_kwargs_are_not_modified(self):
1961 GIMarshallingTests.int_three_in_three_out(1, c=4, **d)
1962 self.assertEqual(d, d2)
1965 class TestPropertiesObject(unittest.TestCase):
1968 self.obj = GIMarshallingTests.PropertiesObject()
1970 def test_boolean(self):
1971 self.assertEqual(self.obj.props.some_boolean, False)
1972 self.obj.props.some_boolean = True
1973 self.assertEqual(self.obj.props.some_boolean, True)
1975 @unittest.expectedFailure
1976 def test_char(self):
1977 # gobject-introspection thinks it has a guint8 type tag, which is wrong
1978 self.assertEqual(self.obj.props.some_char, 0)
1979 self.obj.props.some_char = GObject.G_MAXINT8
1980 self.assertEqual(self.obj.props.some_char, GObject.G_MAXINT8)
1982 def test_uchar(self):
1983 self.assertEqual(self.obj.props.some_uchar, 0)
1984 self.obj.props.some_uchar = GObject.G_MAXUINT8
1985 self.assertEqual(self.obj.props.some_uchar, GObject.G_MAXUINT8)
1988 self.assertEqual(self.obj.props.some_int, 0)
1989 self.obj.props.some_int = GObject.G_MAXINT
1990 self.assertEqual(self.obj.props.some_int, GObject.G_MAXINT)
1992 def test_uint(self):
1993 self.assertEqual(self.obj.props.some_uint, 0)
1994 self.obj.props.some_uint = GObject.G_MAXUINT
1995 self.assertEqual(self.obj.props.some_uint, GObject.G_MAXUINT)
1997 def test_long(self):
1998 self.assertEqual(self.obj.props.some_long, 0)
1999 self.obj.props.some_long = GObject.G_MAXLONG
2000 self.assertEqual(self.obj.props.some_long, GObject.G_MAXLONG)
2002 def test_ulong(self):
2003 self.assertEqual(self.obj.props.some_ulong, 0)
2004 self.obj.props.some_ulong = GObject.G_MAXULONG
2005 self.assertEqual(self.obj.props.some_ulong, GObject.G_MAXULONG)
2007 def test_int64(self):
2008 self.assertEqual(self.obj.props.some_int64, 0)
2009 self.obj.props.some_int64 = GObject.G_MAXINT64
2010 self.assertEqual(self.obj.props.some_int64, GObject.G_MAXINT64)
2012 def test_uint64(self):
2013 self.assertEqual(self.obj.props.some_uint64, 0)
2014 self.obj.props.some_uint64 = GObject.G_MAXUINT64
2015 self.assertEqual(self.obj.props.some_uint64, GObject.G_MAXUINT64)
2017 def test_float(self):
2018 self.assertEqual(self.obj.props.some_float, 0)
2019 self.obj.props.some_float = GObject.G_MAXFLOAT
2020 self.assertEqual(self.obj.props.some_float, GObject.G_MAXFLOAT)
2022 def test_double(self):
2023 self.assertEqual(self.obj.props.some_double, 0)
2024 self.obj.props.some_double = GObject.G_MAXDOUBLE
2025 self.assertEqual(self.obj.props.some_double, GObject.G_MAXDOUBLE)