Repository: openconfig/goyang
Pull request #164 (2021-02-10 20:29:43)

Add tests for parser
`Parse` is now fully tested.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/37035451/badge)](https://coveralls.io/builds/37035451)

Coverage increased (+0.4%) to 77.355% when pulling **41ad3403616c198c287f3b643f0626226c63aba4 on add-parser-tests** into **3e62119f9ff4ae8db06280ac725d7f4df5be5d09 on cleanup-parser-lexer**.


Patch:
diff --git a/pkg/yang/parse.go b/pkg/yang/parse.go
index 7634f58..60c388b 100644
--- a/pkg/yang/parse.go
+++ b/pkg/yang/parse.go
@@ -75,13 +75,6 @@ func (s *Statement) Arg() (string, bool) { return s.Argument, s.HasArgument }
 // SubStatements returns a slice of Statements found in s.
 func (s *Statement) SubStatements() []*Statement { return s.statements }
 
-// String returns s's tree as a string.
-func (s *Statement) String() string {
-	var b bytes.Buffer
-	s.Write(&b, "")
-	return b.String()
-}
-
 // Location returns the location in the source where s was defined.
 func (s *Statement) Location() string {
 	switch {
@@ -319,7 +312,7 @@ func (p *parser) nextStatement() *Statement {
 			}
 		}
 	default:
-		fmt.Fprintf(p.errout, "%v: syntax error, expected ';' or nested statements in {}\n", t)
+		fmt.Fprintf(p.errout, "%v: syntax error, expected ';' or '{'\n", t)
 		return ignoreMe
 	}
 }


Test patch:
diff --git a/pkg/yang/entry_test.go b/pkg/yang/entry_test.go
index bb5d847..1d23be5 100644
--- a/pkg/yang/entry_test.go
+++ b/pkg/yang/entry_test.go
@@ -2696,7 +2696,7 @@ func TestFixChoice(t *testing.T) {
 			}
 
 			if insertedNode.Statement() != originalNode.Statement() {
-				t.Errorf("Got inserted node's statement %v, expected %s",
+				t.Errorf("Got inserted node's statement %v, expected %v",
 					insertedNode.Statement(), originalNode.Statement())
 			}
 
diff --git a/pkg/yang/parse_test.go b/pkg/yang/parse_test.go
index f56732a..3326996 100644
--- a/pkg/yang/parse_test.go
+++ b/pkg/yang/parse_test.go
@@ -124,6 +124,114 @@ pattern '\\ \S \n';
 			},
 		},
 		{line: line(), in: `
+foo "bar" + "baz";
+`,
+			out: []*Statement{
+				SA("foo", "barbaz"),
+			},
+		},
+		{line: line(), in: `
+foo "bar" + "+" + "baz";
+`,
+			out: []*Statement{
+				SA("foo", "bar+baz"),
+			},
+		},
+		{line: line(), in: `
+foo "bar"
+`,
+			err: `test.yang: unexpected EOF`,
+		},
+		{line: line(), in: `
+foo "bar" + "baz"
+`,
+			err: `test.yang: unexpected EOF`,
+		},
+		{line: line(), in: `
+foo "bar" baz;
+`,
+			err: `test.yang:2:11: baz: syntax error, expected ';' or '{'
+test.yang:2:14: ;: keyword token not an unquoted string`,
+		},
+		{line: line(), in: `
+foo "bar" + baz;
+`,
+			err: `test.yang:2:11: +: syntax error, expected ';' or '{'`,
+		},
+		{line: line(), in: `
+foo "bar" +
+`,
+			err: `test.yang:2:11: +: syntax error, expected ';' or '{'`,
+		},
+		{line: line(), in: `
+foo "bar";
+`,
+			out: []*Statement{
+				SA("foo", "bar"),
+			},
+		},
+		{line: line(), in: `
+foo "bar" {}
+`,
+			out: []*Statement{
+				SA("foo", "bar"),
+			},
+		},
+		{line: line(), in: `
+foo 'bar' + 'baz';
+`,
+			out: []*Statement{
+				SA("foo", "barbaz"),
+			},
+		},
+		{line: line(), in: `
+foo 'bar' + '+' + 'baz';
+`,
+			out: []*Statement{
+				SA("foo", "bar+baz"),
+			},
+		},
+		{line: line(), in: `
+foo 'bar'
+`,
+			err: `test.yang: unexpected EOF`,
+		},
+		{line: line(), in: `
+foo 'bar' + 'baz'
+`,
+			err: `test.yang: unexpected EOF`,
+		},
+		{line: line(), in: `
+foo 'bar' baz;
+`,
+			err: `test.yang:2:11: baz: syntax error, expected ';' or '{'
+test.yang:2:14: ;: keyword token not an unquoted string`,
+		},
+		{line: line(), in: `
+foo 'bar' + baz;
+`,
+			err: `test.yang:2:11: +: syntax error, expected ';' or '{'`,
+		},
+		{line: line(), in: `
+foo 'bar' +
+`,
+			err: `test.yang:2:11: +: syntax error, expected ';' or '{'`,
+		},
+		{line: line(), in: `
+foo 'bar';
+`,
+			out: []*Statement{
+				SA("foo", "bar"),
+			},
+		},
+		{line: line(), in: `
+foo 'bar' {}
+`,
+			out: []*Statement{
+				SA("foo", "bar"),
+			},
+		},
+		{line: line(), in: `
 foo bar;
 red black;
 `,
@@ -260,7 +368,7 @@ id
 		{line: line(), in: `
 statement one two { }
 `,
-			err: `test.yang:2:15: two: syntax error, expected ';' or nested statements in {}
+			err: `test.yang:2:15: two: syntax error, expected ';' or '{'
 test.yang:2:19: {: keyword token not an unquoted string
 test.yang:2:21: unexpected }`,
 		},
@@ -313,9 +421,9 @@ module base {
 		s, err := Parse(tt.in, "test.yang")
 		if (s == nil) != (tt.out == nil) {
 			if s == nil {
-				t.Errorf("%d: did not get expected statements: %s", tt.line, tt.out)
+				t.Errorf("%d: did not get expected statements: %v", tt.line, tt.out)
 			} else {
-				t.Errorf("%d: get unexpected statements: %s", tt.line, s)
+				t.Errorf("%d: get unexpected statements: %v", tt.line, s)
 			}
 		}
 		switch {
@@ -335,7 +443,7 @@ module base {
 		s1 := &Statement{statements: s}
 		s2 := &Statement{statements: tt.out}
 		if !s1.equal(s2) {
-			t.Errorf("%d: got:\n%s\nwant:\n%s", tt.line, s1, s2)
+			t.Errorf("%d: got:\n%v\nwant:\n%v", tt.line, s1, s2)
 		}
 	}
 }