OLD | NEW |
---|---|
(Empty) | |
1 import struct | |
2 import unittest | |
3 | |
4 # Generated files | |
5 # pylint: disable=F0401 | |
6 import test_unions_mojom | |
7 import mojo_bindings.serialization as serialization | |
8 | |
9 class UnionBindingsTest(unittest.TestCase): | |
10 | |
11 def testBasics(self): | |
12 u = test_unions_mojom.PodUnion() | |
13 u.f_uint32 = 32 | |
14 self.assertEquals(u.f_uint32, 32) | |
15 self.assertEquals(u.data, 32) | |
16 self.assertEquals(test_unions_mojom.PodUnion.Tags.f_uint32, u.tag) | |
17 | |
18 u = test_unions_mojom.PodUnion(f_uint8=8) | |
19 self.assertEquals(u.f_uint8, 8) | |
20 self.assertEquals(u.data, 8) | |
21 self.assertEquals(test_unions_mojom.PodUnion.Tags.f_uint8, u.tag) | |
22 | |
23 with self.assertRaises(TypeError): | |
24 test_unions_mojom.PodUnion(f_uint8=8, f_int16=10) | |
25 | |
26 with self.assertRaises(AttributeError): | |
27 test_unions_mojom.PodUnion(bad_field=10) | |
28 | |
29 with self.assertRaises(AttributeError): | |
30 u = test_unions_mojom.PodUnion() | |
31 u.bad_field = 32 | |
32 | |
33 with self.assertRaises(AttributeError): | |
34 _ = u.f_uint16 | |
35 | |
36 def testPodUnionSerialization(self): | |
37 u = test_unions_mojom.PodUnion(f_uint32=32) | |
38 (data, handles) = u.Serialize() | |
39 context = serialization.RootDeserializationContext(data, handles) | |
40 decoded = test_unions_mojom.PodUnion.Deserialize(context) | |
41 | |
42 self.assertEquals(u, decoded) | |
43 | |
44 def testObjectInUnionSerialization(self): | |
45 u = test_unions_mojom.ObjectUnion( | |
46 f_dummy=test_unions_mojom.DummyStruct()) | |
47 u.f_dummy.f_int8 = 8 | |
48 (data, handles) = u.Serialize() | |
49 context = serialization.RootDeserializationContext(data, handles) | |
50 decoded = test_unions_mojom.ObjectUnion.Deserialize(context) | |
51 | |
52 self.assertEquals(u, decoded) | |
53 | |
54 def testObjectInUnionInObjectSerialization(self): | |
55 s = test_unions_mojom.SmallObjStruct() | |
56 s.obj_union = test_unions_mojom.ObjectUnion( | |
57 f_dummy=test_unions_mojom.DummyStruct()) | |
58 s.obj_union.f_dummy.f_int8 = 25 | |
59 (data, handles) = s.Serialize() | |
60 context = serialization.RootDeserializationContext(data, handles) | |
61 decoded = test_unions_mojom.SmallObjStruct.Deserialize(context) | |
62 | |
63 self.assertEquals(s, decoded) | |
64 | |
65 def testNestedUnionSerialization(self): | |
66 u = test_unions_mojom.ObjectUnion( | |
67 f_pod_union=test_unions_mojom.PodUnion(f_int32=32)) | |
68 | |
69 (data, handles) = u.Serialize() | |
70 context = serialization.RootDeserializationContext(data, handles) | |
71 decoded = test_unions_mojom.ObjectUnion.Deserialize(context) | |
72 | |
73 self.assertEquals(u, decoded) | |
74 | |
75 def testNullableNullObjectInUnionSerialization(self): | |
76 u = test_unions_mojom.ObjectUnion(f_nullable=None) | |
77 (data, handles) = u.Serialize() | |
78 context = serialization.RootDeserializationContext(data, handles) | |
79 decoded = test_unions_mojom.ObjectUnion.Deserialize(context) | |
80 | |
81 self.assertEquals(u, decoded) | |
82 | |
83 def testNonNullableNullObjectInUnionSerialization(self): | |
84 u = test_unions_mojom.ObjectUnion(f_dummy=None) | |
85 with self.assertRaises(serialization.SerializationException): | |
86 u.Serialize() | |
87 | |
88 def testArrayInUnionSerialization(self): | |
89 u = test_unions_mojom.ObjectUnion( | |
90 f_array_int8=[1, 2, 3, 4, 5]) | |
91 (data, handles) = u.Serialize() | |
92 context = serialization.RootDeserializationContext(data, handles) | |
93 decoded = test_unions_mojom.ObjectUnion.Deserialize(context) | |
94 | |
95 self.assertEquals(u, decoded) | |
96 | |
97 def testMapInUnionSerialization(self): | |
98 u = test_unions_mojom.ObjectUnion( | |
99 f_map_int8={'one': 1, 'two': 2, 'three': 3}) | |
100 (data, handles) = u.Serialize() | |
101 context = serialization.RootDeserializationContext(data, handles) | |
102 decoded = test_unions_mojom.ObjectUnion.Deserialize(context) | |
103 | |
104 self.assertEquals(u, decoded) | |
105 | |
106 def testUnionInObject(self): | |
107 s = test_unions_mojom.SmallStruct() | |
108 s.pod_union = test_unions_mojom.PodUnion(f_uint32=32) | |
109 (data, handles) = s.Serialize() | |
110 | |
111 # This is where the data should be serialized to. | |
112 size, tag, value = struct.unpack_from('<IIQ', buffer(data), 16) | |
113 self.assertEquals(16, size) | |
114 self.assertEquals(6, tag) | |
115 self.assertEquals(32, value) | |
116 | |
117 context = serialization.RootDeserializationContext(data, handles) | |
118 decoded = test_unions_mojom.SmallStruct.Deserialize(context) | |
119 | |
120 self.assertEquals(s, decoded) | |
121 | |
122 def testUnionInArray(self): | |
123 s = test_unions_mojom.SmallStruct() | |
124 s.pod_union_array = [ | |
125 test_unions_mojom.PodUnion(f_uint32=32), | |
126 test_unions_mojom.PodUnion(f_uint16=16), | |
127 test_unions_mojom.PodUnion(f_uint64=64), | |
128 ] | |
129 (data, handles) = s.Serialize() | |
130 | |
131 context = serialization.RootDeserializationContext(data, handles) | |
132 decoded = test_unions_mojom.SmallStruct.Deserialize(context) | |
133 | |
134 self.assertEquals(s, decoded) | |
135 | |
136 def testNonNullableNullUnionInArray(self): | |
137 s = test_unions_mojom.SmallStruct() | |
138 s.pod_union_array = [ | |
139 test_unions_mojom.PodUnion(f_uint32=32), | |
140 None, | |
141 test_unions_mojom.PodUnion(f_uint64=64), | |
142 ] | |
143 with self.assertRaises(serialization.SerializationException): | |
144 s.Serialize() | |
145 | |
146 def testNullableNullUnionInArray(self): | |
147 s = test_unions_mojom.SmallStruct() | |
148 s.nullable_pod_union_array = [ | |
149 test_unions_mojom.PodUnion(f_uint32=32), | |
150 None, | |
151 test_unions_mojom.PodUnion(f_uint64=64), | |
152 ] | |
153 (data, handles) = s.Serialize() | |
154 | |
155 context = serialization.RootDeserializationContext(data, handles) | |
156 decoded = test_unions_mojom.SmallStruct.Deserialize(context) | |
157 | |
158 self.assertEquals(s, decoded) | |
159 | |
160 def testUnionInMap(self): | |
qsr
2015/07/20 21:29:59
Can you add a test for unknown case? Maybe define
azani
2015/07/20 22:17:52
Done.
| |
161 s = test_unions_mojom.SmallStruct() | |
162 s.pod_union_map = { | |
163 'f_uint32': test_unions_mojom.PodUnion(f_uint32=32), | |
164 'f_uint16': test_unions_mojom.PodUnion(f_uint16=16), | |
165 'f_uint64': test_unions_mojom.PodUnion(f_uint64=64), | |
166 } | |
167 (data, handles) = s.Serialize() | |
168 | |
169 context = serialization.RootDeserializationContext(data, handles) | |
170 decoded = test_unions_mojom.SmallStruct.Deserialize(context) | |
171 | |
172 self.assertEquals(s, decoded) | |
OLD | NEW |