Skip to content

Commit

Permalink
Add coverage for genutil functions. (#371)
Browse files Browse the repository at this point in the history
* (M) genutil/{names,names_test}.go
  - Fix a couple of TODOs for missing unit test coverage.

Co-authored-by: Wen Bo Li <50884368+wenovus@users.noreply.github.com>
  • Loading branch information
robshakir and wenovus authored Apr 13, 2020
1 parent 0729528 commit 57f70c1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
4 changes: 1 addition & 3 deletions genutil/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func definingModule(node yang.Node) yang.Node {

// ParentModuleName returns the name of the module or submodule that defined
// the supplied node.
// TODO(wenbli): add unit test
func ParentModuleName(node yang.Node) string {
return definingModule(node).NName()
}
Expand All @@ -59,14 +58,13 @@ func ParentModuleName(node yang.Node) string {
// supplied as the node argument. If the discovered root node of the node is found
// to be a submodule, the name of the parent module is returned. If the root has
// a camel case extension, this is returned rather than the actual module name.
// TODO(wenbli): add unit test
func ParentModulePrettyName(node yang.Node) string {
definingMod := definingModule(node)
if name, ok := CamelCaseNameExt(definingMod.Exts()); ok {
return name
}

return definingMod.NName()
return yang.CamelCase(definingMod.NName())
}

// MakeNameUnique makes the name specified as an argument unique based on the names
Expand Down
86 changes: 86 additions & 0 deletions genutil/names_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package genutil
import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/openconfig/goyang/pkg/yang"
)

Expand Down Expand Up @@ -100,3 +102,87 @@ func TestCamelCase(t *testing.T) {
}
}
}

func TestDefiningModule(t *testing.T) {
tests := []struct {
name string
inNode yang.Node
wantNode yang.Node
wantName string
wantPrettyName string
}{{
name: "direct child of module",
inNode: &yang.Container{
Name: "child",
Parent: &yang.Module{
Name: "parent",
},
},
wantNode: &yang.Module{
Name: "parent",
},
wantName: "parent",
wantPrettyName: "Parent",
}, {
name: "submodule",
inNode: &yang.Container{
Name: "child",
Parent: &yang.Module{
Name: "parent",
BelongsTo: &yang.BelongsTo{
Name: "parent-module",
},
},
},
wantNode: &yang.BelongsTo{
Name: "parent-module",
},
wantName: "parent-module",
wantPrettyName: "ParentModule",
}, {
name: "module with extension",
inNode: &yang.Leaf{
Name: "leaf",
Parent: &yang.Container{
Name: "container",
Parent: &yang.Module{
Name: "root",
Extensions: []*yang.Statement{{
Keyword: "some-module:camelcase-name",
HasArgument: true,
Argument: "FooBar",
}},
},
},
},
wantNode: &yang.Module{
Name: "root",
Extensions: []*yang.Statement{{
Keyword: "some-module:camelcase-name",
HasArgument: true,
Argument: "FooBar",
}},
},
wantName: "root",
wantPrettyName: "FooBar",
}}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if diff := cmp.Diff(
definingModule(tt.inNode),
tt.wantNode,
cmpopts.IgnoreUnexported(yang.Module{}),
cmpopts.IgnoreUnexported(yang.Statement{}),
); diff != "" {
t.Errorf("did not get expected node, diff(-got,+want): %s", diff)
}
if got := ParentModuleName(tt.inNode); !cmp.Equal(got, tt.wantName) {
t.Errorf("did not get expected parent name, got: %s, want: %s", got, tt.wantName)
}
if got := ParentModulePrettyName(tt.inNode); !cmp.Equal(got, tt.wantPrettyName) {
t.Errorf("did not get expected parent pretty name, got: %s, want: %s", got, tt.wantPrettyName)
}
})
}
}

0 comments on commit 57f70c1

Please sign in to comment.