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