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