OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 package serialization | 5 package serialization |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "mojo/public/go/bindings" | 9 "mojo/public/go/bindings" |
10 "mojom/mojom_parser/generated/mojom_files" | 10 "mojom/mojom_parser/generated/mojom_files" |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 | 499 |
500 test.endTestCase() | 500 test.endTestCase() |
501 } | 501 } |
502 | 502 |
503 //////////////////////////////////////////////////////////// | 503 //////////////////////////////////////////////////////////// |
504 // Test Case: Use of the ServiceName attribute | 504 // Test Case: Use of the ServiceName attribute |
505 //////////////////////////////////////////////////////////// | 505 //////////////////////////////////////////////////////////// |
506 { | 506 { |
507 | 507 |
508 contents := ` | 508 contents := ` |
509 » module test; | 509 module test; |
510 | 510 |
511 » [ServiceName = "my.test.EchoService"] | 511 [ServiceName = "my.test.EchoService"] |
512 » interface EchoService { | 512 interface EchoService { |
513 EchoString(string? value) => (string? value); | 513 EchoString(string? value) => (string? value); |
514 };` | 514 };` |
515 | 515 |
516 test.addTestCase("test", contents) | 516 test.addTestCase("test", contents) |
517 | 517 |
518 // DeclaredMojomObjects | 518 // DeclaredMojomObjects |
519 test.expectedFile().DeclaredMojomObjects.Interfaces = &[]string{
"TYPE_KEY:test.EchoService"} | 519 test.expectedFile().DeclaredMojomObjects.Interfaces = &[]string{
"TYPE_KEY:test.EchoService"} |
520 | 520 |
521 // ResolvedTypes | 521 // ResolvedTypes |
522 | 522 |
523 // interface EchoService | 523 // interface EchoService |
524 test.expectedGraph().ResolvedTypes["TYPE_KEY:test.EchoService"]
= &mojom_types.UserDefinedTypeInterfaceType{mojom_types.MojomInterface{ | 524 test.expectedGraph().ResolvedTypes["TYPE_KEY:test.EchoService"]
= &mojom_types.UserDefinedTypeInterfaceType{mojom_types.MojomInterface{ |
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
866 // this step is done in parser_driver.go when each of the import
ed files are parsed. | 866 // this step is done in parser_driver.go when each of the import
ed files are parsed. |
867 mojomFile := parser.GetMojomFile() | 867 mojomFile := parser.GetMojomFile() |
868 if mojomFile.Imports != nil { | 868 if mojomFile.Imports != nil { |
869 for _, imp := range mojomFile.Imports { | 869 for _, imp := range mojomFile.Imports { |
870 imp.CanonicalFileName = fmt.Sprintf("%s.canonica
l", imp.SpecifiedName) | 870 imp.CanonicalFileName = fmt.Sprintf("%s.canonica
l", imp.SpecifiedName) |
871 } | 871 } |
872 } | 872 } |
873 | 873 |
874 // Serialize | 874 // Serialize |
875 EmitLineAndColumnNumbers = c.lineAndcolumnNumbers | 875 EmitLineAndColumnNumbers = c.lineAndcolumnNumbers |
| 876 EmitSerializedRuntimeTypeInfo = false |
876 bytes, _, err := Serialize(descriptor, false) | 877 bytes, _, err := Serialize(descriptor, false) |
877 if err != nil { | 878 if err != nil { |
878 t.Errorf("Serialization error for %s: %s", c.fileName, e
rr.Error()) | 879 t.Errorf("Serialization error for %s: %s", c.fileName, e
rr.Error()) |
879 continue | 880 continue |
880 } | 881 } |
881 | 882 |
882 // Deserialize | 883 // Deserialize |
883 decoder := bindings.NewDecoder(bytes, nil) | 884 decoder := bindings.NewDecoder(bytes, nil) |
884 fileGraph := mojom_files.MojomFileGraph{} | 885 fileGraph := mojom_files.MojomFileGraph{} |
885 fileGraph.Decode(decoder) | 886 fileGraph.Decode(decoder) |
886 | 887 |
887 // Compare | 888 // Compare |
888 » » if err := compareFileGraphs(c.expectedGraph, &fileGraph); err !=
nil { | 889 » » if err := compareTwoGoObjects(c.expectedGraph, &fileGraph); err
!= nil { |
889 t.Errorf("%s:\n%s", c.fileName, err.Error()) | 890 t.Errorf("%s:\n%s", c.fileName, err.Error()) |
890 continue | 891 continue |
891 } | 892 } |
892 } | 893 } |
893 } | 894 } |
894 | 895 |
895 //////////////////////////////////////////// | 896 //////////////////////////////////////////// |
896 /// Two-File Tests | 897 /// Two-File Tests |
897 /////////////////////////////////////////// | 898 /////////////////////////////////////////// |
898 | 899 |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1146 t.Errorf("ComputeEnumValueIntegers error for case %d: %s
", i, err.Error()) | 1147 t.Errorf("ComputeEnumValueIntegers error for case %d: %s
", i, err.Error()) |
1147 continue | 1148 continue |
1148 } | 1149 } |
1149 if err := descriptor.ComputeDataForGenerators(); err != nil { | 1150 if err := descriptor.ComputeDataForGenerators(); err != nil { |
1150 t.Errorf("ComputeDataForGenerators error for case %d: %s
", i, err.Error()) | 1151 t.Errorf("ComputeDataForGenerators error for case %d: %s
", i, err.Error()) |
1151 continue | 1152 continue |
1152 } | 1153 } |
1153 | 1154 |
1154 // Serialize | 1155 // Serialize |
1155 EmitLineAndColumnNumbers = c.lineAndcolumnNumbers | 1156 EmitLineAndColumnNumbers = c.lineAndcolumnNumbers |
| 1157 EmitSerializedRuntimeTypeInfo = false |
1156 bytes, _, err := Serialize(descriptor, false) | 1158 bytes, _, err := Serialize(descriptor, false) |
1157 if err != nil { | 1159 if err != nil { |
1158 t.Errorf("Serialization error for case %d: %s", i, err.E
rror()) | 1160 t.Errorf("Serialization error for case %d: %s", i, err.E
rror()) |
1159 continue | 1161 continue |
1160 } | 1162 } |
1161 | 1163 |
1162 // Deserialize | 1164 // Deserialize |
1163 decoder := bindings.NewDecoder(bytes, nil) | 1165 decoder := bindings.NewDecoder(bytes, nil) |
1164 fileGraph := mojom_files.MojomFileGraph{} | 1166 fileGraph := mojom_files.MojomFileGraph{} |
1165 fileGraph.Decode(decoder) | 1167 fileGraph.Decode(decoder) |
1166 | 1168 |
1167 // Compare | 1169 // Compare |
1168 » » if err := compareFileGraphs(c.expectedGraph, &fileGraph); err !=
nil { | 1170 » » if err := compareTwoGoObjects(c.expectedGraph, &fileGraph); err
!= nil { |
1169 t.Errorf("case %d:\n%s", i, err.Error()) | 1171 t.Errorf("case %d:\n%s", i, err.Error()) |
1170 continue | 1172 continue |
1171 } | 1173 } |
1172 } | 1174 } |
1173 } | 1175 } |
1174 | 1176 |
1175 // compareFileGraphs compares |expected| and |actual| and returns a non-nil | 1177 //////////////////////////////////////////// |
| 1178 /// Serialized Runtime Type Info Tests |
| 1179 /////////////////////////////////////////// |
| 1180 |
| 1181 type runtimeTypeInfoTestCase struct { |
| 1182 // The contents of the two files |
| 1183 mojomContentsA string |
| 1184 mojomContentsB string |
| 1185 expectedRuntimeTypeInfoA *mojom_files.RuntimeTypeInfo |
| 1186 expectedRuntimeTypeInfoB *mojom_files.RuntimeTypeInfo |
| 1187 } |
| 1188 |
| 1189 type runtimeTypeInfoTest struct { |
| 1190 cases []runtimeTypeInfoTestCase |
| 1191 testCaseNum int |
| 1192 } |
| 1193 |
| 1194 func (t *runtimeTypeInfoTest) expectedRuntimeTypeInfoA() *mojom_files.RuntimeTyp
eInfo { |
| 1195 return t.cases[t.testCaseNum].expectedRuntimeTypeInfoA |
| 1196 } |
| 1197 |
| 1198 func (t *runtimeTypeInfoTest) expectedRuntimeTypeInfoB() *mojom_files.RuntimeTyp
eInfo { |
| 1199 return t.cases[t.testCaseNum].expectedRuntimeTypeInfoB |
| 1200 } |
| 1201 |
| 1202 func (test *runtimeTypeInfoTest) addTestCase(contentsA, contentsB string) { |
| 1203 test.cases = append(test.cases, runtimeTypeInfoTestCase{contentsA, conte
ntsB, new(mojom_files.RuntimeTypeInfo), new(mojom_files.RuntimeTypeInfo)}) |
| 1204 test.expectedRuntimeTypeInfoA().ServicesByName = make(map[string]mojom_f
iles.ServiceTypeInfo) |
| 1205 test.expectedRuntimeTypeInfoA().TypeMap = make(map[string]mojom_types.Us
erDefinedType) |
| 1206 test.expectedRuntimeTypeInfoB().ServicesByName = make(map[string]mojom_f
iles.ServiceTypeInfo) |
| 1207 test.expectedRuntimeTypeInfoB().TypeMap = make(map[string]mojom_types.Us
erDefinedType) |
| 1208 } |
| 1209 |
| 1210 func (test *runtimeTypeInfoTest) fileNameA() string { |
| 1211 return fmt.Sprintf("file%dA", test.testCaseNum) |
| 1212 } |
| 1213 |
| 1214 func (test *runtimeTypeInfoTest) fileNameB() string { |
| 1215 return fmt.Sprintf("file%dB", test.testCaseNum) |
| 1216 } |
| 1217 |
| 1218 func (test *runtimeTypeInfoTest) endTestCase() { |
| 1219 test.testCaseNum += 1 |
| 1220 } |
| 1221 |
| 1222 // TestRuntimeTypeInfo uses a series of test cases in which the text of two .moj
om |
| 1223 // files is specified and the expected RuntimeTypeInfos are specified using Go s
truct literals. |
| 1224 func TestRuntimeTypeInfo(t *testing.T) { |
| 1225 test := runtimeTypeInfoTest{} |
| 1226 |
| 1227 ///////////////////////////////////////////////////////////// |
| 1228 // Test Case: No nterfaces. |
| 1229 ///////////////////////////////////////////////////////////// |
| 1230 { |
| 1231 |
| 1232 contentsA := ` |
| 1233 module a.b.c; |
| 1234 struct FooA{ |
| 1235 };` |
| 1236 |
| 1237 contentsB := ` |
| 1238 module b.c.d; |
| 1239 struct FooB{ |
| 1240 };` |
| 1241 |
| 1242 test.addTestCase(contentsA, contentsB) |
| 1243 |
| 1244 // TypeMap for file A |
| 1245 test.expectedRuntimeTypeInfoA().TypeMap["TYPE_KEY:a.b.c.FooA"] =
&mojom_types.UserDefinedTypeStructType{mojom_types.MojomStruct{ |
| 1246 DeclData: newDeclData(test.fileNameA(), "FooA", "a.b.c.F
ooA"), |
| 1247 Fields: []mojom_types.StructField{}}} |
| 1248 |
| 1249 // TypeMap for file B |
| 1250 test.expectedRuntimeTypeInfoB().TypeMap["TYPE_KEY:b.c.d.FooB"] =
&mojom_types.UserDefinedTypeStructType{mojom_types.MojomStruct{ |
| 1251 DeclData: newDeclData(test.fileNameB(), "FooB", "b.c.d.F
ooB"), |
| 1252 Fields: []mojom_types.StructField{}}} |
| 1253 |
| 1254 test.endTestCase() |
| 1255 } |
| 1256 |
| 1257 ///////////////////////////////////////////////////////////// |
| 1258 // Test Case: Two interfaces: Not top-level. |
| 1259 ///////////////////////////////////////////////////////////// |
| 1260 { |
| 1261 |
| 1262 contentsA := ` |
| 1263 module a.b.c; |
| 1264 struct FooA{ |
| 1265 }; |
| 1266 |
| 1267 interface InterfaceA { |
| 1268 DoIt(FooA x) => (b.c.d.FooB? y); |
| 1269 }; |
| 1270 ` |
| 1271 |
| 1272 contentsB := ` |
| 1273 module b.c.d; |
| 1274 struct FooB{ |
| 1275 }; |
| 1276 |
| 1277 interface InterfaceB { |
| 1278 DoIt(a.b.c.FooA x) => (FooB? y); |
| 1279 }; |
| 1280 |
| 1281 ` |
| 1282 test.addTestCase(contentsA, contentsB) |
| 1283 |
| 1284 // TypeMap for file A |
| 1285 |
| 1286 // FooA |
| 1287 test.expectedRuntimeTypeInfoA().TypeMap["TYPE_KEY:a.b.c.FooA"] =
&mojom_types.UserDefinedTypeStructType{mojom_types.MojomStruct{ |
| 1288 DeclData: newDeclData(test.fileNameA(), "FooA", "a.b.c.F
ooA"), |
| 1289 Fields: []mojom_types.StructField{}}} |
| 1290 |
| 1291 // InterfaceA |
| 1292 test.expectedRuntimeTypeInfoA().TypeMap["TYPE_KEY:a.b.c.Interfac
eA"] = &mojom_types.UserDefinedTypeInterfaceType{mojom_types.MojomInterface{ |
| 1293 DeclData: newDeclData(test.fileNameA(), "InterfaceA", "a
.b.c.InterfaceA"), |
| 1294 Methods: map[uint32]mojom_types.MojomMethod{ |
| 1295 0: mojom_types.MojomMethod{ |
| 1296 DeclData: newDeclData(test.fileNameA(),
"DoIt", ""), |
| 1297 Parameters: mojom_types.MojomStruct{ |
| 1298 DeclData: newDeclData(test.fileN
ameA(), "DoIt-request", ""), |
| 1299 Fields: []mojom_types.StructFiel
d{ |
| 1300 mojom_types.StructField{ |
| 1301 DeclData: newDec
lData(test.fileNameA(), "x", ""), |
| 1302 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1303 false, f
alse, stringPointer("FooA"), stringPointer("TYPE_KEY:a.b.c.FooA")}}, |
| 1304 }, |
| 1305 }, |
| 1306 }, |
| 1307 ResponseParams: &mojom_types.MojomStruct
{ |
| 1308 DeclData: newDeclData(test.fileN
ameA(), "DoIt-response", ""), |
| 1309 Fields: []mojom_types.StructFiel
d{ |
| 1310 mojom_types.StructField{ |
| 1311 DeclData: newDec
lData(test.fileNameA(), "y", ""), |
| 1312 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1313 true, fa
lse, stringPointer("b.c.d.FooB"), stringPointer("TYPE_KEY:b.c.d.FooB")}}, |
| 1314 }, |
| 1315 }, |
| 1316 }, |
| 1317 }, |
| 1318 }, |
| 1319 }} |
| 1320 |
| 1321 ////////////////////////////////////////////////////////////////
//////// |
| 1322 |
| 1323 // TypeMap for file B |
| 1324 |
| 1325 // FooA |
| 1326 test.expectedRuntimeTypeInfoB().TypeMap["TYPE_KEY:b.c.d.FooB"] =
&mojom_types.UserDefinedTypeStructType{mojom_types.MojomStruct{ |
| 1327 DeclData: newDeclData(test.fileNameB(), "FooB", "b.c.d.F
ooB"), |
| 1328 Fields: []mojom_types.StructField{}}} |
| 1329 |
| 1330 // InterfaceB |
| 1331 test.expectedRuntimeTypeInfoB().TypeMap["TYPE_KEY:b.c.d.Interfac
eB"] = &mojom_types.UserDefinedTypeInterfaceType{mojom_types.MojomInterface{ |
| 1332 DeclData: newDeclData(test.fileNameB(), "InterfaceB", "b
.c.d.InterfaceB"), |
| 1333 Methods: map[uint32]mojom_types.MojomMethod{ |
| 1334 0: mojom_types.MojomMethod{ |
| 1335 DeclData: newDeclData(test.fileNameB(),
"DoIt", ""), |
| 1336 Parameters: mojom_types.MojomStruct{ |
| 1337 DeclData: newDeclData(test.fileN
ameB(), "DoIt-request", ""), |
| 1338 Fields: []mojom_types.StructFiel
d{ |
| 1339 mojom_types.StructField{ |
| 1340 DeclData: newDec
lData(test.fileNameB(), "x", ""), |
| 1341 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1342 false, f
alse, stringPointer("a.b.c.FooA"), stringPointer("TYPE_KEY:a.b.c.FooA")}}, |
| 1343 }, |
| 1344 }, |
| 1345 }, |
| 1346 ResponseParams: &mojom_types.MojomStruct
{ |
| 1347 DeclData: newDeclData(test.fileN
ameB(), "DoIt-response", ""), |
| 1348 Fields: []mojom_types.StructFiel
d{ |
| 1349 mojom_types.StructField{ |
| 1350 DeclData: newDec
lData(test.fileNameB(), "y", ""), |
| 1351 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1352 true, fa
lse, stringPointer("FooB"), stringPointer("TYPE_KEY:b.c.d.FooB")}}, |
| 1353 }, |
| 1354 }, |
| 1355 }, |
| 1356 }, |
| 1357 }, |
| 1358 }} |
| 1359 |
| 1360 test.endTestCase() |
| 1361 } |
| 1362 |
| 1363 ///////////////////////////////////////////////////////////// |
| 1364 // Test Case: Two interfaces: One of them top-level. |
| 1365 ///////////////////////////////////////////////////////////// |
| 1366 { |
| 1367 |
| 1368 contentsA := ` |
| 1369 module a.b.c; |
| 1370 struct FooA{ |
| 1371 }; |
| 1372 |
| 1373 [ServiceName = "AwesomeService"] |
| 1374 interface InterfaceA { |
| 1375 DoIt(FooA x) => (b.c.d.FooB? y); |
| 1376 }; |
| 1377 ` |
| 1378 |
| 1379 contentsB := ` |
| 1380 module b.c.d; |
| 1381 struct FooB{ |
| 1382 }; |
| 1383 |
| 1384 interface InterfaceB { |
| 1385 DoIt(a.b.c.FooA x) => (FooB? y); |
| 1386 }; |
| 1387 |
| 1388 ` |
| 1389 test.addTestCase(contentsA, contentsB) |
| 1390 |
| 1391 // ServicesByName for file A |
| 1392 test.expectedRuntimeTypeInfoA().ServicesByName["AwesomeService"]
= mojom_files.ServiceTypeInfo{ |
| 1393 TopLevelInterface: "TYPE_KEY:a.b.c.InterfaceA", |
| 1394 CompleteTypeSet: []string{"TYPE_KEY:a.b.c.FooA", "TYPE
_KEY:a.b.c.InterfaceA", "TYPE_KEY:b.c.d.FooB"}, |
| 1395 } |
| 1396 |
| 1397 // TypeMap for file A |
| 1398 |
| 1399 // FooA |
| 1400 test.expectedRuntimeTypeInfoA().TypeMap["TYPE_KEY:a.b.c.FooA"] =
&mojom_types.UserDefinedTypeStructType{mojom_types.MojomStruct{ |
| 1401 DeclData: newDeclData(test.fileNameA(), "FooA", "a.b.c.F
ooA"), |
| 1402 Fields: []mojom_types.StructField{}}} |
| 1403 |
| 1404 // InterfaceA |
| 1405 test.expectedRuntimeTypeInfoA().TypeMap["TYPE_KEY:a.b.c.Interfac
eA"] = &mojom_types.UserDefinedTypeInterfaceType{mojom_types.MojomInterface{ |
| 1406 DeclData: newDeclDataA(test.fileNameA(), "InterfaceA", "
a.b.c.InterfaceA", |
| 1407 &[]mojom_types.Attribute{{"ServiceName", &mojom_
types.LiteralValueStringValue{"AwesomeService"}}}), |
| 1408 ServiceName: stringPointer("AwesomeService"), |
| 1409 Methods: map[uint32]mojom_types.MojomMethod{ |
| 1410 0: mojom_types.MojomMethod{ |
| 1411 DeclData: newDeclData(test.fileNameA(),
"DoIt", ""), |
| 1412 Parameters: mojom_types.MojomStruct{ |
| 1413 DeclData: newDeclData(test.fileN
ameA(), "DoIt-request", ""), |
| 1414 Fields: []mojom_types.StructFiel
d{ |
| 1415 mojom_types.StructField{ |
| 1416 DeclData: newDec
lData(test.fileNameA(), "x", ""), |
| 1417 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1418 false, f
alse, stringPointer("FooA"), stringPointer("TYPE_KEY:a.b.c.FooA")}}, |
| 1419 }, |
| 1420 }, |
| 1421 }, |
| 1422 ResponseParams: &mojom_types.MojomStruct
{ |
| 1423 DeclData: newDeclData(test.fileN
ameA(), "DoIt-response", ""), |
| 1424 Fields: []mojom_types.StructFiel
d{ |
| 1425 mojom_types.StructField{ |
| 1426 DeclData: newDec
lData(test.fileNameA(), "y", ""), |
| 1427 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1428 true, fa
lse, stringPointer("b.c.d.FooB"), stringPointer("TYPE_KEY:b.c.d.FooB")}}, |
| 1429 }, |
| 1430 }, |
| 1431 }, |
| 1432 }, |
| 1433 }, |
| 1434 }} |
| 1435 |
| 1436 ////////////////////////////////////////////////////////////////
//////// |
| 1437 |
| 1438 // TypeMap for file B |
| 1439 |
| 1440 // FooA |
| 1441 test.expectedRuntimeTypeInfoB().TypeMap["TYPE_KEY:b.c.d.FooB"] =
&mojom_types.UserDefinedTypeStructType{mojom_types.MojomStruct{ |
| 1442 DeclData: newDeclData(test.fileNameB(), "FooB", "b.c.d.F
ooB"), |
| 1443 Fields: []mojom_types.StructField{}}} |
| 1444 |
| 1445 // InterfaceB |
| 1446 test.expectedRuntimeTypeInfoB().TypeMap["TYPE_KEY:b.c.d.Interfac
eB"] = &mojom_types.UserDefinedTypeInterfaceType{mojom_types.MojomInterface{ |
| 1447 DeclData: newDeclData(test.fileNameB(), "InterfaceB", "b
.c.d.InterfaceB"), |
| 1448 Methods: map[uint32]mojom_types.MojomMethod{ |
| 1449 0: mojom_types.MojomMethod{ |
| 1450 DeclData: newDeclData(test.fileNameB(),
"DoIt", ""), |
| 1451 Parameters: mojom_types.MojomStruct{ |
| 1452 DeclData: newDeclData(test.fileN
ameB(), "DoIt-request", ""), |
| 1453 Fields: []mojom_types.StructFiel
d{ |
| 1454 mojom_types.StructField{ |
| 1455 DeclData: newDec
lData(test.fileNameB(), "x", ""), |
| 1456 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1457 false, f
alse, stringPointer("a.b.c.FooA"), stringPointer("TYPE_KEY:a.b.c.FooA")}}, |
| 1458 }, |
| 1459 }, |
| 1460 }, |
| 1461 ResponseParams: &mojom_types.MojomStruct
{ |
| 1462 DeclData: newDeclData(test.fileN
ameB(), "DoIt-response", ""), |
| 1463 Fields: []mojom_types.StructFiel
d{ |
| 1464 mojom_types.StructField{ |
| 1465 DeclData: newDec
lData(test.fileNameB(), "y", ""), |
| 1466 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1467 true, fa
lse, stringPointer("FooB"), stringPointer("TYPE_KEY:b.c.d.FooB")}}, |
| 1468 }, |
| 1469 }, |
| 1470 }, |
| 1471 }, |
| 1472 }, |
| 1473 }} |
| 1474 |
| 1475 test.endTestCase() |
| 1476 } |
| 1477 |
| 1478 ///////////////////////////////////////////////////////////// |
| 1479 // Test Case: Same as above with extra attributes. |
| 1480 ///////////////////////////////////////////////////////////// |
| 1481 { |
| 1482 |
| 1483 contentsA := ` |
| 1484 module a.b.c; |
| 1485 struct FooA{ |
| 1486 }; |
| 1487 |
| 1488 [Color="RED", ServiceName = 7, ServiceName = "AwesomeService", Height=10
.1] |
| 1489 interface InterfaceA { |
| 1490 DoIt(FooA x) => (b.c.d.FooB? y); |
| 1491 }; |
| 1492 ` |
| 1493 |
| 1494 contentsB := ` |
| 1495 module b.c.d; |
| 1496 struct FooB{ |
| 1497 }; |
| 1498 |
| 1499 [ServiceName = 42] |
| 1500 interface InterfaceB { |
| 1501 DoIt(a.b.c.FooA x) => (FooB? y); |
| 1502 }; |
| 1503 |
| 1504 ` |
| 1505 test.addTestCase(contentsA, contentsB) |
| 1506 |
| 1507 // ServicesByName for file A |
| 1508 test.expectedRuntimeTypeInfoA().ServicesByName["AwesomeService"]
= mojom_files.ServiceTypeInfo{ |
| 1509 TopLevelInterface: "TYPE_KEY:a.b.c.InterfaceA", |
| 1510 CompleteTypeSet: []string{"TYPE_KEY:a.b.c.FooA", "TYPE
_KEY:a.b.c.InterfaceA", "TYPE_KEY:b.c.d.FooB"}, |
| 1511 } |
| 1512 |
| 1513 // TypeMap for file A |
| 1514 |
| 1515 // FooA |
| 1516 test.expectedRuntimeTypeInfoA().TypeMap["TYPE_KEY:a.b.c.FooA"] =
&mojom_types.UserDefinedTypeStructType{mojom_types.MojomStruct{ |
| 1517 DeclData: newDeclData(test.fileNameA(), "FooA", "a.b.c.F
ooA"), |
| 1518 Fields: []mojom_types.StructField{}}} |
| 1519 |
| 1520 // InterfaceA |
| 1521 test.expectedRuntimeTypeInfoA().TypeMap["TYPE_KEY:a.b.c.Interfac
eA"] = &mojom_types.UserDefinedTypeInterfaceType{mojom_types.MojomInterface{ |
| 1522 DeclData: newDeclDataA(test.fileNameA(), "InterfaceA", "
a.b.c.InterfaceA", |
| 1523 &[]mojom_types.Attribute{ |
| 1524 {"Color", &mojom_types.LiteralValueStrin
gValue{"RED"}}, |
| 1525 {"ServiceName", &mojom_types.LiteralValu
eInt8Value{7}}, |
| 1526 {"ServiceName", &mojom_types.LiteralValu
eStringValue{"AwesomeService"}}, |
| 1527 {"Height", &mojom_types.LiteralValueDoub
leValue{10.1}}}), |
| 1528 ServiceName: stringPointer("AwesomeService"), |
| 1529 Methods: map[uint32]mojom_types.MojomMethod{ |
| 1530 0: mojom_types.MojomMethod{ |
| 1531 DeclData: newDeclData(test.fileNameA(),
"DoIt", ""), |
| 1532 Parameters: mojom_types.MojomStruct{ |
| 1533 DeclData: newDeclData(test.fileN
ameA(), "DoIt-request", ""), |
| 1534 Fields: []mojom_types.StructFiel
d{ |
| 1535 mojom_types.StructField{ |
| 1536 DeclData: newDec
lData(test.fileNameA(), "x", ""), |
| 1537 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1538 false, f
alse, stringPointer("FooA"), stringPointer("TYPE_KEY:a.b.c.FooA")}}, |
| 1539 }, |
| 1540 }, |
| 1541 }, |
| 1542 ResponseParams: &mojom_types.MojomStruct
{ |
| 1543 DeclData: newDeclData(test.fileN
ameA(), "DoIt-response", ""), |
| 1544 Fields: []mojom_types.StructFiel
d{ |
| 1545 mojom_types.StructField{ |
| 1546 DeclData: newDec
lData(test.fileNameA(), "y", ""), |
| 1547 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1548 true, fa
lse, stringPointer("b.c.d.FooB"), stringPointer("TYPE_KEY:b.c.d.FooB")}}, |
| 1549 }, |
| 1550 }, |
| 1551 }, |
| 1552 }, |
| 1553 }, |
| 1554 }} |
| 1555 |
| 1556 ////////////////////////////////////////////////////////////////
//////// |
| 1557 |
| 1558 // TypeMap for file B |
| 1559 |
| 1560 // FooA |
| 1561 test.expectedRuntimeTypeInfoB().TypeMap["TYPE_KEY:b.c.d.FooB"] =
&mojom_types.UserDefinedTypeStructType{mojom_types.MojomStruct{ |
| 1562 DeclData: newDeclData(test.fileNameB(), "FooB", "b.c.d.F
ooB"), |
| 1563 Fields: []mojom_types.StructField{}}} |
| 1564 |
| 1565 // InterfaceB |
| 1566 test.expectedRuntimeTypeInfoB().TypeMap["TYPE_KEY:b.c.d.Interfac
eB"] = &mojom_types.UserDefinedTypeInterfaceType{mojom_types.MojomInterface{ |
| 1567 DeclData: newDeclDataA(test.fileNameB(), "InterfaceB", "
b.c.d.InterfaceB", |
| 1568 &[]mojom_types.Attribute{{"ServiceName", &mojom_
types.LiteralValueInt8Value{42}}}), |
| 1569 Methods: map[uint32]mojom_types.MojomMethod{ |
| 1570 0: mojom_types.MojomMethod{ |
| 1571 DeclData: newDeclData(test.fileNameB(),
"DoIt", ""), |
| 1572 Parameters: mojom_types.MojomStruct{ |
| 1573 DeclData: newDeclData(test.fileN
ameB(), "DoIt-request", ""), |
| 1574 Fields: []mojom_types.StructFiel
d{ |
| 1575 mojom_types.StructField{ |
| 1576 DeclData: newDec
lData(test.fileNameB(), "x", ""), |
| 1577 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1578 false, f
alse, stringPointer("a.b.c.FooA"), stringPointer("TYPE_KEY:a.b.c.FooA")}}, |
| 1579 }, |
| 1580 }, |
| 1581 }, |
| 1582 ResponseParams: &mojom_types.MojomStruct
{ |
| 1583 DeclData: newDeclData(test.fileN
ameB(), "DoIt-response", ""), |
| 1584 Fields: []mojom_types.StructFiel
d{ |
| 1585 mojom_types.StructField{ |
| 1586 DeclData: newDec
lData(test.fileNameB(), "y", ""), |
| 1587 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1588 true, fa
lse, stringPointer("FooB"), stringPointer("TYPE_KEY:b.c.d.FooB")}}, |
| 1589 }, |
| 1590 }, |
| 1591 }, |
| 1592 }, |
| 1593 }, |
| 1594 }} |
| 1595 |
| 1596 test.endTestCase() |
| 1597 } |
| 1598 |
| 1599 ///////////////////////////////////////////////////////////// |
| 1600 // Test Case: Additional reachable types. |
| 1601 ///////////////////////////////////////////////////////////// |
| 1602 { |
| 1603 |
| 1604 contentsA := ` |
| 1605 module a.b.c; |
| 1606 struct FooA{ |
| 1607 }; |
| 1608 |
| 1609 [ServiceName = "AwesomeService"] |
| 1610 interface InterfaceA { |
| 1611 DoIt(FooA x) => (b.c.d.FooB? y); |
| 1612 }; |
| 1613 ` |
| 1614 |
| 1615 contentsB := ` |
| 1616 module b.c.d; |
| 1617 |
| 1618 enum Enum1{}; // This is in the CompleteTypeSet of InterfaceA |
| 1619 enum Enum2{}; // This is not. |
| 1620 |
| 1621 struct FooB{ |
| 1622 Enum1 x; |
| 1623 }; |
| 1624 |
| 1625 interface InterfaceB { |
| 1626 DoIt(a.b.c.FooA x) => (FooB? y, Enum2 z); |
| 1627 }; |
| 1628 |
| 1629 ` |
| 1630 test.addTestCase(contentsA, contentsB) |
| 1631 |
| 1632 // ServicesByName for file A |
| 1633 test.expectedRuntimeTypeInfoA().ServicesByName["AwesomeService"]
= mojom_files.ServiceTypeInfo{ |
| 1634 TopLevelInterface: "TYPE_KEY:a.b.c.InterfaceA", |
| 1635 CompleteTypeSet: []string{"TYPE_KEY:a.b.c.FooA", "TYPE
_KEY:a.b.c.InterfaceA", "TYPE_KEY:b.c.d.Enum1", "TYPE_KEY:b.c.d.FooB"}, |
| 1636 } |
| 1637 |
| 1638 // TypeMap for file A |
| 1639 |
| 1640 // FooA |
| 1641 test.expectedRuntimeTypeInfoA().TypeMap["TYPE_KEY:a.b.c.FooA"] =
&mojom_types.UserDefinedTypeStructType{mojom_types.MojomStruct{ |
| 1642 DeclData: newDeclData(test.fileNameA(), "FooA", "a.b.c.F
ooA"), |
| 1643 Fields: []mojom_types.StructField{}}} |
| 1644 |
| 1645 // InterfaceA |
| 1646 test.expectedRuntimeTypeInfoA().TypeMap["TYPE_KEY:a.b.c.Interfac
eA"] = &mojom_types.UserDefinedTypeInterfaceType{mojom_types.MojomInterface{ |
| 1647 DeclData: newDeclDataA(test.fileNameA(), "InterfaceA", "
a.b.c.InterfaceA", |
| 1648 &[]mojom_types.Attribute{{"ServiceName", &mojom_
types.LiteralValueStringValue{"AwesomeService"}}}), |
| 1649 ServiceName: stringPointer("AwesomeService"), |
| 1650 Methods: map[uint32]mojom_types.MojomMethod{ |
| 1651 0: mojom_types.MojomMethod{ |
| 1652 DeclData: newDeclData(test.fileNameA(),
"DoIt", ""), |
| 1653 Parameters: mojom_types.MojomStruct{ |
| 1654 DeclData: newDeclData(test.fileN
ameA(), "DoIt-request", ""), |
| 1655 Fields: []mojom_types.StructFiel
d{ |
| 1656 mojom_types.StructField{ |
| 1657 DeclData: newDec
lData(test.fileNameA(), "x", ""), |
| 1658 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1659 false, f
alse, stringPointer("FooA"), stringPointer("TYPE_KEY:a.b.c.FooA")}}, |
| 1660 }, |
| 1661 }, |
| 1662 }, |
| 1663 ResponseParams: &mojom_types.MojomStruct
{ |
| 1664 DeclData: newDeclData(test.fileN
ameA(), "DoIt-response", ""), |
| 1665 Fields: []mojom_types.StructFiel
d{ |
| 1666 mojom_types.StructField{ |
| 1667 DeclData: newDec
lData(test.fileNameA(), "y", ""), |
| 1668 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1669 true, fa
lse, stringPointer("b.c.d.FooB"), stringPointer("TYPE_KEY:b.c.d.FooB")}}, |
| 1670 }, |
| 1671 }, |
| 1672 }, |
| 1673 }, |
| 1674 }, |
| 1675 }} |
| 1676 |
| 1677 ////////////////////////////////////////////////////////////////
//////// |
| 1678 |
| 1679 // TypeMap for file B |
| 1680 |
| 1681 // Enum1 |
| 1682 test.expectedRuntimeTypeInfoB().TypeMap["TYPE_KEY:b.c.d.Enum1"]
= &mojom_types.UserDefinedTypeEnumType{mojom_types.MojomEnum{ |
| 1683 DeclData: newDeclData(test.fileNameB(), "Enum1", "b.c.d.
Enum1"), |
| 1684 Values: []mojom_types.EnumValue{}, |
| 1685 }} |
| 1686 |
| 1687 // Enum2 |
| 1688 test.expectedRuntimeTypeInfoB().TypeMap["TYPE_KEY:b.c.d.Enum2"]
= &mojom_types.UserDefinedTypeEnumType{mojom_types.MojomEnum{ |
| 1689 DeclData: newDeclData(test.fileNameB(), "Enum2", "b.c.d.
Enum2"), |
| 1690 Values: []mojom_types.EnumValue{}, |
| 1691 }} |
| 1692 |
| 1693 // FooA |
| 1694 test.expectedRuntimeTypeInfoB().TypeMap["TYPE_KEY:b.c.d.FooB"] =
&mojom_types.UserDefinedTypeStructType{mojom_types.MojomStruct{ |
| 1695 DeclData: newDeclData(test.fileNameB(), "FooB", "b.c.d.F
ooB"), |
| 1696 Fields: []mojom_types.StructField{ |
| 1697 mojom_types.StructField{ |
| 1698 DeclData: newDeclData(test.fileNameB(),
"x", ""), |
| 1699 Type: &mojom_types.TypeTypeReference{moj
om_types.TypeReference{ |
| 1700 false, false, stringPointer("Enu
m1"), stringPointer("TYPE_KEY:b.c.d.Enum1")}}, |
| 1701 }, |
| 1702 }, |
| 1703 }} |
| 1704 |
| 1705 // InterfaceB |
| 1706 test.expectedRuntimeTypeInfoB().TypeMap["TYPE_KEY:b.c.d.Interfac
eB"] = &mojom_types.UserDefinedTypeInterfaceType{mojom_types.MojomInterface{ |
| 1707 DeclData: newDeclData(test.fileNameB(), "InterfaceB", "b
.c.d.InterfaceB"), |
| 1708 Methods: map[uint32]mojom_types.MojomMethod{ |
| 1709 0: mojom_types.MojomMethod{ |
| 1710 DeclData: newDeclData(test.fileNameB(),
"DoIt", ""), |
| 1711 Parameters: mojom_types.MojomStruct{ |
| 1712 DeclData: newDeclData(test.fileN
ameB(), "DoIt-request", ""), |
| 1713 Fields: []mojom_types.StructFiel
d{ |
| 1714 mojom_types.StructField{ |
| 1715 DeclData: newDec
lData(test.fileNameB(), "x", ""), |
| 1716 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1717 false, f
alse, stringPointer("a.b.c.FooA"), stringPointer("TYPE_KEY:a.b.c.FooA")}}, |
| 1718 }, |
| 1719 }, |
| 1720 }, |
| 1721 ResponseParams: &mojom_types.MojomStruct
{ |
| 1722 DeclData: newDeclData(test.fileN
ameB(), "DoIt-response", ""), |
| 1723 Fields: []mojom_types.StructFiel
d{ |
| 1724 mojom_types.StructField{ |
| 1725 DeclData: newDec
lData(test.fileNameB(), "y", ""), |
| 1726 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1727 true, fa
lse, stringPointer("FooB"), stringPointer("TYPE_KEY:b.c.d.FooB")}}, |
| 1728 }, |
| 1729 mojom_types.StructField{ |
| 1730 DeclData: newDec
lData(test.fileNameB(), "z", ""), |
| 1731 Type: &mojom_typ
es.TypeTypeReference{mojom_types.TypeReference{ |
| 1732 false, f
alse, stringPointer("Enum2"), stringPointer("TYPE_KEY:b.c.d.Enum2")}}, |
| 1733 }, |
| 1734 }, |
| 1735 }, |
| 1736 }, |
| 1737 }, |
| 1738 }} |
| 1739 |
| 1740 test.endTestCase() |
| 1741 } |
| 1742 |
| 1743 //////////////////////////////////////////////////////////// |
| 1744 // Execute all of the test cases. |
| 1745 //////////////////////////////////////////////////////////// |
| 1746 for i, c := range test.cases { |
| 1747 descriptor := mojom.NewMojomDescriptor() |
| 1748 fileNameA := fmt.Sprintf("file%dA", i) |
| 1749 fileNameB := fmt.Sprintf("file%dB", i) |
| 1750 |
| 1751 // Parse file A. |
| 1752 parserA := parser.MakeParser(fileNameA, fileNameA, c.mojomConten
tsA, descriptor, nil) |
| 1753 parserA.Parse() |
| 1754 if !parserA.OK() { |
| 1755 t.Errorf("Parsing error for %s: %s", fileNameA, parserA.
GetError().Error()) |
| 1756 continue |
| 1757 } |
| 1758 |
| 1759 // Parse file B. |
| 1760 parserB := parser.MakeParser(fileNameB, fileNameB, c.mojomConten
tsB, descriptor, nil) |
| 1761 parserB.Parse() |
| 1762 if !parserB.OK() { |
| 1763 t.Errorf("Parsing error for %s: %s", fileNameB, parserB.
GetError().Error()) |
| 1764 continue |
| 1765 } |
| 1766 |
| 1767 // Resolve |
| 1768 if err := descriptor.Resolve(); err != nil { |
| 1769 t.Errorf("Resolve error for case %d: %s", i, err.Error()
) |
| 1770 continue |
| 1771 } |
| 1772 if err := descriptor.ComputeEnumValueIntegers(); err != nil { |
| 1773 t.Errorf("ComputeEnumValueIntegers error for case %d: %s
", i, err.Error()) |
| 1774 continue |
| 1775 } |
| 1776 if err := descriptor.ComputeDataForGenerators(); err != nil { |
| 1777 t.Errorf("ComputeDataForGenerators error for case %d: %s
", i, err.Error()) |
| 1778 continue |
| 1779 } |
| 1780 |
| 1781 // Serialize |
| 1782 EmitLineAndColumnNumbers = false |
| 1783 EmitSerializedRuntimeTypeInfo = true |
| 1784 bytes, _, err := Serialize(descriptor, false) |
| 1785 if err != nil { |
| 1786 t.Errorf("Serialization error for case %d: %s", i, err.E
rror()) |
| 1787 continue |
| 1788 } |
| 1789 |
| 1790 // Deserialize |
| 1791 decoder := bindings.NewDecoder(bytes, nil) |
| 1792 fileGraph := mojom_files.MojomFileGraph{} |
| 1793 fileGraph.Decode(decoder) |
| 1794 |
| 1795 // Deserialize RuntimeTypeInfo A |
| 1796 decoder = bindings.NewDecoder(*fileGraph.Files[fileNameA].Serial
izedRuntimeTypeInfo, nil) |
| 1797 runtimeTypeInfoA := mojom_files.RuntimeTypeInfo{} |
| 1798 runtimeTypeInfoA.Decode(decoder) |
| 1799 |
| 1800 // Deserialize RuntimeTypeInfo B |
| 1801 decoder = bindings.NewDecoder(*fileGraph.Files[fileNameB].Serial
izedRuntimeTypeInfo, nil) |
| 1802 runtimeTypeInfoB := mojom_files.RuntimeTypeInfo{} |
| 1803 runtimeTypeInfoB.Decode(decoder) |
| 1804 |
| 1805 // Compare A |
| 1806 if err := compareTwoGoObjects(c.expectedRuntimeTypeInfoA, &runti
meTypeInfoA); err != nil { |
| 1807 t.Errorf("case %d A:\n%s", i, err.Error()) |
| 1808 } |
| 1809 |
| 1810 // Compare B |
| 1811 if err := compareTwoGoObjects(c.expectedRuntimeTypeInfoB, &runti
meTypeInfoB); err != nil { |
| 1812 t.Errorf("case %d B:\n%s", i, err.Error()) |
| 1813 } |
| 1814 } |
| 1815 } |
| 1816 |
| 1817 // compareTwoGoObjects compares |expected| and |actual| and returns a non-nil |
1176 // error if they are not deeply equal. The error message contains a human-readab
le | 1818 // error if they are not deeply equal. The error message contains a human-readab
le |
1177 // string containing a deep-print of expected and actual along with the substrin
gs | 1819 // string containing a deep-print of expected and actual along with the substrin
gs |
1178 // starting from the first character where they differ. | 1820 // starting from the first character where they differ. |
1179 func compareFileGraphs(expected *mojom_files.MojomFileGraph, actual *mojom_files
.MojomFileGraph) error { | 1821 func compareTwoGoObjects(expected interface{}, actual interface{}) error { |
1180 if !reflect.DeepEqual(expected, actual) { | 1822 if !reflect.DeepEqual(expected, actual) { |
1181 // Note(rudominer) The myfmt package is a local modification of
the fmt package | 1823 // Note(rudominer) The myfmt package is a local modification of
the fmt package |
1182 // that does a deep printing that follows pointers for up to 50
levels. | 1824 // that does a deep printing that follows pointers for up to 50
levels. |
1183 // Thus expectedString and actualString should contain enough in
formation to | 1825 // Thus expectedString and actualString should contain enough in
formation to |
1184 // precisely capture the structure of expected and actual. | 1826 // precisely capture the structure of expected and actual. |
1185 expectedString := myfmt.Sprintf("%#v", expected) | 1827 expectedString := myfmt.Sprintf("%#v", expected) |
1186 actualString := myfmt.Sprintf("%#v", actual) | 1828 actualString := myfmt.Sprintf("%#v", actual) |
1187 if expectedString != actualString { | 1829 if expectedString != actualString { |
1188 diffPos := -1 | 1830 diffPos := -1 |
1189 for i := 0; i < len(expectedString) && i < len(actualStr
ing); i++ { | 1831 for i := 0; i < len(expectedString) && i < len(actualStr
ing); i++ { |
(...skipping 10 matching lines...) Expand all Loading... |
1200 } | 1842 } |
1201 return fmt.Errorf("*****\nexpected=\n*****\n%q\n*****\na
ctual=\n*****\n%q\n*****\n"+ | 1843 return fmt.Errorf("*****\nexpected=\n*****\n%q\n*****\na
ctual=\n*****\n%q\n*****\n"+ |
1202 "match failed at position %d: expected=\n*****\n
%q\n******\nactual=\n*****\n%q\n******\n", | 1844 "match failed at position %d: expected=\n*****\n
%q\n******\nactual=\n*****\n%q\n******\n", |
1203 expectedString, actualString, diffPos, mismatchE
xpected, mismatchActual) | 1845 expectedString, actualString, diffPos, mismatchE
xpected, mismatchActual) |
1204 } else { | 1846 } else { |
1205 return fmt.Errorf("expected != actual but the two printe
d equal.") | 1847 return fmt.Errorf("expected != actual but the two printe
d equal.") |
1206 } | 1848 } |
1207 } | 1849 } |
1208 return nil | 1850 return nil |
1209 } | 1851 } |
OLD | NEW |