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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojom/mojom_parser/parser/parser_test.go
diff --git a/mojom/mojom_parser/parser/parser_test.go b/mojom/mojom_parser/parser/parser_test.go
index 54cc3da98d71455bfbb0218308402c377da3e9d5..c0fc317772221bfa6fd5b9d567ae4c90a5d459f9 100644
--- a/mojom/mojom_parser/parser/parser_test.go
+++ b/mojom/mojom_parser/parser/parser_test.go
@@ -198,7 +198,7 @@ func TestSuccessfulParsing(t *testing.T) {
import "and.another.file";
struct Foo{
- [happy=true] int32 x@4;
+ [happy=true] int32 x@0;
};`
{
expectedFile.AddImport(mojom.NewImportedFile("another.file", nil))
@@ -208,7 +208,7 @@ func TestSuccessfulParsing(t *testing.T) {
structFoo.InitAsScope(mojom.NewTestFileScope("test.scope"))
attributes := mojom.NewAttributes(lexer.Token{})
attributes.List = append(attributes.List, mojom.NewMojomAttribute("happy", nil, mojom.MakeBoolLiteralValue(true, nil)))
- structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataAWithOrdinal("x", attributes, 4), mojom.SimpleTypeInt32, nil))
+ structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataAWithOrdinal("x", attributes, 0), mojom.SimpleTypeInt32, nil))
expectedFile.AddStruct(structFoo)
}
endTestCase()
@@ -224,10 +224,10 @@ func TestSuccessfulParsing(t *testing.T) {
import "and.another.file";
struct Foo{
- int32 x@4 = 42;
+ int32 x@0 = 42;
[age=7, level="high"] string y = "Howdy!";
string? z;
- bool w@6 = false;
+ bool w@3 = false;
};`
{
expectedFile.AddImport(mojom.NewImportedFile("another.file", nil))
@@ -235,13 +235,13 @@ func TestSuccessfulParsing(t *testing.T) {
structFoo := mojom.NewMojomStruct(mojom.DeclTestData("Foo"))
structFoo.InitAsScope(mojom.NewTestFileScope("test.scope"))
- structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataWithOrdinal("x", 4), mojom.SimpleTypeInt32, mojom.MakeInt8LiteralValue(42, nil)))
+ structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataWithOrdinal("x", 0), mojom.SimpleTypeInt32, mojom.MakeInt8LiteralValue(42, nil)))
attributes := mojom.NewAttributes(lexer.Token{})
attributes.List = append(attributes.List, mojom.NewMojomAttribute("age", nil, mojom.MakeInt8LiteralValue(7, nil)))
attributes.List = append(attributes.List, mojom.NewMojomAttribute("level", nil, mojom.MakeStringLiteralValue("high", nil)))
structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataA("y", attributes), mojom.BuiltInType("string"), mojom.MakeStringLiteralValue("Howdy!", nil)))
structFoo.AddField(mojom.NewStructField(mojom.DeclTestData("z"), mojom.BuiltInType("string?"), nil))
- structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataWithOrdinal("w", 6), mojom.BuiltInType("bool"), mojom.MakeBoolLiteralValue(false, nil)))
+ structFoo.AddField(mojom.NewStructField(mojom.DeclTestDataWithOrdinal("w", 3), mojom.BuiltInType("bool"), mojom.MakeBoolLiteralValue(false, nil)))
expectedFile.AddStruct(structFoo)
}
endTestCase()
@@ -515,6 +515,141 @@ func TestErrorParsing(t *testing.T) {
endTestCase()
////////////////////////////////////////////////////////////
+ // Test Case (Invalid struct field ordinal: empty string
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ struct MyStruct {
+ int32 x@;
+ };
+
+ `
+ expectError("field \"x\": Invalid ordinal string following '@'")
+ expectError("Ordinals must be decimal integers between 0 and 4294967294")
+ endTestCase()
+
+ ////////////////////////////////////////////////////////////
+ // Test Case (Invalid struct field ordinal: Not a number
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ struct MyStruct {
+ int32 x@happy;
+ };
+
+ `
+ expectError("field \"x\": Invalid ordinal string following '@'")
+ expectError("Ordinals must be decimal integers between 0 and 4294967294")
+ endTestCase()
+
+ ////////////////////////////////////////////////////////////
+ // Test Case (Invalid struct field ordinal: Negative
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ struct MyStruct {
+ int32 x@-500;
+ };
+
+ `
+ expectError("field \"x\": Invalid ordinal string following '@'")
+ expectError("Ordinals must be decimal integers between 0 and 4294967294")
+ endTestCase()
+
+ ////////////////////////////////////////////////////////////
+ // Test Case (Invalid struct field ordinal: too big for uint32)
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ struct MyStruct {
+ int32 x@4294967295;
+ };
+
+ `
+ expectError("field \"x\": Invalid ordinal string following '@'")
+ expectError("4294967295")
+ expectError("Ordinals must be decimal integers between 0 and 4294967294")
+ endTestCase()
+
+ ////////////////////////////////////////////////////////////
+ // Test Case (Invalid struct field ordinal: too big for uint64)
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ struct MyStruct {
+ int32 x@999999999999999999999999999999999999999;
+ };
+
+ `
+ expectError("field \"x\": Invalid ordinal string following '@'")
+ expectError("999999999999999999999999999999999999999")
+ expectError("Ordinals must be decimal integers between 0 and 4294967294")
+ endTestCase()
+
+ ////////////////////////////////////////////////////////////
+ // Test Case (Invalid struct field ordinal: too big for size of struct)
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ struct MyStruct {
+ int32 x;
+ int32 y@2;
+ };
+
+ `
+ expectError("Invalid ordinal for field y: 2.")
+ expectError("A struct field ordinal must be a non-negative integer value less than the number of fields in the struct.")
+ endTestCase()
+
+ ////////////////////////////////////////////////////////////
+ // Test Case (Invalid struct field ordinal: implicit next value too big for size of struct)
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ struct MyStruct {
+ int32 x;
+ int32 y@2;
+ int32 z;
+ };
+
+ `
+ expectError("Invalid ordinal for field z: 3.")
+ expectError("A struct field ordinal must be a non-negative integer value less than the number of fields in the struct.")
+ endTestCase()
+
+ ////////////////////////////////////////////////////////////
+ // Test Case (Invalid struct field ordinal: Duplicate explicit)
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ struct MyStruct {
+ int32 x@0;
+ int32 y;
+ int32 z@0;
+ };
+
+ `
+ expectError("Invalid ordinal for field z: 0.")
+ expectError("There is already a field in struct MyStruct with that ordinal: x")
+ endTestCase()
+
+ ////////////////////////////////////////////////////////////
+ // Test Case (Invalid struct field ordinal: Duplicate implicit)
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ struct MyStruct {
+ int32 x;
+ int32 y;
+ int32 z@1;
+ };
+
+ `
+ expectError("Invalid ordinal for field z: 1.")
+ expectError("There is already a field in struct MyStruct with that ordinal: y")
+ endTestCase()
+
+ ////////////////////////////////////////////////////////////
// Test Case (Invalid method ordinal: too big for uint32)
////////////////////////////////////////////////////////////
startTestCase("")
« 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