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