Imported Upstream version 2.90.2
[platform/upstream/pygobject2.git] / tests / test_gi.py
1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # coding=utf-8
3 # vim: tabstop=4 shiftwidth=4 expandtab
4
5 import sys
6
7 import unittest
8 import tempfile
9 import shutil
10 import os
11 import locale
12 import subprocess
13 from gi.repository import GObject
14
15 from gi.repository import GIMarshallingTests
16
17 from compathelper import _bytes
18
19 if sys.version_info < (3, 0):
20     CONSTANT_UTF8 = "const \xe2\x99\xa5 utf8"
21     PY2_UNICODE_UTF8 = unicode(CONSTANT_UTF8, 'UTF-8')
22     CHAR_255='\xff'
23 else:
24     CONSTANT_UTF8 = "const ♥ utf8"
25     CHAR_255=bytes([255])
26
27 CONSTANT_NUMBER = 42
28
29
30 class Number(object):
31
32     def __init__(self, value):
33         self.value = value
34
35     def __int__(self):
36         return int(self.value)
37
38     def __float__(self):
39         return float(self.value)
40
41
42 class Sequence(object):
43
44     def __init__(self, sequence):
45         self.sequence = sequence
46
47     def __len__(self):
48         return len(self.sequence)
49
50     def __getitem__(self, key):
51         return self.sequence[key]
52
53
54 class TestConstant(unittest.TestCase):
55
56 # Blocked by https://bugzilla.gnome.org/show_bug.cgi?id=595773
57 #    def test_constant_utf8(self):
58 #        self.assertEquals(CONSTANT_UTF8, GIMarshallingTests.CONSTANT_UTF8)
59
60     def test_constant_number(self):
61         self.assertEquals(CONSTANT_NUMBER, GIMarshallingTests.CONSTANT_NUMBER)
62
63
64 class TestBoolean(unittest.TestCase):
65
66     def test_boolean_return(self):
67         self.assertEquals(True, GIMarshallingTests.boolean_return_true())
68         self.assertEquals(False, GIMarshallingTests.boolean_return_false())
69
70     def test_boolean_in(self):
71         GIMarshallingTests.boolean_in_true(True)
72         GIMarshallingTests.boolean_in_false(False)
73
74         GIMarshallingTests.boolean_in_true(1)
75         GIMarshallingTests.boolean_in_false(0)
76
77     def test_boolean_out(self):
78         self.assertEquals(True, GIMarshallingTests.boolean_out_true())
79         self.assertEquals(False, GIMarshallingTests.boolean_out_false())
80
81     def test_boolean_inout(self):
82         self.assertEquals(False, GIMarshallingTests.boolean_inout_true_false(True))
83         self.assertEquals(True, GIMarshallingTests.boolean_inout_false_true(False))
84
85
86 class TestInt8(unittest.TestCase):
87
88     MAX = GObject.G_MAXINT8
89     MIN = GObject.G_MININT8
90
91     def test_int8_return(self):
92         self.assertEquals(self.MAX, GIMarshallingTests.int8_return_max())
93         self.assertEquals(self.MIN, GIMarshallingTests.int8_return_min())
94
95     def test_int8_in(self):
96         max = Number(self.MAX)
97         min = Number(self.MIN)
98
99         GIMarshallingTests.int8_in_max(max)
100         GIMarshallingTests.int8_in_min(min)
101
102         max.value += 1
103         min.value -= 1
104
105         self.assertRaises(ValueError, GIMarshallingTests.int8_in_max, max)
106         self.assertRaises(ValueError, GIMarshallingTests.int8_in_min, min)
107
108         self.assertRaises(TypeError, GIMarshallingTests.int8_in_max, "self.MAX")
109
110     def test_int8_out(self):
111         self.assertEquals(self.MAX, GIMarshallingTests.int8_out_max())
112         self.assertEquals(self.MIN, GIMarshallingTests.int8_out_min())
113
114     def test_int8_inout(self):
115         self.assertEquals(self.MIN, GIMarshallingTests.int8_inout_max_min(Number(self.MAX)))
116         self.assertEquals(self.MAX, GIMarshallingTests.int8_inout_min_max(Number(self.MIN)))
117
118
119 class TestUInt8(unittest.TestCase):
120
121     MAX = GObject.G_MAXUINT8
122
123     def test_uint8_return(self):
124         self.assertEquals(self.MAX, GIMarshallingTests.uint8_return())
125
126     def test_uint8_in(self):
127         number = Number(self.MAX)
128
129         GIMarshallingTests.uint8_in(number)
130         GIMarshallingTests.uint8_in(CHAR_255)
131
132         number.value += 1
133         self.assertRaises(ValueError, GIMarshallingTests.uint8_in, number)
134         self.assertRaises(ValueError, GIMarshallingTests.uint8_in, Number(-1))
135
136         self.assertRaises(TypeError, GIMarshallingTests.uint8_in, "self.MAX")
137
138     def test_uint8_out(self):
139         self.assertEquals(self.MAX, GIMarshallingTests.uint8_out())
140
141     def test_uint8_inout(self):
142         self.assertEquals(0, GIMarshallingTests.uint8_inout(Number(self.MAX)))
143
144
145 class TestInt16(unittest.TestCase):
146
147     MAX = GObject.G_MAXINT16
148     MIN = GObject.G_MININT16
149
150     def test_int16_return(self):
151         self.assertEquals(self.MAX, GIMarshallingTests.int16_return_max())
152         self.assertEquals(self.MIN, GIMarshallingTests.int16_return_min())
153
154     def test_int16_in(self):
155         max = Number(self.MAX)
156         min = Number(self.MIN)
157
158         GIMarshallingTests.int16_in_max(max)
159         GIMarshallingTests.int16_in_min(min)
160
161         max.value += 1
162         min.value -= 1
163
164         self.assertRaises(ValueError, GIMarshallingTests.int16_in_max, max)
165         self.assertRaises(ValueError, GIMarshallingTests.int16_in_min, min)
166
167         self.assertRaises(TypeError, GIMarshallingTests.int16_in_max, "self.MAX")
168
169     def test_int16_out(self):
170         self.assertEquals(self.MAX, GIMarshallingTests.int16_out_max())
171         self.assertEquals(self.MIN, GIMarshallingTests.int16_out_min())
172
173     def test_int16_inout(self):
174         self.assertEquals(self.MIN, GIMarshallingTests.int16_inout_max_min(Number(self.MAX)))
175         self.assertEquals(self.MAX, GIMarshallingTests.int16_inout_min_max(Number(self.MIN)))
176
177
178 class TestUInt16(unittest.TestCase):
179
180     MAX = GObject.G_MAXUINT16
181
182     def test_uint16_return(self):
183         self.assertEquals(self.MAX, GIMarshallingTests.uint16_return())
184
185     def test_uint16_in(self):
186         number = Number(self.MAX)
187
188         GIMarshallingTests.uint16_in(number)
189
190         number.value += 1
191
192         self.assertRaises(ValueError, GIMarshallingTests.uint16_in, number)
193         self.assertRaises(ValueError, GIMarshallingTests.uint16_in, Number(-1))
194
195         self.assertRaises(TypeError, GIMarshallingTests.uint16_in, "self.MAX")
196
197     def test_uint16_out(self):
198         self.assertEquals(self.MAX, GIMarshallingTests.uint16_out())
199
200     def test_uint16_inout(self):
201         self.assertEquals(0, GIMarshallingTests.uint16_inout(Number(self.MAX)))
202
203
204 class TestInt32(unittest.TestCase):
205
206     MAX = GObject.G_MAXINT32
207     MIN = GObject.G_MININT32
208
209     def test_int32_return(self):
210         self.assertEquals(self.MAX, GIMarshallingTests.int32_return_max())
211         self.assertEquals(self.MIN, GIMarshallingTests.int32_return_min())
212
213     def test_int32_in(self):
214         max = Number(self.MAX)
215         min = Number(self.MIN)
216
217         GIMarshallingTests.int32_in_max(max)
218         GIMarshallingTests.int32_in_min(min)
219
220         max.value += 1
221         min.value -= 1
222
223         self.assertRaises(ValueError, GIMarshallingTests.int32_in_max, max)
224         self.assertRaises(ValueError, GIMarshallingTests.int32_in_min, min)
225
226         self.assertRaises(TypeError, GIMarshallingTests.int32_in_max, "self.MAX")
227
228     def test_int32_out(self):
229         self.assertEquals(self.MAX, GIMarshallingTests.int32_out_max())
230         self.assertEquals(self.MIN, GIMarshallingTests.int32_out_min())
231
232     def test_int32_inout(self):
233         self.assertEquals(self.MIN, GIMarshallingTests.int32_inout_max_min(Number(self.MAX)))
234         self.assertEquals(self.MAX, GIMarshallingTests.int32_inout_min_max(Number(self.MIN)))
235
236
237 class TestUInt32(unittest.TestCase):
238
239     MAX = GObject.G_MAXUINT32
240
241     def test_uint32_return(self):
242         self.assertEquals(self.MAX, GIMarshallingTests.uint32_return())
243
244     def test_uint32_in(self):
245         number = Number(self.MAX)
246
247         GIMarshallingTests.uint32_in(number)
248
249         number.value += 1
250
251         self.assertRaises(ValueError, GIMarshallingTests.uint32_in, number)
252         self.assertRaises(ValueError, GIMarshallingTests.uint32_in, Number(-1))
253
254         self.assertRaises(TypeError, GIMarshallingTests.uint32_in, "self.MAX")
255
256     def test_uint32_out(self):
257         self.assertEquals(self.MAX, GIMarshallingTests.uint32_out())
258
259     def test_uint32_inout(self):
260         self.assertEquals(0, GIMarshallingTests.uint32_inout(Number(self.MAX)))
261
262
263 class TestInt64(unittest.TestCase):
264
265     MAX = 2 ** 63 - 1
266     MIN = - (2 ** 63)
267
268     def test_int64_return(self):
269         self.assertEquals(self.MAX, GIMarshallingTests.int64_return_max())
270         self.assertEquals(self.MIN, GIMarshallingTests.int64_return_min())
271
272     def test_int64_in(self):
273         max = Number(self.MAX)
274         min = Number(self.MIN)
275
276         GIMarshallingTests.int64_in_max(max)
277         GIMarshallingTests.int64_in_min(min)
278
279         max.value += 1
280         min.value -= 1
281
282         self.assertRaises(ValueError, GIMarshallingTests.int64_in_max, max)
283         self.assertRaises(ValueError, GIMarshallingTests.int64_in_min, min)
284
285         self.assertRaises(TypeError, GIMarshallingTests.int64_in_max, "self.MAX")
286
287     def test_int64_out(self):
288         self.assertEquals(self.MAX, GIMarshallingTests.int64_out_max())
289         self.assertEquals(self.MIN, GIMarshallingTests.int64_out_min())
290
291     def test_int64_inout(self):
292         self.assertEquals(self.MIN, GIMarshallingTests.int64_inout_max_min(Number(self.MAX)))
293         self.assertEquals(self.MAX, GIMarshallingTests.int64_inout_min_max(Number(self.MIN)))
294
295
296 class TestUInt64(unittest.TestCase):
297
298     MAX = 2 ** 64 - 1
299
300     def test_uint64_return(self):
301         self.assertEquals(self.MAX, GIMarshallingTests.uint64_return())
302
303     def test_uint64_in(self):
304         number = Number(self.MAX)
305
306         GIMarshallingTests.uint64_in(number)
307
308         number.value += 1
309
310         self.assertRaises(ValueError, GIMarshallingTests.uint64_in, number)
311         self.assertRaises(ValueError, GIMarshallingTests.uint64_in, Number(-1))
312
313         self.assertRaises(TypeError, GIMarshallingTests.uint64_in, "self.MAX")
314
315     def test_uint64_out(self):
316         self.assertEquals(self.MAX, GIMarshallingTests.uint64_out())
317
318     def test_uint64_inout(self):
319         self.assertEquals(0, GIMarshallingTests.uint64_inout(Number(self.MAX)))
320
321
322 class TestShort(unittest.TestCase):
323
324     MAX = GObject.constants.G_MAXSHORT
325     MIN = GObject.constants.G_MINSHORT
326
327     def test_short_return(self):
328         self.assertEquals(self.MAX, GIMarshallingTests.short_return_max())
329         self.assertEquals(self.MIN, GIMarshallingTests.short_return_min())
330
331     def test_short_in(self):
332         max = Number(self.MAX)
333         min = Number(self.MIN)
334
335         GIMarshallingTests.short_in_max(max)
336         GIMarshallingTests.short_in_min(min)
337
338         max.value += 1
339         min.value -= 1
340
341         self.assertRaises(ValueError, GIMarshallingTests.short_in_max, max)
342         self.assertRaises(ValueError, GIMarshallingTests.short_in_min, min)
343
344         self.assertRaises(TypeError, GIMarshallingTests.short_in_max, "self.MAX")
345
346     def test_short_out(self):
347         self.assertEquals(self.MAX, GIMarshallingTests.short_out_max())
348         self.assertEquals(self.MIN, GIMarshallingTests.short_out_min())
349
350     def test_short_inout(self):
351         self.assertEquals(self.MIN, GIMarshallingTests.short_inout_max_min(Number(self.MAX)))
352         self.assertEquals(self.MAX, GIMarshallingTests.short_inout_min_max(Number(self.MIN)))
353
354
355 class TestUShort(unittest.TestCase):
356
357     MAX = GObject.constants.G_MAXUSHORT
358
359     def test_ushort_return(self):
360         self.assertEquals(self.MAX, GIMarshallingTests.ushort_return())
361
362     def test_ushort_in(self):
363         number = Number(self.MAX)
364
365         GIMarshallingTests.ushort_in(number)
366
367         number.value += 1
368
369         self.assertRaises(ValueError, GIMarshallingTests.ushort_in, number)
370         self.assertRaises(ValueError, GIMarshallingTests.ushort_in, Number(-1))
371
372         self.assertRaises(TypeError, GIMarshallingTests.ushort_in, "self.MAX")
373
374     def test_ushort_out(self):
375         self.assertEquals(self.MAX, GIMarshallingTests.ushort_out())
376
377     def test_ushort_inout(self):
378         self.assertEquals(0, GIMarshallingTests.ushort_inout(Number(self.MAX)))
379
380
381 class TestInt(unittest.TestCase):
382
383     MAX = GObject.constants.G_MAXINT
384     MIN = GObject.constants.G_MININT
385
386     def test_int_return(self):
387         self.assertEquals(self.MAX, GIMarshallingTests.int_return_max())
388         self.assertEquals(self.MIN, GIMarshallingTests.int_return_min())
389
390     def test_int_in(self):
391         max = Number(self.MAX)
392         min = Number(self.MIN)
393
394         GIMarshallingTests.int_in_max(max)
395         GIMarshallingTests.int_in_min(min)
396
397         max.value += 1
398         min.value -= 1
399
400         self.assertRaises(ValueError, GIMarshallingTests.int_in_max, max)
401         self.assertRaises(ValueError, GIMarshallingTests.int_in_min, min)
402
403         self.assertRaises(TypeError, GIMarshallingTests.int_in_max, "self.MAX")
404
405     def test_int_out(self):
406         self.assertEquals(self.MAX, GIMarshallingTests.int_out_max())
407         self.assertEquals(self.MIN, GIMarshallingTests.int_out_min())
408
409     def test_int_inout(self):
410         self.assertEquals(self.MIN, GIMarshallingTests.int_inout_max_min(Number(self.MAX)))
411         self.assertEquals(self.MAX, GIMarshallingTests.int_inout_min_max(Number(self.MIN)))
412         self.assertRaises(TypeError, GIMarshallingTests.int_inout_min_max, Number(self.MIN), CONSTANT_NUMBER)
413
414
415 class TestUInt(unittest.TestCase):
416
417     MAX = GObject.constants.G_MAXUINT
418
419     def test_uint_return(self):
420         self.assertEquals(self.MAX, GIMarshallingTests.uint_return())
421
422     def test_uint_in(self):
423         number = Number(self.MAX)
424
425         GIMarshallingTests.uint_in(number)
426
427         number.value += 1
428
429         self.assertRaises(ValueError, GIMarshallingTests.uint_in, number)
430         self.assertRaises(ValueError, GIMarshallingTests.uint_in, Number(-1))
431
432         self.assertRaises(TypeError, GIMarshallingTests.uint_in, "self.MAX")
433
434     def test_uint_out(self):
435         self.assertEquals(self.MAX, GIMarshallingTests.uint_out())
436
437     def test_uint_inout(self):
438         self.assertEquals(0, GIMarshallingTests.uint_inout(Number(self.MAX)))
439
440
441 class TestLong(unittest.TestCase):
442
443     MAX = GObject.constants.G_MAXLONG
444     MIN = GObject.constants.G_MINLONG
445
446     def test_long_return(self):
447         self.assertEquals(self.MAX, GIMarshallingTests.long_return_max())
448         self.assertEquals(self.MIN, GIMarshallingTests.long_return_min())
449
450     def test_long_in(self):
451         max = Number(self.MAX)
452         min = Number(self.MIN)
453
454         GIMarshallingTests.long_in_max(max)
455         GIMarshallingTests.long_in_min(min)
456
457         max.value += 1
458         min.value -= 1
459
460         self.assertRaises(ValueError, GIMarshallingTests.long_in_max, max)
461         self.assertRaises(ValueError, GIMarshallingTests.long_in_min, min)
462
463         self.assertRaises(TypeError, GIMarshallingTests.long_in_max, "self.MAX")
464
465     def test_long_out(self):
466         self.assertEquals(self.MAX, GIMarshallingTests.long_out_max())
467         self.assertEquals(self.MIN, GIMarshallingTests.long_out_min())
468
469     def test_long_inout(self):
470         self.assertEquals(self.MIN, GIMarshallingTests.long_inout_max_min(Number(self.MAX)))
471         self.assertEquals(self.MAX, GIMarshallingTests.long_inout_min_max(Number(self.MIN)))
472
473
474 class TestULong(unittest.TestCase):
475
476     MAX = GObject.constants.G_MAXULONG
477
478     def test_ulong_return(self):
479         self.assertEquals(self.MAX, GIMarshallingTests.ulong_return())
480
481     def test_ulong_in(self):
482         number = Number(self.MAX)
483
484         GIMarshallingTests.ulong_in(number)
485
486         number.value += 1
487
488         self.assertRaises(ValueError, GIMarshallingTests.ulong_in, number)
489         self.assertRaises(ValueError, GIMarshallingTests.ulong_in, Number(-1))
490
491         self.assertRaises(TypeError, GIMarshallingTests.ulong_in, "self.MAX")
492
493     def test_ulong_out(self):
494         self.assertEquals(self.MAX, GIMarshallingTests.ulong_out())
495
496     def test_ulong_inout(self):
497         self.assertEquals(0, GIMarshallingTests.ulong_inout(Number(self.MAX)))
498
499
500 class TestSSize(unittest.TestCase):
501
502     MAX = GObject.constants.G_MAXLONG
503     MIN = GObject.constants.G_MINLONG
504
505     def test_ssize_return(self):
506         self.assertEquals(self.MAX, GIMarshallingTests.ssize_return_max())
507         self.assertEquals(self.MIN, GIMarshallingTests.ssize_return_min())
508
509     def test_ssize_in(self):
510         max = Number(self.MAX)
511         min = Number(self.MIN)
512
513         GIMarshallingTests.ssize_in_max(max)
514         GIMarshallingTests.ssize_in_min(min)
515
516         max.value += 1
517         min.value -= 1
518
519         self.assertRaises(ValueError, GIMarshallingTests.ssize_in_max, max)
520         self.assertRaises(ValueError, GIMarshallingTests.ssize_in_min, min)
521
522         self.assertRaises(TypeError, GIMarshallingTests.ssize_in_max, "self.MAX")
523
524     def test_ssize_out(self):
525         self.assertEquals(self.MAX, GIMarshallingTests.ssize_out_max())
526         self.assertEquals(self.MIN, GIMarshallingTests.ssize_out_min())
527
528     def test_ssize_inout(self):
529         self.assertEquals(self.MIN, GIMarshallingTests.ssize_inout_max_min(Number(self.MAX)))
530         self.assertEquals(self.MAX, GIMarshallingTests.ssize_inout_min_max(Number(self.MIN)))
531
532
533 class TestSize(unittest.TestCase):
534
535     MAX = GObject.constants.G_MAXULONG
536
537     def test_size_return(self):
538         self.assertEquals(self.MAX, GIMarshallingTests.size_return())
539
540     def test_size_in(self):
541         number = Number(self.MAX)
542
543         GIMarshallingTests.size_in(number)
544
545         number.value += 1
546
547         self.assertRaises(ValueError, GIMarshallingTests.size_in, number)
548         self.assertRaises(ValueError, GIMarshallingTests.size_in, Number(-1))
549
550         self.assertRaises(TypeError, GIMarshallingTests.size_in, "self.MAX")
551
552     def test_size_out(self):
553         self.assertEquals(self.MAX, GIMarshallingTests.size_out())
554
555     def test_size_inout(self):
556         self.assertEquals(0, GIMarshallingTests.size_inout(Number(self.MAX)))
557
558
559 class TestFloat(unittest.TestCase):
560
561     MAX = GObject.constants.G_MAXFLOAT
562     MIN = GObject.constants.G_MINFLOAT
563
564     def test_float_return(self):
565         self.assertAlmostEquals(self.MAX, GIMarshallingTests.float_return())
566
567     def test_float_in(self):
568         GIMarshallingTests.float_in(Number(self.MAX))
569
570         self.assertRaises(TypeError, GIMarshallingTests.float_in, "self.MAX")
571
572     def test_float_out(self):
573         self.assertAlmostEquals(self.MAX, GIMarshallingTests.float_out())
574
575     def test_float_inout(self):
576         self.assertAlmostEquals(self.MIN, GIMarshallingTests.float_inout(Number(self.MAX)))
577
578
579 class TestDouble(unittest.TestCase):
580
581     MAX = GObject.constants.G_MAXDOUBLE
582     MIN = GObject.constants.G_MINDOUBLE
583
584     def test_double_return(self):
585         self.assertAlmostEquals(self.MAX, GIMarshallingTests.double_return())
586
587     def test_double_in(self):
588         GIMarshallingTests.double_in(Number(self.MAX))
589
590         self.assertRaises(TypeError, GIMarshallingTests.double_in, "self.MAX")
591
592     def test_double_out(self):
593         self.assertAlmostEquals(self.MAX, GIMarshallingTests.double_out())
594
595     def test_double_inout(self):
596         self.assertAlmostEquals(self.MIN, GIMarshallingTests.double_inout(Number(self.MAX)))
597
598
599 class TestGType(unittest.TestCase):
600
601     def test_gtype_return(self):
602         self.assertEquals(GObject.TYPE_NONE, GIMarshallingTests.gtype_return())
603
604     def test_gtype_in(self):
605         GIMarshallingTests.gtype_in(GObject.TYPE_NONE)
606
607         self.assertRaises(TypeError, GIMarshallingTests.gtype_in, "GObject.TYPE_NONE")
608
609     def test_gtype_out(self):
610         self.assertEquals(GObject.TYPE_NONE, GIMarshallingTests.gtype_out())
611
612     def test_gtype_inout(self):
613         self.assertEquals(GObject.TYPE_INT, GIMarshallingTests.gtype_inout(GObject.TYPE_NONE))
614
615
616 class TestUtf8(unittest.TestCase):
617
618     def test_utf8_none_return(self):
619         self.assertEquals(CONSTANT_UTF8, GIMarshallingTests.utf8_none_return())
620
621     def test_utf8_full_return(self):
622         self.assertEquals(CONSTANT_UTF8, GIMarshallingTests.utf8_full_return())
623
624     def test_utf8_none_in(self):
625         GIMarshallingTests.utf8_none_in(CONSTANT_UTF8)
626         if sys.version_info < (3, 0):
627             GIMarshallingTests.utf8_none_in(PY2_UNICODE_UTF8)
628
629         self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, CONSTANT_NUMBER)
630         self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, None)
631
632     def test_utf8_none_out(self):
633         self.assertEquals(CONSTANT_UTF8, GIMarshallingTests.utf8_none_out())
634
635     def test_utf8_full_out(self):
636         self.assertEquals(CONSTANT_UTF8, GIMarshallingTests.utf8_full_out())
637
638     def test_utf8_dangling_out(self):
639         GIMarshallingTests.utf8_dangling_out()
640
641     def test_utf8_none_inout(self):
642         self.assertEquals("", GIMarshallingTests.utf8_none_inout(CONSTANT_UTF8))
643
644     def test_utf8_full_inout(self):
645         self.assertEquals("", GIMarshallingTests.utf8_full_inout(CONSTANT_UTF8))
646
647
648 class TestArray(unittest.TestCase):
649
650     def test_array_fixed_int_return(self):
651         self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.array_fixed_int_return())
652
653     def test_array_fixed_short_return(self):
654         self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.array_fixed_short_return())
655
656     def test_array_fixed_int_in(self):
657         GIMarshallingTests.array_fixed_int_in(Sequence([-1, 0, 1, 2]))
658
659         self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, Sequence([-1, '0', 1, 2]))
660
661         self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, 42)
662         self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, None)
663
664     def test_array_fixed_short_in(self):
665         GIMarshallingTests.array_fixed_short_in(Sequence([-1, 0, 1, 2]))
666
667     def test_array_fixed_out(self):
668         self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.array_fixed_out())
669
670     def test_array_fixed_inout(self):
671         self.assertEquals([2, 1, 0, -1], GIMarshallingTests.array_fixed_inout([-1, 0, 1, 2]))
672
673
674     def test_array_return(self):
675         self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.array_return())
676
677     def test_array_in(self):
678         GIMarshallingTests.array_in(Sequence([-1, 0, 1, 2]))
679
680     def test_array_uint8_in(self):
681         GIMarshallingTests.array_uint8_in(Sequence([97, 98, 99, 100]))
682         GIMarshallingTests.array_uint8_in(_bytes("abcd"))
683
684     def test_array_out(self):
685         self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.array_out())
686
687     def test_array_inout(self):
688         self.assertEquals([-2, -1, 0, 1, 2], GIMarshallingTests.array_inout(Sequence([-1, 0, 1, 2])))
689
690     def test_method_array_in(self):
691         object_ = GIMarshallingTests.Object()
692         object_.method_array_in(Sequence([-1, 0, 1, 2]))
693
694     def test_method_array_out(self):
695         object_ = GIMarshallingTests.Object()
696         self.assertEquals([-1, 0, 1, 2], object_.method_array_out())
697
698     def test_method_array_inout(self):
699         object_ = GIMarshallingTests.Object()
700         self.assertEquals([-2, -1, 0, 1, 2], object_.method_array_inout(Sequence([-1, 0, 1, 2])))
701
702     def test_method_array_return(self):
703         object_ = GIMarshallingTests.Object()
704         self.assertEquals([-1, 0, 1, 2], object_.method_array_return())
705
706     def test_array_zero_terminated_return(self):
707         self.assertEquals(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_return())
708
709     def test_array_zero_terminated_in(self):
710         GIMarshallingTests.array_zero_terminated_in(Sequence(['0', '1', '2']))
711
712     def test_array_zero_terminated_out(self):
713         self.assertEquals(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
714
715     def test_array_zero_terminated_out(self):
716         self.assertEquals(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
717
718     def test_array_zero_terminated_inout(self):
719         self.assertEquals(['-1', '0', '1', '2'], GIMarshallingTests.array_zero_terminated_inout(['0', '1', '2']))
720
721     def test_gstrv_return(self):
722         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gstrv_return())
723
724     def test_gstrv_in(self):
725         GIMarshallingTests.gstrv_in(Sequence(['0', '1', '2']))
726
727     def test_gstrv_out(self):
728         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gstrv_out())
729
730     def test_gstrv_out(self):
731         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gstrv_out())
732
733     def test_gstrv_inout(self):
734         self.assertEquals(['-1', '0', '1', '2'], GIMarshallingTests.gstrv_inout(['0', '1', '2']))
735
736 class TestGArray(unittest.TestCase):
737
738     def test_garray_int_none_return(self):
739         self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.garray_int_none_return())
740
741     def test_garray_utf8_none_return(self):
742         self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_return())
743
744     def test_garray_utf8_container_return(self):
745         self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_return())
746
747     def test_garray_utf8_full_return(self):
748         self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_return())
749
750     def test_garray_int_none_in(self):
751         GIMarshallingTests.garray_int_none_in(Sequence([-1, 0, 1, 2]))
752
753         self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, Sequence([-1, '0', 1, 2]))
754
755         self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, 42)
756         self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, None)
757
758     def test_garray_utf8_none_in(self):
759         GIMarshallingTests.garray_utf8_none_in(Sequence(['0', '1', '2']))
760
761     def test_garray_utf8_none_out(self):
762         self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_out())
763
764     def test_garray_utf8_container_out(self):
765         self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_out())
766
767     def test_garray_utf8_full_out(self):
768         self.assertEquals(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out())
769
770     def test_garray_utf8_none_inout(self):
771         self.assertEquals(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_none_inout(Sequence(('0', '1', '2'))))
772
773     def test_garray_utf8_container_inout(self):
774         self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.garray_utf8_container_inout(['0', '1', '2']))
775
776     def test_garray_utf8_full_inout(self):
777         self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.garray_utf8_full_inout(['0', '1', '2']))
778
779 class TestGPtrArray(unittest.TestCase):
780
781     def test_gptrarray_utf8_none_return(self):
782         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_return())
783
784     def test_gptrarray_utf8_container_return(self):
785         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_return())
786
787     def test_gptrarray_utf8_full_return(self):
788         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_return())
789
790     def test_gptrarray_utf8_none_in(self):
791         GIMarshallingTests.gptrarray_utf8_none_in(Sequence(['0', '1', '2']))
792
793     def test_gptrarray_utf8_none_out(self):
794         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_out())
795
796     def test_gptrarray_utf8_container_out(self):
797         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_out())
798
799     def test_gptrarray_utf8_full_out(self):
800         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_out())
801
802     def test_gptrarray_utf8_none_inout(self):
803         self.assertEquals(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_none_inout(Sequence(('0', '1', '2'))))
804
805     def test_gptrarray_utf8_container_inout(self):
806         self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.gptrarray_utf8_container_inout(['0', '1', '2']))
807
808     def test_gptrarray_utf8_full_inout(self):
809         self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.gptrarray_utf8_full_inout(['0', '1', '2']))
810
811 class TestGList(unittest.TestCase):
812
813     def test_glist_int_none_return(self):
814         self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.glist_int_none_return())
815
816     def test_glist_utf8_none_return(self):
817         self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_return())
818
819     def test_glist_utf8_container_return(self):
820         self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_return())
821
822     def test_glist_utf8_full_return(self):
823         self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_return())
824
825     def test_glist_int_none_in(self):
826         GIMarshallingTests.glist_int_none_in(Sequence((-1, 0, 1, 2)))
827
828         self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, Sequence((-1, '0', 1, 2)))
829
830         self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, 42)
831         self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, None)
832
833     def test_glist_utf8_none_in(self):
834         GIMarshallingTests.glist_utf8_none_in(Sequence(('0', '1', '2')))
835
836     def test_glist_utf8_none_out(self):
837         self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_out())
838
839     def test_glist_utf8_container_out(self):
840         self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_out())
841
842     def test_glist_utf8_full_out(self):
843         self.assertEquals(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_out())
844
845     def test_glist_utf8_none_inout(self):
846         self.assertEquals(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_none_inout(Sequence(('0', '1', '2'))))
847
848     def test_glist_utf8_container_inout(self):
849         self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.glist_utf8_container_inout(('0', '1', '2')))
850
851     def test_glist_utf8_full_inout(self):
852         self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.glist_utf8_full_inout(('0', '1', '2')))
853
854
855 class TestGSList(unittest.TestCase):
856
857     def test_gslist_int_none_return(self):
858         self.assertEquals([-1, 0, 1, 2], GIMarshallingTests.gslist_int_none_return())
859
860     def test_gslist_utf8_none_return(self):
861         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_return())
862
863     def test_gslist_utf8_container_return(self):
864         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_return())
865
866     def test_gslist_utf8_full_return(self):
867         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_return())
868
869     def test_gslist_int_none_in(self):
870         GIMarshallingTests.gslist_int_none_in(Sequence((-1, 0, 1, 2)))
871
872         self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, Sequence((-1, '0', 1, 2)))
873
874         self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, 42)
875         self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, None)
876
877     def test_gslist_utf8_none_in(self):
878         GIMarshallingTests.gslist_utf8_none_in(Sequence(('0', '1', '2')))
879
880     def test_gslist_utf8_none_out(self):
881         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_out())
882
883     def test_gslist_utf8_container_out(self):
884         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_out())
885
886     def test_gslist_utf8_full_out(self):
887         self.assertEquals(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_out())
888
889     def test_gslist_utf8_none_inout(self):
890         self.assertEquals(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_none_inout(Sequence(('0', '1', '2'))))
891
892     def test_gslist_utf8_container_inout(self):
893         self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.gslist_utf8_container_inout(('0', '1', '2')))
894
895     def test_gslist_utf8_full_inout(self):
896         self.assertEquals(['-2', '-1','0', '1'], GIMarshallingTests.gslist_utf8_full_inout(('0', '1', '2')))
897
898
899 class TestGHashTable(unittest.TestCase):
900
901     def test_ghashtable_int_none_return(self):
902         self.assertEquals({-1: 1, 0: 0, 1: -1, 2: -2}, GIMarshallingTests.ghashtable_int_none_return())
903
904     def test_ghashtable_int_none_return(self):
905         self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_return())
906
907     def test_ghashtable_int_container_return(self):
908         self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_return())
909
910     def test_ghashtable_int_full_return(self):
911         self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_return())
912
913     def test_ghashtable_int_none_in(self):
914         GIMarshallingTests.ghashtable_int_none_in({-1: 1, 0: 0, 1: -1, 2: -2})
915
916         self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, '0': 0, 1: -1, 2: -2})
917         self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, 0: '0', 1: -1, 2: -2})
918
919         self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, '{-1: 1, 0: 0, 1: -1, 2: -2}')
920         self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, None)
921
922     def test_ghashtable_utf8_none_in(self):
923         GIMarshallingTests.ghashtable_utf8_none_in({'-1': '1', '0': '0', '1': '-1', '2': '-2'})
924
925     def test_ghashtable_utf8_none_out(self):
926         self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_out())
927
928     def test_ghashtable_utf8_container_out(self):
929         self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_out())
930
931     def test_ghashtable_utf8_full_out(self):
932         self.assertEquals({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_out())
933
934     def test_ghashtable_utf8_none_inout(self):
935         self.assertEquals({'-1': '1', '0': '0', '1': '1'},
936             GIMarshallingTests.ghashtable_utf8_none_inout({'-1': '1', '0': '0', '1': '-1', '2': '-2'}))
937
938     def test_ghashtable_utf8_container_inout(self):
939         self.assertEquals({'-1': '1', '0': '0', '1': '1'},
940             GIMarshallingTests.ghashtable_utf8_container_inout({'-1': '1', '0': '0', '1': '-1', '2': '-2'}))
941
942     def test_ghashtable_utf8_full_inout(self):
943         self.assertEquals({'-1': '1', '0': '0', '1': '1'},
944             GIMarshallingTests.ghashtable_utf8_full_inout({'-1': '1', '0': '0', '1': '-1', '2': '-2'}))
945
946
947 class TestGValue(unittest.TestCase):
948
949     def test_gvalue_return(self):
950         self.assertEquals(42, GIMarshallingTests.gvalue_return())
951
952     def test_gvalue_in(self):
953         GIMarshallingTests.gvalue_in(42)
954         value = GObject.Value()
955         value.init(GObject.TYPE_INT)
956         value.set_int(42)
957         GIMarshallingTests.gvalue_in(value)
958
959     def test_gvalue_out(self):
960         self.assertEquals(42, GIMarshallingTests.gvalue_out())
961
962     def test_gvalue_inout(self):
963         self.assertEquals('42', GIMarshallingTests.gvalue_inout(42))
964         value = GObject.Value()
965         value.init(GObject.TYPE_INT)
966         value.set_int(42)
967         self.assertEquals('42', GIMarshallingTests.gvalue_inout(value))
968
969 class TestGClosure(unittest.TestCase):
970
971     def test_gclosure_in(self):
972         GIMarshallingTests.gclosure_in(lambda: 42)
973
974         # test passing a closure between two C calls
975         closure = GIMarshallingTests.gclosure_return()
976         GIMarshallingTests.gclosure_in(closure)
977
978         self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, 42)
979         self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, None)
980
981
982 class TestPointer(unittest.TestCase):
983     def test_pointer_in_return(self):
984         self.assertEquals(GIMarshallingTests.pointer_in_return(42), 42)
985
986
987 class TestEnum(unittest.TestCase):
988
989     @classmethod
990     def setUpClass(cls):
991         '''Run tests under a test locale.
992
993         Upper case conversion of member names should not be locale specific;
994         e.  g. in Turkish, "i".upper() == "i", which gives results like "iNVALiD"
995
996         Run test under a locale which defines toupper('a') == 'a'
997         '''
998         cls.locale_dir = tempfile.mkdtemp()
999         subprocess.check_call(['localedef', '-i',
1000             os.path.join(os.path.dirname(os.path.realpath(__file__)), 'te_ST@nouppera'),
1001             '-c', '-f', 'UTF-8', os.path.join(cls.locale_dir, 'te_ST.UTF-8@nouppera')])
1002         os.environ['LOCPATH'] = cls.locale_dir
1003         locale.setlocale(locale.LC_ALL, 'te_ST.UTF-8@nouppera')
1004
1005     @classmethod
1006     def tearDownClass(cls):
1007         locale.setlocale(locale.LC_ALL, 'C')
1008         shutil.rmtree(cls.locale_dir)
1009         try:
1010             del os.environ['LOCPATH']
1011         except KeyError:
1012             pass
1013
1014     def test_enum(self):
1015         self.assertTrue(issubclass(GIMarshallingTests.Enum, int))
1016         self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE1, GIMarshallingTests.Enum))
1017         self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE2, GIMarshallingTests.Enum))
1018         self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE3, GIMarshallingTests.Enum))
1019         self.assertEquals(42, GIMarshallingTests.Enum.VALUE3)
1020
1021     def test_value_nick_and_name(self):
1022         self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_nick, 'value1')
1023         self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_nick, 'value2')
1024         self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_nick, 'value3')
1025
1026         self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE1')
1027         self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE2')
1028         self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE3')
1029
1030     def test_enum_in(self):
1031         GIMarshallingTests.enum_in(GIMarshallingTests.Enum.VALUE3)
1032         GIMarshallingTests.enum_in(42)
1033
1034         self.assertRaises(TypeError, GIMarshallingTests.enum_in, 43)
1035         self.assertRaises(TypeError, GIMarshallingTests.enum_in, 'GIMarshallingTests.Enum.VALUE3')
1036
1037     def test_enum_out(self):
1038         enum = GIMarshallingTests.enum_out()
1039         self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1040         self.assertEquals(enum, GIMarshallingTests.Enum.VALUE3)
1041
1042     def test_enum_inout(self):
1043         enum = GIMarshallingTests.enum_inout(GIMarshallingTests.Enum.VALUE3)
1044         self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1045         self.assertEquals(enum, GIMarshallingTests.Enum.VALUE1)
1046
1047     def test_enum_second(self):
1048         # check for the bug where different non-gtype enums share the same class
1049         self.assertNotEqual(GIMarshallingTests.Enum, GIMarshallingTests.SecondEnum)
1050
1051         # check that values are not being shared between different enums
1052         self.assertTrue(hasattr(GIMarshallingTests.SecondEnum, "SECONDVALUE1"))
1053         self.assertRaises(AttributeError, getattr, GIMarshallingTests.Enum, "SECONDVALUE1")
1054         self.assertTrue(hasattr(GIMarshallingTests.Enum, "VALUE1"))
1055         self.assertRaises(AttributeError, getattr, GIMarshallingTests.SecondEnum, "VALUE1")
1056
1057
1058 class TestGEnum(unittest.TestCase):
1059
1060     def test_genum(self):
1061         self.assertTrue(issubclass(GIMarshallingTests.GEnum, GObject.GEnum))
1062         self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE1, GIMarshallingTests.GEnum))
1063         self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE2, GIMarshallingTests.GEnum))
1064         self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE3, GIMarshallingTests.GEnum))
1065         self.assertEquals(42, GIMarshallingTests.GEnum.VALUE3)
1066
1067     def test_value_nick_and_name(self):
1068         self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_nick, 'value1')
1069         self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_nick, 'value2')
1070         self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_nick, 'value3')
1071
1072         self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE1')
1073         self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE2')
1074         self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE3')
1075
1076     def test_genum_in(self):
1077         GIMarshallingTests.genum_in(GIMarshallingTests.GEnum.VALUE3)
1078         GIMarshallingTests.genum_in(42)
1079
1080         self.assertRaises(TypeError, GIMarshallingTests.genum_in, 43)
1081         self.assertRaises(TypeError, GIMarshallingTests.genum_in, 'GIMarshallingTests.GEnum.VALUE3')
1082
1083     def test_genum_out(self):
1084         genum = GIMarshallingTests.genum_out()
1085         self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1086         self.assertEquals(genum, GIMarshallingTests.GEnum.VALUE3)
1087
1088     def test_genum_inout(self):
1089         genum = GIMarshallingTests.genum_inout(GIMarshallingTests.GEnum.VALUE3)
1090         self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1091         self.assertEquals(genum, GIMarshallingTests.GEnum.VALUE1)
1092
1093
1094 class TestGFlags(unittest.TestCase):
1095
1096     def test_flags(self):
1097         self.assertTrue(issubclass(GIMarshallingTests.Flags, GObject.GFlags))
1098         self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1, GIMarshallingTests.Flags))
1099         self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE2, GIMarshallingTests.Flags))
1100         self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE3, GIMarshallingTests.Flags))
1101         # __or__() operation should still return an instance, not an int.
1102         self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1 | GIMarshallingTests.Flags.VALUE2,
1103                                    GIMarshallingTests.Flags))
1104         self.assertEquals(1 << 1, GIMarshallingTests.Flags.VALUE2)
1105
1106     def test_value_nick_and_name(self):
1107         self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_nick, 'value1')
1108         self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_nick, 'value2')
1109         self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_nick, 'value3')
1110
1111         self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE1')
1112         self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE2')
1113         self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE3')
1114
1115     def test_flags_in(self):
1116         GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2)
1117         # result of __or__() operation should still be valid instance, not an int.
1118         GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE2)
1119         GIMarshallingTests.flags_in_zero(Number(0))
1120
1121         self.assertRaises(TypeError, GIMarshallingTests.flags_in, 1 << 1)
1122         self.assertRaises(TypeError, GIMarshallingTests.flags_in, 'GIMarshallingTests.Flags.VALUE2')
1123
1124     def test_flags_out(self):
1125         flags = GIMarshallingTests.flags_out()
1126         self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1127         self.assertEquals(flags, GIMarshallingTests.Flags.VALUE2)
1128
1129     def test_flags_inout(self):
1130         flags = GIMarshallingTests.flags_inout(GIMarshallingTests.Flags.VALUE2)
1131         self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1132         self.assertEquals(flags, GIMarshallingTests.Flags.VALUE1)
1133
1134 class TestNoTypeFlags(unittest.TestCase):
1135
1136     def test_flags(self):
1137         self.assertTrue(issubclass(GIMarshallingTests.NoTypeFlags, GObject.GFlags))
1138         self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1, GIMarshallingTests.NoTypeFlags))
1139         self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE2, GIMarshallingTests.NoTypeFlags))
1140         self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE3, GIMarshallingTests.NoTypeFlags))
1141         # __or__() operation should still return an instance, not an int.
1142         self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1 | GIMarshallingTests.NoTypeFlags.VALUE2,
1143                                    GIMarshallingTests.NoTypeFlags))
1144         self.assertEquals(1 << 1, GIMarshallingTests.NoTypeFlags.VALUE2)
1145
1146     def test_value_nick_and_name(self):
1147         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_nick, 'value1')
1148         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_nick, 'value2')
1149         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_nick, 'value3')
1150
1151         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE1')
1152         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2')
1153         self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE3')
1154
1155     def test_flags_in(self):
1156         GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2)
1157         GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2 | GIMarshallingTests.NoTypeFlags.VALUE2)
1158         GIMarshallingTests.no_type_flags_in_zero(Number(0))
1159
1160         self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 1 << 1)
1161         self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 'GIMarshallingTests.NoTypeFlags.VALUE2')
1162
1163     def test_flags_out(self):
1164         flags = GIMarshallingTests.no_type_flags_out()
1165         self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1166         self.assertEquals(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1167
1168     def test_flags_inout(self):
1169         flags = GIMarshallingTests.no_type_flags_inout(GIMarshallingTests.NoTypeFlags.VALUE2)
1170         self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1171         self.assertEquals(flags, GIMarshallingTests.NoTypeFlags.VALUE1)
1172
1173
1174 class TestStructure(unittest.TestCase):
1175
1176     def test_simple_struct(self):
1177         self.assertTrue(issubclass(GIMarshallingTests.SimpleStruct, GObject.GPointer))
1178
1179         struct = GIMarshallingTests.SimpleStruct()
1180         self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1181
1182         self.assertEquals(0, struct.long_)
1183         self.assertEquals(0, struct.int8)
1184
1185         struct.long_ = 6
1186         struct.int8 = 7
1187
1188         self.assertEquals(6, struct.long_)
1189         self.assertEquals(7, struct.int8)
1190
1191         del struct
1192
1193     def test_nested_struct(self):
1194         struct = GIMarshallingTests.NestedStruct()
1195
1196         self.assertTrue(isinstance(struct.simple_struct, GIMarshallingTests.SimpleStruct))
1197
1198         struct.simple_struct.long_ = 42
1199         self.assertEquals(42, struct.simple_struct.long_)
1200
1201         del struct
1202
1203     def test_not_simple_struct(self):
1204         struct = GIMarshallingTests.NotSimpleStruct()
1205         self.assertEquals(None, struct.pointer)
1206
1207     def test_simple_struct_return(self):
1208         struct = GIMarshallingTests.simple_struct_returnv()
1209
1210         self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1211         self.assertEquals(6, struct.long_)
1212         self.assertEquals(7, struct.int8)
1213
1214         del struct
1215
1216     def test_simple_struct_in(self):
1217         struct = GIMarshallingTests.SimpleStruct()
1218         struct.long_ = 6
1219         struct.int8 = 7
1220
1221         GIMarshallingTests.SimpleStruct.inv(struct)
1222
1223         del struct
1224
1225         struct = GIMarshallingTests.NestedStruct()
1226
1227         self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, struct)
1228
1229         del struct
1230
1231         self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, None)
1232
1233     def test_simple_struct_method(self):
1234         struct = GIMarshallingTests.SimpleStruct()
1235         struct.long_ = 6
1236         struct.int8 = 7
1237
1238         struct.method()
1239
1240         del struct
1241
1242         self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.method)
1243
1244
1245     def test_pointer_struct(self):
1246         self.assertTrue(issubclass(GIMarshallingTests.PointerStruct, GObject.GPointer))
1247
1248         struct = GIMarshallingTests.PointerStruct()
1249         self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1250
1251         del struct
1252
1253     def test_pointer_struct_return(self):
1254         struct = GIMarshallingTests.pointer_struct_returnv()
1255
1256         self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1257         self.assertEquals(42, struct.long_)
1258
1259         del struct
1260
1261     def test_pointer_struct_in(self):
1262         struct = GIMarshallingTests.PointerStruct()
1263         struct.long_ = 42
1264
1265         struct.inv()
1266
1267         del struct
1268
1269     def test_boxed_struct(self):
1270         self.assertTrue(issubclass(GIMarshallingTests.BoxedStruct, GObject.GBoxed))
1271
1272         struct = GIMarshallingTests.BoxedStruct()
1273         self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1274
1275         self.assertEquals(0, struct.long_)
1276         self.assertEquals([], struct.g_strv)
1277
1278         del struct
1279
1280     def test_boxed_struct_new(self):
1281         struct = GIMarshallingTests.BoxedStruct.new()
1282         self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1283
1284         del struct
1285
1286     def test_boxed_struct_copy(self):
1287         struct = GIMarshallingTests.BoxedStruct()
1288
1289         new_struct = struct.copy()
1290         self.assertTrue(isinstance(new_struct, GIMarshallingTests.BoxedStruct))
1291
1292         del new_struct
1293         del struct
1294
1295     def test_boxed_struct_return(self):
1296         struct = GIMarshallingTests.boxed_struct_returnv()
1297
1298         self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1299         self.assertEquals(42, struct.long_)
1300         self.assertEquals(['0', '1', '2'], struct.g_strv)
1301
1302         del struct
1303
1304     def test_boxed_struct_in(self):
1305         struct = GIMarshallingTests.BoxedStruct()
1306         struct.long_ = 42
1307
1308         struct.inv()
1309
1310         del struct
1311
1312     def test_boxed_struct_out(self):
1313         struct = GIMarshallingTests.boxed_struct_out()
1314
1315         self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1316         self.assertEquals(42, struct.long_)
1317
1318         del struct
1319
1320     def test_boxed_struct_inout(self):
1321         in_struct = GIMarshallingTests.BoxedStruct()
1322         in_struct.long_ = 42
1323
1324         out_struct = GIMarshallingTests.boxed_struct_inout(in_struct)
1325
1326         self.assertTrue(isinstance(out_struct, GIMarshallingTests.BoxedStruct))
1327         self.assertEquals(0, out_struct.long_)
1328
1329         del in_struct
1330         del out_struct
1331
1332     def test_union(self):
1333         union = GIMarshallingTests.Union()
1334
1335         self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1336
1337         new_union = union.copy()
1338         self.assertTrue(isinstance(new_union, GIMarshallingTests.Union))
1339
1340         del union
1341         del new_union
1342
1343     def test_union_return(self):
1344         union = GIMarshallingTests.union_returnv()
1345
1346         self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1347         self.assertEquals(42, union.long_)
1348
1349         del union
1350
1351     def test_union_in(self):
1352         union = GIMarshallingTests.Union()
1353         union.long_ = 42
1354
1355         union.inv()
1356
1357         del union
1358
1359     def test_union_method(self):
1360         union = GIMarshallingTests.Union()
1361         union.long_ = 42
1362
1363         union.method()
1364
1365         del union
1366
1367         self.assertRaises(TypeError, GIMarshallingTests.Union.method)
1368
1369 class TestGObject(unittest.TestCase):
1370
1371     def test_object(self):
1372         self.assertTrue(issubclass(GIMarshallingTests.Object, GObject.GObject))
1373
1374         object_ = GIMarshallingTests.Object()
1375         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1376         self.assertEquals(object_.__grefcount__, 1)
1377
1378     def test_object_new(self):
1379         object_ = GIMarshallingTests.Object.new(42)
1380         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1381         self.assertEquals(object_.__grefcount__, 1)
1382
1383     def test_object_int(self):
1384         object_ = GIMarshallingTests.Object(int = 42)
1385         self.assertEquals(object_.int_, 42)
1386 # FIXME: Don't work yet.
1387 #        object_.int_ = 0
1388 #        self.assertEquals(object_.int_, 0)
1389
1390     def test_object_static_method(self):
1391         GIMarshallingTests.Object.static_method()
1392
1393     def test_object_method(self):
1394         GIMarshallingTests.Object(int = 42).method()
1395         self.assertRaises(TypeError, GIMarshallingTests.Object.method, GObject.GObject())
1396         self.assertRaises(TypeError, GIMarshallingTests.Object.method)
1397
1398
1399     def test_sub_object(self):
1400         self.assertTrue(issubclass(GIMarshallingTests.SubObject, GIMarshallingTests.Object))
1401
1402         object_ = GIMarshallingTests.SubObject()
1403         self.assertTrue(isinstance(object_, GIMarshallingTests.SubObject))
1404
1405     def test_sub_object_new(self):
1406         self.assertRaises(TypeError, GIMarshallingTests.SubObject.new, 42)
1407
1408     def test_sub_object_static_method(self):
1409         object_ = GIMarshallingTests.SubObject()
1410         object_.static_method()
1411
1412     def test_sub_object_method(self):
1413         object_ = GIMarshallingTests.SubObject(int = 42)
1414         object_.method()
1415
1416     def test_sub_object_sub_method(self):
1417         object_ = GIMarshallingTests.SubObject()
1418         object_.sub_method()
1419
1420     def test_sub_object_overwritten_method(self):
1421         object_ = GIMarshallingTests.SubObject()
1422         object_.overwritten_method()
1423
1424         self.assertRaises(TypeError, GIMarshallingTests.SubObject.overwritten_method, GIMarshallingTests.Object())
1425
1426     def test_sub_object_int(self):
1427         object_ = GIMarshallingTests.SubObject()
1428         self.assertEquals(object_.int_, 0)
1429 # FIXME: Don't work yet.
1430 #        object_.int_ = 42
1431 #        self.assertEquals(object_.int_, 42)
1432
1433     def test_object_none_return(self):
1434         object_ = GIMarshallingTests.Object.none_return()
1435         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1436         self.assertEquals(object_.__grefcount__, 2)
1437
1438     def test_object_full_return(self):
1439         object_ = GIMarshallingTests.Object.full_return()
1440         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1441         self.assertEquals(object_.__grefcount__, 1)
1442
1443     def test_object_none_in(self):
1444         object_ = GIMarshallingTests.Object(int = 42)
1445         GIMarshallingTests.Object.none_in(object_)
1446         self.assertEquals(object_.__grefcount__, 1)
1447
1448         object_ = GIMarshallingTests.SubObject(int = 42)
1449         GIMarshallingTests.Object.none_in(object_)
1450
1451         object_ = GObject.GObject()
1452         self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, object_)
1453
1454         self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, None)
1455
1456     def test_object_none_out(self):
1457         object_ = GIMarshallingTests.Object.none_out()
1458         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1459         self.assertEquals(object_.__grefcount__, 2)
1460
1461         new_object = GIMarshallingTests.Object.none_out()
1462         self.assertTrue(new_object is object_)
1463
1464     def test_object_full_out(self):
1465         object_ = GIMarshallingTests.Object.full_out()
1466         self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
1467         self.assertEquals(object_.__grefcount__, 1)
1468
1469     def test_object_none_inout(self):
1470         object_ = GIMarshallingTests.Object(int = 42)
1471         new_object = GIMarshallingTests.Object.none_inout(object_)
1472
1473         self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1474
1475         self.assertFalse(object_ is new_object)
1476
1477         self.assertEquals(object_.__grefcount__, 1)
1478         self.assertEquals(new_object.__grefcount__, 2)
1479
1480         new_new_object = GIMarshallingTests.Object.none_inout(object_)
1481         self.assertTrue(new_new_object is new_object)
1482
1483         GIMarshallingTests.Object.none_inout(GIMarshallingTests.SubObject(int = 42))
1484
1485     def test_object_full_inout(self):
1486         object_ = GIMarshallingTests.Object(int = 42)
1487         new_object = GIMarshallingTests.Object.full_inout(object_)
1488
1489         self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
1490
1491         self.assertFalse(object_ is new_object)
1492
1493         self.assertEquals(object_.__grefcount__, 2)
1494         self.assertEquals(new_object.__grefcount__, 1)
1495
1496 # FIXME: Doesn't actually return the same object.
1497 #    def test_object_inout_same(self):
1498 #        object_ = GIMarshallingTests.Object()
1499 #        new_object = GIMarshallingTests.object_full_inout(object_)
1500 #        self.assertTrue(object_ is new_object)
1501 #        self.assertEquals(object_.__grefcount__, 1)
1502
1503
1504 class TestPythonGObject(unittest.TestCase):
1505
1506     class Object(GIMarshallingTests.Object):
1507         def __init__(self, int):
1508             GIMarshallingTests.Object.__init__(self)
1509             self.val = None
1510
1511         def method(self):
1512             # Don't call super, which asserts that self.int == 42.
1513             pass
1514
1515         def do_method_int8_in(self, int8):
1516             self.val = int8
1517
1518         def do_method_int8_out(self):
1519             return 42
1520
1521         def do_method_with_default_implementation(self, int8):
1522             GIMarshallingTests.Object.do_method_with_default_implementation(self, int8)
1523             self.props.int += int8
1524
1525     class SubObject(GIMarshallingTests.SubObject):
1526         def __init__(self, int):
1527             GIMarshallingTests.SubObject.__init__(self)
1528             self.val = None
1529
1530         def do_method_with_default_implementation(self, int8):
1531             self.val = int8
1532
1533     def test_object(self):
1534         self.assertTrue(issubclass(self.Object, GIMarshallingTests.Object))
1535
1536         object_ = self.Object(int = 42)
1537         self.assertTrue(isinstance(object_, self.Object))
1538
1539     def test_object_method(self):
1540         self.Object(int = 0).method()
1541
1542     def test_object_vfuncs(self):
1543         object_ = self.Object(int = 42)
1544         object_.method_int8_in(84)
1545         self.assertEqual(object_.val, 84)
1546         self.assertEqual(object_.method_int8_out(), 42)
1547
1548         object_.method_with_default_implementation(42)
1549         self.assertEqual(object_.props.int, 84)
1550
1551         class ObjectWithoutVFunc(GIMarshallingTests.Object):
1552             def __init__(self, int):
1553                 GIMarshallingTests.Object.__init__(self)
1554
1555         object_ = ObjectWithoutVFunc(int = 42)
1556         object_.method_with_default_implementation(84)
1557         self.assertEqual(object_.props.int, 84)
1558
1559     def test_subobject_parent_vfunc(self):
1560         object_ = self.SubObject(int = 81)
1561         object_.method_with_default_implementation(87)
1562         self.assertEquals(object_.val, 87)
1563
1564     def test_dynamic_module(self):
1565         from gi.module import DynamicGObjectModule
1566         self.assertTrue(isinstance(GObject, DynamicGObjectModule))
1567         # compare the same enum from both the pygobject attrs and gi GObject attrs
1568         self.assertEquals(GObject.SIGNAL_ACTION, GObject.SignalFlags.ACTION)
1569         # compare a static gobject attr with a dynamic GObject attr
1570         import gi._gobject
1571         self.assertEquals(GObject.GObject, gi._gobject.GObject)
1572
1573     def test_subobject_non_vfunc_do_method(self):
1574         class PythonObjectWithNonVFuncDoMethod:
1575             def do_not_a_vfunc(self):
1576                 return 5
1577
1578         class ObjectOverrideNonVFuncDoMethod(GIMarshallingTests.Object, PythonObjectWithNonVFuncDoMethod):
1579             def do_not_a_vfunc(self):
1580                 value = super(ObjectOverrideNonVFuncDoMethod, self).do_not_a_vfunc()
1581                 return 13 + value
1582
1583         object_ = ObjectOverrideNonVFuncDoMethod()
1584         self.assertEquals(18, object_.do_not_a_vfunc())
1585
1586     def test_native_function_not_set_in_subclass_dict(self):
1587         # Previously, GI was setting virtual functions on the class as well
1588         # as any *native* class that subclasses it. Here we check that it is only
1589         # set on the class that the method is originally from.
1590         self.assertTrue('do_method_with_default_implementation' in GIMarshallingTests.Object.__dict__)
1591         self.assertTrue('do_method_with_default_implementation' not in GIMarshallingTests.SubObject.__dict__)
1592
1593         # Here we check that accessing a vfunc from the subclass returns the same wrapper object,
1594         # meaning that multiple wrapper objects have not been created for the same vfunc.
1595         func1 = GIMarshallingTests.Object.do_method_with_default_implementation
1596         func2 = GIMarshallingTests.SubObject.do_method_with_default_implementation
1597         if sys.version_info < (3,0):
1598             func1 = func1.im_func
1599             func2 = func2.im_func
1600             
1601         self.assertTrue(func1 is func2)
1602
1603     def test_subobject_with_interface_and_non_vfunc_do_method(self):
1604         # There was a bug for searching for vfuncs in interfaces. It was
1605         # triggered by having a do_* method that wasn't overriding
1606         # a native vfunc, as well as inheriting from an interface.
1607         class GObjectSubclassWithInterface(GObject.GObject, GIMarshallingTests.Interface):
1608             def do_method_not_a_vfunc(self):
1609                 pass
1610
1611 class TestMultiOutputArgs(unittest.TestCase):
1612
1613     def test_int_out_out(self):
1614         self.assertEquals((6, 7), GIMarshallingTests.int_out_out())
1615
1616     def test_int_return_out(self):
1617         self.assertEquals((6, 7), GIMarshallingTests.int_return_out())
1618
1619 class TestGErrorException(unittest.TestCase):
1620     def test_gerror_exception(self):
1621         self.assertRaises(GObject.GError, GIMarshallingTests.gerror)
1622         try:
1623             GIMarshallingTests.gerror()
1624         except Exception:
1625             etype, e = sys.exc_info()[:2]
1626             self.assertEquals(e.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
1627             self.assertEquals(e.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
1628             self.assertEquals(e.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
1629
1630
1631 # Interface
1632
1633 class TestInterfaces(unittest.TestCase):
1634
1635     def test_wrapper(self):
1636         self.assertTrue(issubclass(GIMarshallingTests.Interface, GObject.GInterface))
1637         self.assertEquals(GIMarshallingTests.Interface.__gtype__.name, 'GIMarshallingTestsInterface')
1638         self.assertRaises(NotImplementedError, GIMarshallingTests.Interface)
1639
1640     def test_implementation(self):
1641
1642         class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
1643             def __init__(self):
1644                 GObject.GObject.__init__(self)
1645                 self.val = None
1646
1647             def do_test_int8_in(self, int8):
1648                 self.val = int8
1649
1650         self.assertTrue(issubclass(TestInterfaceImpl, GIMarshallingTests.Interface))
1651
1652         instance = TestInterfaceImpl()
1653         self.assertTrue(isinstance(instance, GIMarshallingTests.Interface))
1654
1655         GIMarshallingTests.test_interface_test_int8_in(instance, 42)
1656         self.assertEquals(instance.val, 42)
1657
1658         class TestInterfaceImplA(TestInterfaceImpl):
1659             pass
1660
1661         class TestInterfaceImplB(TestInterfaceImplA):
1662             pass
1663
1664         instance = TestInterfaceImplA()
1665         GIMarshallingTests.test_interface_test_int8_in(instance, 42)
1666         self.assertEquals(instance.val, 42)
1667
1668     def test_mro(self):
1669         # there was a problem with Python bailing out because of
1670         # http://en.wikipedia.org/wiki/Diamond_problem with interfaces,
1671         # which shouldn't really be a problem.
1672
1673         class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
1674             pass
1675
1676         class TestInterfaceImpl2(GIMarshallingTests.Interface,
1677                                  TestInterfaceImpl):
1678             pass
1679
1680         class TestInterfaceImpl3(TestInterfaceImpl,
1681                                  GIMarshallingTests.Interface2):
1682             pass
1683
1684
1685 class TestInterfaceClash(unittest.TestCase):
1686
1687     def test_clash(self):
1688         def create_clash():
1689             class TestClash(GObject.GObject, GIMarshallingTests.Interface, GIMarshallingTests.Interface2):
1690                 def do_test_int8_in(self, int8):
1691                     pass
1692             TestClash()
1693
1694         self.assertRaises(TypeError, create_clash)
1695
1696
1697 class TestOverrides(unittest.TestCase):
1698
1699     def test_constant(self):
1700         self.assertEquals(GIMarshallingTests.OVERRIDES_CONSTANT, 7)
1701
1702     def test_struct(self):
1703         # Test that the constructor has been overridden.
1704         struct = GIMarshallingTests.OverridesStruct(42)
1705
1706         self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
1707
1708         # Test that the method has been overridden.
1709         self.assertEquals(6, struct.method())
1710
1711         del struct
1712
1713         # Test that the overrides wrapper has been registered.
1714         struct = GIMarshallingTests.overrides_struct_returnv()
1715
1716         self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
1717
1718         del struct
1719
1720     def test_object(self):
1721         # Test that the constructor has been overridden.
1722         object_ = GIMarshallingTests.OverridesObject(42)
1723
1724         self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
1725
1726         # Test that the alternate constructor has been overridden.
1727         object_ = GIMarshallingTests.OverridesObject.new(42)
1728
1729         self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
1730
1731         # Test that the method has been overridden.
1732         self.assertEquals(6, object_.method())
1733
1734         # Test that the overrides wrapper has been registered.
1735         object_ = GIMarshallingTests.OverridesObject.returnv()
1736
1737         self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
1738
1739     def test_module_name(self):
1740         self.assertEquals(GIMarshallingTests.OverridesStruct.__module__, 'gi.overrides.GIMarshallingTests')
1741         self.assertEquals(GObject.InitiallyUnowned.__module__, 'gi.repository.GObject')
1742
1743 class TestDir(unittest.TestCase):
1744     def test_members_list(self):
1745         list = dir(GIMarshallingTests)
1746         self.assertTrue('OverridesStruct' in list)
1747         self.assertTrue('BoxedStruct' in list)
1748         self.assertTrue('OVERRIDES_CONSTANT' in list)
1749         self.assertTrue('GEnum' in list)
1750         self.assertTrue('int32_return_max' in list)
1751
1752     def test_modules_list(self):
1753         import gi.repository
1754         list = dir(gi.repository)
1755         self.assertTrue('GIMarshallingTests' in list)
1756
1757         # FIXME: test to see if a module which was not imported is in the list
1758         #        we should be listing every typelib we find, not just the ones
1759         #        which are imported
1760         #
1761         #        to test this I recommend we compile a fake module which
1762         #        our tests would never import and check to see if it is
1763         #        in the list:
1764         #
1765         # self.assertTrue('DoNotImportDummyTests' in list)
1766
1767 class TestGErrorArrayInCrash(unittest.TestCase):
1768     # Previously there was a bug in invoke, in which C arrays were unwrapped
1769     # from inside GArrays to be passed to the C function. But when a GError was
1770     # set, invoke would attempt to free the C array as if it were a GArray.
1771     # This crash is only for C arrays. It does not happen for C functions which
1772     # take in GArrays. See https://bugzilla.gnome.org/show_bug.cgi?id=642708
1773     def test_gerror_array_in_crash(self):
1774         self.assertRaises(GObject.GError, GIMarshallingTests.gerror_array_in, [1, 2, 3])
1775
1776 class TestKeywordArgs(unittest.TestCase):
1777     def test_calling(self):
1778         kw_func = GIMarshallingTests.int_three_in_three_out
1779
1780         self.assertEquals(kw_func(1, 2, 3),                 (1, 2, 3))
1781         self.assertEquals(kw_func(**{'a':4, 'b':5, 'c':6}), (4, 5, 6))
1782         self.assertEquals(kw_func(1, **{'b':7, 'c':8}),     (1, 7, 8))
1783         self.assertEquals(kw_func(1, 7, **{'c':8}),         (1, 7, 8))
1784         self.assertEquals(kw_func(1, c=8, **{'b':7}),       (1, 7, 8))
1785         self.assertEquals(kw_func(2, c=4, b=3),             (2, 3, 4))
1786         self.assertEquals(kw_func(a=2, c=4, b=3),           (2, 3, 4))
1787
1788     def assertRaisesMessage(self, exception, message, func, *args, **kwargs):
1789         try:
1790             func(*args, **kwargs)
1791         except exception:
1792             (e_type, e) = sys.exc_info()[:2]
1793             if message is not None:
1794                 self.assertEqual(str(e), message)
1795         except:
1796             raise
1797         else:
1798             msg = "%s() did not raise %s" % (func.__name__, exception.__name__)
1799             raise AssertionError(msg)
1800
1801     def test_type_errors(self):
1802         # test too few args
1803         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
1804                                  GIMarshallingTests.int_three_in_three_out)
1805         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (1 given)",
1806                                  GIMarshallingTests.int_three_in_three_out, 1)
1807         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
1808                                  GIMarshallingTests.int_three_in_three_out, *())
1809         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (0 given)",
1810                                  GIMarshallingTests.int_three_in_three_out, *(), **{})
1811         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (0 given)",
1812                                  GIMarshallingTests.int_three_in_three_out, *(), **{'c':4})
1813
1814         # test too many args
1815         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 arguments (4 given)",
1816                                  GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4))
1817         self.assertRaisesMessage(TypeError, "int_three_in_three_out() takes exactly 3 non-keyword arguments (4 given)",
1818                                  GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4), c=6)
1819
1820         # test too many keyword args
1821         self.assertRaisesMessage(TypeError, "int_three_in_three_out() got multiple values for keyword argument 'a'",
1822                                  GIMarshallingTests.int_three_in_three_out, 1, 2, 3, **{'a': 4, 'b': 5})
1823         self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'd'",
1824                                  GIMarshallingTests.int_three_in_three_out, d=4)
1825         self.assertRaisesMessage(TypeError, "int_three_in_three_out() got an unexpected keyword argument 'e'",
1826                                  GIMarshallingTests.int_three_in_three_out, **{'e': 2})
1827
1828     def test_kwargs_are_not_modified(self):
1829         d = {'b': 2}
1830         d2 = d.copy()
1831         GIMarshallingTests.int_three_in_three_out(1, c=4, **d)
1832         self.assertEqual(d, d2)