Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for parser #164

Merged
merged 8 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/yang/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
9 changes: 1 addition & 8 deletions pkg/yang/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand Down
116 changes: 112 additions & 4 deletions pkg/yang/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;
`,
Expand Down Expand Up @@ -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 }`,
},
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
}
Expand Down