Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: mojom/mojom_parser/parser/parser_test.go

Issue 1767033002: Mojom parser: Compute and validate struct field ordinals. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Maintain the property that the fields of a module.Struct are sorted in declaration order. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojom/mojom_parser/parser/comment_merger_test.go ('k') | mojom/mojom_parser/parser/parsing.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 parser 5 package parser
6 6
7 import ( 7 import (
8 "fmt" 8 "fmt"
9 "mojom/mojom_parser/lexer" 9 "mojom/mojom_parser/lexer"
10 "mojom/mojom_parser/mojom" 10 "mojom/mojom_parser/mojom"
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 // Test Case 191 // Test Case
192 //////////////////////////////////////////////////////////// 192 ////////////////////////////////////////////////////////////
193 startTestCase("mojom.test") 193 startTestCase("mojom.test")
194 cases[testCaseNum].mojomContents = ` 194 cases[testCaseNum].mojomContents = `
195 module mojom.test; 195 module mojom.test;
196 196
197 import "another.file"; 197 import "another.file";
198 import "and.another.file"; 198 import "and.another.file";
199 199
200 struct Foo{ 200 struct Foo{
201 » » [happy=true] int32 x@4; 201 » » [happy=true] int32 x@0;
202 };` 202 };`
203 { 203 {
204 expectedFile.AddImport(mojom.NewImportedFile("another.file", nil )) 204 expectedFile.AddImport(mojom.NewImportedFile("another.file", nil ))
205 expectedFile.AddImport(mojom.NewImportedFile("and.another.file", nil)) 205 expectedFile.AddImport(mojom.NewImportedFile("and.another.file", nil))
206 206
207 structFoo := mojom.NewMojomStruct(mojom.DeclTestData("Foo")) 207 structFoo := mojom.NewMojomStruct(mojom.DeclTestData("Foo"))
208 structFoo.InitAsScope(mojom.NewTestFileScope("test.scope")) 208 structFoo.InitAsScope(mojom.NewTestFileScope("test.scope"))
209 attributes := mojom.NewAttributes(lexer.Token{}) 209 attributes := mojom.NewAttributes(lexer.Token{})
210 attributes.List = append(attributes.List, mojom.NewMojomAttribut e("happy", nil, mojom.MakeBoolLiteralValue(true, nil))) 210 attributes.List = append(attributes.List, mojom.NewMojomAttribut e("happy", nil, mojom.MakeBoolLiteralValue(true, nil)))
211 » » structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataAWithO rdinal("x", attributes, 4), mojom.SimpleTypeInt32, nil)) 211 » » structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataAWithO rdinal("x", attributes, 0), mojom.SimpleTypeInt32, nil))
212 expectedFile.AddStruct(structFoo) 212 expectedFile.AddStruct(structFoo)
213 } 213 }
214 endTestCase() 214 endTestCase()
215 215
216 //////////////////////////////////////////////////////////// 216 ////////////////////////////////////////////////////////////
217 // Test Case 217 // Test Case
218 //////////////////////////////////////////////////////////// 218 ////////////////////////////////////////////////////////////
219 startTestCase("mojom.test") 219 startTestCase("mojom.test")
220 cases[testCaseNum].mojomContents = ` 220 cases[testCaseNum].mojomContents = `
221 module mojom.test; 221 module mojom.test;
222 222
223 import "another.file"; 223 import "another.file";
224 import "and.another.file"; 224 import "and.another.file";
225 225
226 struct Foo{ 226 struct Foo{
227 » » int32 x@4 = 42; 227 » » int32 x@0 = 42;
228 [age=7, level="high"] string y = "Howdy!"; 228 [age=7, level="high"] string y = "Howdy!";
229 string? z; 229 string? z;
230 » » bool w@6 = false; 230 » » bool w@3 = false;
231 };` 231 };`
232 { 232 {
233 expectedFile.AddImport(mojom.NewImportedFile("another.file", nil )) 233 expectedFile.AddImport(mojom.NewImportedFile("another.file", nil ))
234 expectedFile.AddImport(mojom.NewImportedFile("and.another.file", nil)) 234 expectedFile.AddImport(mojom.NewImportedFile("and.another.file", nil))
235 235
236 structFoo := mojom.NewMojomStruct(mojom.DeclTestData("Foo")) 236 structFoo := mojom.NewMojomStruct(mojom.DeclTestData("Foo"))
237 structFoo.InitAsScope(mojom.NewTestFileScope("test.scope")) 237 structFoo.InitAsScope(mojom.NewTestFileScope("test.scope"))
238 » » structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataWithOr dinal("x", 4), mojom.SimpleTypeInt32, mojom.MakeInt8LiteralValue(42, nil))) 238 » » structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataWithOr dinal("x", 0), mojom.SimpleTypeInt32, mojom.MakeInt8LiteralValue(42, nil)))
239 attributes := mojom.NewAttributes(lexer.Token{}) 239 attributes := mojom.NewAttributes(lexer.Token{})
240 attributes.List = append(attributes.List, mojom.NewMojomAttribut e("age", nil, mojom.MakeInt8LiteralValue(7, nil))) 240 attributes.List = append(attributes.List, mojom.NewMojomAttribut e("age", nil, mojom.MakeInt8LiteralValue(7, nil)))
241 attributes.List = append(attributes.List, mojom.NewMojomAttribut e("level", nil, mojom.MakeStringLiteralValue("high", nil))) 241 attributes.List = append(attributes.List, mojom.NewMojomAttribut e("level", nil, mojom.MakeStringLiteralValue("high", nil)))
242 structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataA("y", attributes), mojom.BuiltInType("string"), mojom.MakeStringLiteralValue("Howdy!" , nil))) 242 structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataA("y", attributes), mojom.BuiltInType("string"), mojom.MakeStringLiteralValue("Howdy!" , nil)))
243 structFoo.AddField(mojom.NewStructField(mojom.DeclTestData("z"), mojom.BuiltInType("string?"), nil)) 243 structFoo.AddField(mojom.NewStructField(mojom.DeclTestData("z"), mojom.BuiltInType("string?"), nil))
244 » » structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataWithOr dinal("w", 6), mojom.BuiltInType("bool"), mojom.MakeBoolLiteralValue(false, nil) )) 244 » » structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataWithOr dinal("w", 3), mojom.BuiltInType("bool"), mojom.MakeBoolLiteralValue(false, nil) ))
245 expectedFile.AddStruct(structFoo) 245 expectedFile.AddStruct(structFoo)
246 } 246 }
247 endTestCase() 247 endTestCase()
248 248
249 //////////////////////////////////////////////////////////// 249 ////////////////////////////////////////////////////////////
250 // Test Case 250 // Test Case
251 //////////////////////////////////////////////////////////// 251 ////////////////////////////////////////////////////////////
252 startTestCase("mojom.test") 252 startTestCase("mojom.test")
253 cases[testCaseNum].mojomContents = ` 253 cases[testCaseNum].mojomContents = `
254 module mojom.test; 254 module mojom.test;
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 startTestCase("") 508 startTestCase("")
509 cases[testCaseNum].mojomContents = ` 509 cases[testCaseNum].mojomContents = `
510 import "another.file"; 510 import "another.file";
511 511
512 module mojom.test; 512 module mojom.test;
513 ` 513 `
514 expectError("The module declaration must come before the import statemen ts.") 514 expectError("The module declaration must come before the import statemen ts.")
515 endTestCase() 515 endTestCase()
516 516
517 //////////////////////////////////////////////////////////// 517 ////////////////////////////////////////////////////////////
518 // Test Case (Invalid struct field ordinal: empty string
519 ////////////////////////////////////////////////////////////
520 startTestCase("")
521 cases[testCaseNum].mojomContents = `
522 struct MyStruct {
523 int32 x@;
524 };
525
526 `
527 expectError("field \"x\": Invalid ordinal string following '@'")
528 expectError("Ordinals must be decimal integers between 0 and 4294967294" )
529 endTestCase()
530
531 ////////////////////////////////////////////////////////////
532 // Test Case (Invalid struct field ordinal: Not a number
533 ////////////////////////////////////////////////////////////
534 startTestCase("")
535 cases[testCaseNum].mojomContents = `
536 struct MyStruct {
537 int32 x@happy;
538 };
539
540 `
541 expectError("field \"x\": Invalid ordinal string following '@'")
542 expectError("Ordinals must be decimal integers between 0 and 4294967294" )
543 endTestCase()
544
545 ////////////////////////////////////////////////////////////
546 // Test Case (Invalid struct field ordinal: Negative
547 ////////////////////////////////////////////////////////////
548 startTestCase("")
549 cases[testCaseNum].mojomContents = `
550 struct MyStruct {
551 int32 x@-500;
552 };
553
554 `
555 expectError("field \"x\": Invalid ordinal string following '@'")
556 expectError("Ordinals must be decimal integers between 0 and 4294967294" )
557 endTestCase()
558
559 ////////////////////////////////////////////////////////////
560 // Test Case (Invalid struct field ordinal: too big for uint32)
561 ////////////////////////////////////////////////////////////
562 startTestCase("")
563 cases[testCaseNum].mojomContents = `
564 struct MyStruct {
565 int32 x@4294967295;
566 };
567
568 `
569 expectError("field \"x\": Invalid ordinal string following '@'")
570 expectError("4294967295")
571 expectError("Ordinals must be decimal integers between 0 and 4294967294" )
572 endTestCase()
573
574 ////////////////////////////////////////////////////////////
575 // Test Case (Invalid struct field ordinal: too big for uint64)
576 ////////////////////////////////////////////////////////////
577 startTestCase("")
578 cases[testCaseNum].mojomContents = `
579 struct MyStruct {
580 int32 x@999999999999999999999999999999999999999;
581 };
582
583 `
584 expectError("field \"x\": Invalid ordinal string following '@'")
585 expectError("999999999999999999999999999999999999999")
586 expectError("Ordinals must be decimal integers between 0 and 4294967294" )
587 endTestCase()
588
589 ////////////////////////////////////////////////////////////
590 // Test Case (Invalid struct field ordinal: too big for size of struct)
591 ////////////////////////////////////////////////////////////
592 startTestCase("")
593 cases[testCaseNum].mojomContents = `
594 struct MyStruct {
595 int32 x;
596 int32 y@2;
597 };
598
599 `
600 expectError("Invalid ordinal for field y: 2.")
601 expectError("A struct field ordinal must be a non-negative integer value less than the number of fields in the struct.")
602 endTestCase()
603
604 ////////////////////////////////////////////////////////////
605 // Test Case (Invalid struct field ordinal: implicit next value too big for size of struct)
606 ////////////////////////////////////////////////////////////
607 startTestCase("")
608 cases[testCaseNum].mojomContents = `
609 struct MyStruct {
610 int32 x;
611 int32 y@2;
612 int32 z;
613 };
614
615 `
616 expectError("Invalid ordinal for field z: 3.")
617 expectError("A struct field ordinal must be a non-negative integer value less than the number of fields in the struct.")
618 endTestCase()
619
620 ////////////////////////////////////////////////////////////
621 // Test Case (Invalid struct field ordinal: Duplicate explicit)
622 ////////////////////////////////////////////////////////////
623 startTestCase("")
624 cases[testCaseNum].mojomContents = `
625 struct MyStruct {
626 int32 x@0;
627 int32 y;
628 int32 z@0;
629 };
630
631 `
632 expectError("Invalid ordinal for field z: 0.")
633 expectError("There is already a field in struct MyStruct with that ordin al: x")
634 endTestCase()
635
636 ////////////////////////////////////////////////////////////
637 // Test Case (Invalid struct field ordinal: Duplicate implicit)
638 ////////////////////////////////////////////////////////////
639 startTestCase("")
640 cases[testCaseNum].mojomContents = `
641 struct MyStruct {
642 int32 x;
643 int32 y;
644 int32 z@1;
645 };
646
647 `
648 expectError("Invalid ordinal for field z: 1.")
649 expectError("There is already a field in struct MyStruct with that ordin al: y")
650 endTestCase()
651
652 ////////////////////////////////////////////////////////////
518 // Test Case (Invalid method ordinal: too big for uint32) 653 // Test Case (Invalid method ordinal: too big for uint32)
519 //////////////////////////////////////////////////////////// 654 ////////////////////////////////////////////////////////////
520 startTestCase("") 655 startTestCase("")
521 cases[testCaseNum].mojomContents = ` 656 cases[testCaseNum].mojomContents = `
522 interface MyInterface { 657 interface MyInterface {
523 MethodA@4294967295(); 658 MethodA@4294967295();
524 }; 659 };
525 660
526 ` 661 `
527 expectError("MethodA") 662 expectError("MethodA")
(...skipping 1053 matching lines...) Expand 10 before | Expand all | Expand 10 after
1581 checkEq("field1", mojomUnion.DeclaredObjects[0].(*mojom.UnionField).Simp leName()) 1716 checkEq("field1", mojomUnion.DeclaredObjects[0].(*mojom.UnionField).Simp leName())
1582 checkEq("field2", mojomUnion.DeclaredObjects[1].(*mojom.UnionField).Simp leName()) 1717 checkEq("field2", mojomUnion.DeclaredObjects[1].(*mojom.UnionField).Simp leName())
1583 1718
1584 mojomEnum := mojomFile.DeclaredObjects[3].(*mojom.MojomEnum) 1719 mojomEnum := mojomFile.DeclaredObjects[3].(*mojom.MojomEnum)
1585 checkEq("EnumFoo", mojomEnum.SimpleName()) 1720 checkEq("EnumFoo", mojomEnum.SimpleName())
1586 checkEq("VALUE1", mojomEnum.DeclaredObjects[0].(*mojom.EnumValue).Simple Name()) 1721 checkEq("VALUE1", mojomEnum.DeclaredObjects[0].(*mojom.EnumValue).Simple Name())
1587 checkEq("VALUE2", mojomEnum.DeclaredObjects[1].(*mojom.EnumValue).Simple Name()) 1722 checkEq("VALUE2", mojomEnum.DeclaredObjects[1].(*mojom.EnumValue).Simple Name())
1588 1723
1589 checkEq("const_foo", mojomFile.DeclaredObjects[4].(*mojom.UserDefinedCon stant).SimpleName()) 1724 checkEq("const_foo", mojomFile.DeclaredObjects[4].(*mojom.UserDefinedCon stant).SimpleName())
1590 } 1725 }
OLDNEW
« no previous file with comments | « mojom/mojom_parser/parser/comment_merger_test.go ('k') | mojom/mojom_parser/parser/parsing.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698