1 # -*- Mode: Python; py-indent-offset: 4 -*-
7 from gi
.repository
import GLib
8 from gi
.repository
import Regress
9 from gi
.repository
import GIMarshallingTests
11 from compathelper
import _unicode
16 def __init__(self
, value
):
20 return int(self
.value
)
23 return float(self
.value
)
26 class TestFields(unittest
.TestCase
):
29 s
= Regress
.TestStructA()
31 self
.assertEqual(s
.some_int8
, 21)
34 self
.assertEqual(s
.some_int8
, 0x42)
36 self
.assertRaises(TypeError, setattr, s
, "some_int8", b
"ab")
37 self
.assertRaises(TypeError, setattr, s
, "some_int8", None)
38 self
.assertRaises(OverflowError, setattr, s
, "some_int8", 128)
39 self
.assertRaises(OverflowError, setattr, s
, "some_int8", -129)
42 self
.assertEqual(s
.some_int8
, 3)
44 s
.some_int8
= Number(55)
45 self
.assertEqual(s
.some_int8
, 55)
48 s
= Regress
.TestStructA()
49 s
.some_int
= GLib
.MAXINT
50 self
.assertEqual(s
.some_int
, GLib
.MAXINT
)
52 self
.assertRaises(TypeError, setattr, s
, "some_int", b
"a")
53 self
.assertRaises(TypeError, setattr, s
, "some_int", None)
55 OverflowError, setattr, s
, "some_int", GLib
.MAXINT
+ 1)
57 OverflowError, setattr, s
, "some_int", GLib
.MININT
- 1)
60 self
.assertEqual(s
.some_int
, 3)
62 s
.some_int
= Number(GLib
.MININT
)
63 self
.assertEqual(s
.some_int
, GLib
.MININT
)
66 s
= GIMarshallingTests
.SimpleStruct()
67 s
.long_
= GLib
.MAXLONG
68 self
.assertEqual(s
.long_
, GLib
.MAXLONG
)
70 self
.assertRaises(TypeError, setattr, s
, "long_", b
"a")
71 self
.assertRaises(TypeError, setattr, s
, "long_", None)
72 self
.assertRaises(OverflowError, setattr, s
, "long_", GLib
.MAXLONG
+ 1)
73 self
.assertRaises(OverflowError, setattr, s
, "long_", GLib
.MINLONG
- 1)
76 self
.assertEqual(s
.long_
, 3)
78 s
.long_
= Number(GLib
.MINLONG
)
79 self
.assertEqual(s
.long_
, GLib
.MINLONG
)
81 def test_double(self
):
82 s
= Regress
.TestStructA()
83 s
.some_double
= GLib
.MAXDOUBLE
84 self
.assertEqual(s
.some_double
, GLib
.MAXDOUBLE
)
85 s
.some_double
= GLib
.MINDOUBLE
86 self
.assertEqual(s
.some_double
, GLib
.MINDOUBLE
)
88 s
.some_double
= float("nan")
89 self
.assertTrue(math
.isnan(s
.some_double
))
91 self
.assertRaises(TypeError, setattr, s
, "some_double", b
"a")
92 self
.assertRaises(TypeError, setattr, s
, "some_double", None)
95 s
= Regress
.TestStructE()
97 s
.some_type
= Regress
.TestObj
98 self
.assertEqual(s
.some_type
, Regress
.TestObj
.__gtype
__)
100 self
.assertRaises(TypeError, setattr, s
, "some_type", 42)
102 def test_unichar(self
):
103 # I can't find a unichar field..
107 s
= GIMarshallingTests
.BoxedStruct()
109 self
.assertEqual(s
.string_
, "hello")
111 s
.string_
= _unicode("hello")
112 self
.assertEqual(s
.string_
, _unicode("hello"))
115 self
.assertEqual(s
.string_
, None)
117 self
.assertRaises(TypeError, setattr, s
, "string_", 42)
119 def test_array_of_structs(self
):
120 s
= Regress
.TestStructD()
121 self
.assertEqual(s
.array1
, [])
122 self
.assertEqual(s
.array2
, [])
124 def test_interface(self
):
125 s
= Regress
.TestStructC()
127 obj
= Regress
.TestObj()
129 self
.assertTrue(s
.obj
is obj
)
132 self
.assertTrue(s
.obj
is None)
134 self
.assertRaises(TypeError, setattr, s
, "obj", object())
136 def test_glist(self
):
137 s
= Regress
.TestStructD()
138 self
.assertEqual(s
.list, [])
140 self
.assertRaises(TypeError, setattr, s
, "list", [object()])
142 def test_gpointer(self
):
146 self
.assertEqual(glist
.data
, 123)
149 self
.assertEqual(glist
.data
, 0)
151 def test_gptrarray(self
):
152 s
= Regress
.TestStructD()
153 self
.assertEqual(s
.garray
, [])
155 self
.assertRaises(TypeError, setattr, s
, "garray", [object()])
158 s
= Regress
.TestStructA()
160 s
.some_enum
= Regress
.TestEnum
.VALUE3
161 self
.assertEqual(s
.some_enum
, Regress
.TestEnum
.VALUE3
)
163 self
.assertRaises(TypeError, setattr, s
, "some_enum", object())
166 self
.assertEqual(s
.some_enum
, Regress
.TestEnum
.VALUE1
)
168 def test_union(self
):
169 s
= Regress
.TestStructE()
170 self
.assertEqual(s
.some_union
, [None, None])
172 def test_struct(self
):
173 s
= GIMarshallingTests
.NestedStruct()
176 # https://bugzilla.gnome.org/show_bug.cgi?id=747002
177 # s.simple_struct = None
179 self
.assertRaises(TypeError, setattr, s
, "simple_struct", object())
181 sub
= GIMarshallingTests
.SimpleStruct()
183 s
.simple_struct
= sub
184 self
.assertEqual(s
.simple_struct
.long_
, 42)
186 def test_ghashtable(self
):
187 obj
= Regress
.TestObj()
188 self
.assertTrue(obj
.hash_table
is None)