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