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