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

Unified Diff: mojom/mojom_parser/parser/parser_test.go

Issue 1506023005: New Mojom parser: Fix up error messages for lexing errors. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Adds new sha1. Created 5 years 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/lexer/tokens.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 ad1bc55599a7903cf4e7405e6425aaf87e3d1568..d07f42688b656598dc9bd353ed3ab93898ddc2c0 100644
--- a/mojom/mojom_parser/parser/parser_test.go
+++ b/mojom/mojom_parser/parser/parser_test.go
@@ -794,3 +794,185 @@ func TestInvalidAssignmentDuringParsing(t *testing.T) {
}
}
}
+
+// TestLexerErrors contains a series of test cases in which we
+// run the parser on invalid mojom input string and compare the resulting
+// error message to an expected one. The particular type of error we are testing
+// here are cases in which the lexer detects an error and returns one of
+// its error tokens.
+func TestLexerErrors(t *testing.T) {
+ type testCase struct {
+ fileName string
+ mojomContents string
+ expectedErrors []string
+ }
+ cases := make([]testCase, 0)
+ testCaseNum := 0
+
+ startTestCase := func(moduleNameSpace string) {
+ fileName := fmt.Sprintf("file%d", testCaseNum)
+ cases = append(cases, testCase{fileName: fileName})
+ }
+
+ expectError := func(expectedError string) {
+ cases[testCaseNum].expectedErrors = append(cases[testCaseNum].expectedErrors, expectedError)
+ }
+
+ endTestCase := func() {
+ testCaseNum += 1
+ }
+
+ ////////////////////////////////////////////////////////////
+ // Group 1: Unterminated comment
+ ////////////////////////////////////////////////////////////
+
+ ////////////////////////////////////////////////////////////
+ // Test Case: Unterminated comment at start of file.
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ /*
+ * The woods are lovely
+ * dark and deep
+ * but I have promises to keep.
+ struct Foo {
+ int32 x = "hello";
+ };
+ `
+ expectError("Error")
+ expectError("unterminated comment")
+ endTestCase()
+
+ ////////////////////////////////////////////////////////////
+ // Test Case: Unterminated comment at end of file.
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ struct Foo {
+ int32 x ;
+ };
+
+ /*
+ * The woods are lovely
+ * dark and deep
+ * but I have promises to keep.
+ `
+ expectError("Error")
+ expectError("unterminated comment")
+ endTestCase()
+
+ ////////////////////////////////////////////////////////////
+ // Test Case: Unterminated comment in the middle.
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ struct Foo {
+ int32 x ;
+ /*
+ * The woods are lovely
+ * dark and deep
+ * but I have promises to keep.
+ };`
+ expectError("Error")
+ expectError("unterminated comment")
+ endTestCase()
+
+ ////////////////////////////////////////////////////////////
+ // Group 1: Unterminated string literal
+ ////////////////////////////////////////////////////////////
+
+ /// ////////////////////////////////////////////////////////////
+ // Test Case: Unterminated string literal in import.
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ import "foo.bar
+ struct Foo {
+ int32 x = 42;
+ };
+ `
+ expectError("Error")
+ expectError("unterminated string literal")
+ endTestCase()
+
+ /// ////////////////////////////////////////////////////////////
+ // Test Case: Unterminated string literal in assignment.
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ import "foo.bar";
+ struct Foo {
+ string x = "hello;
+ };
+ `
+ expectError("Error")
+ expectError("unterminated string literal")
+ endTestCase()
+
+ ////////////////////////////////////////////////////////////
+ // Group 3: ErrorIllegalChar
+ ////////////////////////////////////////////////////////////
+
+ /// ////////////////////////////////////////////////////////////
+ // Test Case: ErrorIllegalChar at the beginning of a file.
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ /? What the ?/
+ import "foo.bar"
+ struct Foo {
+ int32 x = 42;
+ };
+ `
+ expectError("Error:")
+ expectError("Unexpected \"/\"")
+ endTestCase()
+
+ /// ////////////////////////////////////////////////////////////
+ // Test Case: ErrorIllegalChar in the middle.
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ import "foo.bar";
+ struct Foo {
+ int32 x = %42;
+ };
+ `
+ expectError("Error:")
+ expectError("Unexpected \"%\"")
+ endTestCase()
+
+ /// ////////////////////////////////////////////////////////////
+ // Test Case: ErrorIllegalChar at the end
+ ////////////////////////////////////////////////////////////
+ startTestCase("")
+ cases[testCaseNum].mojomContents = `
+ import "foo.bar";
+ struct Foo {
+ int32 x = 42;
+ };
+ *
+ `
+ expectError("Error:")
+ expectError("Unexpected \"*\"")
+ endTestCase()
+
+ ////////////////////////////////////////////////////////////
+ // Execute all of the test cases.
+ ////////////////////////////////////////////////////////////
+ for i, c := range cases {
+ descriptor := mojom.NewMojomDescriptor()
+ parser := MakeParser(c.fileName, c.mojomContents, descriptor, nil)
+ parser.Parse()
+ if parser.OK() {
+ t.Errorf("Parsing was supposed to fail but did not for test case %d", i)
+ } else {
+ got := parser.GetError().Error()
+ for _, expected := range c.expectedErrors {
+ if !strings.Contains(got, expected) {
+ t.Errorf("%s:\n*****expected to contain:\n%s\n****actual\n%s", c.fileName, expected, got)
+ }
+ }
+ }
+ }
+}
« no previous file with comments | « mojom/mojom_parser/lexer/tokens.go ('k') | mojom/mojom_parser/parser/parsing.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698