OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import imp | 5 import imp |
6 import os.path | 6 import os.path |
7 import sys | 7 import sys |
8 import unittest | 8 import unittest |
9 | 9 |
10 def _GetDirAbove(dirname): | 10 def _GetDirAbove(dirname): |
(...skipping 1135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1146 struct MyStruct { | 1146 struct MyStruct { |
1147 some_interface?& a; | 1147 some_interface?& a; |
1148 }; | 1148 }; |
1149 """ | 1149 """ |
1150 with self.assertRaisesRegexp( | 1150 with self.assertRaisesRegexp( |
1151 parser.ParseError, | 1151 parser.ParseError, |
1152 r"^my_file\.mojom:2: Error: Unexpected '&':\n" | 1152 r"^my_file\.mojom:2: Error: Unexpected '&':\n" |
1153 r" *some_interface\?& a;$"): | 1153 r" *some_interface\?& a;$"): |
1154 parser.Parse(source3, "my_file.mojom") | 1154 parser.Parse(source3, "my_file.mojom") |
1155 | 1155 |
| 1156 def testSimpleUnion(self): |
| 1157 """Tests a simple .mojom source that just defines a union.""" |
| 1158 source = """\ |
| 1159 module my_module; |
| 1160 |
| 1161 union MyUnion { |
| 1162 int32 a; |
| 1163 double b; |
| 1164 }; |
| 1165 """ |
| 1166 expected = ast.Mojom( |
| 1167 ast.Module(('IDENTIFIER', 'my_module'), None), |
| 1168 ast.ImportList(), |
| 1169 [ast.Union( |
| 1170 'MyUnion', |
| 1171 ast.UnionBody([ |
| 1172 ast.UnionField('a', None, 'int32'), |
| 1173 ast.UnionField('b', None, 'double') |
| 1174 ]))]) |
| 1175 actual = parser.Parse(source, "my_file.mojom") |
| 1176 self.assertEquals(actual, expected) |
| 1177 |
| 1178 def testUnionWithOrdinals(self): |
| 1179 """Test that ordinals are assigned to fields.""" |
| 1180 source = """\ |
| 1181 module my_module; |
| 1182 |
| 1183 union MyUnion { |
| 1184 int32 a @10; |
| 1185 double b @30; |
| 1186 }; |
| 1187 """ |
| 1188 expected = ast.Mojom( |
| 1189 ast.Module(('IDENTIFIER', 'my_module'), None), |
| 1190 ast.ImportList(), |
| 1191 [ast.Union( |
| 1192 'MyUnion', |
| 1193 ast.UnionBody([ |
| 1194 ast.UnionField('a', ast.Ordinal(10), 'int32'), |
| 1195 ast.UnionField('b', ast.Ordinal(30), 'double') |
| 1196 ]))]) |
| 1197 actual = parser.Parse(source, "my_file.mojom") |
| 1198 self.assertEquals(actual, expected) |
| 1199 |
| 1200 def testUnionWithStructMembers(self): |
| 1201 """Test that struct members are accepted.""" |
| 1202 source = """\ |
| 1203 module my_module; |
| 1204 |
| 1205 union MyUnion { |
| 1206 SomeStruct s; |
| 1207 }; |
| 1208 """ |
| 1209 expected = ast.Mojom( |
| 1210 ast.Module(('IDENTIFIER', 'my_module'), None), |
| 1211 ast.ImportList(), |
| 1212 [ast.Union( |
| 1213 'MyUnion', |
| 1214 ast.UnionBody([ |
| 1215 ast.UnionField('s', None, 'SomeStruct') |
| 1216 ]))]) |
| 1217 actual = parser.Parse(source, "my_file.mojom") |
| 1218 self.assertEquals(actual, expected) |
| 1219 |
| 1220 def testUnionWithArrayMember(self): |
| 1221 """Test that array members are accepted.""" |
| 1222 source = """\ |
| 1223 module my_module; |
| 1224 |
| 1225 union MyUnion { |
| 1226 array<int32> a; |
| 1227 }; |
| 1228 """ |
| 1229 expected = ast.Mojom( |
| 1230 ast.Module(('IDENTIFIER', 'my_module'), None), |
| 1231 ast.ImportList(), |
| 1232 [ast.Union( |
| 1233 'MyUnion', |
| 1234 ast.UnionBody([ |
| 1235 ast.UnionField('a', None, 'int32[]') |
| 1236 ]))]) |
| 1237 actual = parser.Parse(source, "my_file.mojom") |
| 1238 self.assertEquals(actual, expected) |
| 1239 |
| 1240 def testUnionWithMapMember(self): |
| 1241 """Test that map members are accepted.""" |
| 1242 source = """\ |
| 1243 module my_module; |
| 1244 |
| 1245 union MyUnion { |
| 1246 map<int32, string> m; |
| 1247 }; |
| 1248 """ |
| 1249 expected = ast.Mojom( |
| 1250 ast.Module(('IDENTIFIER', 'my_module'), None), |
| 1251 ast.ImportList(), |
| 1252 [ast.Union( |
| 1253 'MyUnion', |
| 1254 ast.UnionBody([ |
| 1255 ast.UnionField('m', None, 'string{int32}') |
| 1256 ]))]) |
| 1257 actual = parser.Parse(source, "my_file.mojom") |
| 1258 self.assertEquals(actual, expected) |
| 1259 |
| 1260 def testUnionDisallowNestedStruct(self): |
| 1261 """Tests that structs cannot be nested in unions.""" |
| 1262 source = """\ |
| 1263 module my_module; |
| 1264 |
| 1265 union MyUnion { |
| 1266 struct MyStruct { |
| 1267 int32 a; |
| 1268 }; |
| 1269 }; |
| 1270 """ |
| 1271 with self.assertRaisesRegexp( |
| 1272 parser.ParseError, |
| 1273 r"^my_file\.mojom:4: Error: Unexpected 'struct':\n" |
| 1274 r" *struct MyStruct {$"): |
| 1275 parser.Parse(source, "my_file.mojom") |
| 1276 |
| 1277 def testUnionDisallowNestedInterfaces(self): |
| 1278 """Tests that interfaces cannot be nested in unions.""" |
| 1279 source = """\ |
| 1280 module my_module; |
| 1281 |
| 1282 union MyUnion { |
| 1283 interface MyInterface { |
| 1284 MyMethod(int32 a); |
| 1285 }; |
| 1286 }; |
| 1287 """ |
| 1288 with self.assertRaisesRegexp( |
| 1289 parser.ParseError, |
| 1290 r"^my_file\.mojom:4: Error: Unexpected 'interface':\n" |
| 1291 r" *interface MyInterface {$"): |
| 1292 parser.Parse(source, "my_file.mojom") |
| 1293 |
| 1294 def testUnionDisallowNestedUnion(self): |
| 1295 """Tests that unions cannot be nested in unions.""" |
| 1296 source = """\ |
| 1297 module my_module; |
| 1298 |
| 1299 union MyUnion { |
| 1300 union MyOtherUnion { |
| 1301 int32 a; |
| 1302 }; |
| 1303 }; |
| 1304 """ |
| 1305 with self.assertRaisesRegexp( |
| 1306 parser.ParseError, |
| 1307 r"^my_file\.mojom:4: Error: Unexpected 'union':\n" |
| 1308 r" *union MyOtherUnion {$"): |
| 1309 parser.Parse(source, "my_file.mojom") |
| 1310 |
| 1311 def testUnionDisallowNestedEnum(self): |
| 1312 """Tests that enums cannot be nested in unions.""" |
| 1313 source = """\ |
| 1314 module my_module; |
| 1315 |
| 1316 union MyUnion { |
| 1317 enum MyEnum { |
| 1318 A, |
| 1319 }; |
| 1320 }; |
| 1321 """ |
| 1322 with self.assertRaisesRegexp( |
| 1323 parser.ParseError, |
| 1324 r"^my_file\.mojom:4: Error: Unexpected 'enum':\n" |
| 1325 r" *enum MyEnum {$"): |
| 1326 parser.Parse(source, "my_file.mojom") |
| 1327 |
| 1328 |
1156 if __name__ == "__main__": | 1329 if __name__ == "__main__": |
1157 unittest.main() | 1330 unittest.main() |
OLD | NEW |