Skip to content

Commit

Permalink
Add unit tests for stripPackagePrefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
robshakir committed Oct 14, 2017
1 parent 0ddfb44 commit 8a1590b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ygen/protogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ func writeProto3Msg(msg *yangDirectory, msgs map[string]*yangDirectory, state *g
// code representing it. It uses the set of messages that have been extracted and the
// current generator state to map to other messages and ensure uniqueness of names.
// The configuration parameters for the current code generation required are supplied
// as a protoMsgConfig struct.
// as a protoMsgConfig struct. The parentPkg argument specifies the name of the parent
// package for the protobuf message(s) that are being generated, such that relative
// paths can be used in the messages.
// TODO(robjs): Split the logic of this function into multiple subfunctions.
func genProto3Msg(msg *yangDirectory, msgs map[string]*yangDirectory, state *genState, cfg protoMsgConfig, parentPkg string) ([]protoMsg, []error) {
var errs []error
Expand Down
31 changes: 31 additions & 0 deletions ygen/protogen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1506,3 +1506,34 @@ func TestUnionFieldToOneOf(t *testing.T) {
}
}
}

func TestStripPackagePrefix(t *testing.T) {
tests := []struct {
name string
inPrefix string
inPath string
want string
}{{
name: "invalid prefix at element one",
inPrefix: "one.two",
inPath: "two.four",
want: "two.four",
}, {
name: "single element prefix",
inPrefix: "one",
inPath: "one.three",
want: "three",
}, {
name: "longer prefix",
inPrefix: "one.two.three",
inPath: "one.two.three.five",
want: "five",
}}

for _, tt := range tests {
if got := stripPackagePrefix(tt.inPrefix, tt.inPath); got != tt.want {
t.Errorf("%s: stripPackagePrefix(%s, %s): did not get expected output, got: %s, want: %s", tt.name, tt.inPrefix, tt.inPath, got, tt.want)
}
}

}

0 comments on commit 8a1590b

Please sign in to comment.