@@ -23,6 +23,7 @@ import (
2323 "github.com/openconfig/goyang/pkg/yang"
2424 "google.golang.org/protobuf/encoding/prototext"
2525 "google.golang.org/protobuf/proto"
26+ "google.golang.org/protobuf/testing/protocmp"
2627
2728 gpb "github.com/openconfig/gnmi/proto/gnmi"
2829)
@@ -766,3 +767,61 @@ func TestFindModelData(t *testing.T) {
766767 }
767768 }
768769}
770+
771+ func TestJoinPaths (t * testing.T ) {
772+ tests := []struct {
773+ desc string
774+ prefix , suffix , want * gpb.Path
775+ wantErrSubstring string
776+ }{{
777+ desc : "all empty" ,
778+ prefix : & gpb.Path {},
779+ suffix : & gpb.Path {},
780+ want : & gpb.Path {},
781+ }, {
782+ desc : "prefix only" ,
783+ prefix : & gpb.Path {Origin : "o" , Target : "t" , Elem : []* gpb.PathElem {{Name : "p" }}},
784+ suffix : & gpb.Path {},
785+ want : & gpb.Path {Origin : "o" , Target : "t" , Elem : []* gpb.PathElem {{Name : "p" }}},
786+ }, {
787+ desc : "suffix only" ,
788+ prefix : & gpb.Path {},
789+ suffix : & gpb.Path {Origin : "o" , Target : "t" , Elem : []* gpb.PathElem {{Name : "s" }}},
790+ want : & gpb.Path {Origin : "o" , Target : "t" , Elem : []* gpb.PathElem {{Name : "s" }}},
791+ }, {
792+ desc : "elements joined" ,
793+ prefix : & gpb.Path {Elem : []* gpb.PathElem {{Name : "p" }}},
794+ suffix : & gpb.Path {Elem : []* gpb.PathElem {{Name : "s" }}},
795+ want : & gpb.Path {Elem : []* gpb.PathElem {{Name : "p" }, {Name : "s" }}},
796+ }, {
797+ desc : "same origin and target" ,
798+ prefix : & gpb.Path {Origin : "o" , Target : "t" },
799+ suffix : & gpb.Path {Origin : "o" , Target : "t" },
800+ want : & gpb.Path {Origin : "o" , Target : "t" },
801+ }, {
802+ desc : "mismatch origins" ,
803+ prefix : & gpb.Path {Origin : "o1" },
804+ suffix : & gpb.Path {Origin : "o2" },
805+ wantErrSubstring : "different origins" ,
806+ }, {
807+ desc : "mismatch targets" ,
808+ prefix : & gpb.Path {Target : "t1" },
809+ suffix : & gpb.Path {Target : "t2" },
810+ wantErrSubstring : "different targets" ,
811+ }}
812+
813+ for _ , tt := range tests {
814+ t .Run (tt .desc , func (t * testing.T ) {
815+ got , err := JoinPaths (tt .prefix , tt .suffix )
816+ if diff := errdiff .Substring (err , tt .wantErrSubstring ); diff != "" {
817+ t .Errorf ("JoinPaths(%v, %v) got unexpected error diff: %s" , tt .prefix , tt .suffix , diff )
818+ }
819+ if err != nil {
820+ return
821+ }
822+ if diff := cmp .Diff (tt .want , got , protocmp .Transform ()); diff != "" {
823+ t .Errorf ("JoinPaths(%v, %v) got unexpected result diff(-want, +got): %s" , tt .prefix , tt .suffix , diff )
824+ }
825+ })
826+ }
827+ }
0 commit comments