Imported Upstream version 3.7.3
[platform/upstream/python-gobject.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     def test_array_boxed_struct_take_in(self):
816         struct1 = GIMarshallingTests.BoxedStruct()
817         struct1.long_ = 1
818         struct2 = GIMarshallingTests.BoxedStruct()
819         struct2.long_ = 2
820         struct3 = GIMarshallingTests.BoxedStruct()
821         struct3.long_ = 3
822
823         GIMarshallingTests.array_struct_take_in([struct1, struct2, struct3])
824
825         self.assertEqual(1, struct1.long_)
826
827     @unittest.expectedFailure
828     def test_array_boxed_struct_return(self):
829         (struct1, struct2, struct3) = GIMarshallingTests.array_zero_terminated_return_struct()
830         self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct1))
831         self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct2))
832         self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct3))
833         # FIXME: gets bogus values
834         self.assertEqual(42, struct1.long_)
835         self.assertEqual(43, struct2.long_)
836         self.assertEqual(44, struct3.long_)
837
838     def test_array_simple_struct_in(self):
839         struct1 = GIMarshallingTests.SimpleStruct()
840         struct1.long_ = 1
841         struct2 = GIMarshallingTests.SimpleStruct()
842         struct2.long_ = 2
843         struct3 = GIMarshallingTests.SimpleStruct()
844         struct3.long_ = 3
845
846         GIMarshallingTests.array_simple_struct_in([struct1, struct2, struct3])
847
848     def test_array_multi_array_key_value_in(self):
849         GIMarshallingTests.multi_array_key_value_in(["one", "two", "three"],
850                                                     [1, 2, 3])
851
852     def test_array_in_nonzero_nonlen(self):
853         GIMarshallingTests.array_in_nonzero_nonlen(1, b'abcd')
854
855     def test_array_fixed_out_struct(self):
856         struct1, struct2 = GIMarshallingTests.array_fixed_out_struct()
857
858         self.assertEqual(7, struct1.long_)
859         self.assertEqual(6, struct1.int8)
860         self.assertEqual(6, struct2.long_)
861         self.assertEqual(7, struct2.int8)
862
863     def test_array_zero_terminated_return(self):
864         self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_return())
865
866     def test_array_zero_terminated_return_null(self):
867         self.assertEqual([], GIMarshallingTests.array_zero_terminated_return_null())
868
869     def test_array_zero_terminated_in(self):
870         GIMarshallingTests.array_zero_terminated_in(Sequence(['0', '1', '2']))
871
872     def test_array_zero_terminated_out(self):
873         self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
874
875     def test_array_zero_terminated_inout(self):
876         self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.array_zero_terminated_inout(['0', '1', '2']))
877
878     def test_init_function(self):
879         self.assertEqual((True, []), GIMarshallingTests.init_function([]))
880         self.assertEqual((True, []), GIMarshallingTests.init_function(['hello']))
881         self.assertEqual((True, ['hello']),
882                          GIMarshallingTests.init_function(['hello', 'world']))
883
884
885 class TestGStrv(unittest.TestCase):
886
887     def test_gstrv_return(self):
888         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_return())
889
890     def test_gstrv_in(self):
891         GIMarshallingTests.gstrv_in(Sequence(['0', '1', '2']))
892
893     def test_gstrv_out(self):
894         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_out())
895
896     def test_gstrv_inout(self):
897         self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.gstrv_inout(['0', '1', '2']))
898
899
900 class TestArrayGVariant(unittest.TestCase):
901
902     def test_array_gvariant_none_in(self):
903         v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
904         returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_none_in(v)]
905         self.assertEqual([27, "Hello"], returned)
906
907     def test_array_gvariant_container_in(self):
908         v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
909         returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_container_in(v)]
910         self.assertEqual([27, "Hello"], returned)
911
912     def test_array_gvariant_full_in(self):
913         v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
914         returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_full_in(v)]
915         self.assertEqual([27, "Hello"], returned)
916
917     def test_bytearray_gvariant(self):
918         v = GLib.Variant.new_bytestring(b"foo")
919         self.assertEqual(v.get_bytestring(), b"foo")
920
921
922 class TestGArray(unittest.TestCase):
923
924     def test_garray_int_none_return(self):
925         self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.garray_int_none_return())
926
927     def test_garray_uint64_none_return(self):
928         self.assertEqual([0, GObject.G_MAXUINT64], GIMarshallingTests.garray_uint64_none_return())
929
930     def test_garray_utf8_none_return(self):
931         self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_return())
932
933     def test_garray_utf8_container_return(self):
934         self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_return())
935
936     def test_garray_utf8_full_return(self):
937         self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_return())
938
939     def test_garray_int_none_in(self):
940         GIMarshallingTests.garray_int_none_in(Sequence([-1, 0, 1, 2]))
941
942         self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, Sequence([-1, '0', 1, 2]))
943
944         self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, 42)
945         self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, None)
946
947     def test_garray_uint64_none_in(self):
948         GIMarshallingTests.garray_uint64_none_in(Sequence([0, GObject.G_MAXUINT64]))
949
950     def test_garray_utf8_none_in(self):
951         GIMarshallingTests.garray_utf8_none_in(Sequence(['0', '1', '2']))
952
953     def test_garray_utf8_none_out(self):
954         self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_out())
955
956     def test_garray_utf8_container_out(self):
957         self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_out())
958
959     def test_garray_utf8_full_out(self):
960         self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out())
961
962     @unittest.skipUnless(hasattr(GIMarshallingTests, 'garray_utf8_full_out_caller_allocated'),
963                          'too old gobject-introspection')
964     def test_garray_utf8_full_out_caller_allocated(self):
965         self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out_caller_allocated())
966
967     def test_garray_utf8_none_inout(self):
968         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_none_inout(Sequence(('0', '1', '2'))))
969
970     def test_garray_utf8_container_inout(self):
971         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_container_inout(['0', '1', '2']))
972
973     def test_garray_utf8_full_inout(self):
974         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_full_inout(['0', '1', '2']))
975
976
977 class TestGPtrArray(unittest.TestCase):
978
979     def test_gptrarray_utf8_none_return(self):
980         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_return())
981
982     def test_gptrarray_utf8_container_return(self):
983         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_return())
984
985     def test_gptrarray_utf8_full_return(self):
986         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_return())
987
988     def test_gptrarray_utf8_none_in(self):
989         GIMarshallingTests.gptrarray_utf8_none_in(Sequence(['0', '1', '2']))
990
991     def test_gptrarray_utf8_none_out(self):
992         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_out())
993
994     def test_gptrarray_utf8_container_out(self):
995         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_out())
996
997     def test_gptrarray_utf8_full_out(self):
998         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_out())
999
1000     def test_gptrarray_utf8_none_inout(self):
1001         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_none_inout(Sequence(('0', '1', '2'))))
1002
1003     def test_gptrarray_utf8_container_inout(self):
1004         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_container_inout(['0', '1', '2']))
1005
1006     def test_gptrarray_utf8_full_inout(self):
1007         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_full_inout(['0', '1', '2']))
1008
1009
1010 class TestGBytes(unittest.TestCase):
1011     def test_gbytes_create(self):
1012         b = GLib.Bytes.new(b'\x00\x01\xFF')
1013         self.assertEqual(3, b.get_size())
1014         self.assertEqual(b'\x00\x01\xFF', b.get_data())
1015
1016     def test_gbytes_create_take(self):
1017         b = GLib.Bytes.new_take(b'\x00\x01\xFF')
1018         self.assertEqual(3, b.get_size())
1019         self.assertEqual(b'\x00\x01\xFF', b.get_data())
1020
1021     @unittest.skipUnless(hasattr(GIMarshallingTests, 'gbytes_full_return'),
1022                          'too old gobject-introspection')
1023     def test_gbytes_full_return(self):
1024         b = GIMarshallingTests.gbytes_full_return()
1025         self.assertEqual(4, b.get_size())
1026         self.assertEqual(b'\x00\x31\xFF\x33', b.get_data())
1027
1028     @unittest.skipUnless(hasattr(GIMarshallingTests, 'gbytes_full_return'),
1029                          'too old gobject-introspection')
1030     def test_gbytes_none_in(self):
1031         b = GIMarshallingTests.gbytes_full_return()
1032         GIMarshallingTests.gbytes_none_in(b)
1033
1034     def test_compare(self):
1035         a1 = GLib.Bytes.new(b'\x00\x01\xFF')
1036         a2 = GLib.Bytes.new(b'\x00\x01\xFF')
1037         b = GLib.Bytes.new(b'\x00\x01\xFE')
1038
1039         self.assertTrue(a1.equal(a2))
1040         self.assertTrue(a2.equal(a1))
1041         self.assertFalse(a1.equal(b))
1042         self.assertFalse(b.equal(a2))
1043
1044         self.assertEqual(0, a1.compare(a2))
1045         self.assertEqual(1, a1.compare(b))
1046         self.assertEqual(-1, b.compare(a1))
1047
1048
1049 class TestGByteArray(unittest.TestCase):
1050     def test_new(self):
1051         ba = GLib.ByteArray.new()
1052         self.assertEqual(b'', ba)
1053
1054         ba = GLib.ByteArray.new_take(b'\x01\x02\xFF')
1055         self.assertEqual(b'\x01\x02\xFF', ba)
1056
1057     def test_bytearray_full_return(self):
1058         self.assertEqual(b'\x001\xFF3', GIMarshallingTests.bytearray_full_return())
1059
1060     def test_bytearray_none_in(self):
1061         GIMarshallingTests.bytearray_none_in(b'\x00\x31\xFF\x33')
1062
1063
1064 class TestGList(unittest.TestCase):
1065
1066     def test_glist_int_none_return(self):
1067         self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.glist_int_none_return())
1068
1069     def test_glist_uint32_none_return(self):
1070         self.assertEqual([0, GObject.G_MAXUINT32], GIMarshallingTests.glist_uint32_none_return())
1071
1072     def test_glist_utf8_none_return(self):
1073         self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_return())
1074
1075     def test_glist_utf8_container_return(self):
1076         self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_return())
1077
1078     def test_glist_utf8_full_return(self):
1079         self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_return())
1080
1081     def test_glist_int_none_in(self):
1082         GIMarshallingTests.glist_int_none_in(Sequence((-1, 0, 1, 2)))
1083
1084         self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, Sequence((-1, '0', 1, 2)))
1085
1086         self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, 42)
1087         self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, None)
1088
1089     def test_glist_uint32_none_in(self):
1090         GIMarshallingTests.glist_uint32_none_in(Sequence((0, GObject.G_MAXUINT32)))
1091
1092     def test_glist_utf8_none_in(self):
1093         GIMarshallingTests.glist_utf8_none_in(Sequence(('0', '1', '2')))
1094
1095     def test_glist_utf8_none_out(self):
1096         self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_out())
1097
1098     def test_glist_utf8_container_out(self):
1099         self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_out())
1100
1101     def test_glist_utf8_full_out(self):
1102         self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_out())
1103
1104     def test_glist_utf8_none_inout(self):
1105         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_none_inout(Sequence(('0', '1', '2'))))
1106
1107     def test_glist_utf8_container_inout(self):
1108         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_container_inout(('0', '1', '2')))
1109
1110     def test_glist_utf8_full_inout(self):
1111         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_full_inout(('0', '1', '2')))
1112
1113
1114 class TestGSList(unittest.TestCase):
1115
1116     def test_gslist_int_none_return(self):
1117         self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.gslist_int_none_return())
1118
1119     def test_gslist_utf8_none_return(self):
1120         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_return())
1121
1122     def test_gslist_utf8_container_return(self):
1123         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_return())
1124
1125     def test_gslist_utf8_full_return(self):
1126         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_return())
1127
1128     def test_gslist_int_none_in(self):
1129         GIMarshallingTests.gslist_int_none_in(Sequence((-1, 0, 1, 2)))
1130
1131         self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, Sequence((-1, '0', 1, 2)))
1132
1133         self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, 42)
1134         self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, None)
1135
1136     def test_gslist_utf8_none_in(self):
1137         GIMarshallingTests.gslist_utf8_none_in(Sequence(('0', '1', '2')))
1138
1139     def test_gslist_utf8_none_out(self):
1140         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_out())
1141
1142     def test_gslist_utf8_container_out(self):
1143         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_out())
1144
1145     def test_gslist_utf8_full_out(self):
1146         self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_out())
1147
1148     def test_gslist_utf8_none_inout(self):
1149         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_none_inout(Sequence(('0', '1', '2'))))
1150
1151     def test_gslist_utf8_container_inout(self):
1152         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_container_inout(('0', '1', '2')))
1153
1154     def test_gslist_utf8_full_inout(self):
1155         self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_full_inout(('0', '1', '2')))
1156
1157
1158 class TestGHashTable(unittest.TestCase):
1159
1160     def test_ghashtable_int_none_return(self):
1161         self.assertEqual({-1: 1, 0: 0, 1: -1, 2: -2}, GIMarshallingTests.ghashtable_int_none_return())
1162
1163     def test_ghashtable_int_none_return2(self):
1164         self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_return())
1165
1166     def test_ghashtable_int_container_return(self):
1167         self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_return())
1168
1169     def test_ghashtable_int_full_return(self):
1170         self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_return())
1171
1172     def test_ghashtable_int_none_in(self):
1173         GIMarshallingTests.ghashtable_int_none_in({-1: 1, 0: 0, 1: -1, 2: -2})
1174
1175         self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, '0': 0, 1: -1, 2: -2})
1176         self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, 0: '0', 1: -1, 2: -2})
1177
1178         self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, '{-1: 1, 0: 0, 1: -1, 2: -2}')
1179         self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, None)
1180
1181     def test_ghashtable_utf8_none_in(self):
1182         GIMarshallingTests.ghashtable_utf8_none_in({'-1': '1', '0': '0', '1': '-1', '2': '-2'})
1183
1184     def test_ghashtable_utf8_none_out(self):
1185         self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_out())
1186
1187     def test_ghashtable_utf8_container_out(self):
1188         self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_out())
1189
1190     def test_ghashtable_utf8_full_out(self):
1191         self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_out())
1192
1193     def test_ghashtable_utf8_none_inout(self):
1194         i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1195         self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1196                          GIMarshallingTests.ghashtable_utf8_none_inout(i))
1197
1198     def test_ghashtable_utf8_container_inout(self):
1199         i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1200         self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1201                          GIMarshallingTests.ghashtable_utf8_container_inout(i))
1202
1203     def test_ghashtable_utf8_full_inout(self):
1204         i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1205         self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1206                          GIMarshallingTests.ghashtable_utf8_full_inout(i))
1207
1208
1209 class TestGValue(unittest.TestCase):
1210
1211     def test_gvalue_return(self):
1212         self.assertEqual(42, GIMarshallingTests.gvalue_return())
1213
1214     def test_gvalue_in(self):
1215         GIMarshallingTests.gvalue_in(42)
1216         value = GObject.Value()
1217         value.init(GObject.TYPE_INT)
1218         value.set_int(42)
1219         GIMarshallingTests.gvalue_in(value)
1220
1221     def test_gvalue_int64_in(self):
1222         value = GObject.Value()
1223         value.init(GObject.TYPE_INT64)
1224         value.set_int64(GObject.G_MAXINT64)
1225         GIMarshallingTests.gvalue_int64_in(value)
1226
1227     def test_gvalue_in_with_type(self):
1228         value = GObject.Value()
1229         value.init(GObject.TYPE_STRING)
1230         value.set_string('foo')
1231         GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_STRING)
1232
1233         value = GObject.Value()
1234         value.init(GIMarshallingTests.Flags.__gtype__)
1235         value.set_flags(GIMarshallingTests.Flags.VALUE1)
1236         GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_FLAGS)
1237
1238     def test_gvalue_in_enum(self):
1239         value = GObject.Value()
1240         value.init(GIMarshallingTests.Enum.__gtype__)
1241         value.set_enum(GIMarshallingTests.Enum.VALUE3)
1242         GIMarshallingTests.gvalue_in_enum(value)
1243
1244     def test_gvalue_out(self):
1245         self.assertEqual(42, GIMarshallingTests.gvalue_out())
1246
1247     def test_gvalue_int64_out(self):
1248         self.assertEqual(GObject.G_MAXINT64, GIMarshallingTests.gvalue_int64_out())
1249
1250     def test_gvalue_out_caller_allocates(self):
1251         self.assertEqual(42, GIMarshallingTests.gvalue_out_caller_allocates())
1252
1253     def test_gvalue_inout(self):
1254         self.assertEqual('42', GIMarshallingTests.gvalue_inout(42))
1255         value = GObject.Value()
1256         value.init(GObject.TYPE_INT)
1257         value.set_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     @unittest.expectedFailure
1371     def test_one_out_arg(self):
1372         def cb():
1373             return 5.5
1374         self.assertAlmostEqual(GIMarshallingTests.callback_one_out_parameter(cb), 5.5)
1375
1376     @unittest.expectedFailure
1377     def test_multiple_out_args(self):
1378         def cb():
1379             return (5.5, 42.0)
1380         res = GIMarshallingTests.callback_multiple_out_parameters(cb)
1381         self.assertAlmostEqual(res[0], 5.5)
1382         self.assertAlmostEqual(res[1], 42.0)
1383
1384     @unittest.expectedFailure
1385     def test_return_and_one_out_arg(self):
1386         def cb():
1387             return (5, 42.0)
1388         res = GIMarshallingTests.callback_return_value_and_one_out_parameter(cb)
1389         self.assertEqual(res[0], 5)
1390         self.assertAlmostEqual(res[1], 42.0)
1391
1392     @unittest.expectedFailure
1393     def test_return_and_multiple_out_arg(self):
1394         def cb():
1395             return (5, 42, -1000)
1396         self.assertEqual(GIMarshallingTests.callback_return_value_and_multiple_out_parameters(cb),
1397                          (5, 42, -1000))
1398
1399
1400 class TestPointer(unittest.TestCase):
1401     def test_pointer_in_return(self):
1402         self.assertEqual(GIMarshallingTests.pointer_in_return(42), 42)
1403
1404
1405 class TestEnum(unittest.TestCase):
1406
1407     @classmethod
1408     def setUpClass(cls):
1409         '''Run tests under a test locale.
1410
1411         Upper case conversion of member names should not be locale specific
1412         e.  g. in Turkish, "i".upper() == "i", which gives results like "iNVALiD"
1413
1414         Run test under a locale which defines toupper('a') == 'a'
1415         '''
1416         cls.locale_dir = tempfile.mkdtemp()
1417         src = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'te_ST@nouppera')
1418         dest = os.path.join(cls.locale_dir, 'te_ST.UTF-8@nouppera')
1419         subprocess.check_call(['localedef', '-i', src, '-c', '-f', 'UTF-8', dest])
1420         os.environ['LOCPATH'] = cls.locale_dir
1421         locale.setlocale(locale.LC_ALL, 'te_ST.UTF-8@nouppera')
1422
1423     @classmethod
1424     def tearDownClass(cls):
1425         locale.setlocale(locale.LC_ALL, 'C')
1426         shutil.rmtree(cls.locale_dir)
1427         try:
1428             del os.environ['LOCPATH']
1429         except KeyError:
1430             pass
1431
1432     def test_enum(self):
1433         self.assertTrue(issubclass(GIMarshallingTests.Enum, int))
1434         self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE1, GIMarshallingTests.Enum))
1435         self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE2, GIMarshallingTests.Enum))
1436         self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE3, GIMarshallingTests.Enum))
1437         self.assertEqual(42, GIMarshallingTests.Enum.VALUE3)
1438
1439     def test_value_nick_and_name(self):
1440         self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_nick, 'value1')
1441         self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_nick, 'value2')
1442         self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_nick, 'value3')
1443
1444         self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE1')
1445         self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE2')
1446         self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE3')
1447
1448     def test_enum_in(self):
1449         GIMarshallingTests.enum_in(GIMarshallingTests.Enum.VALUE3)
1450         GIMarshallingTests.enum_in(42)
1451
1452         self.assertRaises(TypeError, GIMarshallingTests.enum_in, 43)
1453         self.assertRaises(TypeError, GIMarshallingTests.enum_in, 'GIMarshallingTests.Enum.VALUE3')
1454
1455     def test_enum_return(self):
1456         enum = GIMarshallingTests.enum_returnv()
1457         self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1458         self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1459
1460     def test_enum_out(self):
1461         enum = GIMarshallingTests.enum_out()
1462         self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1463         self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1464
1465     def test_enum_inout(self):
1466         enum = GIMarshallingTests.enum_inout(GIMarshallingTests.Enum.VALUE3)
1467         self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1468         self.assertEqual(enum, GIMarshallingTests.Enum.VALUE1)
1469
1470     def test_enum_second(self):
1471         # check for the bug where different non-gtype enums share the same class
1472         self.assertNotEqual(GIMarshallingTests.Enum, GIMarshallingTests.SecondEnum)
1473
1474         # check that values are not being shared between different enums
1475         self.assertTrue(hasattr(GIMarshallingTests.SecondEnum, "SECONDVALUE1"))
1476         self.assertRaises(AttributeError, getattr, GIMarshallingTests.Enum, "SECONDVALUE1")
1477         self.assertTrue(hasattr(GIMarshallingTests.Enum, "VALUE1"))
1478         self.assertRaises(AttributeError, getattr, GIMarshallingTests.SecondEnum, "VALUE1")
1479
1480
1481 class TestGEnum(unittest.TestCase):
1482
1483     def test_genum(self):
1484         self.assertTrue(issubclass(GIMarshallingTests.GEnum, GObject.GEnum))
1485         self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE1, GIMarshallingTests.GEnum))
1486         self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE2, GIMarshallingTests.GEnum))
1487         self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE3, GIMarshallingTests.GEnum))
1488         self.assertEqual(42, GIMarshallingTests.GEnum.VALUE3)
1489
1490     def test_value_nick_and_name(self):
1491         self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_nick, 'value1')
1492         self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_nick, 'value2')
1493         self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_nick, 'value3')
1494
1495         self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE1')
1496         self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE2')
1497         self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE3')
1498
1499     def test_genum_in(self):
1500         GIMarshallingTests.genum_in(GIMarshallingTests.GEnum.VALUE3)
1501         GIMarshallingTests.genum_in(42)
1502
1503         self.assertRaises(TypeError, GIMarshallingTests.genum_in, 43)
1504         self.assertRaises(TypeError, GIMarshallingTests.genum_in, 'GIMarshallingTests.GEnum.VALUE3')
1505
1506     def test_genum_return(self):
1507         genum = GIMarshallingTests.genum_returnv()
1508         self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1509         self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1510
1511     def test_genum_out(self):
1512         genum = GIMarshallingTests.genum_out()
1513         self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1514         self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1515
1516     def test_genum_inout(self):
1517         genum = GIMarshallingTests.genum_inout(GIMarshallingTests.GEnum.VALUE3)
1518         self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1519         self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE1)
1520
1521
1522 class TestGFlags(unittest.TestCase):
1523
1524     def test_flags(self):
1525         self.assertTrue(issubclass(GIMarshallingTests.Flags, GObject.GFlags))
1526         self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1, GIMarshallingTests.Flags))
1527         self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE2, GIMarshallingTests.Flags))
1528         self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE3, GIMarshallingTests.Flags))
1529         # __or__() operation should still return an instance, not an int.
1530         self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1 | GIMarshallingTests.Flags.VALUE2,
1531                                    GIMarshallingTests.Flags))
1532         self.assertEqual(1 << 1, GIMarshallingTests.Flags.VALUE2)
1533
1534     def test_value_nick_and_name(self):
1535         self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_nick, 'value1')
1536         self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_nick, 'value2')
1537         self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_nick, 'value3')
1538
1539         self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE1')
1540         self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE2')
1541         self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE3')
1542
1543     def test_flags_in(self):
1544         GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2)
1545         # result of __or__() operation should still be valid instance, not an int.
1546         GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE2)
1547         GIMarshallingTests.flags_in_zero(Number(0))
1548
1549         self.assertRaises(TypeError, GIMarshallingTests.flags_in, 1 << 1)
1550         self.assertRaises(TypeError, GIMarshallingTests.flags_in, 'GIMarshallingTests.Flags.VALUE2')
1551
1552     def test_flags_return(self):
1553         flags = GIMarshallingTests.flags_returnv()
1554         self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1555         self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1556
1557     def test_flags_out(self):
1558         flags = GIMarshallingTests.flags_out()
1559         self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1560         self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1561
1562     def test_flags_inout(self):
1563         flags = GIMarshallingTests.flags_inout(GIMarshallingTests.Flags.VALUE2)
1564         self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1565         self.assertEqual(flags, GIMarshallingTests.Flags.VALUE1)
1566
1567
1568 class TestNoTypeFlags(unittest.TestCase):
1569
1570     def test_flags(self):
1571         self.assertTrue(issubclass(GIMarshallingTests.NoTypeFlags, GObject.GFlags))
1572         self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1, GIMarshallingTests.NoTypeFlags))
1573         self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE2, GIMarshallingTests.NoTypeFlags))
1574         self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE3, GIMarshallingTests.NoTypeFlags))
1575         # __or__() operation should still return an instance, not an int.
1576         self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1 | GIMarshallingTests.NoTypeFlags.VALUE2,
1577                                    GIMarshallingTests.NoTypeFlags))
1578         self.assertEqual(1 << 1, GIMarshallingTests.NoTypeFlags.VALUE2)
1579
1580     def test_value_nick_and_name(self):
1581         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_nick, 'value1')
1582         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_nick, 'value2')
1583         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_nick, 'value3')
1584
1585         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE1')
1586         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2')
1587         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE3')
1588
1589     def test_flags_in(self):
1590         GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2)
1591         GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2 | GIMarshallingTests.NoTypeFlags.VALUE2)
1592         GIMarshallingTests.no_type_flags_in_zero(Number(0))
1593
1594         self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 1 << 1)
1595         self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 'GIMarshallingTests.NoTypeFlags.VALUE2')
1596
1597     def test_flags_return(self):
1598         flags = GIMarshallingTests.no_type_flags_returnv()
1599         self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1600         self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1601
1602     def test_flags_out(self):
1603         flags = GIMarshallingTests.no_type_flags_out()
1604         self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1605         self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1606
1607     def test_flags_inout(self):
1608         flags = GIMarshallingTests.no_type_flags_inout(GIMarshallingTests.NoTypeFlags.VALUE2)
1609         self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1610         self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE1)
1611
1612
1613 class TestStructure(unittest.TestCase):
1614
1615     def test_simple_struct(self):
1616         self.assertTrue(issubclass(GIMarshallingTests.SimpleStruct, GObject.GPointer))
1617
1618         struct = GIMarshallingTests.SimpleStruct()
1619         self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1620
1621         self.assertEqual(0, struct.long_)
1622         self.assertEqual(0, struct.int8)
1623
1624         struct.long_ = 6
1625         struct.int8 = 7
1626
1627         self.assertEqual(6, struct.long_)
1628         self.assertEqual(7, struct.int8)
1629
1630         del struct
1631
1632     def test_nested_struct(self):
1633         struct = GIMarshallingTests.NestedStruct()
1634
1635         self.assertTrue(isinstance(struct.simple_struct, GIMarshallingTests.SimpleStruct))
1636
1637         struct.simple_struct.long_ = 42
1638         self.assertEqual(42, struct.simple_struct.long_)
1639
1640         del struct
1641
1642     def test_not_simple_struct(self):
1643         struct = GIMarshallingTests.NotSimpleStruct()
1644         self.assertEqual(None, struct.pointer)
1645
1646     def test_simple_struct_return(self):
1647         struct = GIMarshallingTests.simple_struct_returnv()
1648
1649         self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1650         self.assertEqual(6, struct.long_)
1651         self.assertEqual(7, struct.int8)
1652
1653         del struct
1654
1655     def test_simple_struct_in(self):
1656         struct = GIMarshallingTests.SimpleStruct()
1657         struct.long_ = 6
1658         struct.int8 = 7
1659
1660         GIMarshallingTests.SimpleStruct.inv(struct)
1661
1662         del struct
1663
1664         struct = GIMarshallingTests.NestedStruct()
1665
1666         self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, struct)
1667
1668         del struct
1669
1670         self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, None)
1671
1672     def test_simple_struct_method(self):
1673         struct = GIMarshallingTests.SimpleStruct()
1674         struct.long_ = 6
1675         struct.int8 = 7
1676
1677         struct.method()
1678
1679         del struct
1680
1681         self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.method)
1682
1683     def test_pointer_struct(self):
1684         self.assertTrue(issubclass(GIMarshallingTests.PointerStruct, GObject.GPointer))
1685
1686         struct = GIMarshallingTests.PointerStruct()
1687         self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1688
1689         del struct
1690
1691     def test_pointer_struct_return(self):
1692         struct = GIMarshallingTests.pointer_struct_returnv()
1693
1694         self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1695         self.assertEqual(42, struct.long_)
1696
1697         del struct
1698
1699     def test_pointer_struct_in(self):
1700         struct = GIMarshallingTests.PointerStruct()
1701         struct.long_ = 42
1702
1703         struct.inv()
1704
1705         del struct
1706
1707     def test_boxed_struct(self):
1708         self.assertTrue(issubclass(GIMarshallingTests.BoxedStruct, GObject.GBoxed))
1709
1710         struct = GIMarshallingTests.BoxedStruct()
1711         self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1712
1713         self.assertEqual(0, struct.long_)
1714         self.assertEqual([], struct.g_strv)
1715
1716         del struct
1717
1718     def test_boxed_struct_new(self):
1719         struct = GIMarshallingTests.BoxedStruct.new()
1720         self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1721
1722         del struct
1723
1724     def test_boxed_struct_copy(self):
1725         struct = GIMarshallingTests.BoxedStruct()
1726
1727         new_struct = struct.copy()
1728         self.assertTrue(isinstance(new_struct, GIMarshallingTests.BoxedStruct))
1729
1730         del new_struct
1731         del struct
1732
1733     def test_boxed_struct_return(self):
1734         struct = GIMarshallingTests.boxed_struct_returnv()
1735
1736         self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1737         self.assertEqual(42, struct.long_)
1738         self.assertEqual(['0', '1', '2'], struct.g_strv)
1739
1740         del struct
1741
1742     def test_boxed_struct_in(self):
1743         struct = GIMarshallingTests.BoxedStruct()
1744         struct.long_ = 42
1745
1746         struct.inv()
1747
1748         del struct
1749
1750     def test_boxed_struct_out(self):
1751         struct = GIMarshallingTests.boxed_struct_out()
1752
1753         self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1754         self.assertEqual(42, struct.long_)
1755
1756         del struct
1757
1758     def test_boxed_struct_inout(self):
1759         in_struct = GIMarshallingTests.BoxedStruct()
1760         in_struct.long_ = 42
1761
1762         out_struct = GIMarshallingTests.boxed_struct_inout(in_struct)
1763
1764         self.assertTrue(isinstance(out_struct, GIMarshallingTests.BoxedStruct))
1765         self.assertEqual(0, out_struct.long_)
1766
1767         del in_struct
1768         del out_struct
1769
1770     def test_union(self):
1771         union = GIMarshallingTests.Union()
1772
1773         self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1774
1775         new_union = union.copy()
1776         self.assertTrue(isinstance(new_union, GIMarshallingTests.Union))
1777
1778         del union
1779         del new_union
1780
1781     def test_union_return(self):
1782         union = GIMarshallingTests.union_returnv()
1783
1784         self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1785         self.assertEqual(42, union.long_)
1786
1787         del union
1788
1789     def test_union_in(self):
1790         union = GIMarshallingTests.Union()
1791         union.long_ = 42
1792
1793         union.inv()
1794
1795         del union
1796
1797     def test_union_method(self):
1798         union = GIMarshallingTests.Union()
1799         union.long_ = 42
1800
1801         union.method()
1802
1803         del union
1804
1805         self.assertRaises(TypeError, GIMarshallingTests.Union.method)
1806
1807
1808 class TestGObject(unittest.TestCase):
1809
1810     def test_object(self):
1811         self.assertTrue(issubclass(GIMarshallingTests.Object, GObject.GObject))
1812
1813         object_ = GIMarshallingTests.Object()
1814         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1815         self.assertEqual(object_.__grefcount__, 1)
1816
1817     def test_object_new(self):
1818         object_ = GIMarshallingTests.Object.new(42)
1819         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1820         self.assertEqual(object_.__grefcount__, 1)
1821
1822     def test_object_int(self):
1823         object_ = GIMarshallingTests.Object(int=42)
1824         self.assertEqual(object_.int_, 42)
1825 # FIXME: Don't work yet.
1826 #        object_.int_ = 0
1827 #        self.assertEqual(object_.int_, 0)
1828
1829     def test_object_static_method(self):
1830         GIMarshallingTests.Object.static_method()
1831
1832     def test_object_method(self):
1833         GIMarshallingTests.Object(int=42).method()
1834         self.assertRaises(TypeError, GIMarshallingTests.Object.method, GObject.GObject())
1835         self.assertRaises(TypeError, GIMarshallingTests.Object.method)
1836
1837     def test_sub_object(self):
1838         self.assertTrue(issubclass(GIMarshallingTests.SubObject, GIMarshallingTests.Object))
1839
1840         object_ = GIMarshallingTests.SubObject()
1841         self.assertTrue(isinstance(object_, GIMarshallingTests.SubObject))
1842
1843     def test_sub_object_new(self):
1844         self.assertRaises(TypeError, GIMarshallingTests.SubObject.new, 42)
1845
1846     def test_sub_object_static_method(self):
1847         object_ = GIMarshallingTests.SubObject()
1848         object_.static_method()
1849
1850     def test_sub_object_method(self):
1851         object_ = GIMarshallingTests.SubObject(int=42)
1852         object_.method()
1853
1854     def test_sub_object_sub_method(self):
1855         object_ = GIMarshallingTests.SubObject()
1856         object_.sub_method()
1857
1858     def test_sub_object_overwritten_method(self):
1859         object_ = GIMarshallingTests.SubObject()
1860         object_.overwritten_method()
1861
1862         self.assertRaises(TypeError, GIMarshallingTests.SubObject.overwritten_method, GIMarshallingTests.Object())
1863
1864     def test_sub_object_int(self):
1865         object_ = GIMarshallingTests.SubObject()
1866         self.assertEqual(object_.int_, 0)
1867 # FIXME: Don't work yet.
1868 #        object_.int_ = 42
1869 #        self.assertEqual(object_.int_, 42)
1870
1871     def test_object_none_return(self):
1872         object_ = GIMarshallingTests.Object.none_return()
1873         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1874         self.assertEqual(object_.__grefcount__, 2)
1875
1876     def test_object_full_return(self):
1877         object_ = GIMarshallingTests.Object.full_return()
1878         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1879         self.assertEqual(object_.__grefcount__, 1)
1880
1881     def test_object_none_in(self):
1882         object_ = GIMarshallingTests.Object(int=42)
1883         GIMarshallingTests.Object.none_in(object_)
1884         self.assertEqual(object_.__grefcount__, 1)
1885
1886         object_ = GIMarshallingTests.SubObject(int=42)
1887         GIMarshallingTests.Object.none_in(object_)
1888
1889         object_ = GObject.GObject()
1890         self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, object_)
1891
1892         self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, None)
1893
1894     def test_object_none_out(self):
1895         object_ = GIMarshallingTests.Object.none_out()
1896         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1897         self.assertEqual(object_.__grefcount__, 2)
1898
1899         new_object = GIMarshallingTests.Object.none_out()
1900         self.assertTrue(new_object is object_)
1901
1902     def test_object_full_out(self):
1903         object_ = GIMarshallingTests.Object.full_out()
1904         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1905         self.assertEqual(object_.__grefcount__, 1)
1906
1907     def test_object_none_inout(self):
1908         object_ = GIMarshallingTests.Object(int=42)
1909         new_object = GIMarshallingTests.Object.none_inout(object_)
1910
1911         self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1912
1913         self.assertFalse(object_ is new_object)
1914
1915         self.assertEqual(object_.__grefcount__, 1)
1916         self.assertEqual(new_object.__grefcount__, 2)
1917
1918         new_new_object = GIMarshallingTests.Object.none_inout(object_)
1919         self.assertTrue(new_new_object is new_object)
1920
1921         GIMarshallingTests.Object.none_inout(GIMarshallingTests.SubObject(int=42))
1922
1923     def test_object_full_inout(self):
1924         object_ = GIMarshallingTests.Object(int=42)
1925         new_object = GIMarshallingTests.Object.full_inout(object_)
1926
1927         self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1928
1929         self.assertFalse(object_ is new_object)
1930
1931         self.assertEqual(object_.__grefcount__, 2)
1932         self.assertEqual(new_object.__grefcount__, 1)
1933
1934 # FIXME: Doesn't actually return the same object.
1935 #    def test_object_inout_same(self):
1936 #        object_ = GIMarshallingTests.Object()
1937 #        new_object = GIMarshallingTests.object_full_inout(object_)
1938 #        self.assertTrue(object_ is new_object)
1939 #        self.assertEqual(object_.__grefcount__, 1)
1940
1941
1942 class TestPythonGObject(unittest.TestCase):
1943
1944     class Object(GIMarshallingTests.Object):
1945         return_for_caller_allocated_out_parameter = 'test caller alloc return'
1946
1947         def __init__(self, int):
1948             GIMarshallingTests.Object.__init__(self)
1949             self.val = None
1950
1951         def method(self):
1952             # Don't call super, which asserts that self.int == 42.
1953             pass
1954
1955         def do_method_int8_in(self, int8):
1956             self.val = int8
1957
1958         def do_method_int8_out(self):
1959             return 42
1960
1961         def do_method_int8_arg_and_out_caller(self, arg):
1962             return arg + 1
1963
1964         def do_method_int8_arg_and_out_callee(self, arg):
1965             return arg + 1
1966
1967         def do_method_str_arg_out_ret(self, arg):
1968             return (arg.upper(), len(arg))
1969
1970         def do_method_with_default_implementation(self, int8):
1971             GIMarshallingTests.Object.do_method_with_default_implementation(self, int8)
1972             self.props.int += int8
1973
1974         def do_vfunc_return_value_only(self):
1975             return 4242
1976
1977         def do_vfunc_one_out_parameter(self):
1978             return 42.42
1979
1980         def do_vfunc_multiple_out_parameters(self):
1981             return (42.42, 3.14)
1982
1983         def do_vfunc_return_value_and_one_out_parameter(self):
1984             return (5, 42)
1985
1986         def do_vfunc_return_value_and_multiple_out_parameters(self):
1987             return (5, 42, 99)
1988
1989         def do_vfunc_caller_allocated_out_parameter(self):
1990             return self.return_for_caller_allocated_out_parameter
1991
1992     class SubObject(GIMarshallingTests.SubObject):
1993         def __init__(self, int):
1994             GIMarshallingTests.SubObject.__init__(self)
1995             self.val = None
1996
1997         def do_method_with_default_implementation(self, int8):
1998             self.val = int8
1999
2000     class Interface3Impl(GObject.Object, GIMarshallingTests.Interface3):
2001         def __init__(self):
2002             GObject.Object.__init__(self)
2003             self.variants = None
2004             self.n_variants = None
2005
2006         def do_test_variant_array_in(self, variants, n_variants):
2007             self.variants = variants
2008             self.n_variants = n_variants
2009
2010     def test_object(self):
2011         self.assertTrue(issubclass(self.Object, GIMarshallingTests.Object))
2012
2013         object_ = self.Object(int=42)
2014         self.assertTrue(isinstance(object_, self.Object))
2015
2016     def test_object_method(self):
2017         self.Object(int=0).method()
2018
2019     def test_object_vfuncs(self):
2020         object_ = self.Object(int=42)
2021         object_.method_int8_in(84)
2022         self.assertEqual(object_.val, 84)
2023         self.assertEqual(object_.method_int8_out(), 42)
2024
2025         # can be dropped when bumping g-i dependencies to >= 1.35.2
2026         if hasattr(object_, 'method_int8_arg_and_out_caller'):
2027             self.assertEqual(object_.method_int8_arg_and_out_caller(42), 43)
2028             self.assertEqual(object_.method_int8_arg_and_out_callee(42), 43)
2029             self.assertEqual(object_.method_str_arg_out_ret('hello'), ('HELLO', 5))
2030
2031         object_.method_with_default_implementation(42)
2032         self.assertEqual(object_.props.int, 84)
2033
2034         self.assertEqual(object_.vfunc_return_value_only(), 4242)
2035         self.assertAlmostEqual(object_.vfunc_one_out_parameter(), 42.42, places=5)
2036
2037         (a, b) = object_.vfunc_multiple_out_parameters()
2038         self.assertAlmostEqual(a, 42.42, places=5)
2039         self.assertAlmostEqual(b, 3.14, places=5)
2040
2041         self.assertEqual(object_.vfunc_return_value_and_one_out_parameter(), (5, 42))
2042         self.assertEqual(object_.vfunc_return_value_and_multiple_out_parameters(), (5, 42, 99))
2043
2044         self.assertEqual(object_.vfunc_caller_allocated_out_parameter(),
2045                          object_.return_for_caller_allocated_out_parameter)
2046
2047         class ObjectWithoutVFunc(GIMarshallingTests.Object):
2048             def __init__(self, int):
2049                 GIMarshallingTests.Object.__init__(self)
2050
2051         object_ = ObjectWithoutVFunc(int=42)
2052         object_.method_with_default_implementation(84)
2053         self.assertEqual(object_.props.int, 84)
2054
2055     def test_vfunc_return_ref_count(self):
2056         obj = self.Object(int=42)
2057         ref_count = sys.getrefcount(obj.return_for_caller_allocated_out_parameter)
2058         ret = obj.vfunc_caller_allocated_out_parameter()
2059         gc.collect()
2060
2061         # Make sure the return and what the vfunc returned
2062         # are equal but not the same object.
2063         self.assertEqual(ret, obj.return_for_caller_allocated_out_parameter)
2064         self.assertFalse(ret is obj.return_for_caller_allocated_out_parameter)
2065         self.assertEqual(sys.getrefcount(obj.return_for_caller_allocated_out_parameter),
2066                          ref_count)
2067
2068     def test_subobject_parent_vfunc(self):
2069         object_ = self.SubObject(int=81)
2070         object_.method_with_default_implementation(87)
2071         self.assertEqual(object_.val, 87)
2072
2073     def test_dynamic_module(self):
2074         from gi.module import DynamicModule
2075         self.assertTrue(isinstance(GObject, DynamicModule))
2076
2077     def test_subobject_non_vfunc_do_method(self):
2078         class PythonObjectWithNonVFuncDoMethod:
2079             def do_not_a_vfunc(self):
2080                 return 5
2081
2082         class ObjectOverrideNonVFuncDoMethod(GIMarshallingTests.Object, PythonObjectWithNonVFuncDoMethod):
2083             def do_not_a_vfunc(self):
2084                 value = super(ObjectOverrideNonVFuncDoMethod, self).do_not_a_vfunc()
2085                 return 13 + value
2086
2087         object_ = ObjectOverrideNonVFuncDoMethod()
2088         self.assertEqual(18, object_.do_not_a_vfunc())
2089
2090     def test_native_function_not_set_in_subclass_dict(self):
2091         # Previously, GI was setting virtual functions on the class as well
2092         # as any *native* class that subclasses it. Here we check that it is only
2093         # set on the class that the method is originally from.
2094         self.assertTrue('do_method_with_default_implementation' in GIMarshallingTests.Object.__dict__)
2095         self.assertTrue('do_method_with_default_implementation' not in GIMarshallingTests.SubObject.__dict__)
2096
2097     def test_subobject_with_interface_and_non_vfunc_do_method(self):
2098         # There was a bug for searching for vfuncs in interfaces. It was
2099         # triggered by having a do_* method that wasn't overriding
2100         # a native vfunc, as well as inheriting from an interface.
2101         class GObjectSubclassWithInterface(GObject.GObject, GIMarshallingTests.Interface):
2102             def do_method_not_a_vfunc(self):
2103                 pass
2104
2105     def test_subsubobject(self):
2106         class SubSubSubObject(GIMarshallingTests.SubSubObject):
2107             def do_method_deep_hierarchy(self, num):
2108                 self.props.int = num * 2
2109
2110         sub_sub_sub_object = SubSubSubObject()
2111         GIMarshallingTests.SubSubObject.do_method_deep_hierarchy(sub_sub_sub_object, 5)
2112         self.assertEqual(sub_sub_sub_object.props.int, 5)
2113
2114     def test_interface3impl(self):
2115         iface3 = self.Interface3Impl()
2116         variants = [GLib.Variant('i', 27), GLib.Variant('s', 'Hello')]
2117         iface3.test_variant_array_in(variants)
2118         self.assertEqual(iface3.n_variants, 2)
2119         self.assertEqual(iface3.variants[0].unpack(), 27)
2120         self.assertEqual(iface3.variants[1].unpack(), 'Hello')
2121
2122     def test_python_subsubobject_vfunc(self):
2123         class PySubObject(GIMarshallingTests.Object):
2124             def __init__(self):
2125                 GIMarshallingTests.Object.__init__(self)
2126                 self.sub_method_int8_called = 0
2127
2128             def do_method_int8_in(self, int8):
2129                 self.sub_method_int8_called += 1
2130
2131         class PySubSubObject(PySubObject):
2132             def __init__(self):
2133                 PySubObject.__init__(self)
2134                 self.subsub_method_int8_called = 0
2135
2136             def do_method_int8_in(self, int8):
2137                 self.subsub_method_int8_called += 1
2138
2139         so = PySubObject()
2140         so.method_int8_in(1)
2141         self.assertEqual(so.sub_method_int8_called, 1)
2142
2143         # it should call the method on the SubSub object only
2144         sso = PySubSubObject()
2145         sso.method_int8_in(1)
2146         self.assertEqual(sso.subsub_method_int8_called, 1)
2147         self.assertEqual(sso.sub_method_int8_called, 0)
2148
2149     def test_callback_in_vfunc(self):
2150         class SubObject(GIMarshallingTests.Object):
2151             def __init__(self):
2152                 GObject.GObject.__init__(self)
2153                 self.worked = False
2154
2155             def do_vfunc_with_callback(self, callback):
2156                 self.worked = callback(42) == 42
2157
2158         _object = SubObject()
2159         _object.call_vfunc_with_callback()
2160         self.assertTrue(_object.worked)
2161         _object.worked = False
2162         _object.call_vfunc_with_callback()
2163         self.assertTrue(_object.worked)
2164
2165
2166 class TestMultiOutputArgs(unittest.TestCase):
2167
2168     def test_int_out_out(self):
2169         self.assertEqual((6, 7), GIMarshallingTests.int_out_out())
2170
2171     def test_int_return_out(self):
2172         self.assertEqual((6, 7), GIMarshallingTests.int_return_out())
2173
2174
2175 class TestGErrorException(unittest.TestCase):
2176     def test_gerror_exception(self):
2177         self.assertRaises(GObject.GError, GIMarshallingTests.gerror)
2178         try:
2179             GIMarshallingTests.gerror()
2180         except Exception:
2181             etype, e = sys.exc_info()[:2]
2182             self.assertEqual(e.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2183             self.assertEqual(e.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2184             self.assertEqual(e.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2185
2186
2187 # Interface
2188
2189
2190 class TestInterfaces(unittest.TestCase):
2191
2192     class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2193         def __init__(self):
2194             GObject.GObject.__init__(self)
2195             self.val = None
2196
2197         def do_test_int8_in(self, int8):
2198             self.val = int8
2199
2200     def setUp(self):
2201         self.instance = self.TestInterfaceImpl()
2202
2203     def test_wrapper(self):
2204         self.assertTrue(issubclass(GIMarshallingTests.Interface, GObject.GInterface))
2205         self.assertEqual(GIMarshallingTests.Interface.__gtype__.name, 'GIMarshallingTestsInterface')
2206         self.assertRaises(NotImplementedError, GIMarshallingTests.Interface)
2207
2208     def test_implementation(self):
2209         self.assertTrue(issubclass(self.TestInterfaceImpl, GIMarshallingTests.Interface))
2210         self.assertTrue(isinstance(self.instance, GIMarshallingTests.Interface))
2211
2212     def test_int8_int(self):
2213         GIMarshallingTests.test_interface_test_int8_in(self.instance, 42)
2214         self.assertEqual(self.instance.val, 42)
2215
2216     def test_subclass(self):
2217         class TestInterfaceImplA(self.TestInterfaceImpl):
2218             pass
2219
2220         class TestInterfaceImplB(TestInterfaceImplA):
2221             pass
2222
2223         instance = TestInterfaceImplA()
2224         GIMarshallingTests.test_interface_test_int8_in(instance, 42)
2225         self.assertEqual(instance.val, 42)
2226
2227     def test_mro(self):
2228         # there was a problem with Python bailing out because of
2229         # http://en.wikipedia.org/wiki/Diamond_problem with interfaces,
2230         # which shouldn't really be a problem.
2231
2232         class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2233             pass
2234
2235         class TestInterfaceImpl2(GIMarshallingTests.Interface,
2236                                  TestInterfaceImpl):
2237             pass
2238
2239         class TestInterfaceImpl3(self.TestInterfaceImpl,
2240                                  GIMarshallingTests.Interface2):
2241             pass
2242
2243     def test_type_mismatch(self):
2244         obj = GIMarshallingTests.Object()
2245
2246         # wrong type for first argument: interface
2247         enum = Gio.File.new_for_path('.').enumerate_children(
2248             '', Gio.FileQueryInfoFlags.NONE, None)
2249         try:
2250             enum.next_file(obj)
2251             self.fail('call with wrong type argument unexpectedly succeeded')
2252         except TypeError as e:
2253             # should have argument name
2254             self.assertTrue('cancellable' in str(e), e)
2255             # should have expected type
2256             self.assertTrue('xpected Gio.Cancellable' in str(e), e)
2257             # should have actual type
2258             self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2259
2260         # wrong type for self argument: interface
2261         try:
2262             Gio.FileEnumerator.next_file(obj, None)
2263             self.fail('call with wrong type argument unexpectedly succeeded')
2264         except TypeError as e:
2265             if sys.version_info < (3, 0):
2266                 self.assertTrue('FileEnumerator' in str(e), e)
2267                 self.assertTrue('Object' in str(e), e)
2268             else:
2269                 # should have argument name
2270                 self.assertTrue('self' in str(e), e)
2271                 # should have expected type
2272                 self.assertTrue('xpected Gio.FileEnumerator' in str(e), e)
2273                 # should have actual type
2274                 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2275
2276         # wrong type for first argument: GObject
2277         var = GLib.Variant('s', 'mystring')
2278         action = Gio.SimpleAction.new('foo', var.get_type())
2279         try:
2280             action.activate(obj)
2281             self.fail('call with wrong type argument unexpectedly succeeded')
2282         except TypeError as e:
2283             # should have argument name
2284             self.assertTrue('parameter' in str(e), e)
2285             # should have expected type
2286             self.assertTrue('xpected GLib.Variant' in str(e), e)
2287             # should have actual type
2288             self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2289
2290         # wrong type for self argument: GObject
2291         try:
2292             Gio.SimpleAction.activate(obj, obj)
2293             self.fail('call with wrong type argument unexpectedly succeeded')
2294         except TypeError as e:
2295             if sys.version_info < (3, 0):
2296                 self.assertTrue('SimpleAction' in str(e), e)
2297                 self.assertTrue('Object' in str(e), e)
2298             else:
2299                 # should have argument name
2300                 self.assertTrue('self' in str(e), e)
2301                 # should have expected type
2302                 self.assertTrue('xpected Gio.Action' in str(e), e)
2303                 # should have actual type
2304                 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2305
2306
2307 class TestInterfaceClash(unittest.TestCase):
2308
2309     def test_clash(self):
2310         def create_clash():
2311             class TestClash(GObject.GObject, GIMarshallingTests.Interface, GIMarshallingTests.Interface2):
2312                 def do_test_int8_in(self, int8):
2313                     pass
2314             TestClash()
2315
2316         self.assertRaises(TypeError, create_clash)
2317
2318
2319 class TestOverrides(unittest.TestCase):
2320
2321     def test_constant(self):
2322         self.assertEqual(GIMarshallingTests.OVERRIDES_CONSTANT, 7)
2323
2324     def test_struct(self):
2325         # Test that the constructor has been overridden.
2326         struct = GIMarshallingTests.OverridesStruct(42)
2327
2328         self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2329
2330         # Test that the method has been overridden.
2331         self.assertEqual(6, struct.method())
2332
2333         del struct
2334
2335         # Test that the overrides wrapper has been registered.
2336         struct = GIMarshallingTests.overrides_struct_returnv()
2337
2338         self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2339
2340         del struct
2341
2342     def test_object(self):
2343         # Test that the constructor has been overridden.
2344         object_ = GIMarshallingTests.OverridesObject(42)
2345
2346         self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2347
2348         # Test that the alternate constructor has been overridden.
2349         object_ = GIMarshallingTests.OverridesObject.new(42)
2350
2351         self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2352
2353         # Test that the method has been overridden.
2354         self.assertEqual(6, object_.method())
2355
2356         # Test that the overrides wrapper has been registered.
2357         object_ = GIMarshallingTests.OverridesObject.returnv()
2358
2359         self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2360
2361     def test_module_name(self):
2362         # overridden types
2363         self.assertEqual(GIMarshallingTests.OverridesStruct.__module__, 'gi.overrides.GIMarshallingTests')
2364         self.assertEqual(GIMarshallingTests.OverridesObject.__module__, 'gi.overrides.GIMarshallingTests')
2365         self.assertEqual(GObject.Object.__module__, 'gi.overrides.GObject')
2366
2367         # not overridden
2368         self.assertEqual(GIMarshallingTests.SubObject.__module__, 'gi.repository.GIMarshallingTests')
2369         # FIXME: does not work with TEST_NAMES='test_thread test_gi.TestOverrides',
2370         # it is importlib._bootstrap then
2371         #self.assertEqual(GObject.InitiallyUnowned.__module__, 'gi.repository.GObject')
2372
2373
2374 class TestDir(unittest.TestCase):
2375     def test_members_list(self):
2376         list = dir(GIMarshallingTests)
2377         self.assertTrue('OverridesStruct' in list)
2378         self.assertTrue('BoxedStruct' in list)
2379         self.assertTrue('OVERRIDES_CONSTANT' in list)
2380         self.assertTrue('GEnum' in list)
2381         self.assertTrue('int32_return_max' in list)
2382
2383     def test_modules_list(self):
2384         import gi.repository
2385         list = dir(gi.repository)
2386         self.assertTrue('GIMarshallingTests' in list)
2387
2388         # FIXME: test to see if a module which was not imported is in the list
2389         #        we should be listing every typelib we find, not just the ones
2390         #        which are imported
2391         #
2392         #        to test this I recommend we compile a fake module which
2393         #        our tests would never import and check to see if it is
2394         #        in the list:
2395         #
2396         # self.assertTrue('DoNotImportDummyTests' in list)
2397
2398
2399 class TestGErrorArrayInCrash(unittest.TestCase):
2400     # Previously there was a bug in invoke, in which C arrays were unwrapped
2401     # from inside GArrays to be passed to the C function. But when a GError was
2402     # set, invoke would attempt to free the C array as if it were a GArray.
2403     # This crash is only for C arrays. It does not happen for C functions which
2404     # take in GArrays. See https://bugzilla.gnome.org/show_bug.cgi?id=642708
2405     def test_gerror_array_in_crash(self):
2406         self.assertRaises(GObject.GError, GIMarshallingTests.gerror_array_in, [1, 2, 3])
2407
2408
2409 class TestGErrorOut(unittest.TestCase):
2410     # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2411     def test_gerror_out(self):
2412         error, debug = GIMarshallingTests.gerror_out()
2413
2414         self.assertIsInstance(error, GObject.GError)
2415         self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2416         self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2417         self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2418         self.assertEqual(debug, GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE)
2419
2420
2421 class TestGErrorOutTransferNone(unittest.TestCase):
2422     # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2423     def test_gerror_out_transfer_none(self):
2424         error, debug = GIMarshallingTests.gerror_out_transfer_none()
2425
2426         self.assertIsInstance(error, GObject.GError)
2427         self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2428         self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2429         self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2430         self.assertEqual(GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE, debug)
2431
2432
2433 class TestGErrorReturn(unittest.TestCase):
2434     # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
2435     def test_return_gerror(self):
2436         error = GIMarshallingTests.gerror_return()
2437
2438         self.assertIsInstance(error, GObject.GError)
2439         self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
2440         self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
2441         self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
2442
2443
2444 class TestParamSpec(unittest.TestCase):
2445     def test_param_spec_return(self):
2446         obj = GIMarshallingTests.param_spec_return()
2447         self.assertEqual(obj.name, 'test-param')
2448         self.assertEqual(obj.nick, 'test')
2449         self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2450
2451     def test_param_spec_out(self):
2452         obj = GIMarshallingTests.param_spec_out()
2453         self.assertEqual(obj.name, 'test-param')
2454         self.assertEqual(obj.nick, 'test')
2455         self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2456
2457
2458 class TestKeywordArgs(unittest.TestCase):
2459
2460     def test_calling(self):
2461         kw_func = GIMarshallingTests.int_three_in_three_out
2462
2463         self.assertEqual(kw_func(1, 2, 3), (1, 2, 3))
2464         self.assertEqual(kw_func(**{'a': 4, 'b': 5, 'c': 6}), (4, 5, 6))
2465         self.assertEqual(kw_func(1, **{'b': 7, 'c': 8}), (1, 7, 8))
2466         self.assertEqual(kw_func(1, 7, **{'c': 8}), (1, 7, 8))
2467         self.assertEqual(kw_func(1, c=8, **{'b': 7}), (1, 7, 8))
2468         self.assertEqual(kw_func(2, c=4, b=3), (2, 3, 4))
2469         self.assertEqual(kw_func(a=2, c=4, b=3), (2, 3, 4))
2470
2471     def assertRaisesMessage(self, exception, message, func, *args, **kwargs):
2472         try:
2473             func(*args, **kwargs)
2474         except exception:
2475             (e_type, e) = sys.exc_info()[:2]
2476             if message is not None:
2477                 self.assertEqual(str(e), message)
2478         except:
2479             raise
2480         else:
2481             msg = "%s() did not raise %s" % (func.__name__, exception.__name__)
2482             raise AssertionError(msg)
2483
2484     def test_type_errors(self):
2485         # test too few args
2486         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2487                                  GIMarshallingTests.int_three_in_three_out)
2488         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (1 given)",
2489                                  GIMarshallingTests.int_three_in_three_out, 1)
2490         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2491                                  GIMarshallingTests.int_three_in_three_out, *())
2492         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
2493                                  GIMarshallingTests.int_three_in_three_out, *(), **{})
2494         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (0 given)",
2495                                  GIMarshallingTests.int_three_in_three_out, *(), **{'c': 4})
2496
2497         # test too many args
2498         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (4 given)",
2499                                  GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4))
2500         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (4 given)",
2501                                  GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4), c=6)
2502
2503         # test too many keyword args
2504         self.assertRaisesMessage(TypeError, "int_three_in_three_out() got multiple values for keyword argument 'a'",
2505                                  GIMarshallingTests.int_three_in_three_out, 1, 2, 3, **{'a': 4, 'b': 5})
2506         self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'd'",
2507                                  GIMarshallingTests.int_three_in_three_out, d=4)
2508         self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'e'",
2509                                  GIMarshallingTests.int_three_in_three_out, **{'e': 2})
2510
2511     def test_kwargs_are_not_modified(self):
2512         d = {'b': 2}
2513         d2 = d.copy()
2514         GIMarshallingTests.int_three_in_three_out(1, c=4, **d)
2515         self.assertEqual(d, d2)
2516
2517
2518 class TestPropertiesObject(unittest.TestCase):
2519
2520     def setUp(self):
2521         self.obj = GIMarshallingTests.PropertiesObject()
2522
2523     def test_boolean(self):
2524         self.assertEqual(self.obj.props.some_boolean, False)
2525         self.obj.props.some_boolean = True
2526         self.assertEqual(self.obj.props.some_boolean, True)
2527
2528         obj = GIMarshallingTests.PropertiesObject(some_boolean=True)
2529         self.assertEqual(obj.props.some_boolean, True)
2530
2531     @unittest.expectedFailure
2532     def test_char(self):
2533         # gobject-introspection thinks it has a guint8 type tag, which is
2534         # wrong; this will raise an assertion critical which we need to ignore
2535         old_mask = GLib.log_set_always_fatal(
2536             GLib.LogLevelFlags.LEVEL_WARNING | GLib.LogLevelFlags.LEVEL_ERROR)
2537         try:
2538             self.assertEqual(self.obj.props.some_char, 0)
2539             self.obj.props.some_char = GObject.G_MAXINT8
2540             self.assertEqual(self.obj.props.some_char, GObject.G_MAXINT8)
2541         finally:
2542             GLib.log_set_always_fatal(old_mask)
2543
2544         obj = GIMarshallingTests.PropertiesObject(some_char=-42)
2545         self.assertEqual(obj.props.some_char, -42)
2546
2547     def test_uchar(self):
2548         self.assertEqual(self.obj.props.some_uchar, 0)
2549         self.obj.props.some_uchar = GObject.G_MAXUINT8
2550         self.assertEqual(self.obj.props.some_uchar, GObject.G_MAXUINT8)
2551
2552         obj = GIMarshallingTests.PropertiesObject(some_uchar=42)
2553         self.assertEqual(obj.props.some_uchar, 42)
2554
2555     def test_int(self):
2556         self.assertEqual(self.obj.props.some_int, 0)
2557         self.obj.props.some_int = GObject.G_MAXINT
2558         self.assertEqual(self.obj.props.some_int, GObject.G_MAXINT)
2559
2560         obj = GIMarshallingTests.PropertiesObject(some_int=-42)
2561         self.assertEqual(obj.props.some_int, -42)
2562
2563         self.assertRaises(TypeError, setattr, self.obj.props, 'some_int', 'foo')
2564         self.assertRaises(TypeError, setattr, self.obj.props, 'some_int', None)
2565
2566         self.assertEqual(obj.props.some_int, -42)
2567
2568     def test_uint(self):
2569         self.assertEqual(self.obj.props.some_uint, 0)
2570         self.obj.props.some_uint = GObject.G_MAXUINT
2571         self.assertEqual(self.obj.props.some_uint, GObject.G_MAXUINT)
2572
2573         obj = GIMarshallingTests.PropertiesObject(some_uint=42)
2574         self.assertEqual(obj.props.some_uint, 42)
2575
2576         self.assertRaises(TypeError, setattr, self.obj.props, 'some_uint', 'foo')
2577         self.assertRaises(TypeError, setattr, self.obj.props, 'some_uint', None)
2578
2579         self.assertEqual(obj.props.some_uint, 42)
2580
2581     def test_long(self):
2582         self.assertEqual(self.obj.props.some_long, 0)
2583         self.obj.props.some_long = GObject.G_MAXLONG
2584         self.assertEqual(self.obj.props.some_long, GObject.G_MAXLONG)
2585
2586         obj = GIMarshallingTests.PropertiesObject(some_long=-42)
2587         self.assertEqual(obj.props.some_long, -42)
2588
2589         self.assertRaises(TypeError, setattr, self.obj.props, 'some_long', 'foo')
2590         self.assertRaises(TypeError, setattr, self.obj.props, 'some_long', None)
2591
2592         self.assertEqual(obj.props.some_long, -42)
2593
2594     def test_ulong(self):
2595         self.assertEqual(self.obj.props.some_ulong, 0)
2596         self.obj.props.some_ulong = GObject.G_MAXULONG
2597         self.assertEqual(self.obj.props.some_ulong, GObject.G_MAXULONG)
2598
2599         obj = GIMarshallingTests.PropertiesObject(some_ulong=42)
2600         self.assertEqual(obj.props.some_ulong, 42)
2601
2602         self.assertRaises(TypeError, setattr, self.obj.props, 'some_ulong', 'foo')
2603         self.assertRaises(TypeError, setattr, self.obj.props, 'some_ulong', None)
2604
2605         self.assertEqual(obj.props.some_ulong, 42)
2606
2607     def test_int64(self):
2608         self.assertEqual(self.obj.props.some_int64, 0)
2609         self.obj.props.some_int64 = GObject.G_MAXINT64
2610         self.assertEqual(self.obj.props.some_int64, GObject.G_MAXINT64)
2611
2612         obj = GIMarshallingTests.PropertiesObject(some_int64=-4200000000000000)
2613         self.assertEqual(obj.props.some_int64, -4200000000000000)
2614
2615     def test_uint64(self):
2616         self.assertEqual(self.obj.props.some_uint64, 0)
2617         self.obj.props.some_uint64 = GObject.G_MAXUINT64
2618         self.assertEqual(self.obj.props.some_uint64, GObject.G_MAXUINT64)
2619
2620         obj = GIMarshallingTests.PropertiesObject(some_uint64=4200000000000000)
2621         self.assertEqual(obj.props.some_uint64, 4200000000000000)
2622
2623     def test_float(self):
2624         self.assertEqual(self.obj.props.some_float, 0)
2625         self.obj.props.some_float = GObject.G_MAXFLOAT
2626         self.assertEqual(self.obj.props.some_float, GObject.G_MAXFLOAT)
2627
2628         obj = GIMarshallingTests.PropertiesObject(some_float=42.42)
2629         self.assertAlmostEqual(obj.props.some_float, 42.42, 4)
2630
2631         obj = GIMarshallingTests.PropertiesObject(some_float=42)
2632         self.assertAlmostEqual(obj.props.some_float, 42.0, 4)
2633
2634         self.assertRaises(TypeError, setattr, self.obj.props, 'some_float', 'foo')
2635         self.assertRaises(TypeError, setattr, self.obj.props, 'some_float', None)
2636
2637         self.assertAlmostEqual(obj.props.some_float, 42.0, 4)
2638
2639     def test_double(self):
2640         self.assertEqual(self.obj.props.some_double, 0)
2641         self.obj.props.some_double = GObject.G_MAXDOUBLE
2642         self.assertEqual(self.obj.props.some_double, GObject.G_MAXDOUBLE)
2643
2644         obj = GIMarshallingTests.PropertiesObject(some_double=42.42)
2645         self.assertAlmostEqual(obj.props.some_double, 42.42)
2646
2647         obj = GIMarshallingTests.PropertiesObject(some_double=42)
2648         self.assertAlmostEqual(obj.props.some_double, 42.0)
2649
2650         self.assertRaises(TypeError, setattr, self.obj.props, 'some_double', 'foo')
2651         self.assertRaises(TypeError, setattr, self.obj.props, 'some_double', None)
2652
2653         self.assertAlmostEqual(obj.props.some_double, 42.0)
2654
2655     def test_strv(self):
2656         self.assertEqual(self.obj.props.some_strv, [])
2657         self.obj.props.some_strv = ['hello', 'world']
2658         self.assertEqual(self.obj.props.some_strv, ['hello', 'world'])
2659
2660         self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', 1)
2661         self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', 'foo')
2662         self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', [1, 2])
2663         self.assertRaises(TypeError, setattr, self.obj.props, 'some_strv', ['foo', 1])
2664
2665         self.assertEqual(self.obj.props.some_strv, ['hello', 'world'])
2666
2667         obj = GIMarshallingTests.PropertiesObject(some_strv=['hello', 'world'])
2668         self.assertEqual(obj.props.some_strv, ['hello', 'world'])
2669
2670     def test_boxed_struct(self):
2671         self.assertEqual(self.obj.props.some_boxed_struct, None)
2672
2673         class GStrv(list):
2674             __gtype__ = GObject.TYPE_STRV
2675
2676         struct1 = GIMarshallingTests.BoxedStruct()
2677         struct1.long_ = 1
2678
2679         self.obj.props.some_boxed_struct = struct1
2680         self.assertEqual(self.obj.props.some_boxed_struct.long_, 1)
2681         self.assertEqual(self.obj.some_boxed_struct.long_, 1)
2682
2683         self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_struct', 1)
2684         self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_struct', 'foo')
2685
2686         obj = GIMarshallingTests.PropertiesObject(some_boxed_struct=struct1)
2687         self.assertEqual(obj.props.some_boxed_struct.long_, 1)
2688
2689     @unittest.skipUnless(hasattr(GIMarshallingTests.PropertiesObject, 'some_variant'),
2690                          'too old gobject-introspection')
2691     def test_variant(self):
2692         self.assertEqual(self.obj.props.some_variant, None)
2693
2694         self.obj.props.some_variant = GLib.Variant('o', '/myobj')
2695         self.assertEqual(self.obj.props.some_variant.get_type_string(), 'o')
2696         self.assertEqual(self.obj.props.some_variant.print_(False), "'/myobj'")
2697
2698         self.obj.props.some_variant = None
2699         self.assertEqual(self.obj.props.some_variant, None)
2700
2701         obj = GIMarshallingTests.PropertiesObject(some_variant=GLib.Variant('b', True))
2702         self.assertEqual(obj.props.some_variant.get_type_string(), 'b')
2703         self.assertEqual(obj.props.some_variant.get_boolean(), True)
2704
2705         self.assertRaises(TypeError, setattr, self.obj.props, 'some_variant', 'foo')
2706         self.assertRaises(TypeError, setattr, self.obj.props, 'some_variant', 23)
2707
2708         self.assertEqual(obj.props.some_variant.get_type_string(), 'b')
2709         self.assertEqual(obj.props.some_variant.get_boolean(), True)
2710
2711
2712 class TestKeywords(unittest.TestCase):
2713     def test_method(self):
2714         # g_variant_print()
2715         v = GLib.Variant('i', 1)
2716         self.assertEqual(v.print_(False), '1')
2717
2718     def test_function(self):
2719         # g_thread_yield()
2720         self.assertEqual(GLib.Thread.yield_(), None)
2721
2722     def test_struct_method(self):
2723         # g_timer_continue()
2724         # we cannot currently instantiate GLib.Timer objects, so just ensure
2725         # the method exists
2726         self.assertTrue(callable(GLib.Timer.continue_))
2727
2728     def test_uppercase(self):
2729         self.assertEqual(GLib.IOCondition.IN.value_nicks, ['in'])
2730
2731
2732 class TestModule(unittest.TestCase):
2733     def test_path(self):
2734         self.assertTrue(GIMarshallingTests.__path__.endswith('GIMarshallingTests-1.0.typelib'),
2735                         GIMarshallingTests.__path__)
2736
2737     def test_str(self):
2738         self.assertTrue("'GIMarshallingTests' from '" in str(GIMarshallingTests),
2739                         str(GIMarshallingTests))
2740
2741     def test_dir(self):
2742         _dir = dir(GIMarshallingTests)
2743         self.assertGreater(len(_dir), 10)
2744
2745         self.assertTrue('SimpleStruct' in _dir)
2746         self.assertTrue('Interface2' in _dir)
2747         self.assertTrue('CONSTANT_GERROR_CODE' in _dir)
2748         self.assertTrue('array_zero_terminated_inout' in _dir)
2749
2750         # assert that dir() does not contain garbage
2751         for item_name in _dir:
2752             item = getattr(GIMarshallingTests, item_name)
2753             self.assertTrue(hasattr(item, '__class__'))
2754
2755     def test_help(self):
2756         orig_stdout = sys.stdout
2757         try:
2758             if sys.version_info < (3, 0):
2759                 sys.stdout = BytesIO()
2760             else:
2761                 sys.stdout = StringIO()
2762             help(GIMarshallingTests)
2763             output = sys.stdout.getvalue()
2764         finally:
2765             sys.stdout = orig_stdout
2766
2767         self.assertTrue('SimpleStruct' in output, output)
2768         self.assertTrue('Interface2' in output, output)
2769         self.assertTrue('method_array_inout' in output, output)
2770
2771
2772 class TestProjectVersion(unittest.TestCase):
2773     def test_version_str(self):
2774         self.assertGreaterEqual(gi.__version__, "3.3.5")
2775
2776     def test_version_info(self):
2777         self.assertEqual(len(gi.version_info), 3)
2778         self.assertGreaterEqual(gi.version_info, (3, 3, 5))
2779
2780     def test_check_version(self):
2781         self.assertRaises(ValueError, gi.check_version, (99, 0, 0))
2782         self.assertRaises(ValueError, gi.check_version, "99.0.0")
2783         gi.check_version((3, 3, 5))
2784         gi.check_version("3.3.5")
2785
2786
2787 class TestObjectInfo(unittest.TestCase):
2788     def test_get_abstract_with_abstract(self):
2789         repo = gi.gi.Repository.get_default()
2790         info = repo.find_by_name('GObject', 'TypeModule')
2791         self.assertTrue(info.get_abstract())
2792
2793     def test_get_abstract_with_concrete(self):
2794         repo = gi.gi.Repository.get_default()
2795         info = repo.find_by_name('GObject', 'Object')
2796         self.assertFalse(info.get_abstract())
2797
2798
2799 class TestSignatureArgs(unittest.TestCase):
2800     def test_split_args_multi_out(self):
2801         in_args, out_args = gi.types.split_function_info_args(GIMarshallingTests.int_out_out.__info__)
2802         self.assertEqual(len(in_args), 0)
2803         self.assertEqual(len(out_args), 2)
2804         self.assertEqual(out_args[0].get_pytype_hint(), 'int')
2805         self.assertEqual(out_args[1].get_pytype_hint(), 'int')
2806
2807     def test_split_args_inout(self):
2808         in_args, out_args = gi.types.split_function_info_args(GIMarshallingTests.long_inout_max_min.__info__)
2809         self.assertEqual(len(in_args), 1)
2810         self.assertEqual(len(out_args), 1)
2811         self.assertEqual(in_args[0].get_name(), out_args[0].get_name())
2812         self.assertEqual(in_args[0].get_pytype_hint(), out_args[0].get_pytype_hint())
2813
2814     def test_split_args_none(self):
2815         obj = GIMarshallingTests.Object(int=33)
2816         in_args, out_args = gi.types.split_function_info_args(obj.none_inout.__info__)
2817         self.assertEqual(len(in_args), 1)
2818         self.assertEqual(len(out_args), 1)
2819
2820     def test_final_signature_with_full_inout(self):
2821         self.assertEqual(GIMarshallingTests.Object.full_inout.__doc__,
2822                          'full_inout(object:GIMarshallingTests.Object) -> object:GIMarshallingTests.Object')
2823
2824     def test_overridden_doc_is_not_clobbered(self):
2825         self.assertEqual(GIMarshallingTests.OverridesObject.method.__doc__,
2826                          'Overridden doc string.')