Imported Upstream version 3.7.5.1
[platform/upstream/pygobject2.git] / tests / test_gi.py
1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # coding=utf-8
3 # vim: tabstop=4 shiftwidth=4 expandtab
4
5 import sys
6
7 import unittest
8 import tempfile
9 import shutil
10 import os
11 import locale
12 import subprocess
13 import gc
14 import weakref
15 from io import StringIO, BytesIO
16
17 import gi
18 from gi.repository import GObject, GLib, Gio
19
20 from gi.repository import GIMarshallingTests
21
22 from compathelper import _bytes, _unicode
23
24 if sys.version_info < (3, 0):
25     CONSTANT_UTF8 = "const \xe2\x99\xa5 utf8"
26     PY2_UNICODE_UTF8 = unicode(CONSTANT_UTF8, 'UTF-8')
27     CHAR_255 = '\xff'
28 else:
29     CONSTANT_UTF8 = "const ♥ utf8"
30     CHAR_255 = bytes([255])
31
32 CONSTANT_NUMBER = 42
33
34
35 class Number(object):
36
37     def __init__(self, value):
38         self.value = value
39
40     def __int__(self):
41         return int(self.value)
42
43     def __float__(self):
44         return float(self.value)
45
46
47 class Sequence(object):
48
49     def __init__(self, sequence):
50         self.sequence = sequence
51
52     def __len__(self):
53         return len(self.sequence)
54
55     def __getitem__(self, key):
56         return self.sequence[key]
57
58
59 class TestConstant(unittest.TestCase):
60
61 # Blocked by https://bugzilla.gnome.org/show_bug.cgi?id=595773
62 #    def test_constant_utf8(self):
63 #        self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.CONSTANT_UTF8)
64
65     def test_constant_number(self):
66         self.assertEqual(CONSTANT_NUMBER, GIMarshallingTests.CONSTANT_NUMBER)
67
68     def test_min_max_int(self):
69         self.assertEqual(GLib.MAXINT32, 2 ** 31 - 1)
70         self.assertEqual(GLib.MININT32, -2 ** 31)
71         self.assertEqual(GLib.MAXUINT32, 2 ** 32 - 1)
72
73         self.assertEqual(GLib.MAXINT64, 2 ** 63 - 1)
74         self.assertEqual(GLib.MININT64, -2 ** 63)
75         self.assertEqual(GLib.MAXUINT64, 2 ** 64 - 1)
76
77
78 class TestBoolean(unittest.TestCase):
79
80     def test_boolean_return(self):
81         self.assertEqual(True, GIMarshallingTests.boolean_return_true())
82         self.assertEqual(False, GIMarshallingTests.boolean_return_false())
83
84     def test_boolean_in(self):
85         GIMarshallingTests.boolean_in_true(True)
86         GIMarshallingTests.boolean_in_false(False)
87
88         GIMarshallingTests.boolean_in_true(1)
89         GIMarshallingTests.boolean_in_false(0)
90
91     def test_boolean_out(self):
92         self.assertEqual(True, GIMarshallingTests.boolean_out_true())
93         self.assertEqual(False, GIMarshallingTests.boolean_out_false())
94
95     def test_boolean_inout(self):
96         self.assertEqual(False, GIMarshallingTests.boolean_inout_true_false(True))
97         self.assertEqual(True, GIMarshallingTests.boolean_inout_false_true(False))
98
99
100 class TestInt8(unittest.TestCase):
101
102     MAX = GObject.G_MAXINT8
103     MIN = GObject.G_MININT8
104
105     def test_int8_return(self):
106         self.assertEqual(self.MAX, GIMarshallingTests.int8_return_max())
107         self.assertEqual(self.MIN, GIMarshallingTests.int8_return_min())
108
109     def test_int8_in(self):
110         max = Number(self.MAX)
111         min = Number(self.MIN)
112
113         GIMarshallingTests.int8_in_max(max)
114         GIMarshallingTests.int8_in_min(min)
115
116         max.value += 1
117         min.value -= 1
118
119         self.assertRaises(ValueError, GIMarshallingTests.int8_in_max, max)
120         self.assertRaises(ValueError, GIMarshallingTests.int8_in_min, min)
121
122         self.assertRaises(TypeError, GIMarshallingTests.int8_in_max, "self.MAX")
123
124     def test_int8_out(self):
125         self.assertEqual(self.MAX, GIMarshallingTests.int8_out_max())
126         self.assertEqual(self.MIN, GIMarshallingTests.int8_out_min())
127
128     def test_int8_inout(self):
129         self.assertEqual(self.MIN, GIMarshallingTests.int8_inout_max_min(Number(self.MAX)))
130         self.assertEqual(self.MAX, GIMarshallingTests.int8_inout_min_max(Number(self.MIN)))
131
132
133 class TestUInt8(unittest.TestCase):
134
135     MAX = GObject.G_MAXUINT8
136
137     def test_uint8_return(self):
138         self.assertEqual(self.MAX, GIMarshallingTests.uint8_return())
139
140     def test_uint8_in(self):
141         number = Number(self.MAX)
142
143         GIMarshallingTests.uint8_in(number)
144         GIMarshallingTests.uint8_in(CHAR_255)
145
146         number.value += 1
147         self.assertRaises(ValueError, GIMarshallingTests.uint8_in, number)
148         self.assertRaises(ValueError, GIMarshallingTests.uint8_in, Number(-1))
149
150         self.assertRaises(TypeError, GIMarshallingTests.uint8_in, "self.MAX")
151
152     def test_uint8_out(self):
153         self.assertEqual(self.MAX, GIMarshallingTests.uint8_out())
154
155     def test_uint8_inout(self):
156         self.assertEqual(0, GIMarshallingTests.uint8_inout(Number(self.MAX)))
157
158
159 class TestInt16(unittest.TestCase):
160
161     MAX = GObject.G_MAXINT16
162     MIN = GObject.G_MININT16
163
164     def test_int16_return(self):
165         self.assertEqual(self.MAX, GIMarshallingTests.int16_return_max())
166         self.assertEqual(self.MIN, GIMarshallingTests.int16_return_min())
167
168     def test_int16_in(self):
169         max = Number(self.MAX)
170         min = Number(self.MIN)
171
172         GIMarshallingTests.int16_in_max(max)
173         GIMarshallingTests.int16_in_min(min)
174
175         max.value += 1
176         min.value -= 1
177
178         self.assertRaises(ValueError, GIMarshallingTests.int16_in_max, max)
179         self.assertRaises(ValueError, GIMarshallingTests.int16_in_min, min)
180
181         self.assertRaises(TypeError, GIMarshallingTests.int16_in_max, "self.MAX")
182
183     def test_int16_out(self):
184         self.assertEqual(self.MAX, GIMarshallingTests.int16_out_max())
185         self.assertEqual(self.MIN, GIMarshallingTests.int16_out_min())
186
187     def test_int16_inout(self):
188         self.assertEqual(self.MIN, GIMarshallingTests.int16_inout_max_min(Number(self.MAX)))
189         self.assertEqual(self.MAX, GIMarshallingTests.int16_inout_min_max(Number(self.MIN)))
190
191
192 class TestUInt16(unittest.TestCase):
193
194     MAX = GObject.G_MAXUINT16
195
196     def test_uint16_return(self):
197         self.assertEqual(self.MAX, GIMarshallingTests.uint16_return())
198
199     def test_uint16_in(self):
200         number = Number(self.MAX)
201
202         GIMarshallingTests.uint16_in(number)
203
204         number.value += 1
205
206         self.assertRaises(ValueError, GIMarshallingTests.uint16_in, number)
207         self.assertRaises(ValueError, GIMarshallingTests.uint16_in, Number(-1))
208
209         self.assertRaises(TypeError, GIMarshallingTests.uint16_in, "self.MAX")
210
211     def test_uint16_out(self):
212         self.assertEqual(self.MAX, GIMarshallingTests.uint16_out())
213
214     def test_uint16_inout(self):
215         self.assertEqual(0, GIMarshallingTests.uint16_inout(Number(self.MAX)))
216
217
218 class TestInt32(unittest.TestCase):
219
220     MAX = GObject.G_MAXINT32
221     MIN = GObject.G_MININT32
222
223     def test_int32_return(self):
224         self.assertEqual(self.MAX, GIMarshallingTests.int32_return_max())
225         self.assertEqual(self.MIN, GIMarshallingTests.int32_return_min())
226
227     def test_int32_in(self):
228         max = Number(self.MAX)
229         min = Number(self.MIN)
230
231         GIMarshallingTests.int32_in_max(max)
232         GIMarshallingTests.int32_in_min(min)
233
234         max.value += 1
235         min.value -= 1
236
237         self.assertRaises(ValueError, GIMarshallingTests.int32_in_max, max)
238         self.assertRaises(ValueError, GIMarshallingTests.int32_in_min, min)
239
240         self.assertRaises(TypeError, GIMarshallingTests.int32_in_max, "self.MAX")
241
242     def test_int32_out(self):
243         self.assertEqual(self.MAX, GIMarshallingTests.int32_out_max())
244         self.assertEqual(self.MIN, GIMarshallingTests.int32_out_min())
245
246     def test_int32_inout(self):
247         self.assertEqual(self.MIN, GIMarshallingTests.int32_inout_max_min(Number(self.MAX)))
248         self.assertEqual(self.MAX, GIMarshallingTests.int32_inout_min_max(Number(self.MIN)))
249
250
251 class TestUInt32(unittest.TestCase):
252
253     MAX = GObject.G_MAXUINT32
254
255     def test_uint32_return(self):
256         self.assertEqual(self.MAX, GIMarshallingTests.uint32_return())
257
258     def test_uint32_in(self):
259         number = Number(self.MAX)
260
261         GIMarshallingTests.uint32_in(number)
262
263         number.value += 1
264
265         self.assertRaises(ValueError, GIMarshallingTests.uint32_in, number)
266         self.assertRaises(ValueError, GIMarshallingTests.uint32_in, Number(-1))
267
268         self.assertRaises(TypeError, GIMarshallingTests.uint32_in, "self.MAX")
269
270     def test_uint32_out(self):
271         self.assertEqual(self.MAX, GIMarshallingTests.uint32_out())
272
273     def test_uint32_inout(self):
274         self.assertEqual(0, GIMarshallingTests.uint32_inout(Number(self.MAX)))
275
276
277 class TestInt64(unittest.TestCase):
278
279     MAX = 2 ** 63 - 1
280     MIN = - (2 ** 63)
281
282     def test_int64_return(self):
283         self.assertEqual(self.MAX, GIMarshallingTests.int64_return_max())
284         self.assertEqual(self.MIN, GIMarshallingTests.int64_return_min())
285
286     def test_int64_in(self):
287         max = Number(self.MAX)
288         min = Number(self.MIN)
289
290         GIMarshallingTests.int64_in_max(max)
291         GIMarshallingTests.int64_in_min(min)
292
293         max.value += 1
294         min.value -= 1
295
296         self.assertRaises(ValueError, GIMarshallingTests.int64_in_max, max)
297         self.assertRaises(ValueError, GIMarshallingTests.int64_in_min, min)
298
299         self.assertRaises(TypeError, GIMarshallingTests.int64_in_max, "self.MAX")
300
301     def test_int64_out(self):
302         self.assertEqual(self.MAX, GIMarshallingTests.int64_out_max())
303         self.assertEqual(self.MIN, GIMarshallingTests.int64_out_min())
304
305     def test_int64_inout(self):
306         self.assertEqual(self.MIN, GIMarshallingTests.int64_inout_max_min(Number(self.MAX)))
307         self.assertEqual(self.MAX, GIMarshallingTests.int64_inout_min_max(Number(self.MIN)))
308
309
310 class TestUInt64(unittest.TestCase):
311
312     MAX = 2 ** 64 - 1
313
314     def test_uint64_return(self):
315         self.assertEqual(self.MAX, GIMarshallingTests.uint64_return())
316
317     def test_uint64_in(self):
318         number = Number(self.MAX)
319
320         GIMarshallingTests.uint64_in(number)
321
322         number.value += 1
323
324         self.assertRaises(ValueError, GIMarshallingTests.uint64_in, number)
325         self.assertRaises(ValueError, GIMarshallingTests.uint64_in, Number(-1))
326
327         self.assertRaises(TypeError, GIMarshallingTests.uint64_in, "self.MAX")
328
329     def test_uint64_out(self):
330         self.assertEqual(self.MAX, GIMarshallingTests.uint64_out())
331
332     def test_uint64_inout(self):
333         self.assertEqual(0, GIMarshallingTests.uint64_inout(Number(self.MAX)))
334
335
336 class TestShort(unittest.TestCase):
337
338     MAX = GObject.G_MAXSHORT
339     MIN = GObject.G_MINSHORT
340
341     def test_short_return(self):
342         self.assertEqual(self.MAX, GIMarshallingTests.short_return_max())
343         self.assertEqual(self.MIN, GIMarshallingTests.short_return_min())
344
345     def test_short_in(self):
346         max = Number(self.MAX)
347         min = Number(self.MIN)
348
349         GIMarshallingTests.short_in_max(max)
350         GIMarshallingTests.short_in_min(min)
351
352         max.value += 1
353         min.value -= 1
354
355         self.assertRaises(ValueError, GIMarshallingTests.short_in_max, max)
356         self.assertRaises(ValueError, GIMarshallingTests.short_in_min, min)
357
358         self.assertRaises(TypeError, GIMarshallingTests.short_in_max, "self.MAX")
359
360     def test_short_out(self):
361         self.assertEqual(self.MAX, GIMarshallingTests.short_out_max())
362         self.assertEqual(self.MIN, GIMarshallingTests.short_out_min())
363
364     def test_short_inout(self):
365         self.assertEqual(self.MIN, GIMarshallingTests.short_inout_max_min(Number(self.MAX)))
366         self.assertEqual(self.MAX, GIMarshallingTests.short_inout_min_max(Number(self.MIN)))
367
368
369 class TestUShort(unittest.TestCase):
370
371     MAX = GObject.G_MAXUSHORT
372
373     def test_ushort_return(self):
374         self.assertEqual(self.MAX, GIMarshallingTests.ushort_return())
375
376     def test_ushort_in(self):
377         number = Number(self.MAX)
378
379         GIMarshallingTests.ushort_in(number)
380
381         number.value += 1
382
383         self.assertRaises(ValueError, GIMarshallingTests.ushort_in, number)
384         self.assertRaises(ValueError, GIMarshallingTests.ushort_in, Number(-1))
385
386         self.assertRaises(TypeError, GIMarshallingTests.ushort_in, "self.MAX")
387
388     def test_ushort_out(self):
389         self.assertEqual(self.MAX, GIMarshallingTests.ushort_out())
390
391     def test_ushort_inout(self):
392         self.assertEqual(0, GIMarshallingTests.ushort_inout(Number(self.MAX)))
393
394
395 class TestInt(unittest.TestCase):
396
397     MAX = GObject.G_MAXINT
398     MIN = GObject.G_MININT
399
400     def test_int_return(self):
401         self.assertEqual(self.MAX, GIMarshallingTests.int_return_max())
402         self.assertEqual(self.MIN, GIMarshallingTests.int_return_min())
403
404     def test_int_in(self):
405         max = Number(self.MAX)
406         min = Number(self.MIN)
407
408         GIMarshallingTests.int_in_max(max)
409         GIMarshallingTests.int_in_min(min)
410
411         max.value += 1
412         min.value -= 1
413
414         self.assertRaises(ValueError, GIMarshallingTests.int_in_max, max)
415         self.assertRaises(ValueError, GIMarshallingTests.int_in_min, min)
416
417         self.assertRaises(TypeError, GIMarshallingTests.int_in_max, "self.MAX")
418
419     def test_int_out(self):
420         self.assertEqual(self.MAX, GIMarshallingTests.int_out_max())
421         self.assertEqual(self.MIN, GIMarshallingTests.int_out_min())
422
423     def test_int_inout(self):
424         self.assertEqual(self.MIN, GIMarshallingTests.int_inout_max_min(Number(self.MAX)))
425         self.assertEqual(self.MAX, GIMarshallingTests.int_inout_min_max(Number(self.MIN)))
426         self.assertRaises(TypeError, GIMarshallingTests.int_inout_min_max, Number(self.MIN), CONSTANT_NUMBER)
427
428
429 class TestUInt(unittest.TestCase):
430
431     MAX = GObject.G_MAXUINT
432
433     def test_uint_return(self):
434         self.assertEqual(self.MAX, GIMarshallingTests.uint_return())
435
436     def test_uint_in(self):
437         number = Number(self.MAX)
438
439         GIMarshallingTests.uint_in(number)
440
441         number.value += 1
442
443         self.assertRaises(ValueError, GIMarshallingTests.uint_in, number)
444         self.assertRaises(ValueError, GIMarshallingTests.uint_in, Number(-1))
445
446         self.assertRaises(TypeError, GIMarshallingTests.uint_in, "self.MAX")
447
448     def test_uint_out(self):
449         self.assertEqual(self.MAX, GIMarshallingTests.uint_out())
450
451     def test_uint_inout(self):
452         self.assertEqual(0, GIMarshallingTests.uint_inout(Number(self.MAX)))
453
454
455 class TestLong(unittest.TestCase):
456
457     MAX = GObject.G_MAXLONG
458     MIN = GObject.G_MINLONG
459
460     def test_long_return(self):
461         self.assertEqual(self.MAX, GIMarshallingTests.long_return_max())
462         self.assertEqual(self.MIN, GIMarshallingTests.long_return_min())
463
464     def test_long_in(self):
465         max = Number(self.MAX)
466         min = Number(self.MIN)
467
468         GIMarshallingTests.long_in_max(max)
469         GIMarshallingTests.long_in_min(min)
470
471         max.value += 1
472         min.value -= 1
473
474         self.assertRaises(ValueError, GIMarshallingTests.long_in_max, max)
475         self.assertRaises(ValueError, GIMarshallingTests.long_in_min, min)
476
477         self.assertRaises(TypeError, GIMarshallingTests.long_in_max, "self.MAX")
478
479     def test_long_out(self):
480         self.assertEqual(self.MAX, GIMarshallingTests.long_out_max())
481         self.assertEqual(self.MIN, GIMarshallingTests.long_out_min())
482
483     def test_long_inout(self):
484         self.assertEqual(self.MIN, GIMarshallingTests.long_inout_max_min(Number(self.MAX)))
485         self.assertEqual(self.MAX, GIMarshallingTests.long_inout_min_max(Number(self.MIN)))
486
487
488 class TestULong(unittest.TestCase):
489
490     MAX = GObject.G_MAXULONG
491
492     def test_ulong_return(self):
493         self.assertEqual(self.MAX, GIMarshallingTests.ulong_return())
494
495     def test_ulong_in(self):
496         number = Number(self.MAX)
497
498         GIMarshallingTests.ulong_in(number)
499
500         number.value += 1
501
502         self.assertRaises(ValueError, GIMarshallingTests.ulong_in, number)
503         self.assertRaises(ValueError, GIMarshallingTests.ulong_in, Number(-1))
504
505         self.assertRaises(TypeError, GIMarshallingTests.ulong_in, "self.MAX")
506
507     def test_ulong_out(self):
508         self.assertEqual(self.MAX, GIMarshallingTests.ulong_out())
509
510     def test_ulong_inout(self):
511         self.assertEqual(0, GIMarshallingTests.ulong_inout(Number(self.MAX)))
512
513
514 class TestSSize(unittest.TestCase):
515
516     MAX = GObject.G_MAXLONG
517     MIN = GObject.G_MINLONG
518
519     def test_ssize_return(self):
520         self.assertEqual(self.MAX, GIMarshallingTests.ssize_return_max())
521         self.assertEqual(self.MIN, GIMarshallingTests.ssize_return_min())
522
523     def test_ssize_in(self):
524         max = Number(self.MAX)
525         min = Number(self.MIN)
526
527         GIMarshallingTests.ssize_in_max(max)
528         GIMarshallingTests.ssize_in_min(min)
529
530         max.value += 1
531         min.value -= 1
532
533         self.assertRaises(ValueError, GIMarshallingTests.ssize_in_max, max)
534         self.assertRaises(ValueError, GIMarshallingTests.ssize_in_min, min)
535
536         self.assertRaises(TypeError, GIMarshallingTests.ssize_in_max, "self.MAX")
537
538     def test_ssize_out(self):
539         self.assertEqual(self.MAX, GIMarshallingTests.ssize_out_max())
540         self.assertEqual(self.MIN, GIMarshallingTests.ssize_out_min())
541
542     def test_ssize_inout(self):
543         self.assertEqual(self.MIN, GIMarshallingTests.ssize_inout_max_min(Number(self.MAX)))
544         self.assertEqual(self.MAX, GIMarshallingTests.ssize_inout_min_max(Number(self.MIN)))
545
546
547 class TestSize(unittest.TestCase):
548
549     MAX = GObject.G_MAXULONG
550
551     def test_size_return(self):
552         self.assertEqual(self.MAX, GIMarshallingTests.size_return())
553
554     def test_size_in(self):
555         number = Number(self.MAX)
556
557         GIMarshallingTests.size_in(number)
558
559         number.value += 1
560
561         self.assertRaises(ValueError, GIMarshallingTests.size_in, number)
562         self.assertRaises(ValueError, GIMarshallingTests.size_in, Number(-1))
563
564         self.assertRaises(TypeError, GIMarshallingTests.size_in, "self.MAX")
565
566     def test_size_out(self):
567         self.assertEqual(self.MAX, GIMarshallingTests.size_out())
568
569     def test_size_inout(self):
570         self.assertEqual(0, GIMarshallingTests.size_inout(Number(self.MAX)))
571
572
573 class TestTimet(unittest.TestCase):
574
575     def test_time_t_return(self):
576         self.assertEqual(1234567890, GIMarshallingTests.time_t_return())
577
578     def test_time_t_in(self):
579         GIMarshallingTests.time_t_in(1234567890)
580         self.assertRaises(TypeError, GIMarshallingTests.time_t_in, "hello")
581
582     def test_time_t_out(self):
583         self.assertEqual(1234567890, GIMarshallingTests.time_t_out())
584
585     def test_time_t_inout(self):
586         self.assertEqual(0, GIMarshallingTests.time_t_inout(1234567890))
587
588
589 class TestFloat(unittest.TestCase):
590
591     MAX = GObject.G_MAXFLOAT
592     MIN = GObject.G_MINFLOAT
593
594     def test_float_return(self):
595         self.assertAlmostEqual(self.MAX, GIMarshallingTests.float_return())
596
597     def test_float_in(self):
598         GIMarshallingTests.float_in(Number(self.MAX))
599
600         self.assertRaises(TypeError, GIMarshallingTests.float_in, "self.MAX")
601
602     def test_float_out(self):
603         self.assertAlmostEqual(self.MAX, GIMarshallingTests.float_out())
604
605     def test_float_inout(self):
606         self.assertAlmostEqual(self.MIN, GIMarshallingTests.float_inout(Number(self.MAX)))
607
608
609 class TestDouble(unittest.TestCase):
610
611     MAX = GObject.G_MAXDOUBLE
612     MIN = GObject.G_MINDOUBLE
613
614     def test_double_return(self):
615         self.assertAlmostEqual(self.MAX, GIMarshallingTests.double_return())
616
617     def test_double_in(self):
618         GIMarshallingTests.double_in(Number(self.MAX))
619
620         self.assertRaises(TypeError, GIMarshallingTests.double_in, "self.MAX")
621
622     def test_double_out(self):
623         self.assertAlmostEqual(self.MAX, GIMarshallingTests.double_out())
624
625     def test_double_inout(self):
626         self.assertAlmostEqual(self.MIN, GIMarshallingTests.double_inout(Number(self.MAX)))
627
628
629 class TestGType(unittest.TestCase):
630
631     def test_gtype_name(self):
632         self.assertEqual("void", GObject.TYPE_NONE.name)
633         self.assertEqual("gchararray", GObject.TYPE_STRING.name)
634
635         def check_readonly(gtype):
636             gtype.name = "foo"
637
638         self.assertRaises(AttributeError, check_readonly, GObject.TYPE_NONE)
639         self.assertRaises(AttributeError, check_readonly, GObject.TYPE_STRING)
640
641     def test_gtype_return(self):
642         self.assertEqual(GObject.TYPE_NONE, GIMarshallingTests.gtype_return())
643         self.assertEqual(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_return())
644
645     def test_gtype_in(self):
646         GIMarshallingTests.gtype_in(GObject.TYPE_NONE)
647         GIMarshallingTests.gtype_string_in(GObject.TYPE_STRING)
648         self.assertRaises(TypeError, GIMarshallingTests.gtype_in, "foo")
649         self.assertRaises(TypeError, GIMarshallingTests.gtype_string_in, "foo")
650
651     def test_gtype_out(self):
652         self.assertEqual(GObject.TYPE_NONE, GIMarshallingTests.gtype_out())
653         self.assertEqual(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_out())
654
655     def test_gtype_inout(self):
656         self.assertEqual(GObject.TYPE_INT, GIMarshallingTests.gtype_inout(GObject.TYPE_NONE))
657
658
659 class TestUtf8(unittest.TestCase):
660
661     def test_utf8_none_return(self):
662         self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_none_return())
663
664     def test_utf8_full_return(self):
665         self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_full_return())
666
667     def test_utf8_none_in(self):
668         GIMarshallingTests.utf8_none_in(CONSTANT_UTF8)
669         if sys.version_info < (3, 0):
670             GIMarshallingTests.utf8_none_in(PY2_UNICODE_UTF8)
671
672         self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, CONSTANT_NUMBER)
673         self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, None)
674
675     def test_utf8_none_out(self):
676         self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_none_out())
677
678     def test_utf8_full_out(self):
679         self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_full_out())
680
681     def test_utf8_dangling_out(self):
682         GIMarshallingTests.utf8_dangling_out()
683
684     def test_utf8_none_inout(self):
685         self.assertEqual("", GIMarshallingTests.utf8_none_inout(CONSTANT_UTF8))
686
687     def test_utf8_full_inout(self):
688         self.assertEqual("", GIMarshallingTests.utf8_full_inout(CONSTANT_UTF8))
689
690
691 class TestFilename(unittest.TestCase):
692     def setUp(self):
693         self.workdir = tempfile.mkdtemp()
694
695     def tearDown(self):
696         shutil.rmtree(self.workdir)
697
698     def test_filename_in(self):
699         fname = os.path.join(self.workdir, _unicode('testäø.txt'))
700         self.assertRaises(GLib.GError, GLib.file_get_contents, fname)
701
702         with open(fname.encode('UTF-8'), 'wb') as f:
703             f.write(b'hello world!\n\x01\x02')
704
705         (result, contents) = GLib.file_get_contents(fname)
706         self.assertEqual(result, True)
707         self.assertEqual(contents, b'hello world!\n\x01\x02')
708
709     def test_filename_out(self):
710         self.assertRaises(GLib.GError, GLib.Dir.make_tmp, 'test')
711
712         dirname = GLib.Dir.make_tmp('testäø.XXXXXX')
713         self.assertTrue('/testäø.' in dirname, dirname)
714         dirname = _bytes(dirname)
715         self.assertTrue(os.path.isdir(dirname))
716         os.rmdir(dirname)
717
718     def test_filename_type_error(self):
719         self.assertRaises(TypeError, GLib.file_get_contents, 23)
720
721
722 class TestArray(unittest.TestCase):
723
724     def test_array_fixed_int_return(self):
725         self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_int_return())
726
727     def test_array_fixed_short_return(self):
728         self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_short_return())
729
730     def test_array_fixed_int_in(self):
731         GIMarshallingTests.array_fixed_int_in(Sequence([-1, 0, 1, 2]))
732
733         self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, Sequence([-1, '0', 1, 2]))
734
735         self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, 42)
736         self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, None)
737
738     def test_array_fixed_short_in(self):
739         GIMarshallingTests.array_fixed_short_in(Sequence([-1, 0, 1, 2]))
740
741     def test_array_fixed_out(self):
742         self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_out())
743
744     def test_array_fixed_inout(self):
745         self.assertEqual([2, 1, 0, -1], GIMarshallingTests.array_fixed_inout([-1, 0, 1, 2]))
746
747     def test_array_return(self):
748         self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_return())
749
750     def test_array_return_etc(self):
751         self.assertEqual(([5, 0, 1, 9], 14), GIMarshallingTests.array_return_etc(5, 9))
752
753     def test_array_in(self):
754         GIMarshallingTests.array_in(Sequence([-1, 0, 1, 2]))
755         GIMarshallingTests.array_in_guint64_len(Sequence([-1, 0, 1, 2]))
756         GIMarshallingTests.array_in_guint8_len(Sequence([-1, 0, 1, 2]))
757
758     def test_array_in_len_before(self):
759         GIMarshallingTests.array_in_len_before(Sequence([-1, 0, 1, 2]))
760
761     def test_array_in_len_zero_terminated(self):
762         GIMarshallingTests.array_in_len_zero_terminated(Sequence([-1, 0, 1, 2]))
763
764     def test_array_uint8_in(self):
765         GIMarshallingTests.array_uint8_in(Sequence([97, 98, 99, 100]))
766         GIMarshallingTests.array_uint8_in(_bytes("abcd"))
767
768     def test_array_string_in(self):
769         GIMarshallingTests.array_string_in(['foo', 'bar'])
770
771     def test_array_out(self):
772         self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_out())
773
774     def test_array_out_etc(self):
775         self.assertEqual(([-5, 0, 1, 9], 4), GIMarshallingTests.array_out_etc(-5, 9))
776
777     def test_array_inout(self):
778         self.assertEqual([-2, -1, 0, 1, 2], GIMarshallingTests.array_inout(Sequence([-1, 0, 1, 2])))
779
780     def test_array_inout_etc(self):
781         self.assertEqual(([-5, -1, 0, 1, 9], 4),
782                          GIMarshallingTests.array_inout_etc(-5, Sequence([-1, 0, 1, 2]), 9))
783
784     def test_method_array_in(self):
785         object_ = GIMarshallingTests.Object()
786         object_.method_array_in(Sequence([-1, 0, 1, 2]))
787
788     def test_method_array_out(self):
789         object_ = GIMarshallingTests.Object()
790         self.assertEqual([-1, 0, 1, 2], object_.method_array_out())
791
792     def test_method_array_inout(self):
793         object_ = GIMarshallingTests.Object()
794         self.assertEqual([-2, -1, 0, 1, 2], object_.method_array_inout(Sequence([-1, 0, 1, 2])))
795
796     def test_method_array_return(self):
797         object_ = GIMarshallingTests.Object()
798         self.assertEqual([-1, 0, 1, 2], object_.method_array_return())
799
800     def test_array_enum_in(self):
801         GIMarshallingTests.array_enum_in([GIMarshallingTests.Enum.VALUE1,
802                                           GIMarshallingTests.Enum.VALUE2,
803                                           GIMarshallingTests.Enum.VALUE3])
804
805     def test_array_boxed_struct_in(self):
806         struct1 = GIMarshallingTests.BoxedStruct()
807         struct1.long_ = 1
808         struct2 = GIMarshallingTests.BoxedStruct()
809         struct2.long_ = 2
810         struct3 = GIMarshallingTests.BoxedStruct()
811         struct3.long_ = 3
812
813         GIMarshallingTests.array_struct_in([struct1, struct2, struct3])
814
815     @unittest.skipUnless(hasattr(GIMarshallingTests, 'array_struct_value_in'),
816                          'too old gobject-introspection')
817     def test_array_boxed_struct_value_in(self):
818         struct1 = GIMarshallingTests.BoxedStruct()
819         struct1.long_ = 1
820         struct2 = GIMarshallingTests.BoxedStruct()
821         struct2.long_ = 2
822         struct3 = GIMarshallingTests.BoxedStruct()
823         struct3.long_ = 3
824
825         GIMarshallingTests.array_struct_value_in([struct1, struct2, struct3])
826
827     def test_array_boxed_struct_take_in(self):
828         struct1 = GIMarshallingTests.BoxedStruct()
829         struct1.long_ = 1
830         struct2 = GIMarshallingTests.BoxedStruct()
831         struct2.long_ = 2
832         struct3 = GIMarshallingTests.BoxedStruct()
833         struct3.long_ = 3
834
835         GIMarshallingTests.array_struct_take_in([struct1, struct2, struct3])
836
837         self.assertEqual(1, struct1.long_)
838
839     def test_array_boxed_struct_return(self):
840         (struct1, struct2, struct3) = GIMarshallingTests.array_zero_terminated_return_struct()
841         self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct1))
842         self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct2))
843         self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct3))
844         self.assertEqual(42, struct1.long_)
845         self.assertEqual(43, struct2.long_)
846         self.assertEqual(44, struct3.long_)
847
848     def test_array_simple_struct_in(self):
849         struct1 = GIMarshallingTests.SimpleStruct()
850         struct1.long_ = 1
851         struct2 = GIMarshallingTests.SimpleStruct()
852         struct2.long_ = 2
853         struct3 = GIMarshallingTests.SimpleStruct()
854         struct3.long_ = 3
855
856         GIMarshallingTests.array_simple_struct_in([struct1, struct2, struct3])
857
858     def test_array_multi_array_key_value_in(self):
859         GIMarshallingTests.multi_array_key_value_in(["one", "two", "three"],
860                                                     [1, 2, 3])
861
862     def test_array_in_nonzero_nonlen(self):
863         GIMarshallingTests.array_in_nonzero_nonlen(1, b'abcd')
864
865     def test_array_fixed_out_struct(self):
866         struct1, struct2 = GIMarshallingTests.array_fixed_out_struct()
867
868         self.assertEqual(7, struct1.long_)
869         self.assertEqual(6, struct1.int8)
870         self.assertEqual(6, struct2.long_)
871         self.assertEqual(7, struct2.int8)
872
873     def test_array_zero_terminated_return(self):
874         self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_return())
875
876     def test_array_zero_terminated_return_null(self):
877         self.assertEqual([], GIMarshallingTests.array_zero_terminated_return_null())
878
879     def test_array_zero_terminated_in(self):
880         GIMarshallingTests.array_zero_terminated_in(Sequence(['0', '1', '2']))
881
882     def test_array_zero_terminated_out(self):
883         self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
884
885     def test_array_zero_terminated_inout(self):
886         self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.array_zero_terminated_inout(['0', '1', '2']))
887
888     def test_init_function(self):
889         self.assertEqual((True, []), GIMarshallingTests.init_function([]))
890         self.assertEqual((True, []), GIMarshallingTests.init_function(['hello']))
891         self.assertEqual((True, ['hello']),
892                          GIMarshallingTests.init_function(['hello', 'world']))
893
894
895 class TestGStrv(unittest.TestCase):
896
897     def test_gstrv_return(self):
898         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_return())
899
900     def test_gstrv_in(self):
901         GIMarshallingTests.gstrv_in(Sequence(['0', '1', '2']))
902
903     def test_gstrv_out(self):
904         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_out())
905
906     def test_gstrv_inout(self):
907         self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.gstrv_inout(['0', '1', '2']))
908
909
910 class TestArrayGVariant(unittest.TestCase):
911
912     def test_array_gvariant_none_in(self):
913         v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
914         returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_none_in(v)]
915         self.assertEqual([27, "Hello"], returned)
916
917     def test_array_gvariant_container_in(self):
918         v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
919         returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_container_in(v)]
920         self.assertEqual([27, "Hello"], returned)
921
922     def test_array_gvariant_full_in(self):
923         v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
924         returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_full_in(v)]
925         self.assertEqual([27, "Hello"], returned)
926
927     def test_bytearray_gvariant(self):
928         v = GLib.Variant.new_bytestring(b"foo")
929         self.assertEqual(v.get_bytestring(), b"foo")
930
931
932 class TestGArray(unittest.TestCase):
933
934     def test_garray_int_none_return(self):
935         self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.garray_int_none_return())
936
937     def test_garray_uint64_none_return(self):
938         self.assertEqual([0, GObject.G_MAXUINT64], GIMarshallingTests.garray_uint64_none_return())
939
940     def test_garray_utf8_none_return(self):
941         self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_return())
942
943     def test_garray_utf8_container_return(self):
944         self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_return())
945
946     def test_garray_utf8_full_return(self):
947         self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_return())
948
949     def test_garray_int_none_in(self):
950         GIMarshallingTests.garray_int_none_in(Sequence([-1, 0, 1, 2]))
951
952         self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, Sequence([-1, '0', 1, 2]))
953
954         self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, 42)
955         self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, None)
956
957     def test_garray_uint64_none_in(self):
958         GIMarshallingTests.garray_uint64_none_in(Sequence([0, GObject.G_MAXUINT64]))
959
960     def test_garray_utf8_none_in(self):
961         GIMarshallingTests.garray_utf8_none_in(Sequence(['0', '1', '2']))
962
963     def test_garray_utf8_none_out(self):
964         self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_out())
965
966     def test_garray_utf8_container_out(self):
967         self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_out())
968
969     def test_garray_utf8_full_out(self):
970         self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out())
971
972     @unittest.skipUnless(hasattr(GIMarshallingTests, 'garray_utf8_full_out_caller_allocated'),
973                          'too old gobject-introspection')
974     def test_garray_utf8_full_out_caller_allocated(self):
975         self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out_caller_allocated())
976
977     def test_garray_utf8_none_inout(self):
978         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_none_inout(Sequence(('0', '1', '2'))))
979
980     def test_garray_utf8_container_inout(self):
981         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_container_inout(['0', '1', '2']))
982
983     def test_garray_utf8_full_inout(self):
984         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_full_inout(['0', '1', '2']))
985
986
987 class TestGPtrArray(unittest.TestCase):
988
989     def test_gptrarray_utf8_none_return(self):
990         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_return())
991
992     def test_gptrarray_utf8_container_return(self):
993         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_return())
994
995     def test_gptrarray_utf8_full_return(self):
996         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_return())
997
998     def test_gptrarray_utf8_none_in(self):
999         GIMarshallingTests.gptrarray_utf8_none_in(Sequence(['0', '1', '2']))
1000
1001     def test_gptrarray_utf8_none_out(self):
1002         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_out())
1003
1004     def test_gptrarray_utf8_container_out(self):
1005         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_out())
1006
1007     def test_gptrarray_utf8_full_out(self):
1008         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_out())
1009
1010     def test_gptrarray_utf8_none_inout(self):
1011         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_none_inout(Sequence(('0', '1', '2'))))
1012
1013     def test_gptrarray_utf8_container_inout(self):
1014         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_container_inout(['0', '1', '2']))
1015
1016     def test_gptrarray_utf8_full_inout(self):
1017         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_full_inout(['0', '1', '2']))
1018
1019
1020 class TestGBytes(unittest.TestCase):
1021     def test_gbytes_create(self):
1022         b = GLib.Bytes.new(b'\x00\x01\xFF')
1023         self.assertEqual(3, b.get_size())
1024         self.assertEqual(b'\x00\x01\xFF', b.get_data())
1025
1026     def test_gbytes_create_take(self):
1027         b = GLib.Bytes.new_take(b'\x00\x01\xFF')
1028         self.assertEqual(3, b.get_size())
1029         self.assertEqual(b'\x00\x01\xFF', b.get_data())
1030
1031     @unittest.skipUnless(hasattr(GIMarshallingTests, 'gbytes_full_return'),
1032                          'too old gobject-introspection')
1033     def test_gbytes_full_return(self):
1034         b = GIMarshallingTests.gbytes_full_return()
1035         self.assertEqual(4, b.get_size())
1036         self.assertEqual(b'\x00\x31\xFF\x33', b.get_data())
1037
1038     @unittest.skipUnless(hasattr(GIMarshallingTests, 'gbytes_full_return'),
1039                          'too old gobject-introspection')
1040     def test_gbytes_none_in(self):
1041         b = GIMarshallingTests.gbytes_full_return()
1042         GIMarshallingTests.gbytes_none_in(b)
1043
1044     def test_compare(self):
1045         a1 = GLib.Bytes.new(b'\x00\x01\xFF')
1046         a2 = GLib.Bytes.new(b'\x00\x01\xFF')
1047         b = GLib.Bytes.new(b'\x00\x01\xFE')
1048
1049         self.assertTrue(a1.equal(a2))
1050         self.assertTrue(a2.equal(a1))
1051         self.assertFalse(a1.equal(b))
1052         self.assertFalse(b.equal(a2))
1053
1054         self.assertEqual(0, a1.compare(a2))
1055         self.assertLess(0, a1.compare(b))
1056         self.assertGreater(0, b.compare(a1))
1057
1058
1059 class TestGByteArray(unittest.TestCase):
1060     def test_new(self):
1061         ba = GLib.ByteArray.new()
1062         self.assertEqual(b'', ba)
1063
1064         ba = GLib.ByteArray.new_take(b'\x01\x02\xFF')
1065         self.assertEqual(b'\x01\x02\xFF', ba)
1066
1067     def test_bytearray_full_return(self):
1068         self.assertEqual(b'\x001\xFF3', GIMarshallingTests.bytearray_full_return())
1069
1070     def test_bytearray_none_in(self):
1071         GIMarshallingTests.bytearray_none_in(b'\x00\x31\xFF\x33')
1072
1073
1074 class TestGList(unittest.TestCase):
1075
1076     def test_glist_int_none_return(self):
1077         self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.glist_int_none_return())
1078
1079     def test_glist_uint32_none_return(self):
1080         self.assertEqual([0, GObject.G_MAXUINT32], GIMarshallingTests.glist_uint32_none_return())
1081
1082     def test_glist_utf8_none_return(self):
1083         self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_return())
1084
1085     def test_glist_utf8_container_return(self):
1086         self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_return())
1087
1088     def test_glist_utf8_full_return(self):
1089         self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_return())
1090
1091     def test_glist_int_none_in(self):
1092         GIMarshallingTests.glist_int_none_in(Sequence((-1, 0, 1, 2)))
1093
1094         self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, Sequence((-1, '0', 1, 2)))
1095
1096         self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, 42)
1097         self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, None)
1098
1099     def test_glist_uint32_none_in(self):
1100         GIMarshallingTests.glist_uint32_none_in(Sequence((0, GObject.G_MAXUINT32)))
1101
1102     def test_glist_utf8_none_in(self):
1103         GIMarshallingTests.glist_utf8_none_in(Sequence(('0', '1', '2')))
1104
1105     def test_glist_utf8_none_out(self):
1106         self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_out())
1107
1108     def test_glist_utf8_container_out(self):
1109         self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_out())
1110
1111     def test_glist_utf8_full_out(self):
1112         self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_out())
1113
1114     def test_glist_utf8_none_inout(self):
1115         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_none_inout(Sequence(('0', '1', '2'))))
1116
1117     def test_glist_utf8_container_inout(self):
1118         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_container_inout(('0', '1', '2')))
1119
1120     def test_glist_utf8_full_inout(self):
1121         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_full_inout(('0', '1', '2')))
1122
1123
1124 class TestGSList(unittest.TestCase):
1125
1126     def test_gslist_int_none_return(self):
1127         self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.gslist_int_none_return())
1128
1129     def test_gslist_utf8_none_return(self):
1130         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_return())
1131
1132     def test_gslist_utf8_container_return(self):
1133         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_return())
1134
1135     def test_gslist_utf8_full_return(self):
1136         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_return())
1137
1138     def test_gslist_int_none_in(self):
1139         GIMarshallingTests.gslist_int_none_in(Sequence((-1, 0, 1, 2)))
1140
1141         self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, Sequence((-1, '0', 1, 2)))
1142
1143         self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, 42)
1144         self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, None)
1145
1146     def test_gslist_utf8_none_in(self):
1147         GIMarshallingTests.gslist_utf8_none_in(Sequence(('0', '1', '2')))
1148
1149     def test_gslist_utf8_none_out(self):
1150         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_out())
1151
1152     def test_gslist_utf8_container_out(self):
1153         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_out())
1154
1155     def test_gslist_utf8_full_out(self):
1156         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_out())
1157
1158     def test_gslist_utf8_none_inout(self):
1159         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_none_inout(Sequence(('0', '1', '2'))))
1160
1161     def test_gslist_utf8_container_inout(self):
1162         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_container_inout(('0', '1', '2')))
1163
1164     def test_gslist_utf8_full_inout(self):
1165         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_full_inout(('0', '1', '2')))
1166
1167
1168 class TestGHashTable(unittest.TestCase):
1169
1170     def test_ghashtable_int_none_return(self):
1171         self.assertEqual({-1: 1, 0: 0, 1: -1, 2: -2}, GIMarshallingTests.ghashtable_int_none_return())
1172
1173     def test_ghashtable_int_none_return2(self):
1174         self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_return())
1175
1176     def test_ghashtable_int_container_return(self):
1177         self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_return())
1178
1179     def test_ghashtable_int_full_return(self):
1180         self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_return())
1181
1182     def test_ghashtable_int_none_in(self):
1183         GIMarshallingTests.ghashtable_int_none_in({-1: 1, 0: 0, 1: -1, 2: -2})
1184
1185         self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, '0': 0, 1: -1, 2: -2})
1186         self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, 0: '0', 1: -1, 2: -2})
1187
1188         self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, '{-1: 1, 0: 0, 1: -1, 2: -2}')
1189         self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, None)
1190
1191     def test_ghashtable_utf8_none_in(self):
1192         GIMarshallingTests.ghashtable_utf8_none_in({'-1': '1', '0': '0', '1': '-1', '2': '-2'})
1193
1194     def test_ghashtable_utf8_none_out(self):
1195         self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_out())
1196
1197     def test_ghashtable_utf8_container_out(self):
1198         self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_out())
1199
1200     def test_ghashtable_utf8_full_out(self):
1201         self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_out())
1202
1203     def test_ghashtable_utf8_none_inout(self):
1204         i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1205         self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1206                          GIMarshallingTests.ghashtable_utf8_none_inout(i))
1207
1208     def test_ghashtable_utf8_container_inout(self):
1209         i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1210         self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1211                          GIMarshallingTests.ghashtable_utf8_container_inout(i))
1212
1213     def test_ghashtable_utf8_full_inout(self):
1214         i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1215         self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1216                          GIMarshallingTests.ghashtable_utf8_full_inout(i))
1217
1218
1219 class TestGValue(unittest.TestCase):
1220
1221     def test_gvalue_return(self):
1222         self.assertEqual(42, GIMarshallingTests.gvalue_return())
1223
1224     def test_gvalue_in(self):
1225         GIMarshallingTests.gvalue_in(42)
1226         value = GObject.Value(GObject.TYPE_INT, 42)
1227         GIMarshallingTests.gvalue_in(value)
1228
1229     def test_gvalue_int64_in(self):
1230         value = GObject.Value(GObject.TYPE_INT64, GObject.G_MAXINT64)
1231         GIMarshallingTests.gvalue_int64_in(value)
1232
1233     def test_gvalue_in_with_type(self):
1234         value = GObject.Value(GObject.TYPE_STRING, 'foo')
1235         GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_STRING)
1236
1237         value = GObject.Value(GIMarshallingTests.Flags.__gtype__,
1238                               GIMarshallingTests.Flags.VALUE1)
1239         GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_FLAGS)
1240
1241     def test_gvalue_in_enum(self):
1242         value = GObject.Value(GIMarshallingTests.Enum.__gtype__,
1243                               GIMarshallingTests.Enum.VALUE3)
1244         GIMarshallingTests.gvalue_in_enum(value)
1245
1246     def test_gvalue_out(self):
1247         self.assertEqual(42, GIMarshallingTests.gvalue_out())
1248
1249     def test_gvalue_int64_out(self):
1250         self.assertEqual(GObject.G_MAXINT64, GIMarshallingTests.gvalue_int64_out())
1251
1252     def test_gvalue_out_caller_allocates(self):
1253         self.assertEqual(42, GIMarshallingTests.gvalue_out_caller_allocates())
1254
1255     def test_gvalue_inout(self):
1256         self.assertEqual('42', GIMarshallingTests.gvalue_inout(42))
1257         value = GObject.Value(int, 42)
1258         self.assertEqual('42', GIMarshallingTests.gvalue_inout(value))
1259
1260     def test_gvalue_flat_array_in(self):
1261         # the function already asserts the correct values
1262         GIMarshallingTests.gvalue_flat_array([42, "42", True])
1263
1264     def test_gvalue_flat_array_out(self):
1265         values = GIMarshallingTests.return_gvalue_flat_array()
1266         self.assertEqual(values, [42, '42', True])
1267
1268     def test_gvalue_gobject_ref_counts(self):
1269         # Tests a GObject held by a GValue
1270         obj = GObject.Object()
1271         ref = weakref.ref(obj)
1272         grefcount = obj.__grefcount__
1273
1274         value = GObject.Value()
1275         value.init(GObject.TYPE_OBJECT)
1276
1277         # TYPE_OBJECT will inc ref count as it should
1278         value.set_object(obj)
1279         self.assertEqual(obj.__grefcount__, grefcount + 1)
1280
1281         # multiple set_object should not inc ref count
1282         value.set_object(obj)
1283         self.assertEqual(obj.__grefcount__, grefcount + 1)
1284
1285         # get_object will re-use the same wrapper as obj
1286         res = value.get_object()
1287         self.assertEqual(obj, res)
1288         self.assertEqual(obj.__grefcount__, grefcount + 1)
1289
1290         # multiple get_object should not inc ref count
1291         res = value.get_object()
1292         self.assertEqual(obj.__grefcount__, grefcount + 1)
1293
1294         # deletion of the result and value holder should bring the
1295         # refcount back to where we started
1296         del res
1297         del value
1298         gc.collect()
1299         self.assertEqual(obj.__grefcount__, grefcount)
1300
1301         del obj
1302         gc.collect()
1303         self.assertEqual(ref(), None)
1304
1305     def test_gvalue_boxed_ref_counts(self):
1306         # Tests a boxed type wrapping a python object pointer (TYPE_PYOBJECT)
1307         # held by a GValue
1308         class Obj(object):
1309             pass
1310
1311         obj = Obj()
1312         ref = weakref.ref(obj)
1313         refcount = sys.getrefcount(obj)
1314
1315         value = GObject.Value()
1316         value.init(GObject.TYPE_PYOBJECT)
1317
1318         # boxed TYPE_PYOBJECT will inc ref count as it should
1319         value.set_boxed(obj)
1320         self.assertEqual(sys.getrefcount(obj), refcount + 1)
1321
1322         # multiple set_boxed should not inc ref count
1323         value.set_boxed(obj)
1324         self.assertEqual(sys.getrefcount(obj), refcount + 1)
1325
1326         res = value.get_boxed()
1327         self.assertEqual(obj, res)
1328         self.assertEqual(sys.getrefcount(obj), refcount + 2)
1329
1330         # multiple get_boxed should not inc ref count
1331         res = value.get_boxed()
1332         self.assertEqual(sys.getrefcount(obj), refcount + 2)
1333
1334         # deletion of the result and value holder should bring the
1335         # refcount back to where we started
1336         del res
1337         del value
1338         gc.collect()
1339         self.assertEqual(sys.getrefcount(obj), refcount)
1340
1341         del obj
1342         gc.collect()
1343         self.assertEqual(ref(), None)
1344
1345     # FIXME: crashes
1346     def disabled_test_gvalue_flat_array_round_trip(self):
1347         self.assertEqual([42, '42', True],
1348                          GIMarshallingTests.gvalue_flat_array_round_trip(42, '42', True))
1349
1350
1351 class TestGClosure(unittest.TestCase):
1352
1353     def test_gclosure_in(self):
1354         GIMarshallingTests.gclosure_in(lambda: 42)
1355
1356         # test passing a closure between two C calls
1357         closure = GIMarshallingTests.gclosure_return()
1358         GIMarshallingTests.gclosure_in(closure)
1359
1360         self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, 42)
1361         self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, None)
1362
1363
1364 class TestCallbacks(unittest.TestCase):
1365     def test_return_value_only(self):
1366         def cb():
1367             return 5
1368         self.assertEqual(GIMarshallingTests.callback_return_value_only(cb), 5)
1369
1370     def test_one_out_arg(self):
1371         def cb():
1372             return 5.5
1373         self.assertAlmostEqual(GIMarshallingTests.callback_one_out_parameter(cb), 5.5)
1374
1375     def test_multiple_out_args(self):
1376         def cb():
1377             return (5.5, 42.0)
1378         res = GIMarshallingTests.callback_multiple_out_parameters(cb)
1379         self.assertAlmostEqual(res[0], 5.5)
1380         self.assertAlmostEqual(res[1], 42.0)
1381
1382     def test_return_and_one_out_arg(self):
1383         def cb():
1384             return (5, 42.0)
1385         res = GIMarshallingTests.callback_return_value_and_one_out_parameter(cb)
1386         self.assertEqual(res[0], 5)
1387         self.assertAlmostEqual(res[1], 42.0)
1388
1389     def test_return_and_multiple_out_arg(self):
1390         def cb():
1391             return (5, 42, -1000)
1392         self.assertEqual(GIMarshallingTests.callback_return_value_and_multiple_out_parameters(cb),
1393                          (5, 42, -1000))
1394
1395
1396 class TestPointer(unittest.TestCase):
1397     def test_pointer_in_return(self):
1398         self.assertEqual(GIMarshallingTests.pointer_in_return(42), 42)
1399
1400
1401 class TestEnum(unittest.TestCase):
1402
1403     @classmethod
1404     def setUpClass(cls):
1405         '''Run tests under a test locale.
1406
1407         Upper case conversion of member names should not be locale specific
1408         e.  g. in Turkish, "i".upper() == "i", which gives results like "iNVALiD"
1409
1410         Run test under a locale which defines toupper('a') == 'a'
1411         '''
1412         cls.locale_dir = tempfile.mkdtemp()
1413         src = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'te_ST@nouppera')
1414         dest = os.path.join(cls.locale_dir, 'te_ST.UTF-8@nouppera')
1415         subprocess.check_call(['localedef', '-i', src, '-c', '-f', 'UTF-8', dest])
1416         os.environ['LOCPATH'] = cls.locale_dir
1417         locale.setlocale(locale.LC_ALL, 'te_ST.UTF-8@nouppera')
1418
1419     @classmethod
1420     def tearDownClass(cls):
1421         locale.setlocale(locale.LC_ALL, 'C')
1422         shutil.rmtree(cls.locale_dir)
1423         try:
1424             del os.environ['LOCPATH']
1425         except KeyError:
1426             pass
1427
1428     def test_enum(self):
1429         self.assertTrue(issubclass(GIMarshallingTests.Enum, int))
1430         self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE1, GIMarshallingTests.Enum))
1431         self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE2, GIMarshallingTests.Enum))
1432         self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE3, GIMarshallingTests.Enum))
1433         self.assertEqual(42, GIMarshallingTests.Enum.VALUE3)
1434
1435     def test_value_nick_and_name(self):
1436         self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_nick, 'value1')
1437         self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_nick, 'value2')
1438         self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_nick, 'value3')
1439
1440         self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE1')
1441         self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE2')
1442         self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE3')
1443
1444     def test_enum_in(self):
1445         GIMarshallingTests.enum_in(GIMarshallingTests.Enum.VALUE3)
1446         GIMarshallingTests.enum_in(42)
1447
1448         self.assertRaises(TypeError, GIMarshallingTests.enum_in, 43)
1449         self.assertRaises(TypeError, GIMarshallingTests.enum_in, 'GIMarshallingTests.Enum.VALUE3')
1450
1451     def test_enum_return(self):
1452         enum = GIMarshallingTests.enum_returnv()
1453         self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1454         self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1455
1456     def test_enum_out(self):
1457         enum = GIMarshallingTests.enum_out()
1458         self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1459         self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1460
1461     def test_enum_inout(self):
1462         enum = GIMarshallingTests.enum_inout(GIMarshallingTests.Enum.VALUE3)
1463         self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1464         self.assertEqual(enum, GIMarshallingTests.Enum.VALUE1)
1465
1466     def test_enum_second(self):
1467         # check for the bug where different non-gtype enums share the same class
1468         self.assertNotEqual(GIMarshallingTests.Enum, GIMarshallingTests.SecondEnum)
1469
1470         # check that values are not being shared between different enums
1471         self.assertTrue(hasattr(GIMarshallingTests.SecondEnum, "SECONDVALUE1"))
1472         self.assertRaises(AttributeError, getattr, GIMarshallingTests.Enum, "SECONDVALUE1")
1473         self.assertTrue(hasattr(GIMarshallingTests.Enum, "VALUE1"))
1474         self.assertRaises(AttributeError, getattr, GIMarshallingTests.SecondEnum, "VALUE1")
1475
1476     def test_enum_gtype_name_is_namespaced(self):
1477         self.assertEqual(GIMarshallingTests.Enum.__gtype__.name,
1478                          'PyGIMarshallingTestsEnum')
1479
1480     def test_enum_double_registration_error(self):
1481         # a warning is printed for double registration and pygobject will
1482         # also raise a RuntimeError.
1483         old_mask = GLib.log_set_always_fatal(GLib.LogLevelFlags.LEVEL_ERROR)
1484         try:
1485             self.assertRaises(RuntimeError,
1486                               gi._gi.enum_register_new_gtype_and_add,
1487                               GIMarshallingTests.Enum.__info__)
1488         finally:
1489             GLib.log_set_always_fatal(old_mask)
1490
1491     def test_enum_add_type_error(self):
1492         self.assertRaises(TypeError,
1493                           gi._gi.enum_add,
1494                           GIMarshallingTests.NoTypeFlags.__gtype__)
1495
1496
1497 class TestGEnum(unittest.TestCase):
1498
1499     def test_genum(self):
1500         self.assertTrue(issubclass(GIMarshallingTests.GEnum, GObject.GEnum))
1501         self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE1, GIMarshallingTests.GEnum))
1502         self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE2, GIMarshallingTests.GEnum))
1503         self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE3, GIMarshallingTests.GEnum))
1504         self.assertEqual(42, GIMarshallingTests.GEnum.VALUE3)
1505
1506     def test_value_nick_and_name(self):
1507         self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_nick, 'value1')
1508         self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_nick, 'value2')
1509         self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_nick, 'value3')
1510
1511         self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE1')
1512         self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE2')
1513         self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE3')
1514
1515     def test_genum_in(self):
1516         GIMarshallingTests.genum_in(GIMarshallingTests.GEnum.VALUE3)
1517         GIMarshallingTests.genum_in(42)
1518
1519         self.assertRaises(TypeError, GIMarshallingTests.genum_in, 43)
1520         self.assertRaises(TypeError, GIMarshallingTests.genum_in, 'GIMarshallingTests.GEnum.VALUE3')
1521
1522     def test_genum_return(self):
1523         genum = GIMarshallingTests.genum_returnv()
1524         self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1525         self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1526
1527     def test_genum_out(self):
1528         genum = GIMarshallingTests.genum_out()
1529         self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1530         self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1531
1532     def test_genum_inout(self):
1533         genum = GIMarshallingTests.genum_inout(GIMarshallingTests.GEnum.VALUE3)
1534         self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1535         self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE1)
1536
1537
1538 class TestGFlags(unittest.TestCase):
1539
1540     def test_flags(self):
1541         self.assertTrue(issubclass(GIMarshallingTests.Flags, GObject.GFlags))
1542         self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1, GIMarshallingTests.Flags))
1543         self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE2, GIMarshallingTests.Flags))
1544         self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE3, GIMarshallingTests.Flags))
1545         # __or__() operation should still return an instance, not an int.
1546         self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1 | GIMarshallingTests.Flags.VALUE2,
1547                                    GIMarshallingTests.Flags))
1548         self.assertEqual(1 << 1, GIMarshallingTests.Flags.VALUE2)
1549
1550     def test_value_nick_and_name(self):
1551         self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_nick, 'value1')
1552         self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_nick, 'value2')
1553         self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_nick, 'value3')
1554
1555         self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE1')
1556         self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE2')
1557         self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE3')
1558
1559     def test_flags_in(self):
1560         GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2)
1561         # result of __or__() operation should still be valid instance, not an int.
1562         GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE2)
1563         GIMarshallingTests.flags_in_zero(Number(0))
1564
1565         self.assertRaises(TypeError, GIMarshallingTests.flags_in, 1 << 1)
1566         self.assertRaises(TypeError, GIMarshallingTests.flags_in, 'GIMarshallingTests.Flags.VALUE2')
1567
1568     def test_flags_return(self):
1569         flags = GIMarshallingTests.flags_returnv()
1570         self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1571         self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1572
1573     def test_flags_out(self):
1574         flags = GIMarshallingTests.flags_out()
1575         self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1576         self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1577
1578     def test_flags_inout(self):
1579         flags = GIMarshallingTests.flags_inout(GIMarshallingTests.Flags.VALUE2)
1580         self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1581         self.assertEqual(flags, GIMarshallingTests.Flags.VALUE1)
1582
1583
1584 class TestNoTypeFlags(unittest.TestCase):
1585
1586     def test_flags(self):
1587         self.assertTrue(issubclass(GIMarshallingTests.NoTypeFlags, GObject.GFlags))
1588         self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1, GIMarshallingTests.NoTypeFlags))
1589         self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE2, GIMarshallingTests.NoTypeFlags))
1590         self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE3, GIMarshallingTests.NoTypeFlags))
1591         # __or__() operation should still return an instance, not an int.
1592         self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1 | GIMarshallingTests.NoTypeFlags.VALUE2,
1593                                    GIMarshallingTests.NoTypeFlags))
1594         self.assertEqual(1 << 1, GIMarshallingTests.NoTypeFlags.VALUE2)
1595
1596     def test_value_nick_and_name(self):
1597         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_nick, 'value1')
1598         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_nick, 'value2')
1599         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_nick, 'value3')
1600
1601         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE1')
1602         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2')
1603         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE3')
1604
1605     def test_flags_in(self):
1606         GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2)
1607         GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2 | GIMarshallingTests.NoTypeFlags.VALUE2)
1608         GIMarshallingTests.no_type_flags_in_zero(Number(0))
1609
1610         self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 1 << 1)
1611         self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 'GIMarshallingTests.NoTypeFlags.VALUE2')
1612
1613     def test_flags_return(self):
1614         flags = GIMarshallingTests.no_type_flags_returnv()
1615         self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1616         self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1617
1618     def test_flags_out(self):
1619         flags = GIMarshallingTests.no_type_flags_out()
1620         self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1621         self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1622
1623     def test_flags_inout(self):
1624         flags = GIMarshallingTests.no_type_flags_inout(GIMarshallingTests.NoTypeFlags.VALUE2)
1625         self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1626         self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE1)
1627
1628     def test_flags_gtype_name_is_namespaced(self):
1629         self.assertEqual(GIMarshallingTests.NoTypeFlags.__gtype__.name,
1630                          'PyGIMarshallingTestsNoTypeFlags')
1631
1632     def test_flags_double_registration_error(self):
1633         # a warning is printed for double registration and pygobject will
1634         # also raise a RuntimeError.
1635         old_mask = GLib.log_set_always_fatal(GLib.LogLevelFlags.LEVEL_ERROR)
1636         try:
1637             self.assertRaises(RuntimeError,
1638                               gi._gi.flags_register_new_gtype_and_add,
1639                               GIMarshallingTests.NoTypeFlags.__info__)
1640         finally:
1641             GLib.log_set_always_fatal(old_mask)
1642
1643
1644 class TestStructure(unittest.TestCase):
1645
1646     def test_simple_struct(self):
1647         self.assertTrue(issubclass(GIMarshallingTests.SimpleStruct, GObject.GPointer))
1648
1649         struct = GIMarshallingTests.SimpleStruct()
1650         self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1651
1652         self.assertEqual(0, struct.long_)
1653         self.assertEqual(0, struct.int8)
1654
1655         struct.long_ = 6
1656         struct.int8 = 7
1657
1658         self.assertEqual(6, struct.long_)
1659         self.assertEqual(7, struct.int8)
1660
1661         del struct
1662
1663     def test_nested_struct(self):
1664         struct = GIMarshallingTests.NestedStruct()
1665
1666         self.assertTrue(isinstance(struct.simple_struct, GIMarshallingTests.SimpleStruct))
1667
1668         struct.simple_struct.long_ = 42
1669         self.assertEqual(42, struct.simple_struct.long_)
1670
1671         del struct
1672
1673     def test_not_simple_struct(self):
1674         struct = GIMarshallingTests.NotSimpleStruct()
1675         self.assertEqual(None, struct.pointer)
1676
1677     def test_simple_struct_return(self):
1678         struct = GIMarshallingTests.simple_struct_returnv()
1679
1680         self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1681         self.assertEqual(6, struct.long_)
1682         self.assertEqual(7, struct.int8)
1683
1684         del struct
1685
1686     def test_simple_struct_in(self):
1687         struct = GIMarshallingTests.SimpleStruct()
1688         struct.long_ = 6
1689         struct.int8 = 7
1690
1691         GIMarshallingTests.SimpleStruct.inv(struct)
1692
1693         del struct
1694
1695         struct = GIMarshallingTests.NestedStruct()
1696
1697         self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, struct)
1698
1699         del struct
1700
1701         self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, None)
1702
1703     def test_simple_struct_method(self):
1704         struct = GIMarshallingTests.SimpleStruct()
1705         struct.long_ = 6
1706         struct.int8 = 7
1707
1708         struct.method()
1709
1710         del struct
1711
1712         self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.method)
1713
1714     def test_pointer_struct(self):
1715         self.assertTrue(issubclass(GIMarshallingTests.PointerStruct, GObject.GPointer))
1716
1717         struct = GIMarshallingTests.PointerStruct()
1718         self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1719
1720         del struct
1721
1722     def test_pointer_struct_return(self):
1723         struct = GIMarshallingTests.pointer_struct_returnv()
1724
1725         self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1726         self.assertEqual(42, struct.long_)
1727
1728         del struct
1729
1730     def test_pointer_struct_in(self):
1731         struct = GIMarshallingTests.PointerStruct()
1732         struct.long_ = 42
1733
1734         struct.inv()
1735
1736         del struct
1737
1738     @unittest.skipUnless(hasattr(GIMarshallingTests.BoxedStruct, 'string_'),
1739                          'too old gobject-introspection')
1740     def test_boxed_struct(self):
1741         self.assertTrue(issubclass(GIMarshallingTests.BoxedStruct, GObject.GBoxed))
1742
1743         struct = GIMarshallingTests.BoxedStruct()
1744         self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1745
1746         self.assertEqual(0, struct.long_)
1747         self.assertEqual(None, struct.string_)
1748         self.assertEqual([], struct.g_strv)
1749
1750         del struct
1751
1752     @unittest.skipUnless(hasattr(GIMarshallingTests.BoxedStruct, 'string_'),
1753                          'too old gobject-introspection')
1754     def test_boxed_struct_new(self):
1755         struct = GIMarshallingTests.BoxedStruct.new()
1756         self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1757         self.assertEqual(struct.long_, 0)
1758         self.assertEqual(struct.string_, None)
1759
1760         del struct
1761
1762     @unittest.skipUnless(hasattr(GIMarshallingTests.BoxedStruct, 'string_'),
1763                          'too old gobject-introspection')
1764     def test_boxed_struct_copy(self):
1765         struct = GIMarshallingTests.BoxedStruct()
1766         struct.long_ = 42
1767         struct.string_ = 'hello'
1768
1769         new_struct = struct.copy()
1770         self.assertTrue(isinstance(new_struct, GIMarshallingTests.BoxedStruct))
1771         self.assertEqual(new_struct.long_, 42)
1772         self.assertEqual(new_struct.string_, 'hello')
1773
1774         del new_struct
1775         del struct
1776
1777     @unittest.skipUnless(hasattr(GIMarshallingTests.BoxedStruct, 'string_'),
1778                          'too old gobject-introspection')
1779     def test_boxed_struct_return(self):
1780         struct = GIMarshallingTests.boxed_struct_returnv()
1781
1782         self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1783         self.assertEqual(42, struct.long_)
1784         self.assertEqual('hello', struct.string_)
1785         self.assertEqual(['0', '1', '2'], struct.g_strv)
1786
1787         del struct
1788
1789     def test_boxed_struct_in(self):
1790         struct = GIMarshallingTests.BoxedStruct()
1791         struct.long_ = 42
1792
1793         struct.inv()
1794
1795         del struct
1796
1797     def test_boxed_struct_out(self):
1798         struct = GIMarshallingTests.boxed_struct_out()
1799
1800         self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1801         self.assertEqual(42, struct.long_)
1802
1803         del struct
1804
1805     def test_boxed_struct_inout(self):
1806         in_struct = GIMarshallingTests.BoxedStruct()
1807         in_struct.long_ = 42
1808
1809         out_struct = GIMarshallingTests.boxed_struct_inout(in_struct)
1810
1811         self.assertTrue(isinstance(out_struct, GIMarshallingTests.BoxedStruct))
1812         self.assertEqual(0, out_struct.long_)
1813
1814         del in_struct
1815         del out_struct
1816
1817     def test_struct_field_assignment(self):
1818         struct = GIMarshallingTests.BoxedStruct()
1819
1820         struct.long_ = 42
1821         struct.string_ = 'hello'
1822         self.assertEqual(struct.long_, 42)
1823         self.assertEqual(struct.string_, 'hello')
1824
1825     def test_union(self):
1826         union = GIMarshallingTests.Union()
1827
1828         self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1829
1830         new_union = union.copy()
1831         self.assertTrue(isinstance(new_union, GIMarshallingTests.Union))
1832
1833         del union
1834         del new_union
1835
1836     def test_union_return(self):
1837         union = GIMarshallingTests.union_returnv()
1838
1839         self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1840         self.assertEqual(42, union.long_)
1841
1842         del union
1843
1844     def test_union_in(self):
1845         union = GIMarshallingTests.Union()
1846         union.long_ = 42
1847
1848         union.inv()
1849
1850         del union
1851
1852     def test_union_method(self):
1853         union = GIMarshallingTests.Union()
1854         union.long_ = 42
1855
1856         union.method()
1857
1858         del union
1859
1860         self.assertRaises(TypeError, GIMarshallingTests.Union.method)
1861
1862
1863 class TestGObject(unittest.TestCase):
1864
1865     def test_object(self):
1866         self.assertTrue(issubclass(GIMarshallingTests.Object, GObject.GObject))
1867
1868         object_ = GIMarshallingTests.Object()
1869         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1870         self.assertEqual(object_.__grefcount__, 1)
1871
1872     def test_object_new(self):
1873         object_ = GIMarshallingTests.Object.new(42)
1874         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1875         self.assertEqual(object_.__grefcount__, 1)
1876
1877     def test_object_int(self):
1878         object_ = GIMarshallingTests.Object(int=42)
1879         self.assertEqual(object_.int_, 42)
1880 # FIXME: Don't work yet.
1881 #        object_.int_ = 0
1882 #        self.assertEqual(object_.int_, 0)
1883
1884     def test_object_static_method(self):
1885         GIMarshallingTests.Object.static_method()
1886
1887     def test_object_method(self):
1888         GIMarshallingTests.Object(int=42).method()
1889         self.assertRaises(TypeError, GIMarshallingTests.Object.method, GObject.GObject())
1890         self.assertRaises(TypeError, GIMarshallingTests.Object.method)
1891
1892     def test_sub_object(self):
1893         self.assertTrue(issubclass(GIMarshallingTests.SubObject, GIMarshallingTests.Object))
1894
1895         object_ = GIMarshallingTests.SubObject()
1896         self.assertTrue(isinstance(object_, GIMarshallingTests.SubObject))
1897
1898     def test_sub_object_new(self):
1899         self.assertRaises(TypeError, GIMarshallingTests.SubObject.new, 42)
1900
1901     def test_sub_object_static_method(self):
1902         object_ = GIMarshallingTests.SubObject()
1903         object_.static_method()
1904
1905     def test_sub_object_method(self):
1906         object_ = GIMarshallingTests.SubObject(int=42)
1907         object_.method()
1908
1909     def test_sub_object_sub_method(self):
1910         object_ = GIMarshallingTests.SubObject()
1911         object_.sub_method()
1912
1913     def test_sub_object_overwritten_method(self):
1914         object_ = GIMarshallingTests.SubObject()
1915         object_.overwritten_method()
1916
1917         self.assertRaises(TypeError, GIMarshallingTests.SubObject.overwritten_method, GIMarshallingTests.Object())
1918
1919     def test_sub_object_int(self):
1920         object_ = GIMarshallingTests.SubObject()
1921         self.assertEqual(object_.int_, 0)
1922 # FIXME: Don't work yet.
1923 #        object_.int_ = 42
1924 #        self.assertEqual(object_.int_, 42)
1925
1926     def test_object_none_return(self):
1927         object_ = GIMarshallingTests.Object.none_return()
1928         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1929         self.assertEqual(object_.__grefcount__, 2)
1930
1931     def test_object_full_return(self):
1932         object_ = GIMarshallingTests.Object.full_return()
1933         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1934         self.assertEqual(object_.__grefcount__, 1)
1935
1936     def test_object_none_in(self):
1937         object_ = GIMarshallingTests.Object(int=42)
1938         GIMarshallingTests.Object.none_in(object_)
1939         self.assertEqual(object_.__grefcount__, 1)
1940
1941         object_ = GIMarshallingTests.SubObject(int=42)
1942         GIMarshallingTests.Object.none_in(object_)
1943
1944         object_ = GObject.GObject()
1945         self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, object_)
1946
1947         self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, None)
1948
1949     def test_object_none_out(self):
1950         object_ = GIMarshallingTests.Object.none_out()
1951         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1952         self.assertEqual(object_.__grefcount__, 2)
1953
1954         new_object = GIMarshallingTests.Object.none_out()
1955         self.assertTrue(new_object is object_)
1956
1957     def test_object_full_out(self):
1958         object_ = GIMarshallingTests.Object.full_out()
1959         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1960         self.assertEqual(object_.__grefcount__, 1)
1961
1962     def test_object_none_inout(self):
1963         object_ = GIMarshallingTests.Object(int=42)
1964         new_object = GIMarshallingTests.Object.none_inout(object_)
1965
1966         self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1967
1968         self.assertFalse(object_ is new_object)
1969
1970         self.assertEqual(object_.__grefcount__, 1)
1971         self.assertEqual(new_object.__grefcount__, 2)
1972
1973         new_new_object = GIMarshallingTests.Object.none_inout(object_)
1974         self.assertTrue(new_new_object is new_object)
1975
1976         GIMarshallingTests.Object.none_inout(GIMarshallingTests.SubObject(int=42))
1977
1978     def test_object_full_inout(self):
1979         object_ = GIMarshallingTests.Object(int=42)
1980         new_object = GIMarshallingTests.Object.full_inout(object_)
1981
1982         self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1983
1984         self.assertFalse(object_ is new_object)
1985
1986         self.assertEqual(object_.__grefcount__, 2)
1987         self.assertEqual(new_object.__grefcount__, 1)
1988
1989 # FIXME: Doesn't actually return the same object.
1990 #    def test_object_inout_same(self):
1991 #        object_ = GIMarshallingTests.Object()
1992 #        new_object = GIMarshallingTests.object_full_inout(object_)
1993 #        self.assertTrue(object_ is new_object)
1994 #        self.assertEqual(object_.__grefcount__, 1)
1995
1996
1997 class TestPythonGObject(unittest.TestCase):
1998
1999     class Object(GIMarshallingTests.Object):
2000         return_for_caller_allocated_out_parameter = 'test caller alloc return'
2001
2002         def __init__(self, int):
2003             GIMarshallingTests.Object.__init__(self)
2004             self.val = None
2005
2006         def method(self):
2007             # Don't call super, which asserts that self.int == 42.
2008             pass
2009
2010         def do_method_int8_in(self, int8):
2011             self.val = int8
2012
2013         def do_method_int8_out(self):
2014             return 42
2015
2016         def do_method_int8_arg_and_out_caller(self, arg):
2017             return arg + 1
2018
2019         def do_method_int8_arg_and_out_callee(self, arg):
2020             return arg + 1
2021
2022         def do_method_str_arg_out_ret(self, arg):
2023             return (arg.upper(), len(arg))
2024
2025         def do_method_with_default_implementation(self, int8):
2026             GIMarshallingTests.Object.do_method_with_default_implementation(self, int8)
2027             self.props.int += int8
2028
2029         def do_vfunc_return_value_only(self):
2030             return 4242
2031
2032         def do_vfunc_one_out_parameter(self):
2033             return 42.42
2034
2035         def do_vfunc_multiple_out_parameters(self):
2036             return (42.42, 3.14)
2037
2038         def do_vfunc_return_value_and_one_out_parameter(self):
2039             return (5, 42)
2040
2041         def do_vfunc_return_value_and_multiple_out_parameters(self):
2042             return (5, 42, 99)
2043
2044         def do_vfunc_caller_allocated_out_parameter(self):
2045             return self.return_for_caller_allocated_out_parameter
2046
2047     class SubObject(GIMarshallingTests.SubObject):
2048         def __init__(self, int):
2049             GIMarshallingTests.SubObject.__init__(self)
2050             self.val = None
2051
2052         def do_method_with_default_implementation(self, int8):
2053             self.val = int8
2054
2055     class Interface3Impl(GObject.Object, GIMarshallingTests.Interface3):
2056         def __init__(self):
2057             GObject.Object.__init__(self)
2058             self.variants = None
2059             self.n_variants = None
2060
2061         def do_test_variant_array_in(self, variants, n_variants):
2062             self.variants = variants
2063             self.n_variants = n_variants
2064
2065     def test_object(self):
2066         self.assertTrue(issubclass(self.Object, GIMarshallingTests.Object))
2067
2068         object_ = self.Object(int=42)
2069         self.assertTrue(isinstance(object_, self.Object))
2070
2071     def test_object_method(self):
2072         self.Object(int=0).method()
2073
2074     def test_object_vfuncs(self):
2075         object_ = self.Object(int=42)
2076         object_.method_int8_in(84)
2077         self.assertEqual(object_.val, 84)
2078         self.assertEqual(object_.method_int8_out(), 42)
2079
2080         # can be dropped when bumping g-i dependencies to >= 1.35.2
2081         if hasattr(object_, 'method_int8_arg_and_out_caller'):
2082             self.assertEqual(object_.method_int8_arg_and_out_caller(42), 43)
2083             self.assertEqual(object_.method_int8_arg_and_out_callee(42), 43)
2084             self.assertEqual(object_.method_str_arg_out_ret('hello'), ('HELLO', 5))
2085
2086         object_.method_with_default_implementation(42)
2087         self.assertEqual(object_.props.int, 84)
2088
2089         self.assertEqual(object_.vfunc_return_value_only(), 4242)
2090         self.assertAlmostEqual(object_.vfunc_one_out_parameter(), 42.42, places=5)
2091
2092         (a, b) = object_.vfunc_multiple_out_parameters()
2093         self.assertAlmostEqual(a, 42.42, places=5)
2094         self.assertAlmostEqual(b, 3.14, places=5)
2095
2096         self.assertEqual(object_.vfunc_return_value_and_one_out_parameter(), (5, 42))
2097         self.assertEqual(object_.vfunc_return_value_and_multiple_out_parameters(), (5, 42, 99))
2098
2099         self.assertEqual(object_.vfunc_caller_allocated_out_parameter(),
2100                          object_.return_for_caller_allocated_out_parameter)
2101
2102         class ObjectWithoutVFunc(GIMarshallingTests.Object):
2103             def __init__(self, int):
2104                 GIMarshallingTests.Object.__init__(self)
2105
2106         object_ = ObjectWithoutVFunc(int=42)
2107         object_.method_with_default_implementation(84)
2108         self.assertEqual(object_.props.int, 84)
2109
2110     def test_vfunc_return_ref_count(self):
2111         obj = self.Object(int=42)
2112         ref_count = sys.getrefcount(obj.return_for_caller_allocated_out_parameter)
2113         ret = obj.vfunc_caller_allocated_out_parameter()
2114         gc.collect()
2115
2116         # Make sure the return and what the vfunc returned
2117         # are equal but not the same object.
2118         self.assertEqual(ret, obj.return_for_caller_allocated_out_parameter)
2119         self.assertFalse(ret is obj.return_for_caller_allocated_out_parameter)
2120         self.assertEqual(sys.getrefcount(obj.return_for_caller_allocated_out_parameter),
2121                          ref_count)
2122
2123     def test_subobject_parent_vfunc(self):
2124         object_ = self.SubObject(int=81)
2125         object_.method_with_default_implementation(87)
2126         self.assertEqual(object_.val, 87)
2127
2128     def test_dynamic_module(self):
2129         from gi.module import DynamicModule
2130         self.assertTrue(isinstance(GObject, DynamicModule))
2131
2132     def test_subobject_non_vfunc_do_method(self):
2133         class PythonObjectWithNonVFuncDoMethod:
2134             def do_not_a_vfunc(self):
2135                 return 5
2136
2137         class ObjectOverrideNonVFuncDoMethod(GIMarshallingTests.Object, PythonObjectWithNonVFuncDoMethod):
2138             def do_not_a_vfunc(self):
2139                 value = super(ObjectOverrideNonVFuncDoMethod, self).do_not_a_vfunc()
2140                 return 13 + value
2141
2142         object_ = ObjectOverrideNonVFuncDoMethod()
2143         self.assertEqual(18, object_.do_not_a_vfunc())
2144
2145     def test_native_function_not_set_in_subclass_dict(self):
2146         # Previously, GI was setting virtual functions on the class as well
2147         # as any *native* class that subclasses it. Here we check that it is only
2148         # set on the class that the method is originally from.
2149         self.assertTrue('do_method_with_default_implementation' in GIMarshallingTests.Object.__dict__)
2150         self.assertTrue('do_method_with_default_implementation' not in GIMarshallingTests.SubObject.__dict__)
2151
2152     def test_subobject_with_interface_and_non_vfunc_do_method(self):
2153         # There was a bug for searching for vfuncs in interfaces. It was
2154         # triggered by having a do_* method that wasn't overriding
2155         # a native vfunc, as well as inheriting from an interface.
2156         class GObjectSubclassWithInterface(GObject.GObject, GIMarshallingTests.Interface):
2157             def do_method_not_a_vfunc(self):
2158                 pass
2159
2160     def test_subsubobject(self):
2161         class SubSubSubObject(GIMarshallingTests.SubSubObject):
2162             def do_method_deep_hierarchy(self, num):
2163                 self.props.int = num * 2
2164
2165         sub_sub_sub_object = SubSubSubObject()
2166         GIMarshallingTests.SubSubObject.do_method_deep_hierarchy(sub_sub_sub_object, 5)
2167         self.assertEqual(sub_sub_sub_object.props.int, 5)
2168
2169     def test_interface3impl(self):
2170         iface3 = self.Interface3Impl()
2171         variants = [GLib.Variant('i', 27), GLib.Variant('s', 'Hello')]
2172         iface3.test_variant_array_in(variants)
2173         self.assertEqual(iface3.n_variants, 2)
2174         self.assertEqual(iface3.variants[0].unpack(), 27)
2175         self.assertEqual(iface3.variants[1].unpack(), 'Hello')
2176
2177     def test_python_subsubobject_vfunc(self):
2178         class PySubObject(GIMarshallingTests.Object):
2179             def __init__(self):
2180                 GIMarshallingTests.Object.__init__(self)
2181                 self.sub_method_int8_called = 0
2182
2183             def do_method_int8_in(self, int8):
2184                 self.sub_method_int8_called += 1
2185
2186         class PySubSubObject(PySubObject):
2187             def __init__(self):
2188                 PySubObject.__init__(self)
2189                 self.subsub_method_int8_called = 0
2190
2191             def do_method_int8_in(self, int8):
2192                 self.subsub_method_int8_called += 1
2193
2194         so = PySubObject()
2195         so.method_int8_in(1)
2196         self.assertEqual(so.sub_method_int8_called, 1)
2197
2198         # it should call the method on the SubSub object only
2199         sso = PySubSubObject()
2200         sso.method_int8_in(1)
2201         self.assertEqual(sso.subsub_method_int8_called, 1)
2202         self.assertEqual(sso.sub_method_int8_called, 0)
2203
2204     def test_callback_in_vfunc(self):
2205         class SubObject(GIMarshallingTests.Object):
2206             def __init__(self):
2207                 GObject.GObject.__init__(self)
2208                 self.worked = False
2209
2210             def do_vfunc_with_callback(self, callback):
2211                 self.worked = callback(42) == 42
2212
2213         _object = SubObject()
2214         _object.call_vfunc_with_callback()
2215         self.assertTrue(_object.worked)
2216         _object.worked = False
2217         _object.call_vfunc_with_callback()
2218         self.assertTrue(_object.worked)
2219
2220
2221 class TestMultiOutputArgs(unittest.TestCase):
2222
2223     def test_int_out_out(self):
2224         self.assertEqual((6, 7), GIMarshallingTests.int_out_out())
2225
2226     def test_int_return_out(self):
2227         self.assertEqual((6, 7), GIMarshallingTests.int_return_out())
2228
2229
2230 class TestGErrorException(unittest.TestCase):
2231     def test_gerror_exception(self):
2232         self.assertRaises(GObject.GError, GIMarshallingTests.gerror)
2233         try:
2234             GIMarshallingTests.gerror()
2235         except Exception:
2236             etype, e = sys.exc_info()[:2]
2237             self.assertEqual(e.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2238             self.assertEqual(e.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2239             self.assertEqual(e.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2240
2241
2242 # Interface
2243
2244
2245 class TestInterfaces(unittest.TestCase):
2246
2247     class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2248         def __init__(self):
2249             GObject.GObject.__init__(self)
2250             self.val = None
2251
2252         def do_test_int8_in(self, int8):
2253             self.val = int8
2254
2255     def setUp(self):
2256         self.instance = self.TestInterfaceImpl()
2257
2258     def test_wrapper(self):
2259         self.assertTrue(issubclass(GIMarshallingTests.Interface, GObject.GInterface))
2260         self.assertEqual(GIMarshallingTests.Interface.__gtype__.name, 'GIMarshallingTestsInterface')
2261         self.assertRaises(NotImplementedError, GIMarshallingTests.Interface)
2262
2263     def test_implementation(self):
2264         self.assertTrue(issubclass(self.TestInterfaceImpl, GIMarshallingTests.Interface))
2265         self.assertTrue(isinstance(self.instance, GIMarshallingTests.Interface))
2266
2267     def test_int8_int(self):
2268         GIMarshallingTests.test_interface_test_int8_in(self.instance, 42)
2269         self.assertEqual(self.instance.val, 42)
2270
2271     def test_subclass(self):
2272         class TestInterfaceImplA(self.TestInterfaceImpl):
2273             pass
2274
2275         class TestInterfaceImplB(TestInterfaceImplA):
2276             pass
2277
2278         instance = TestInterfaceImplA()
2279         GIMarshallingTests.test_interface_test_int8_in(instance, 42)
2280         self.assertEqual(instance.val, 42)
2281
2282     def test_mro(self):
2283         # there was a problem with Python bailing out because of
2284         # http://en.wikipedia.org/wiki/Diamond_problem with interfaces,
2285         # which shouldn't really be a problem.
2286
2287         class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2288             pass
2289
2290         class TestInterfaceImpl2(GIMarshallingTests.Interface,
2291                                  TestInterfaceImpl):
2292             pass
2293
2294         class TestInterfaceImpl3(self.TestInterfaceImpl,
2295                                  GIMarshallingTests.Interface2):
2296             pass
2297
2298     def test_type_mismatch(self):
2299         obj = GIMarshallingTests.Object()
2300
2301         # wrong type for first argument: interface
2302         enum = Gio.File.new_for_path('.').enumerate_children(
2303             '', Gio.FileQueryInfoFlags.NONE, None)
2304         try:
2305             enum.next_file(obj)
2306             self.fail('call with wrong type argument unexpectedly succeeded')
2307         except TypeError as e:
2308             # should have argument name
2309             self.assertTrue('cancellable' in str(e), e)
2310             # should have expected type
2311             self.assertTrue('xpected Gio.Cancellable' in str(e), e)
2312             # should have actual type
2313             self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2314
2315         # wrong type for self argument: interface
2316         try:
2317             Gio.FileEnumerator.next_file(obj, None)
2318             self.fail('call with wrong type argument unexpectedly succeeded')
2319         except TypeError as e:
2320             if sys.version_info < (3, 0):
2321                 self.assertTrue('FileEnumerator' in str(e), e)
2322                 self.assertTrue('Object' in str(e), e)
2323             else:
2324                 # should have argument name
2325                 self.assertTrue('self' in str(e), e)
2326                 # should have expected type
2327                 self.assertTrue('xpected Gio.FileEnumerator' in str(e), e)
2328                 # should have actual type
2329                 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2330
2331         # wrong type for first argument: GObject
2332         var = GLib.Variant('s', 'mystring')
2333         action = Gio.SimpleAction.new('foo', var.get_type())
2334         try:
2335             action.activate(obj)
2336             self.fail('call with wrong type argument unexpectedly succeeded')
2337         except TypeError as e:
2338             # should have argument name
2339             self.assertTrue('parameter' in str(e), e)
2340             # should have expected type
2341             self.assertTrue('xpected GLib.Variant' in str(e), e)
2342             # should have actual type
2343             self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2344
2345         # wrong type for self argument: GObject
2346         try:
2347             Gio.SimpleAction.activate(obj, obj)
2348             self.fail('call with wrong type argument unexpectedly succeeded')
2349         except TypeError as e:
2350             if sys.version_info < (3, 0):
2351                 self.assertTrue('SimpleAction' in str(e), e)
2352                 self.assertTrue('Object' in str(e), e)
2353             else:
2354                 # should have argument name
2355                 self.assertTrue('self' in str(e), e)
2356                 # should have expected type
2357                 self.assertTrue('xpected Gio.Action' in str(e), e)
2358                 # should have actual type
2359                 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2360
2361
2362 class TestInterfaceClash(unittest.TestCase):
2363
2364     def test_clash(self):
2365         def create_clash():
2366             class TestClash(GObject.GObject, GIMarshallingTests.Interface, GIMarshallingTests.Interface2):
2367                 def do_test_int8_in(self, int8):
2368                     pass
2369             TestClash()
2370
2371         self.assertRaises(TypeError, create_clash)
2372
2373
2374 class TestOverrides(unittest.TestCase):
2375
2376     def test_constant(self):
2377         self.assertEqual(GIMarshallingTests.OVERRIDES_CONSTANT, 7)
2378
2379     def test_struct(self):
2380         # Test that the constructor has been overridden.
2381         struct = GIMarshallingTests.OverridesStruct(42)
2382
2383         self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2384
2385         # Test that the method has been overridden.
2386         self.assertEqual(6, struct.method())
2387
2388         del struct
2389
2390         # Test that the overrides wrapper has been registered.
2391         struct = GIMarshallingTests.overrides_struct_returnv()
2392
2393         self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2394
2395         del struct
2396
2397     def test_object(self):
2398         # Test that the constructor has been overridden.
2399         object_ = GIMarshallingTests.OverridesObject(42)
2400
2401         self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2402
2403         # Test that the alternate constructor has been overridden.
2404         object_ = GIMarshallingTests.OverridesObject.new(42)
2405
2406         self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2407
2408         # Test that the method has been overridden.
2409         self.assertEqual(6, object_.method())
2410
2411         # Test that the overrides wrapper has been registered.
2412         object_ = GIMarshallingTests.OverridesObject.returnv()
2413
2414         self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2415
2416     def test_module_name(self):
2417         # overridden types
2418         self.assertEqual(GIMarshallingTests.OverridesStruct.__module__, 'gi.overrides.GIMarshallingTests')
2419         self.assertEqual(GIMarshallingTests.OverridesObject.__module__, 'gi.overrides.GIMarshallingTests')
2420         self.assertEqual(GObject.Object.__module__, 'gi.overrides.GObject')
2421
2422         # not overridden
2423         self.assertEqual(GIMarshallingTests.SubObject.__module__, 'gi.repository.GIMarshallingTests')
2424         # FIXME: does not work with TEST_NAMES='test_thread test_gi.TestOverrides',
2425         # it is importlib._bootstrap then
2426         #self.assertEqual(GObject.InitiallyUnowned.__module__, 'gi.repository.GObject')
2427
2428
2429 class TestDir(unittest.TestCase):
2430     def test_members_list(self):
2431         list = dir(GIMarshallingTests)
2432         self.assertTrue('OverridesStruct' in list)
2433         self.assertTrue('BoxedStruct' in list)
2434         self.assertTrue('OVERRIDES_CONSTANT' in list)
2435         self.assertTrue('GEnum' in list)
2436         self.assertTrue('int32_return_max' in list)
2437
2438     def test_modules_list(self):
2439         import gi.repository
2440         list = dir(gi.repository)
2441         self.assertTrue('GIMarshallingTests' in list)
2442
2443         # FIXME: test to see if a module which was not imported is in the list
2444         #        we should be listing every typelib we find, not just the ones
2445         #        which are imported
2446         #
2447         #        to test this I recommend we compile a fake module which
2448         #        our tests would never import and check to see if it is
2449         #        in the list:
2450         #
2451         # self.assertTrue('DoNotImportDummyTests' in list)
2452
2453
2454 class TestGErrorArrayInCrash(unittest.TestCase):
2455     # Previously there was a bug in invoke, in which C arrays were unwrapped
2456     # from inside GArrays to be passed to the C function. But when a GError was
2457     # set, invoke would attempt to free the C array as if it were a GArray.
2458     # This crash is only for C arrays. It does not happen for C functions which
2459     # take in GArrays. See https://bugzilla.gnome.org/show_bug.cgi?id=642708
2460     def test_gerror_array_in_crash(self):
2461         self.assertRaises(GObject.GError, GIMarshallingTests.gerror_array_in, [1, 2, 3])
2462
2463
2464 class TestGErrorOut(unittest.TestCase):
2465     # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2466     def test_gerror_out(self):
2467         error, debug = GIMarshallingTests.gerror_out()
2468
2469         self.assertIsInstance(error, GObject.GError)
2470         self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2471         self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2472         self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2473         self.assertEqual(debug, GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE)
2474
2475
2476 class TestGErrorOutTransferNone(unittest.TestCase):
2477     # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2478     def test_gerror_out_transfer_none(self):
2479         error, debug = GIMarshallingTests.gerror_out_transfer_none()
2480
2481         self.assertIsInstance(error, GObject.GError)
2482         self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2483         self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2484         self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2485         self.assertEqual(GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE, debug)
2486
2487
2488 class TestGErrorReturn(unittest.TestCase):
2489     # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2490     def test_return_gerror(self):
2491         error = GIMarshallingTests.gerror_return()
2492
2493         self.assertIsInstance(error, GObject.GError)
2494         self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2495         self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2496         self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2497
2498
2499 class TestParamSpec(unittest.TestCase):
2500     # https://bugzilla.gnome.org/show_bug.cgi?id=682355
2501     @unittest.skipUnless(hasattr(GIMarshallingTests, 'param_spec_in_bool'),
2502                          'too old gobject-introspection')
2503     @unittest.expectedFailure
2504     def test_param_spec_in_bool(self):
2505         ps = GObject.param_spec_boolean('mybool', 'test-bool', 'boolblurb',
2506                                         True, GObject.ParamFlags.READABLE)
2507         GIMarshallingTests.param_spec_in_bool(ps)
2508
2509     def test_param_spec_return(self):
2510         obj = GIMarshallingTests.param_spec_return()
2511         self.assertEqual(obj.name, 'test-param')
2512         self.assertEqual(obj.nick, 'test')
2513         self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2514
2515     def test_param_spec_out(self):
2516         obj = GIMarshallingTests.param_spec_out()
2517         self.assertEqual(obj.name, 'test-param')
2518         self.assertEqual(obj.nick, 'test')
2519         self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2520
2521
2522 class TestKeywordArgs(unittest.TestCase):
2523
2524     def test_calling(self):
2525         kw_func = GIMarshallingTests.int_three_in_three_out
2526
2527         self.assertEqual(kw_func(1, 2, 3), (1, 2, 3))
2528         self.assertEqual(kw_func(**{'a': 4, 'b': 5, 'c': 6}), (4, 5, 6))
2529         self.assertEqual(kw_func(1, **{'b': 7, 'c': 8}), (1, 7, 8))
2530         self.assertEqual(kw_func(1, 7, **{'c': 8}), (1, 7, 8))
2531         self.assertEqual(kw_func(1, c=8, **{'b': 7}), (1, 7, 8))
2532         self.assertEqual(kw_func(2, c=4, b=3), (2, 3, 4))
2533         self.assertEqual(kw_func(a=2, c=4, b=3), (2, 3, 4))
2534
2535     def assertRaisesMessage(self, exception, message, func, *args, **kwargs):
2536         try:
2537             func(*args, **kwargs)
2538         except exception:
2539             (e_type, e) = sys.exc_info()[:2]
2540             if message is not None:
2541                 self.assertEqual(str(e), message)
2542         except:
2543             raise
2544         else:
2545             msg = "%s() did not raise %s" % (func.__name__, exception.__name__)
2546             raise AssertionError(msg)
2547
2548     def test_type_errors(self):
2549         # test too few args
2550         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2551                                  GIMarshallingTests.int_three_in_three_out)
2552         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (1 given)",
2553                                  GIMarshallingTests.int_three_in_three_out, 1)
2554         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2555                                  GIMarshallingTests.int_three_in_three_out, *())
2556         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2557                                  GIMarshallingTests.int_three_in_three_out, *(), **{})
2558         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (0 given)",
2559                                  GIMarshallingTests.int_three_in_three_out, *(), **{'c': 4})
2560
2561         # test too many args
2562         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (4 given)",
2563                                  GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4))
2564         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (4 given)",
2565                                  GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4), c=6)
2566
2567         # test too many keyword args
2568         self.assertRaisesMessage(TypeError, "int_three_in_three_out() got multiple values for keyword argument 'a'",
2569                                  GIMarshallingTests.int_three_in_three_out, 1, 2, 3, **{'a': 4, 'b': 5})
2570         self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'd'",
2571                                  GIMarshallingTests.int_three_in_three_out, d=4)
2572         self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'e'",
2573                                  GIMarshallingTests.int_three_in_three_out, **{'e': 2})
2574
2575     def test_kwargs_are_not_modified(self):
2576         d = {'b': 2}
2577         d2 = d.copy()
2578         GIMarshallingTests.int_three_in_three_out(1, c=4, **d)
2579         self.assertEqual(d, d2)
2580
2581
2582 class TestPropertiesObject(unittest.TestCase):
2583
2584     def setUp(self):
2585         self.obj = GIMarshallingTests.PropertiesObject()
2586
2587     def test_boolean(self):
2588         self.assertEqual(self.obj.props.some_boolean, False)
2589         self.obj.props.some_boolean = True
2590         self.assertEqual(self.obj.props.some_boolean, True)
2591
2592         obj = GIMarshallingTests.PropertiesObject(some_boolean=True)
2593         self.assertEqual(obj.props.some_boolean, True)
2594
2595     def test_char(self):
2596         self.assertEqual(self.obj.props.some_char, 0)
2597         self.obj.props.some_char = GObject.G_MAXINT8
2598         self.assertEqual(self.obj.props.some_char, GObject.G_MAXINT8)
2599
2600         obj = GIMarshallingTests.PropertiesObject(some_char=-42)
2601         self.assertEqual(obj.props.some_char, -42)
2602
2603     def test_uchar(self):
2604         self.assertEqual(self.obj.props.some_uchar, 0)
2605         self.obj.props.some_uchar = GObject.G_MAXUINT8
2606         self.assertEqual(self.obj.props.some_uchar, GObject.G_MAXUINT8)
2607
2608         obj = GIMarshallingTests.PropertiesObject(some_uchar=42)
2609         self.assertEqual(obj.props.some_uchar, 42)
2610
2611     def test_int(self):
2612         self.assertEqual(self.obj.props.some_int, 0)
2613         self.obj.props.some_int = GObject.G_MAXINT
2614         self.assertEqual(self.obj.props.some_int, GObject.G_MAXINT)
2615
2616         obj = GIMarshallingTests.PropertiesObject(some_int=-42)
2617         self.assertEqual(obj.props.some_int, -42)
2618
2619         self.assertRaises(TypeError, setattr, self.obj.props, 'some_int', 'foo')
2620         self.assertRaises(TypeError, setattr, self.obj.props, 'some_int', None)
2621
2622         self.assertEqual(obj.props.some_int, -42)
2623
2624     def test_uint(self):
2625         self.assertEqual(self.obj.props.some_uint, 0)
2626         self.obj.props.some_uint = GObject.G_MAXUINT
2627         self.assertEqual(self.obj.props.some_uint, GObject.G_MAXUINT)
2628
2629         obj = GIMarshallingTests.PropertiesObject(some_uint=42)
2630         self.assertEqual(obj.props.some_uint, 42)
2631
2632         self.assertRaises(TypeError, setattr, self.obj.props, 'some_uint', 'foo')
2633         self.assertRaises(TypeError, setattr, self.obj.props, 'some_uint', None)
2634
2635         self.assertEqual(obj.props.some_uint, 42)
2636
2637     def test_long(self):
2638         self.assertEqual(self.obj.props.some_long, 0)
2639         self.obj.props.some_long = GObject.G_MAXLONG
2640         self.assertEqual(self.obj.props.some_long, GObject.G_MAXLONG)
2641
2642         obj = GIMarshallingTests.PropertiesObject(some_long=-42)
2643         self.assertEqual(obj.props.some_long, -42)
2644
2645         self.assertRaises(TypeError, setattr, self.obj.props, 'some_long', 'foo')
2646         self.assertRaises(TypeError, setattr, self.obj.props, 'some_long', None)
2647
2648         self.assertEqual(obj.props.some_long, -42)
2649
2650     def test_ulong(self):
2651         self.assertEqual(self.obj.props.some_ulong, 0)
2652         self.obj.props.some_ulong = GObject.G_MAXULONG
2653         self.assertEqual(self.obj.props.some_ulong, GObject.G_MAXULONG)
2654
2655         obj = GIMarshallingTests.PropertiesObject(some_ulong=42)
2656         self.assertEqual(obj.props.some_ulong, 42)
2657
2658         self.assertRaises(TypeError, setattr, self.obj.props, 'some_ulong', 'foo')
2659         self.assertRaises(TypeError, setattr, self.obj.props, 'some_ulong', None)
2660
2661         self.assertEqual(obj.props.some_ulong, 42)
2662
2663     def test_int64(self):
2664         self.assertEqual(self.obj.props.some_int64, 0)
2665         self.obj.props.some_int64 = GObject.G_MAXINT64
2666         self.assertEqual(self.obj.props.some_int64, GObject.G_MAXINT64)
2667
2668         obj = GIMarshallingTests.PropertiesObject(some_int64=-4200000000000000)
2669         self.assertEqual(obj.props.some_int64, -4200000000000000)
2670
2671     def test_uint64(self):
2672         self.assertEqual(self.obj.props.some_uint64, 0)
2673         self.obj.props.some_uint64 = GObject.G_MAXUINT64
2674         self.assertEqual(self.obj.props.some_uint64, GObject.G_MAXUINT64)
2675
2676         obj = GIMarshallingTests.PropertiesObject(some_uint64=4200000000000000)
2677         self.assertEqual(obj.props.some_uint64, 4200000000000000)
2678
2679     def test_float(self):
2680         self.assertEqual(self.obj.props.some_float, 0)
2681         self.obj.props.some_float = GObject.G_MAXFLOAT
2682         self.assertEqual(self.obj.props.some_float, GObject.G_MAXFLOAT)
2683
2684         obj = GIMarshallingTests.PropertiesObject(some_float=42.42)
2685         self.assertAlmostEqual(obj.props.some_float, 42.42, 4)
2686
2687         obj = GIMarshallingTests.PropertiesObject(some_float=42)
2688         self.assertAlmostEqual(obj.props.some_float, 42.0, 4)
2689
2690         self.assertRaises(TypeError, setattr, self.obj.props, 'some_float', 'foo')
2691         self.assertRaises(TypeError, setattr, self.obj.props, 'some_float', None)
2692
2693         self.assertAlmostEqual(obj.props.some_float, 42.0, 4)
2694
2695     def test_double(self):
2696         self.assertEqual(self.obj.props.some_double, 0)
2697         self.obj.props.some_double = GObject.G_MAXDOUBLE
2698         self.assertEqual(self.obj.props.some_double, GObject.G_MAXDOUBLE)
2699
2700         obj = GIMarshallingTests.PropertiesObject(some_double=42.42)
2701         self.assertAlmostEqual(obj.props.some_double, 42.42)
2702
2703         obj = GIMarshallingTests.PropertiesObject(some_double=42)
2704         self.assertAlmostEqual(obj.props.some_double, 42.0)
2705
2706         self.assertRaises(TypeError, setattr, self.obj.props, 'some_double', 'foo')
2707         self.assertRaises(TypeError, setattr, self.obj.props, 'some_double', None)
2708
2709         self.assertAlmostEqual(obj.props.some_double, 42.0)
2710
2711     def test_strv(self):
2712         self.assertEqual(self.obj.props.some_strv, [])
2713         self.obj.props.some_strv = ['hello', 'world']
2714         self.assertEqual(self.obj.props.some_strv, ['hello', 'world'])
2715
2716         self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', 1)
2717         self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', 'foo')
2718         self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', [1, 2])
2719         self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', ['foo', 1])
2720
2721         self.assertEqual(self.obj.props.some_strv, ['hello', 'world'])
2722
2723         obj = GIMarshallingTests.PropertiesObject(some_strv=['hello', 'world'])
2724         self.assertEqual(obj.props.some_strv, ['hello', 'world'])
2725
2726     def test_boxed_struct(self):
2727         self.assertEqual(self.obj.props.some_boxed_struct, None)
2728
2729         class GStrv(list):
2730             __gtype__ = GObject.TYPE_STRV
2731
2732         struct1 = GIMarshallingTests.BoxedStruct()
2733         struct1.long_ = 1
2734
2735         self.obj.props.some_boxed_struct = struct1
2736         self.assertEqual(self.obj.props.some_boxed_struct.long_, 1)
2737         self.assertEqual(self.obj.some_boxed_struct.long_, 1)
2738
2739         self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_struct', 1)
2740         self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_struct', 'foo')
2741
2742         obj = GIMarshallingTests.PropertiesObject(some_boxed_struct=struct1)
2743         self.assertEqual(obj.props.some_boxed_struct.long_, 1)
2744
2745     @unittest.skipUnless(hasattr(GIMarshallingTests.PropertiesObject, 'some_boxed_glist'),
2746                          'too old gobject-introspection')
2747     def test_boxed_glist(self):
2748         self.assertEqual(self.obj.props.some_boxed_glist, [])
2749
2750         l = [GObject.G_MININT, 42, GObject.G_MAXINT]
2751         self.obj.props.some_boxed_glist = l
2752         self.assertEqual(self.obj.props.some_boxed_glist, l)
2753         self.obj.props.some_boxed_glist = []
2754         self.assertEqual(self.obj.props.some_boxed_glist, [])
2755
2756         self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_glist', 1)
2757         self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_glist', 'foo')
2758         self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_glist', ['a'])
2759
2760     @unittest.expectedFailure
2761     def test_boxed_glist_ctor(self):
2762         l = [GObject.G_MININT, 42, GObject.G_MAXINT]
2763         obj = GIMarshallingTests.PropertiesObject(some_boxed_glist=l)
2764         self.assertEqual(obj.props.some_boxed_glist, l)
2765
2766     @unittest.skipUnless(hasattr(GIMarshallingTests.PropertiesObject, 'some_variant'),
2767                          'too old gobject-introspection')
2768     def test_variant(self):
2769         self.assertEqual(self.obj.props.some_variant, None)
2770
2771         self.obj.props.some_variant = GLib.Variant('o', '/myobj')
2772         self.assertEqual(self.obj.props.some_variant.get_type_string(), 'o')
2773         self.assertEqual(self.obj.props.some_variant.print_(False), "'/myobj'")
2774
2775         self.obj.props.some_variant = None
2776         self.assertEqual(self.obj.props.some_variant, None)
2777
2778         obj = GIMarshallingTests.PropertiesObject(some_variant=GLib.Variant('b', True))
2779         self.assertEqual(obj.props.some_variant.get_type_string(), 'b')
2780         self.assertEqual(obj.props.some_variant.get_boolean(), True)
2781
2782         self.assertRaises(TypeError, setattr, self.obj.props, 'some_variant', 'foo')
2783         self.assertRaises(TypeError, setattr, self.obj.props, 'some_variant', 23)
2784
2785         self.assertEqual(obj.props.some_variant.get_type_string(), 'b')
2786         self.assertEqual(obj.props.some_variant.get_boolean(), True)
2787
2788
2789 class TestKeywords(unittest.TestCase):
2790     def test_method(self):
2791         # g_variant_print()
2792         v = GLib.Variant('i', 1)
2793         self.assertEqual(v.print_(False), '1')
2794
2795     def test_function(self):
2796         # g_thread_yield()
2797         self.assertEqual(GLib.Thread.yield_(), None)
2798
2799     def test_struct_method(self):
2800         # g_timer_continue()
2801         # we cannot currently instantiate GLib.Timer objects, so just ensure
2802         # the method exists
2803         self.assertTrue(callable(GLib.Timer.continue_))
2804
2805     def test_uppercase(self):
2806         self.assertEqual(GLib.IOCondition.IN.value_nicks, ['in'])
2807
2808
2809 class TestModule(unittest.TestCase):
2810     def test_path(self):
2811         self.assertTrue(GIMarshallingTests.__path__.endswith('GIMarshallingTests-1.0.typelib'),
2812                         GIMarshallingTests.__path__)
2813
2814     def test_str(self):
2815         self.assertTrue("'GIMarshallingTests' from '" in str(GIMarshallingTests),
2816                         str(GIMarshallingTests))
2817
2818     def test_dir(self):
2819         _dir = dir(GIMarshallingTests)
2820         self.assertGreater(len(_dir), 10)
2821
2822         self.assertTrue('SimpleStruct' in _dir)
2823         self.assertTrue('Interface2' in _dir)
2824         self.assertTrue('CONSTANT_GERROR_CODE' in _dir)
2825         self.assertTrue('array_zero_terminated_inout' in _dir)
2826
2827         # assert that dir() does not contain garbage
2828         for item_name in _dir:
2829             item = getattr(GIMarshallingTests, item_name)
2830             self.assertTrue(hasattr(item, '__class__'))
2831
2832     def test_help(self):
2833         orig_stdout = sys.stdout
2834         try:
2835             if sys.version_info < (3, 0):
2836                 sys.stdout = BytesIO()
2837             else:
2838                 sys.stdout = StringIO()
2839             help(GIMarshallingTests)
2840             output = sys.stdout.getvalue()
2841         finally:
2842             sys.stdout = orig_stdout
2843
2844         self.assertTrue('SimpleStruct' in output, output)
2845         self.assertTrue('Interface2' in output, output)
2846         self.assertTrue('method_array_inout' in output, output)
2847
2848
2849 class TestProjectVersion(unittest.TestCase):
2850     def test_version_str(self):
2851         self.assertGreaterEqual(gi.__version__, "3.3.5")
2852
2853     def test_version_info(self):
2854         self.assertEqual(len(gi.version_info), 3)
2855         self.assertGreaterEqual(gi.version_info, (3, 3, 5))
2856
2857     def test_check_version(self):
2858         self.assertRaises(ValueError, gi.check_version, (99, 0, 0))
2859         self.assertRaises(ValueError, gi.check_version, "99.0.0")
2860         gi.check_version((3, 3, 5))
2861         gi.check_version("3.3.5")
2862
2863
2864 class TestObjectInfo(unittest.TestCase):
2865     def test_get_abstract_with_abstract(self):
2866         repo = gi.gi.Repository.get_default()
2867         info = repo.find_by_name('GObject', 'TypeModule')
2868         self.assertTrue(info.get_abstract())
2869
2870     def test_get_abstract_with_concrete(self):
2871         repo = gi.gi.Repository.get_default()
2872         info = repo.find_by_name('GObject', 'Object')
2873         self.assertFalse(info.get_abstract())
2874
2875
2876 class TestSignatureArgs(unittest.TestCase):
2877     def test_split_args_multi_out(self):
2878         in_args, out_args = gi.types.split_function_info_args(GIMarshallingTests.int_out_out.__info__)
2879         self.assertEqual(len(in_args), 0)
2880         self.assertEqual(len(out_args), 2)
2881         self.assertEqual(out_args[0].get_pytype_hint(), 'int')
2882         self.assertEqual(out_args[1].get_pytype_hint(), 'int')
2883
2884     def test_split_args_inout(self):
2885         in_args, out_args = gi.types.split_function_info_args(GIMarshallingTests.long_inout_max_min.__info__)
2886         self.assertEqual(len(in_args), 1)
2887         self.assertEqual(len(out_args), 1)
2888         self.assertEqual(in_args[0].get_name(), out_args[0].get_name())
2889         self.assertEqual(in_args[0].get_pytype_hint(), out_args[0].get_pytype_hint())
2890
2891     def test_split_args_none(self):
2892         obj = GIMarshallingTests.Object(int=33)
2893         in_args, out_args = gi.types.split_function_info_args(obj.none_inout.__info__)
2894         self.assertEqual(len(in_args), 1)
2895         self.assertEqual(len(out_args), 1)
2896
2897     def test_final_signature_with_full_inout(self):
2898         self.assertEqual(GIMarshallingTests.Object.full_inout.__doc__,
2899                          'full_inout(object:GIMarshallingTests.Object) -> object:GIMarshallingTests.Object')
2900
2901     def test_overridden_doc_is_not_clobbered(self):
2902         self.assertEqual(GIMarshallingTests.OverridesObject.method.__doc__,
2903                          'Overridden doc string.')