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