Skip to content

Commit

Permalink
issue freeconf#63 - complain on illegal yang. fix tests too
Browse files Browse the repository at this point in the history
  • Loading branch information
dhubler committed Jul 9, 2023
1 parent 239a5cf commit e47df4d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
6 changes: 4 additions & 2 deletions meta/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ func (c *compiler) compile(o interface{}) error {
}

if x, ok := o.(HasConfig); ok {
p := o.(Meta).Parent()
if !x.IsConfigSet() {
x.setConfig(c.inheritConfig(x.(Meta).Parent()))
x.setConfig(c.inheritConfig(p))
} else if x.Config() && !p.(HasConfig).Config() {
return fmt.Errorf("%s - config cannot be true when parent config is false", SchemaPath(o.(Meta)))
}
}

Expand Down Expand Up @@ -163,7 +166,6 @@ func (c *compiler) inheritConfig(m Meta) bool {
if x, ok := m.(HasDetails); ok {
if !x.IsConfigSet() {
x.setConfig(c.inheritConfig(x.(Meta).Parent()))
//panic(fmt.Sprintf("%s (%T)", SchemaPath(m), x))
}
return x.Config()
}
Expand Down
24 changes: 23 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package parser

import (
"fmt"
"strings"
"testing"

"github.com/freeconf/yang/meta"
Expand Down Expand Up @@ -83,6 +84,28 @@ func TestParseErr(t *testing.T) {
}
}

func TestInvalid(t *testing.T) {
tests := []struct {
dir string
fname string
err string
}{
{"/ddef", "config", "config cannot be true when parent config is false"},
}
for _, test := range tests {
ypath := source.Dir("testdata" + test.dir)
_, err := LoadModule(ypath, test.fname)

// we verify contents of error because we want to make sure it is failing for the right reason.
if err == nil {
fc.AssertEqual(t, false, err == nil, "no error", test.err)
} else {
msg := fmt.Sprintf("got error but unexpected content:\nexpected string: '%s'\n full string: '%s'\n", err.Error(), test.err)
fc.AssertEqual(t, true, strings.Contains(err.Error(), test.err), msg)
}
}
}

// list is used in lex_more_test.go as well
var yangTestFiles = []struct {
dir string
Expand All @@ -102,7 +125,6 @@ var yangTestFiles = []struct {
{"/types", "union"},
{"/types", "leafref"},
{"/types", "leafref-i1"},
{"/types", "leaf-list"},
{"/typedef", "x"},
{"/typedef", "import"},
{"/grouping", "x"},
Expand Down
9 changes: 9 additions & 0 deletions parser/testdata/ddef/config.yang
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module config {

container c {
config false;
container x {
config true; // should be illegal
}
}
}

0 comments on commit e47df4d

Please sign in to comment.