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