1 # -*- Mode: Python; py-indent-offset: 4 -*-
3 # vim: tabstop=4 shiftwidth=4 expandtab
13 from gi.repository import GObject
16 from gi.repository import GIMarshallingTests
18 from compathelper import _bytes
20 if sys.version_info < (3, 0):
21 CONSTANT_UTF8 = "const \xe2\x99\xa5 utf8"
22 PY2_UNICODE_UTF8 = unicode(CONSTANT_UTF8, 'UTF-8')
25 CONSTANT_UTF8 = "const ♥ utf8"
33 def __init__(self, value):
37 return int(self.value)
40 return float(self.value)
43 class Sequence(object):
45 def __init__(self, sequence):
46 self.sequence = sequence
49 return len(self.sequence)
51 def __getitem__(self, key):
52 return self.sequence[key]
55 class TestConstant(unittest.TestCase):
57 # Blocked by https://bugzilla.gnome.org/show_bug.cgi?id=595773
58 # def test_constant_utf8(self):
59 # self.assertEquals(CONSTANT_UTF8, GIMarshallingTests.CONSTANT_UTF8)
61 def test_constant_number(self):
62 self.assertEquals(CONSTANT_NUMBER, GIMarshallingTests.CONSTANT_NUMBER)
65 class TestBoolean(unittest.TestCase):
67 def test_boolean_return(self):
68 self.assertEquals(True, GIMarshallingTests.boolean_return_true())
69 self.assertEquals(False, GIMarshallingTests.boolean_return_false())
71 def test_boolean_in(self):
72 GIMarshallingTests.boolean_in_true(True)
73 GIMarshallingTests.boolean_in_false(False)
75 GIMarshallingTests.boolean_in_true(1)
76 GIMarshallingTests.boolean_in_false(0)
78 def test_boolean_out(self):
79 self.assertEquals(True, GIMarshallingTests.boolean_out_true())
80 self.assertEquals(False, GIMarshallingTests.boolean_out_false())
82 def test_boolean_inout(self):
83 self.assertEquals(False, GIMarshallingTests.boolean_inout_true_false(True))
84 self.assertEquals(True, GIMarshallingTests.boolean_inout_false_true(False))
87 class TestInt8(unittest.TestCase):
89 MAX = GObject.G_MAXINT8
90 MIN = GObject.G_MININT8
92 def test_int8_return(self):
93 self.assertEquals(self.MAX, GIMarshallingTests.int8_return_max())
94 self.assertEquals(self.MIN, GIMarshallingTests.int8_return_min())
96 def test_int8_in(self):
97 max = Number(self.MAX)
98 min = Number(self.MIN)
100 GIMarshallingTests.int8_in_max(max)
101 GIMarshallingTests.int8_in_min(min)
106 self.assertRaises(ValueError, GIMarshallingTests.int8_in_max, max)
107 self.assertRaises(ValueError, GIMarshallingTests.int8_in_min, min)
109 self.assertRaises(TypeError, GIMarshallingTests.int8_in_max, "self.MAX")
111 def test_int8_out(self):
112 self.assertEquals(self.MAX, GIMarshallingTests.int8_out_max())
113 self.assertEquals(self.MIN, GIMarshallingTests.int8_out_min())
115 def test_int8_inout(self):
116 self.assertEquals(self.MIN, GIMarshallingTests.int8_inout_max_min(Number(self.MAX)))
117 self.assertEquals(self.MAX, GIMarshallingTests.int8_inout_min_max(Number(self.MIN)))
120 class TestUInt8(unittest.TestCase):
122 MAX = GObject.G_MAXUINT8
124 def test_uint8_return(self):
125 self.assertEquals(self.MAX, GIMarshallingTests.uint8_return())
127 def test_uint8_in(self):
128 number = Number(self.MAX)
130 GIMarshallingTests.uint8_in(number)
131 GIMarshallingTests.uint8_in(CHAR_255)
134 self.assertRaises(ValueError, GIMarshallingTests.uint8_in, number)
135 self.assertRaises(ValueError, GIMarshallingTests.uint8_in, Number(-1))
137 self.assertRaises(TypeError, GIMarshallingTests.uint8_in, "self.MAX")
139 def test_uint8_out(self):
140 self.assertEquals(self.MAX, GIMarshallingTests.uint8_out())
142 def test_uint8_inout(self):
143 self.assertEquals(0, GIMarshallingTests.uint8_inout(Number(self.MAX)))
146 class TestInt16(unittest.TestCase):
148 MAX = GObject.G_MAXINT16
149 MIN = GObject.G_MININT16
151 def test_int16_return(self):
152 self.assertEquals(self.MAX, GIMarshallingTests.int16_return_max())
153 self.assertEquals(self.MIN, GIMarshallingTests.int16_return_min())
155 def test_int16_in(self):
156 max = Number(self.MAX)
157 min = Number(self.MIN)
159 GIMarshallingTests.int16_in_max(max)
160 GIMarshallingTests.int16_in_min(min)
165 self.assertRaises(ValueError, GIMarshallingTests.int16_in_max, max)
166 self.assertRaises(ValueError, GIMarshallingTests.int16_in_min, min)
168 self.assertRaises(TypeError, GIMarshallingTests.int16_in_max, "self.MAX")
170 def test_int16_out(self):
171 self.assertEquals(self.MAX, GIMarshallingTests.int16_out_max())
172 self.assertEquals(self.MIN, GIMarshallingTests.int16_out_min())
174 def test_int16_inout(self):
175 self.assertEquals(self.MIN, GIMarshallingTests.int16_inout_max_min(Number(self.MAX)))
176 self.assertEquals(self.MAX, GIMarshallingTests.int16_inout_min_max(Number(self.MIN)))
179 class TestUInt16(unittest.TestCase):
181 MAX = GObject.G_MAXUINT16
183 def test_uint16_return(self):
184 self.assertEquals(self.MAX, GIMarshallingTests.uint16_return())
186 def test_uint16_in(self):
187 number = Number(self.MAX)
189 GIMarshallingTests.uint16_in(number)
193 self.assertRaises(ValueError, GIMarshallingTests.uint16_in, number)
194 self.assertRaises(ValueError, GIMarshallingTests.uint16_in, Number(-1))
196 self.assertRaises(TypeError, GIMarshallingTests.uint16_in, "self.MAX")
198 def test_uint16_out(self):
199 self.assertEquals(self.MAX, GIMarshallingTests.uint16_out())
201 def test_uint16_inout(self):
202 self.assertEquals(0, GIMarshallingTests.uint16_inout(Number(self.MAX)))
205 class TestInt32(unittest.TestCase):
207 MAX = GObject.G_MAXINT32
208 MIN = GObject.G_MININT32
210 def test_int32_return(self):
211 self.assertEquals(self.MAX, GIMarshallingTests.int32_return_max())
212 self.assertEquals(self.MIN, GIMarshallingTests.int32_return_min())
214 def test_int32_in(self):
215 max = Number(self.MAX)
216 min = Number(self.MIN)
218 GIMarshallingTests.int32_in_max(max)
219 GIMarshallingTests.int32_in_min(min)
224 self.assertRaises(ValueError, GIMarshallingTests.int32_in_max, max)
225 self.assertRaises(ValueError, GIMarshallingTests.int32_in_min, min)
227 self.assertRaises(TypeError, GIMarshallingTests.int32_in_max, "self.MAX")
229 def test_int32_out(self):
230 self.assertEquals(self.MAX, GIMarshallingTests.int32_out_max())
231 self.assertEquals(self.MIN, GIMarshallingTests.int32_out_min())
233 def test_int32_inout(self):
234 self.assertEquals(self.MIN, GIMarshallingTests.int32_inout_max_min(Number(self.MAX)))
235 self.assertEquals(self.MAX, GIMarshallingTests.int32_inout_min_max(Number(self.MIN)))
238 class TestUInt32(unittest.TestCase):
240 MAX = GObject.G_MAXUINT32
242 def test_uint32_return(self):
243 self.assertEquals(self.MAX, GIMarshallingTests.uint32_return())
245 def test_uint32_in(self):
246 number = Number(self.MAX)
248 GIMarshallingTests.uint32_in(number)
252 self.assertRaises(ValueError, GIMarshallingTests.uint32_in, number)
253 self.assertRaises(ValueError, GIMarshallingTests.uint32_in, Number(-1))
255 self.assertRaises(TypeError, GIMarshallingTests.uint32_in, "self.MAX")
257 def test_uint32_out(self):
258 self.assertEquals(self.MAX, GIMarshallingTests.uint32_out())
260 def test_uint32_inout(self):
261 self.assertEquals(0, GIMarshallingTests.uint32_inout(Number(self.MAX)))
264 class TestInt64(unittest.TestCase):
269 def test_int64_return(self):
270 self.assertEquals(self.MAX, GIMarshallingTests.int64_return_max())
271 self.assertEquals(self.MIN, GIMarshallingTests.int64_return_min())
273 def test_int64_in(self):
274 max = Number(self.MAX)
275 min = Number(self.MIN)
277 GIMarshallingTests.int64_in_max(max)
278 GIMarshallingTests.int64_in_min(min)
283 self.assertRaises(ValueError, GIMarshallingTests.int64_in_max, max)
284 self.assertRaises(ValueError, GIMarshallingTests.int64_in_min, min)
286 self.assertRaises(TypeError, GIMarshallingTests.int64_in_max, "self.MAX")
288 def test_int64_out(self):
289 self.assertEquals(self.MAX, GIMarshallingTests.int64_out_max())
290 self.assertEquals(self.MIN, GIMarshallingTests.int64_out_min())
292 def test_int64_inout(self):
293 self.assertEquals(self.MIN, GIMarshallingTests.int64_inout_max_min(Number(self.MAX)))
294 self.assertEquals(self.MAX, GIMarshallingTests.int64_inout_min_max(Number(self.MIN)))
297 class TestUInt64(unittest.TestCase):
301 def test_uint64_return(self):
302 self.assertEquals(self.MAX, GIMarshallingTests.uint64_return())
304 def test_uint64_in(self):
305 number = Number(self.MAX)
307 GIMarshallingTests.uint64_in(number)
311 self.assertRaises(ValueError, GIMarshallingTests.uint64_in, number)
312 self.assertRaises(ValueError, GIMarshallingTests.uint64_in, Number(-1))
314 self.assertRaises(TypeError, GIMarshallingTests.uint64_in, "self.MAX")
316 def test_uint64_out(self):
317 self.assertEquals(self.MAX, GIMarshallingTests.uint64_out())
319 def test_uint64_inout(self):
320 self.assertEquals(0, GIMarshallingTests.uint64_inout(Number(self.MAX)))
323 class TestShort(unittest.TestCase):
325 MAX = GObject.constants.G_MAXSHORT
326 MIN = GObject.constants.G_MINSHORT
328 def test_short_return(self):
329 self.assertEquals(self.MAX, GIMarshallingTests.short_return_max())
330 self.assertEquals(self.MIN, GIMarshallingTests.short_return_min())
332 def test_short_in(self):
333 max = Number(self.MAX)
334 min = Number(self.MIN)
336 GIMarshallingTests.short_in_max(max)
337 GIMarshallingTests.short_in_min(min)
342 self.assertRaises(ValueError, GIMarshallingTests.short_in_max, max)
343 self.assertRaises(ValueError, GIMarshallingTests.short_in_min, min)
345 self.assertRaises(TypeError, GIMarshallingTests.short_in_max, "self.MAX")
347 def test_short_out(self):
348 self.assertEquals(self.MAX, GIMarshallingTests.short_out_max())
349 self.assertEquals(self.MIN, GIMarshallingTests.short_out_min())
351 def test_short_inout(self):
352 self.assertEquals(self.MIN, GIMarshallingTests.short_inout_max_min(Number(self.MAX)))
353 self.assertEquals(self.MAX, GIMarshallingTests.short_inout_min_max(Number(self.MIN)))
356 class TestUShort(unittest.TestCase):
358 MAX = GObject.constants.G_MAXUSHORT
360 def test_ushort_return(self):
361 self.assertEquals(self.MAX, GIMarshallingTests.ushort_return())
363 def test_ushort_in(self):
364 number = Number(self.MAX)
366 GIMarshallingTests.ushort_in(number)
370 self.assertRaises(ValueError, GIMarshallingTests.ushort_in, number)
371 self.assertRaises(ValueError, GIMarshallingTests.ushort_in, Number(-1))
373 self.assertRaises(TypeError, GIMarshallingTests.ushort_in, "self.MAX")
375 def test_ushort_out(self):
376 self.assertEquals(self.MAX, GIMarshallingTests.ushort_out())
378 def test_ushort_inout(self):
379 self.assertEquals(0, GIMarshallingTests.ushort_inout(Number(self.MAX)))
382 class TestInt(unittest.TestCase):
384 MAX = GObject.constants.G_MAXINT
385 MIN = GObject.constants.G_MININT
387 def test_int_return(self):
388 self.assertEquals(self.MAX, GIMarshallingTests.int_return_max())
389 self.assertEquals(self.MIN, GIMarshallingTests.int_return_min())
391 def test_int_in(self):
392 max = Number(self.MAX)
393 min = Number(self.MIN)
395 GIMarshallingTests.int_in_max(max)
396 GIMarshallingTests.int_in_min(min)
401 self.assertRaises(ValueError, GIMarshallingTests.int_in_max, max)
402 self.assertRaises(ValueError, GIMarshallingTests.int_in_min, min)
404 self.assertRaises(TypeError, GIMarshallingTests.int_in_max, "self.MAX")
406 def test_int_out(self):
407 self.assertEquals(self.MAX, GIMarshallingTests.int_out_max())
408 self.assertEquals(self.MIN, GIMarshallingTests.int_out_min())
410 def test_int_inout(self):
411 self.assertEquals(self.MIN, GIMarshallingTests.int_inout_max_min(Number(self.MAX)))
412 self.assertEquals(self.MAX, GIMarshallingTests.int_inout_min_max(Number(self.MIN)))
413 self.assertRaises(TypeError, GIMarshallingTests.int_inout_min_max, Number(self.MIN), CONSTANT_NUMBER)
416 class TestUInt(unittest.TestCase):
418 MAX = GObject.constants.G_MAXUINT
420 def test_uint_return(self):
421 self.assertEquals(self.MAX, GIMarshallingTests.uint_return())
423 def test_uint_in(self):
424 number = Number(self.MAX)
426 GIMarshallingTests.uint_in(number)
430 self.assertRaises(ValueError, GIMarshallingTests.uint_in, number)
431 self.assertRaises(ValueError, GIMarshallingTests.uint_in, Number(-1))
433 self.assertRaises(TypeError, GIMarshallingTests.uint_in, "self.MAX")
435 def test_uint_out(self):
436 self.assertEquals(self.MAX, GIMarshallingTests.uint_out())
438 def test_uint_inout(self):
439 self.assertEquals(0, GIMarshallingTests.uint_inout(Number(self.MAX)))
442 class TestLong(unittest.TestCase):
444 MAX = GObject.constants.G_MAXLONG
445 MIN = GObject.constants.G_MINLONG
447 def test_long_return(self):
448 self.assertEquals(self.MAX, GIMarshallingTests.long_return_max())
449 self.assertEquals(self.MIN, GIMarshallingTests.long_return_min())
451 def test_long_in(self):
452 max = Number(self.MAX)
453 min = Number(self.MIN)
455 GIMarshallingTests.long_in_max(max)
456 GIMarshallingTests.long_in_min(min)
461 self.assertRaises(ValueError, GIMarshallingTests.long_in_max, max)
462 self.assertRaises(ValueError, GIMarshallingTests.long_in_min, min)
464 self.assertRaises(TypeError, GIMarshallingTests.long_in_max, "self.MAX")
466 def test_long_out(self):
467 self.assertEquals(self.MAX, GIMarshallingTests.long_out_max())
468 self.assertEquals(self.MIN, GIMarshallingTests.long_out_min())
470 def test_long_inout(self):
471 self.assertEquals(self.MIN, GIMarshallingTests.long_inout_max_min(Number(self.MAX)))
472 self.assertEquals(self.MAX, GIMarshallingTests.long_inout_min_max(Number(self.MIN)))
475 class TestULong(unittest.TestCase):
477 MAX = GObject.constants.G_MAXULONG
479 def test_ulong_return(self):
480 self.assertEquals(self.MAX, GIMarshallingTests.ulong_return())
482 def test_ulong_in(self):
483 number = Number(self.MAX)
485 GIMarshallingTests.ulong_in(number)
489 self.assertRaises(ValueError, GIMarshallingTests.ulong_in, number)
490 self.assertRaises(ValueError, GIMarshallingTests.ulong_in, Number(-1))
492 self.assertRaises(TypeError, GIMarshallingTests.ulong_in, "self.MAX")
494 def test_ulong_out(self):
495 self.assertEquals(self.MAX, GIMarshallingTests.ulong_out())
497 def test_ulong_inout(self):
498 self.assertEquals(0, GIMarshallingTests.ulong_inout(Number(self.MAX)))
501 class TestSSize(unittest.TestCase):
503 MAX = GObject.constants.G_MAXLONG
504 MIN = GObject.constants.G_MINLONG
506 def test_ssize_return(self):
507 self.assertEquals(self.MAX, GIMarshallingTests.ssize_return_max())
508 self.assertEquals(self.MIN, GIMarshallingTests.ssize_return_min())
510 def test_ssize_in(self):
511 max = Number(self.MAX)
512 min = Number(self.MIN)
514 GIMarshallingTests.ssize_in_max(max)
515 GIMarshallingTests.ssize_in_min(min)
520 self.assertRaises(ValueError, GIMarshallingTests.ssize_in_max, max)
521 self.assertRaises(ValueError, GIMarshallingTests.ssize_in_min, min)
523 self.assertRaises(TypeError, GIMarshallingTests.ssize_in_max, "self.MAX")
525 def test_ssize_out(self):
526 self.assertEquals(self.MAX, GIMarshallingTests.ssize_out_max())
527 self.assertEquals(self.MIN, GIMarshallingTests.ssize_out_min())
529 def test_ssize_inout(self):
530 self.assertEquals(self.MIN, GIMarshallingTests.ssize_inout_max_min(Number(self.MAX)))
531 self.assertEquals(self.MAX, GIMarshallingTests.ssize_inout_min_max(Number(self.MIN)))
534 class TestSize(unittest.TestCase):
536 MAX = GObject.constants.G_MAXULONG
538 def test_size_return(self):
539 self.assertEquals(self.MAX, GIMarshallingTests.size_return())
541 def test_size_in(self):
542 number = Number(self.MAX)
544 GIMarshallingTests.size_in(number)
548 self.assertRaises(ValueError, GIMarshallingTests.size_in, number)
549 self.assertRaises(ValueError, GIMarshallingTests.size_in, Number(-1))
551 self.assertRaises(TypeError, GIMarshallingTests.size_in, "self.MAX")
553 def test_size_out(self):
554 self.assertEquals(self.MAX, GIMarshallingTests.size_out())
556 def test_size_inout(self):
557 self.assertEquals(0, GIMarshallingTests.size_inout(Number(self.MAX)))
560 class TestFloat(unittest.TestCase):
562 MAX = GObject.constants.G_MAXFLOAT
563 MIN = GObject.constants.G_MINFLOAT
565 def test_float_return(self):
566 self.assertAlmostEquals(self.MAX, GIMarshallingTests.float_return())
568 def test_float_in(self):
569 GIMarshallingTests.float_in(Number(self.MAX))
571 self.assertRaises(TypeError, GIMarshallingTests.float_in, "self.MAX")
573 def test_float_out(self):
574 self.assertAlmostEquals(self.MAX, GIMarshallingTests.float_out())
576 def test_float_inout(self):
577 self.assertAlmostEquals(self.MIN, GIMarshallingTests.float_inout(Number(self.MAX)))
580 class TestDouble(unittest.TestCase):
582 MAX = GObject.constants.G_MAXDOUBLE
583 MIN = GObject.constants.G_MINDOUBLE
585 def test_double_return(self):
586 self.assertAlmostEquals(self.MAX, GIMarshallingTests.double_return())
588 def test_double_in(self):
589 GIMarshallingTests.double_in(Number(self.MAX))
591 self.assertRaises(TypeError, GIMarshallingTests.double_in, "self.MAX")
593 def test_double_out(self):
594 self.assertAlmostEquals(self.MAX, GIMarshallingTests.double_out())
596 def test_double_inout(self):
597 self.assertAlmostEquals(self.MIN, GIMarshallingTests.double_inout(Number(self.MAX)))
600 class TestGType(unittest.TestCase):
602 def test_gtype_return(self):
603 self.assertEquals(GObject.TYPE_NONE, GIMarshallingTests.gtype_return())
605 def test_gtype_in(self):
606 GIMarshallingTests.gtype_in(GObject.TYPE_NONE)
608 self.assertRaises(TypeError, GIMarshallingTests.gtype_in, "GObject.TYPE_NONE")
610 def test_gtype_out(self):
611 self.assertEquals(GObject.TYPE_NONE, GIMarshallingTests.gtype_out())
613 def test_gtype_inout(self):
614 self.assertEquals(GObject.TYPE_INT, GIMarshallingTests.gtype_inout(GObject.TYPE_NONE))
617 class TestUtf8(unittest.TestCase):
619 def test_utf8_none_return(self):
620 self.assertEquals(CONSTANT_UTF8, GIMarshallingTests.utf8_none_return())
622 def test_utf8_full_return(self):
623 self.assertEquals(CONSTANT_UTF8, GIMarshallingTests.utf8_full_return())
625 def test_utf8_none_in(self):
626 GIMarshallingTests.utf8_none_in(CONSTANT_UTF8)
627 if sys.version_info < (3, 0):
628 GIMarshallingTests.utf8_none_in(PY2_UNICODE_UTF8)
630 self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, CONSTANT_NUMBER)
631 self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, None)
633 def test_utf8_none_out(self):
634 self.assertEquals(CONSTANT_UTF8, GIMarshallingTests.utf8_none_out())
636 def test_utf8_full_out(self):
637 self.assertEquals(CONSTANT_UTF8, GIMarshallingTests.utf8_full_out())
639 def test_utf8_dangling_out(self):
640 GIMarshallingTests.utf8_dangling_out()
642 def test_utf8_none_inout(self):
643 self.assertEquals("", GIMarshallingTests.utf8_none_inout(CONSTANT_UTF8))
645 def test_utf8_full_inout(self):
646 self.assertEquals("", GIMarshallingTests.utf8_full_inout(CONSTANT_UTF8))
649 class TestArray(unittest.TestCase):
651 def test_array_fixed_int_return(self):
652 self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.array_fixed_int_return())
654 def test_array_fixed_short_return(self):
655 self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.array_fixed_short_return())
657 def test_array_fixed_int_in(self):
658 GIMarshallingTests.array_fixed_int_in(Sequence([-1, 0, 1, 2]))
660 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, Sequence([-1, '0', 1, 2]))
662 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, 42)
663 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, None)
665 def test_array_fixed_short_in(self):
666 GIMarshallingTests.array_fixed_short_in(Sequence([-1, 0, 1, 2]))
668 def test_array_fixed_out(self):
669 self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.array_fixed_out())
671 def test_array_fixed_inout(self):
672 self.assertEquals([2, 1, 0, -1], GIMarshallingTests.array_fixed_inout([-1, 0, 1, 2]))
675 def test_array_return(self):
676 self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.array_return())
678 def test_array_in(self):
679 GIMarshallingTests.array_in(Sequence([-1, 0, 1, 2]))
681 def test_array_uint8_in(self):
682 GIMarshallingTests.array_uint8_in(Sequence([97, 98, 99, 100]))
683 GIMarshallingTests.array_uint8_in(_bytes("abcd"))
685 def test_array_out(self):
686 self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.array_out())
688 def test_array_inout(self):
689 self.assertEquals([-2, -1, 0, 1, 2], GIMarshallingTests.array_inout(Sequence([-1, 0, 1, 2])))
691 def test_method_array_in(self):
692 object_ = GIMarshallingTests.Object()
693 object_.method_array_in(Sequence([-1, 0, 1, 2]))
695 def test_method_array_out(self):
696 object_ = GIMarshallingTests.Object()
697 self.assertEquals([-1, 0, 1, 2], object_.method_array_out())
699 def test_method_array_inout(self):
700 object_ = GIMarshallingTests.Object()
701 self.assertEquals([-2, -1, 0, 1, 2], object_.method_array_inout(Sequence([-1, 0, 1, 2])))
703 def test_method_array_return(self):
704 object_ = GIMarshallingTests.Object()
705 self.assertEquals([-1, 0, 1, 2], object_.method_array_return())
707 def test_array_fixed_out_struct(self):
708 struct1, struct2 = GIMarshallingTests.array_fixed_out_struct()
710 self.assertEquals(7, struct1.long_)
711 self.assertEquals(6, struct1.int8)
712 self.assertEquals(6, struct2.long_)
713 self.assertEquals(7, struct2.int8)
715 def test_array_zero_terminated_return(self):
716 self.assertEquals(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_return())
718 def test_array_zero_terminated_in(self):
719 GIMarshallingTests.array_zero_terminated_in(Sequence(['0', '1', '2']))
721 def test_array_zero_terminated_out(self):
722 self.assertEquals(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
724 def test_array_zero_terminated_out(self):
725 self.assertEquals(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
727 def test_array_zero_terminated_inout(self):
728 self.assertEquals(['-1', '0', '1', '2'], GIMarshallingTests.array_zero_terminated_inout(['0', '1', '2']))
730 def test_gstrv_return(self):
731 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gstrv_return())
733 def test_gstrv_in(self):
734 GIMarshallingTests.gstrv_in(Sequence(['0', '1', '2']))
736 def test_gstrv_out(self):
737 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gstrv_out())
739 def test_gstrv_out(self):
740 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gstrv_out())
742 def test_gstrv_inout(self):
743 self.assertEquals(['-1', '0', '1', '2'], GIMarshallingTests.gstrv_inout(['0', '1', '2']))
746 class TestGArray(unittest.TestCase):
748 def test_garray_int_none_return(self):
749 self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.garray_int_none_return())
751 def test_garray_utf8_none_return(self):
752 self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_return())
754 def test_garray_utf8_container_return(self):
755 self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_return())
757 def test_garray_utf8_full_return(self):
758 self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_return())
760 def test_garray_int_none_in(self):
761 GIMarshallingTests.garray_int_none_in(Sequence([-1, 0, 1, 2]))
763 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, Sequence([-1, '0', 1, 2]))
765 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, 42)
766 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, None)
768 def test_garray_utf8_none_in(self):
769 GIMarshallingTests.garray_utf8_none_in(Sequence(['0', '1', '2']))
771 def test_garray_utf8_none_out(self):
772 self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_out())
774 def test_garray_utf8_container_out(self):
775 self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_out())
777 def test_garray_utf8_full_out(self):
778 self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out())
780 def test_garray_utf8_none_inout(self):
781 self.assertEquals(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_none_inout(Sequence(('0', '1', '2'))))
783 def test_garray_utf8_container_inout(self):
784 self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.garray_utf8_container_inout(['0', '1', '2']))
786 def test_garray_utf8_full_inout(self):
787 self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.garray_utf8_full_inout(['0', '1', '2']))
790 class TestGList(unittest.TestCase):
792 def test_glist_int_none_return(self):
793 self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.glist_int_none_return())
795 def test_glist_utf8_none_return(self):
796 self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_return())
798 def test_glist_utf8_container_return(self):
799 self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_return())
801 def test_glist_utf8_full_return(self):
802 self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_return())
804 def test_glist_int_none_in(self):
805 GIMarshallingTests.glist_int_none_in(Sequence((-1, 0, 1, 2)))
807 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, Sequence((-1, '0', 1, 2)))
809 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, 42)
810 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, None)
812 def test_glist_utf8_none_in(self):
813 GIMarshallingTests.glist_utf8_none_in(Sequence(('0', '1', '2')))
815 def test_glist_utf8_none_out(self):
816 self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_out())
818 def test_glist_utf8_container_out(self):
819 self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_out())
821 def test_glist_utf8_full_out(self):
822 self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_out())
824 def test_glist_utf8_none_inout(self):
825 self.assertEquals(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_none_inout(Sequence(('0', '1', '2'))))
827 def test_glist_utf8_container_inout(self):
828 self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.glist_utf8_container_inout(('0', '1', '2')))
830 def test_glist_utf8_full_inout(self):
831 self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.glist_utf8_full_inout(('0', '1', '2')))
834 class TestGSList(unittest.TestCase):
836 def test_gslist_int_none_return(self):
837 self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.gslist_int_none_return())
839 def test_gslist_utf8_none_return(self):
840 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_return())
842 def test_gslist_utf8_container_return(self):
843 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_return())
845 def test_gslist_utf8_full_return(self):
846 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_return())
848 def test_gslist_int_none_in(self):
849 GIMarshallingTests.gslist_int_none_in(Sequence((-1, 0, 1, 2)))
851 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, Sequence((-1, '0', 1, 2)))
853 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, 42)
854 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, None)
856 def test_gslist_utf8_none_in(self):
857 GIMarshallingTests.gslist_utf8_none_in(Sequence(('0', '1', '2')))
859 def test_gslist_utf8_none_out(self):
860 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_out())
862 def test_gslist_utf8_container_out(self):
863 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_out())
865 def test_gslist_utf8_full_out(self):
866 self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_out())
868 def test_gslist_utf8_none_inout(self):
869 self.assertEquals(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_none_inout(Sequence(('0', '1', '2'))))
871 def test_gslist_utf8_container_inout(self):
872 self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.gslist_utf8_container_inout(('0', '1', '2')))
874 def test_gslist_utf8_full_inout(self):
875 self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.gslist_utf8_full_inout(('0', '1', '2')))
878 class TestGHashTable(unittest.TestCase):
880 def test_ghashtable_int_none_return(self):
881 self.assertEquals({-1: 1, 0: 0, 1: -1, 2: -2}, GIMarshallingTests.ghashtable_int_none_return())
883 def test_ghashtable_int_none_return(self):
884 self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_return())
886 def test_ghashtable_int_container_return(self):
887 self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_return())
889 def test_ghashtable_int_full_return(self):
890 self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_return())
892 def test_ghashtable_int_none_in(self):
893 GIMarshallingTests.ghashtable_int_none_in({-1: 1, 0: 0, 1: -1, 2: -2})
895 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, '0': 0, 1: -1, 2: -2})
896 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, 0: '0', 1: -1, 2: -2})
898 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, '{-1: 1, 0: 0, 1: -1, 2: -2}')
899 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, None)
901 def test_ghashtable_utf8_none_in(self):
902 GIMarshallingTests.ghashtable_utf8_none_in({'-1': '1', '0': '0', '1': '-1', '2': '-2'})
904 def test_ghashtable_utf8_none_out(self):
905 self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_out())
907 def test_ghashtable_utf8_container_out(self):
908 self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_out())
910 def test_ghashtable_utf8_full_out(self):
911 self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_out())
913 def test_ghashtable_utf8_none_inout(self):
914 self.assertEquals({'-1': '1', '0': '0', '1': '1'},
915 GIMarshallingTests.ghashtable_utf8_none_inout({'-1': '1', '0': '0', '1': '-1', '2': '-2'}))
917 def test_ghashtable_utf8_container_inout(self):
918 self.assertEquals({'-1': '1', '0': '0', '1': '1'},
919 GIMarshallingTests.ghashtable_utf8_container_inout({'-1': '1', '0': '0', '1': '-1', '2': '-2'}))
921 def test_ghashtable_utf8_full_inout(self):
922 self.assertEquals({'-1': '1', '0': '0', '1': '1'},
923 GIMarshallingTests.ghashtable_utf8_full_inout({'-1': '1', '0': '0', '1': '-1', '2': '-2'}))
926 class TestGValue(unittest.TestCase):
928 def test_gvalue_return(self):
929 self.assertEquals(42, GIMarshallingTests.gvalue_return())
931 def test_gvalue_in(self):
932 GIMarshallingTests.gvalue_in(42)
933 value = GObject.Value()
934 value.init(GObject.TYPE_INT)
936 GIMarshallingTests.gvalue_in(value)
938 def test_gvalue_out(self):
939 self.assertEquals(42, GIMarshallingTests.gvalue_out())
941 def test_gvalue_inout(self):
942 self.assertEquals('42', GIMarshallingTests.gvalue_inout(42))
943 value = GObject.Value()
944 value.init(GObject.TYPE_INT)
946 self.assertEquals('42', GIMarshallingTests.gvalue_inout(value))
948 class TestGClosure(unittest.TestCase):
950 def test_gclosure_in(self):
951 GIMarshallingTests.gclosure_in(lambda: 42)
953 # test passing a closure between two C calls
954 closure = GIMarshallingTests.gclosure_return()
955 GIMarshallingTests.gclosure_in(closure)
957 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, 42)
958 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, None)
961 class TestPointer(unittest.TestCase):
962 def test_pointer_in_return(self):
963 self.assertEquals(GIMarshallingTests.pointer_in_return(42), 42)
966 class TestEnum(unittest.TestCase):
970 '''Run tests under a test locale.
972 Upper case conversion of member names should not be locale specific;
973 e. g. in Turkish, "i".upper() == "i", which gives results like "iNVALiD"
975 Run test under a locale which defines toupper('a') == 'a'
977 cls.locale_dir = tempfile.mkdtemp()
978 subprocess.check_call(['localedef', '-i',
979 os.path.join(os.path.dirname(os.path.realpath(__file__)), 'te_ST@nouppera'),
980 '-c', '-f', 'UTF-8', os.path.join(cls.locale_dir, 'te_ST.UTF-8@nouppera')])
981 os.environ['LOCPATH'] = cls.locale_dir
982 locale.setlocale(locale.LC_ALL, 'te_ST.UTF-8@nouppera')
985 def tearDownClass(cls):
986 locale.setlocale(locale.LC_ALL, 'C')
987 shutil.rmtree(cls.locale_dir)
989 del os.environ['LOCPATH']
994 self.assertTrue(issubclass(GIMarshallingTests.Enum, int))
995 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE1, GIMarshallingTests.Enum))
996 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE2, GIMarshallingTests.Enum))
997 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE3, GIMarshallingTests.Enum))
998 self.assertEquals(42, GIMarshallingTests.Enum.VALUE3)
1000 def test_value_nick_and_name(self):
1001 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_nick, 'value1')
1002 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_nick, 'value2')
1003 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_nick, 'value3')
1005 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE1')
1006 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE2')
1007 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE3')
1009 def test_enum_in(self):
1010 GIMarshallingTests.enum_in(GIMarshallingTests.Enum.VALUE3)
1011 GIMarshallingTests.enum_in(42)
1013 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 43)
1014 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 'GIMarshallingTests.Enum.VALUE3')
1016 def test_enum_out(self):
1017 enum = GIMarshallingTests.enum_out()
1018 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1019 self.assertEquals(enum, GIMarshallingTests.Enum.VALUE3)
1021 def test_enum_inout(self):
1022 enum = GIMarshallingTests.enum_inout(GIMarshallingTests.Enum.VALUE3)
1023 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1024 self.assertEquals(enum, GIMarshallingTests.Enum.VALUE1)
1026 def test_enum_second(self):
1027 # check for the bug where different non-gtype enums share the same class
1028 self.assertNotEqual(GIMarshallingTests.Enum, GIMarshallingTests.SecondEnum)
1030 # check that values are not being shared between different enums
1031 self.assertTrue(hasattr(GIMarshallingTests.SecondEnum, "SECONDVALUE1"))
1032 self.assertRaises(AttributeError, getattr, GIMarshallingTests.Enum, "SECONDVALUE1")
1033 self.assertTrue(hasattr(GIMarshallingTests.Enum, "VALUE1"))
1034 self.assertRaises(AttributeError, getattr, GIMarshallingTests.SecondEnum, "VALUE1")
1037 class TestGEnum(unittest.TestCase):
1039 def test_genum(self):
1040 self.assertTrue(issubclass(GIMarshallingTests.GEnum, GObject.GEnum))
1041 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE1, GIMarshallingTests.GEnum))
1042 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE2, GIMarshallingTests.GEnum))
1043 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE3, GIMarshallingTests.GEnum))
1044 self.assertEquals(42, GIMarshallingTests.GEnum.VALUE3)
1046 def test_value_nick_and_name(self):
1047 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_nick, 'value1')
1048 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_nick, 'value2')
1049 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_nick, 'value3')
1051 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE1')
1052 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE2')
1053 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE3')
1055 def test_genum_in(self):
1056 GIMarshallingTests.genum_in(GIMarshallingTests.GEnum.VALUE3)
1057 GIMarshallingTests.genum_in(42)
1059 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 43)
1060 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 'GIMarshallingTests.GEnum.VALUE3')
1062 def test_genum_out(self):
1063 genum = GIMarshallingTests.genum_out()
1064 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1065 self.assertEquals(genum, GIMarshallingTests.GEnum.VALUE3)
1067 def test_genum_inout(self):
1068 genum = GIMarshallingTests.genum_inout(GIMarshallingTests.GEnum.VALUE3)
1069 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1070 self.assertEquals(genum, GIMarshallingTests.GEnum.VALUE1)
1073 class TestGFlags(unittest.TestCase):
1075 def test_flags(self):
1076 self.assertTrue(issubclass(GIMarshallingTests.Flags, GObject.GFlags))
1077 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1, GIMarshallingTests.Flags))
1078 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE2, GIMarshallingTests.Flags))
1079 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE3, GIMarshallingTests.Flags))
1080 # __or__() operation should still return an instance, not an int.
1081 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1 | GIMarshallingTests.Flags.VALUE2,
1082 GIMarshallingTests.Flags))
1083 self.assertEquals(1 << 1, GIMarshallingTests.Flags.VALUE2)
1085 def test_value_nick_and_name(self):
1086 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_nick, 'value1')
1087 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_nick, 'value2')
1088 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_nick, 'value3')
1090 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE1')
1091 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE2')
1092 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE3')
1094 def test_flags_in(self):
1095 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2)
1096 # result of __or__() operation should still be valid instance, not an int.
1097 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE2)
1098 GIMarshallingTests.flags_in_zero(Number(0))
1100 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 1 << 1)
1101 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 'GIMarshallingTests.Flags.VALUE2')
1103 def test_flags_out(self):
1104 flags = GIMarshallingTests.flags_out()
1105 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1106 self.assertEquals(flags, GIMarshallingTests.Flags.VALUE2)
1108 def test_flags_inout(self):
1109 flags = GIMarshallingTests.flags_inout(GIMarshallingTests.Flags.VALUE2)
1110 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1111 self.assertEquals(flags, GIMarshallingTests.Flags.VALUE1)
1113 class TestNoTypeFlags(unittest.TestCase):
1115 def test_flags(self):
1116 self.assertTrue(issubclass(GIMarshallingTests.NoTypeFlags, GObject.GFlags))
1117 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1, GIMarshallingTests.NoTypeFlags))
1118 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE2, GIMarshallingTests.NoTypeFlags))
1119 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE3, GIMarshallingTests.NoTypeFlags))
1120 # __or__() operation should still return an instance, not an int.
1121 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1 | GIMarshallingTests.NoTypeFlags.VALUE2,
1122 GIMarshallingTests.NoTypeFlags))
1123 self.assertEquals(1 << 1, GIMarshallingTests.NoTypeFlags.VALUE2)
1125 def test_value_nick_and_name(self):
1126 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_nick, 'value1')
1127 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_nick, 'value2')
1128 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_nick, 'value3')
1130 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE1')
1131 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2')
1132 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE3')
1134 def test_flags_in(self):
1135 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2)
1136 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2 | GIMarshallingTests.NoTypeFlags.VALUE2)
1137 GIMarshallingTests.no_type_flags_in_zero(Number(0))
1139 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 1 << 1)
1140 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 'GIMarshallingTests.NoTypeFlags.VALUE2')
1142 def test_flags_out(self):
1143 flags = GIMarshallingTests.no_type_flags_out()
1144 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1145 self.assertEquals(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1147 def test_flags_inout(self):
1148 flags = GIMarshallingTests.no_type_flags_inout(GIMarshallingTests.NoTypeFlags.VALUE2)
1149 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1150 self.assertEquals(flags, GIMarshallingTests.NoTypeFlags.VALUE1)
1153 class TestStructure(unittest.TestCase):
1155 def test_simple_struct(self):
1156 self.assertTrue(issubclass(GIMarshallingTests.SimpleStruct, GObject.GPointer))
1158 struct = GIMarshallingTests.SimpleStruct()
1159 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1161 self.assertEquals(0, struct.long_)
1162 self.assertEquals(0, struct.int8)
1167 self.assertEquals(6, struct.long_)
1168 self.assertEquals(7, struct.int8)
1172 def test_nested_struct(self):
1173 struct = GIMarshallingTests.NestedStruct()
1175 self.assertTrue(isinstance(struct.simple_struct, GIMarshallingTests.SimpleStruct))
1177 struct.simple_struct.long_ = 42
1178 self.assertEquals(42, struct.simple_struct.long_)
1182 def test_not_simple_struct(self):
1183 struct = GIMarshallingTests.NotSimpleStruct()
1184 self.assertEquals(None, struct.pointer)
1186 def test_simple_struct_return(self):
1187 struct = GIMarshallingTests.simple_struct_returnv()
1189 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1190 self.assertEquals(6, struct.long_)
1191 self.assertEquals(7, struct.int8)
1195 def test_simple_struct_in(self):
1196 struct = GIMarshallingTests.SimpleStruct()
1200 GIMarshallingTests.SimpleStruct.inv(struct)
1204 struct = GIMarshallingTests.NestedStruct()
1206 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, struct)
1210 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, None)
1212 def test_simple_struct_method(self):
1213 struct = GIMarshallingTests.SimpleStruct()
1221 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.method)
1224 def test_pointer_struct(self):
1225 self.assertTrue(issubclass(GIMarshallingTests.PointerStruct, GObject.GPointer))
1227 struct = GIMarshallingTests.PointerStruct()
1228 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1232 def test_pointer_struct_return(self):
1233 struct = GIMarshallingTests.pointer_struct_returnv()
1235 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1236 self.assertEquals(42, struct.long_)
1240 def test_pointer_struct_in(self):
1241 struct = GIMarshallingTests.PointerStruct()
1248 def test_boxed_struct(self):
1249 self.assertTrue(issubclass(GIMarshallingTests.BoxedStruct, GObject.GBoxed))
1251 struct = GIMarshallingTests.BoxedStruct()
1252 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1254 self.assertEquals(0, struct.long_)
1255 self.assertEquals([], struct.g_strv)
1259 def test_boxed_struct_new(self):
1260 struct = GIMarshallingTests.BoxedStruct.new()
1261 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1265 def test_boxed_struct_copy(self):
1266 struct = GIMarshallingTests.BoxedStruct()
1268 new_struct = struct.copy()
1269 self.assertTrue(isinstance(new_struct, GIMarshallingTests.BoxedStruct))
1274 def test_boxed_struct_return(self):
1275 struct = GIMarshallingTests.boxed_struct_returnv()
1277 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1278 self.assertEquals(42, struct.long_)
1279 self.assertEquals(['0', '1', '2'], struct.g_strv)
1283 def test_boxed_struct_in(self):
1284 struct = GIMarshallingTests.BoxedStruct()
1291 def test_boxed_struct_out(self):
1292 struct = GIMarshallingTests.boxed_struct_out()
1294 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1295 self.assertEquals(42, struct.long_)
1299 def test_boxed_struct_inout(self):
1300 in_struct = GIMarshallingTests.BoxedStruct()
1301 in_struct.long_ = 42
1303 out_struct = GIMarshallingTests.boxed_struct_inout(in_struct)
1305 self.assertTrue(isinstance(out_struct, GIMarshallingTests.BoxedStruct))
1306 self.assertEquals(0, out_struct.long_)
1311 def test_union(self):
1312 union = GIMarshallingTests.Union()
1314 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1316 new_union = union.copy()
1317 self.assertTrue(isinstance(new_union, GIMarshallingTests.Union))
1322 def test_union_return(self):
1323 union = GIMarshallingTests.union_returnv()
1325 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1326 self.assertEquals(42, union.long_)
1330 def test_union_in(self):
1331 union = GIMarshallingTests.Union()
1338 def test_union_method(self):
1339 union = GIMarshallingTests.Union()
1346 self.assertRaises(TypeError, GIMarshallingTests.Union.method)
1348 class TestGObject(unittest.TestCase):
1350 def test_object(self):
1351 self.assertTrue(issubclass(GIMarshallingTests.Object, GObject.GObject))
1353 object_ = GIMarshallingTests.Object()
1354 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1355 self.assertEquals(object_.__grefcount__, 1)
1357 def test_object_new(self):
1358 object_ = GIMarshallingTests.Object.new(42)
1359 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1360 self.assertEquals(object_.__grefcount__, 1)
1362 def test_object_int(self):
1363 object_ = GIMarshallingTests.Object(int = 42)
1364 self.assertEquals(object_.int_, 42)
1365 # FIXME: Don't work yet.
1367 # self.assertEquals(object_.int_, 0)
1369 def test_object_static_method(self):
1370 GIMarshallingTests.Object.static_method()
1372 def test_object_method(self):
1373 GIMarshallingTests.Object(int = 42).method()
1374 self.assertRaises(TypeError, GIMarshallingTests.Object.method, GObject.GObject())
1375 self.assertRaises(TypeError, GIMarshallingTests.Object.method)
1378 def test_sub_object(self):
1379 self.assertTrue(issubclass(GIMarshallingTests.SubObject, GIMarshallingTests.Object))
1381 object_ = GIMarshallingTests.SubObject()
1382 self.assertTrue(isinstance(object_, GIMarshallingTests.SubObject))
1384 def test_sub_object_new(self):
1385 self.assertRaises(TypeError, GIMarshallingTests.SubObject.new, 42)
1387 def test_sub_object_static_method(self):
1388 object_ = GIMarshallingTests.SubObject()
1389 object_.static_method()
1391 def test_sub_object_method(self):
1392 object_ = GIMarshallingTests.SubObject(int = 42)
1395 def test_sub_object_sub_method(self):
1396 object_ = GIMarshallingTests.SubObject()
1397 object_.sub_method()
1399 def test_sub_object_overwritten_method(self):
1400 object_ = GIMarshallingTests.SubObject()
1401 object_.overwritten_method()
1403 self.assertRaises(TypeError, GIMarshallingTests.SubObject.overwritten_method, GIMarshallingTests.Object())
1405 def test_sub_object_int(self):
1406 object_ = GIMarshallingTests.SubObject()
1407 self.assertEquals(object_.int_, 0)
1408 # FIXME: Don't work yet.
1410 # self.assertEquals(object_.int_, 42)
1412 def test_object_none_return(self):
1413 object_ = GIMarshallingTests.Object.none_return()
1414 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1415 self.assertEquals(object_.__grefcount__, 2)
1417 def test_object_full_return(self):
1418 object_ = GIMarshallingTests.Object.full_return()
1419 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1420 self.assertEquals(object_.__grefcount__, 1)
1422 def test_object_none_in(self):
1423 object_ = GIMarshallingTests.Object(int = 42)
1424 GIMarshallingTests.Object.none_in(object_)
1425 self.assertEquals(object_.__grefcount__, 1)
1427 object_ = GIMarshallingTests.SubObject(int = 42)
1428 GIMarshallingTests.Object.none_in(object_)
1430 object_ = GObject.GObject()
1431 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, object_)
1433 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, None)
1435 def test_object_none_out(self):
1436 object_ = GIMarshallingTests.Object.none_out()
1437 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1438 self.assertEquals(object_.__grefcount__, 2)
1440 new_object = GIMarshallingTests.Object.none_out()
1441 self.assertTrue(new_object is object_)
1443 def test_object_full_out(self):
1444 object_ = GIMarshallingTests.Object.full_out()
1445 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1446 self.assertEquals(object_.__grefcount__, 1)
1448 def test_object_none_inout(self):
1449 object_ = GIMarshallingTests.Object(int = 42)
1450 new_object = GIMarshallingTests.Object.none_inout(object_)
1452 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1454 self.assertFalse(object_ is new_object)
1456 self.assertEquals(object_.__grefcount__, 1)
1457 self.assertEquals(new_object.__grefcount__, 2)
1459 new_new_object = GIMarshallingTests.Object.none_inout(object_)
1460 self.assertTrue(new_new_object is new_object)
1462 GIMarshallingTests.Object.none_inout(GIMarshallingTests.SubObject(int = 42))
1464 def test_object_full_inout(self):
1465 object_ = GIMarshallingTests.Object(int = 42)
1466 new_object = GIMarshallingTests.Object.full_inout(object_)
1468 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1470 self.assertFalse(object_ is new_object)
1472 self.assertEquals(object_.__grefcount__, 2)
1473 self.assertEquals(new_object.__grefcount__, 1)
1475 # FIXME: Doesn't actually return the same object.
1476 # def test_object_inout_same(self):
1477 # object_ = GIMarshallingTests.Object()
1478 # new_object = GIMarshallingTests.object_full_inout(object_)
1479 # self.assertTrue(object_ is new_object)
1480 # self.assertEquals(object_.__grefcount__, 1)
1483 class TestPythonGObject(unittest.TestCase):
1485 class Object(GIMarshallingTests.Object):
1486 def __init__(self, int):
1487 GIMarshallingTests.Object.__init__(self)
1491 # Don't call super, which asserts that self.int == 42.
1494 def do_method_int8_in(self, int8):
1497 def do_method_int8_out(self):
1500 def do_method_with_default_implementation(self, int8):
1501 GIMarshallingTests.Object.do_method_with_default_implementation(self, int8)
1502 self.props.int += int8
1504 class SubObject(GIMarshallingTests.SubObject):
1505 def __init__(self, int):
1506 GIMarshallingTests.SubObject.__init__(self)
1509 def do_method_with_default_implementation(self, int8):
1512 def test_object(self):
1513 self.assertTrue(issubclass(self.Object, GIMarshallingTests.Object))
1515 object_ = self.Object(int = 42)
1516 self.assertTrue(isinstance(object_, self.Object))
1518 def test_object_method(self):
1519 self.Object(int = 0).method()
1521 def test_object_vfuncs(self):
1522 object_ = self.Object(int = 42)
1523 object_.method_int8_in(84)
1524 self.assertEqual(object_.val, 84)
1525 self.assertEqual(object_.method_int8_out(), 42)
1527 object_.method_with_default_implementation(42)
1528 self.assertEqual(object_.props.int, 84)
1530 class ObjectWithoutVFunc(GIMarshallingTests.Object):
1531 def __init__(self, int):
1532 GIMarshallingTests.Object.__init__(self)
1534 object_ = ObjectWithoutVFunc(int = 42)
1535 object_.method_with_default_implementation(84)
1536 self.assertEqual(object_.props.int, 84)
1538 def test_subobject_parent_vfunc(self):
1539 object_ = self.SubObject(int = 81)
1540 object_.method_with_default_implementation(87)
1541 self.assertEquals(object_.val, 87)
1543 def test_dynamic_module(self):
1544 from gi.module import DynamicGObjectModule
1545 self.assertTrue(isinstance(GObject, DynamicGObjectModule))
1546 # compare the same enum from both the pygobject attrs and gi GObject attrs
1547 self.assertEquals(GObject.SIGNAL_ACTION, GObject.SignalFlags.ACTION)
1548 # compare a static gobject attr with a dynamic GObject attr
1549 self.assertEquals(GObject.GObject, gobject.GObject)
1551 def test_subobject_non_vfunc_do_method(self):
1552 class PythonObjectWithNonVFuncDoMethod:
1553 def do_not_a_vfunc(self):
1556 class ObjectOverrideNonVFuncDoMethod(GIMarshallingTests.Object, PythonObjectWithNonVFuncDoMethod):
1557 def do_not_a_vfunc(self):
1558 value = super(ObjectOverrideNonVFuncDoMethod, self).do_not_a_vfunc()
1561 object_ = ObjectOverrideNonVFuncDoMethod()
1562 self.assertEquals(18, object_.do_not_a_vfunc())
1564 def test_native_function_not_set_in_subclass_dict(self):
1565 # Previously, GI was setting virtual functions on the class as well
1566 # as any *native* class that subclasses it. Here we check that it is only
1567 # set on the class that the method is originally from.
1568 self.assertTrue('do_method_with_default_implementation' in GIMarshallingTests.Object.__dict__)
1569 self.assertTrue('do_method_with_default_implementation' not in GIMarshallingTests.SubObject.__dict__)
1571 # Here we check that accessing a vfunc from the subclass returns the same wrapper object,
1572 # meaning that multiple wrapper objects have not been created for the same vfunc.
1573 func1 = GIMarshallingTests.Object.do_method_with_default_implementation
1574 func2 = GIMarshallingTests.SubObject.do_method_with_default_implementation
1575 if sys.version_info < (3,0):
1576 func1 = func1.im_func
1577 func2 = func2.im_func
1579 self.assertTrue(func1 is func2)
1581 def test_subobject_with_interface_and_non_vfunc_do_method(self):
1582 # There was a bug for searching for vfuncs in interfaces. It was
1583 # triggered by having a do_* method that wasn't overriding
1584 # a native vfunc, as well as inheriting from an interface.
1585 class GObjectSubclassWithInterface(GObject.GObject, GIMarshallingTests.Interface):
1586 def do_method_not_a_vfunc(self):
1589 class TestMultiOutputArgs(unittest.TestCase):
1591 def test_int_out_out(self):
1592 self.assertEquals((6, 7), GIMarshallingTests.int_out_out())
1594 def test_int_return_out(self):
1595 self.assertEquals((6, 7), GIMarshallingTests.int_return_out())
1597 class TestGErrorException(unittest.TestCase):
1598 def test_gerror_exception(self):
1599 self.assertRaises(GObject.GError, GIMarshallingTests.gerror)
1601 GIMarshallingTests.gerror()
1603 etype, e = sys.exc_info()[:2]
1604 self.assertEquals(e.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
1605 self.assertEquals(e.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
1606 self.assertEquals(e.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
1611 class TestInterfaces(unittest.TestCase):
1613 def test_wrapper(self):
1614 self.assertTrue(issubclass(GIMarshallingTests.Interface, GObject.GInterface))
1615 self.assertEquals(GIMarshallingTests.Interface.__gtype__.name, 'GIMarshallingTestsInterface')
1616 self.assertRaises(NotImplementedError, GIMarshallingTests.Interface)
1618 def test_implementation(self):
1620 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
1622 GObject.GObject.__init__(self)
1625 def do_test_int8_in(self, int8):
1628 self.assertTrue(issubclass(TestInterfaceImpl, GIMarshallingTests.Interface))
1630 instance = TestInterfaceImpl()
1631 self.assertTrue(isinstance(instance, GIMarshallingTests.Interface))
1633 GIMarshallingTests.test_interface_test_int8_in(instance, 42)
1634 self.assertEquals(instance.val, 42)
1636 class TestInterfaceImplA(TestInterfaceImpl):
1639 class TestInterfaceImplB(TestInterfaceImplA):
1642 instance = TestInterfaceImplA()
1643 GIMarshallingTests.test_interface_test_int8_in(instance, 42)
1644 self.assertEquals(instance.val, 42)
1647 # there was a problem with Python bailing out because of
1648 # http://en.wikipedia.org/wiki/Diamond_problem with interfaces,
1649 # which shouldn't really be a problem.
1651 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
1654 class TestInterfaceImpl2(GIMarshallingTests.Interface,
1658 class TestInterfaceImpl3(TestInterfaceImpl,
1659 GIMarshallingTests.Interface2):
1663 class TestInterfaceClash(unittest.TestCase):
1665 def test_clash(self):
1667 class TestClash(GObject.GObject, GIMarshallingTests.Interface, GIMarshallingTests.Interface2):
1668 def do_test_int8_in(self, int8):
1672 self.assertRaises(TypeError, create_clash)
1675 class TestOverrides(unittest.TestCase):
1677 def test_constant(self):
1678 self.assertEquals(GIMarshallingTests.OVERRIDES_CONSTANT, 7)
1680 def test_struct(self):
1681 # Test that the constructor has been overridden.
1682 struct = GIMarshallingTests.OverridesStruct(42)
1684 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
1686 # Test that the method has been overridden.
1687 self.assertEquals(6, struct.method())
1691 # Test that the overrides wrapper has been registered.
1692 struct = GIMarshallingTests.overrides_struct_returnv()
1694 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
1698 def test_object(self):
1699 # Test that the constructor has been overridden.
1700 object_ = GIMarshallingTests.OverridesObject(42)
1702 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
1704 # Test that the alternate constructor has been overridden.
1705 object_ = GIMarshallingTests.OverridesObject.new(42)
1707 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
1709 # Test that the method has been overridden.
1710 self.assertEquals(6, object_.method())
1712 # Test that the overrides wrapper has been registered.
1713 object_ = GIMarshallingTests.OverridesObject.returnv()
1715 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
1717 def test_module_name(self):
1718 self.assertEquals(GIMarshallingTests.OverridesStruct.__module__, 'gi.overrides.GIMarshallingTests')
1719 self.assertEquals(GObject.InitiallyUnowned.__module__, 'gi.repository.GObject')
1721 class TestDir(unittest.TestCase):
1722 def test_members_list(self):
1723 list = dir(GIMarshallingTests)
1724 self.assertTrue('OverridesStruct' in list)
1725 self.assertTrue('BoxedStruct' in list)
1726 self.assertTrue('OVERRIDES_CONSTANT' in list)
1727 self.assertTrue('GEnum' in list)
1728 self.assertTrue('int32_return_max' in list)
1730 def test_modules_list(self):
1731 import gi.repository
1732 list = dir(gi.repository)
1733 self.assertTrue('GIMarshallingTests' in list)
1735 # FIXME: test to see if a module which was not imported is in the list
1736 # we should be listing every typelib we find, not just the ones
1737 # which are imported
1739 # to test this I recommend we compile a fake module which
1740 # our tests would never import and check to see if it is
1743 # self.assertTrue('DoNotImportDummyTests' in list)
1745 class TestGErrorArrayInCrash(unittest.TestCase):
1746 # Previously there was a bug in invoke, in which C arrays were unwrapped
1747 # from inside GArrays to be passed to the C function. But when a GError was
1748 # set, invoke would attempt to free the C array as if it were a GArray.
1749 # This crash is only for C arrays. It does not happen for C functions which
1750 # take in GArrays. See https://bugzilla.gnome.org/show_bug.cgi?id=642708
1751 def test_gerror_array_in_crash(self):
1752 self.assertRaises(GObject.GError, GIMarshallingTests.gerror_array_in, [1, 2, 3])