@@ -24,6 +24,7 @@ import (
2424 "sort"
2525 "testing"
2626
27+ "github.com/google/go-cmp/cmp"
2728 "golang.org/x/tools/go/packages"
2829 "k8s.io/gengo/v2/types"
2930)
@@ -893,6 +894,62 @@ func TestStructParse(t *testing.T) {
893894 }
894895 },
895896 },
897+ {
898+ description : "generic on field" ,
899+ testFile : "./testdata/generic-field" ,
900+ expected : func () * types.Type {
901+ fieldType := & types.Type {
902+ Name : types.Name {
903+ Package : "k8s.io/gengo/v2/parser/testdata/generic-field" ,
904+ Name : "Blah[T]" ,
905+ },
906+ Kind : types .Struct ,
907+ CommentLines : []string {"" },
908+ SecondClosestCommentLines : []string {"" },
909+ Members : []types.Member {
910+ {
911+ Name : "V" ,
912+ Embedded : false ,
913+ CommentLines : []string {"V is the first field." },
914+ Tags : `json:"v"` ,
915+ Type : & types.Type {
916+ Kind : types .TypeParam ,
917+ Name : types.Name {
918+ Name : "T" ,
919+ },
920+ },
921+ },
922+ },
923+ TypeParams : map [string ]* types.Type {
924+ "T" : {
925+ Name : types.Name {
926+ Name : "any" ,
927+ },
928+ Kind : types .Interface ,
929+ },
930+ },
931+ }
932+ return & types.Type {
933+ Name : types.Name {
934+ Package : "k8s.io/gengo/v2/parser/testdata/generic-field" ,
935+ Name : "Foo" ,
936+ },
937+ Kind : types .Struct ,
938+ CommentLines : []string {"" },
939+ SecondClosestCommentLines : []string {"" },
940+ Members : []types.Member {
941+ {
942+ Name : "B" ,
943+ Embedded : false ,
944+ CommentLines : []string {"" },
945+ Tags : `json:"b"` ,
946+ Type : fieldType ,
947+ },
948+ },
949+ TypeParams : map [string ]* types.Type {},
950+ }
951+ },
952+ },
896953 {
897954 description : "generic multiple" ,
898955 testFile : "./testdata/generic-multi" ,
@@ -973,12 +1030,20 @@ func TestStructParse(t *testing.T) {
9731030 recursiveT := & types.Type {
9741031 Name : types.Name {
9751032 Package : "k8s.io/gengo/v2/parser/testdata/generic-recursive" ,
976- Name : "DeepCopyable" ,
1033+ Name : "DeepCopyable[T] " ,
9771034 },
9781035 Kind : types .Interface ,
9791036 CommentLines : []string {"" },
9801037 SecondClosestCommentLines : []string {"" },
9811038 Methods : map [string ]* types.Type {},
1039+ TypeParams : map [string ]* types.Type {
1040+ "T" : {
1041+ Name : types.Name {
1042+ Name : "any" ,
1043+ },
1044+ Kind : types .Interface ,
1045+ },
1046+ },
9821047 }
9831048 recursiveT .Methods ["DeepCopy" ] = & types.Type {
9841049 Name : types.Name {
@@ -1054,7 +1119,29 @@ func TestStructParse(t *testing.T) {
10541119 t .Fatalf ("type %s not found" , expected .Name .Name )
10551120 }
10561121 if e , a := expected , st ; ! reflect .DeepEqual (e , a ) {
1057- t .Errorf ("wanted, got:\n %#v\n %#v" , e , a )
1122+ t .Errorf ("wanted, got:\n %#v\n %#v\n %s" , e , a , cmp .Diff (e , a ))
1123+ }
1124+ })
1125+ }
1126+ }
1127+
1128+ func TestGoNameToName (t * testing.T ) {
1129+ testCases := []struct {
1130+ input string
1131+ expect types.Name
1132+ }{
1133+ {input : "foo" , expect : types.Name {Name : "foo" }},
1134+ {input : "foo.bar" , expect : types.Name {Package : "foo" , Name : "bar" }},
1135+ {input : "foo.bar.baz" , expect : types.Name {Package : "foo.bar" , Name : "baz" }},
1136+ {input : "Foo[T]" , expect : types.Name {Package : "" , Name : "Foo[T]" }},
1137+ {input : "Foo[T any]" , expect : types.Name {Package : "" , Name : "Foo[T any]" }},
1138+ {input : "pkg.Foo[T]" , expect : types.Name {Package : "pkg" , Name : "Foo[T]" }},
1139+ {input : "pkg.Foo[T any]" , expect : types.Name {Package : "pkg" , Name : "Foo[T any]" }},
1140+ }
1141+ for _ , tc := range testCases {
1142+ t .Run (tc .input , func (t * testing.T ) {
1143+ if got , want := goNameToName (tc .input ), tc .expect ; ! reflect .DeepEqual (got , want ) {
1144+ t .Errorf ("\n want: %#v\n got: %#v" , want , got )
10581145 }
10591146 })
10601147 }
0 commit comments