@@ -823,12 +823,14 @@ func TestAddOnePkgToUniverse(t *testing.T) {
823823func TestStructParse (t * testing.T ) {
824824 testCases := []struct {
825825 description string
826- testFile string
826+ testFiles [] string
827827 expected func () * types.Type
828828 }{
829829 {
830830 description : "basic comments" ,
831- testFile : "k8s.io/gengo/v2/parser/testdata/basic" ,
831+ testFiles : []string {
832+ "k8s.io/gengo/v2/parser/testdata/basic" ,
833+ },
832834 expected : func () * types.Type {
833835 return & types.Type {
834836 Name : types.Name {
@@ -860,7 +862,9 @@ func TestStructParse(t *testing.T) {
860862 },
861863 {
862864 description : "generic" ,
863- testFile : "./testdata/generic" ,
865+ testFiles : []string {
866+ "./testdata/generic" ,
867+ },
864868 expected : func () * types.Type {
865869 return & types.Type {
866870 Name : types.Name {
@@ -897,7 +901,9 @@ func TestStructParse(t *testing.T) {
897901 },
898902 {
899903 description : "generic on field" ,
900- testFile : "./testdata/generic-field" ,
904+ testFiles : []string {
905+ "./testdata/generic-field" ,
906+ },
901907 expected : func () * types.Type {
902908 fieldType := & types.Type {
903909 Name : types.Name {
@@ -953,7 +959,9 @@ func TestStructParse(t *testing.T) {
953959 },
954960 {
955961 description : "generic multiple" ,
956- testFile : "./testdata/generic-multi" ,
962+ testFiles : []string {
963+ "./testdata/generic-multi" ,
964+ },
957965 expected : func () * types.Type {
958966 return & types.Type {
959967 Name : types.Name {
@@ -1026,7 +1034,9 @@ func TestStructParse(t *testing.T) {
10261034 },
10271035 {
10281036 description : "generic recursive" ,
1029- testFile : "./testdata/generic-recursive" ,
1037+ testFiles : []string {
1038+ "./testdata/generic-recursive" ,
1039+ },
10301040 expected : func () * types.Type {
10311041 recursiveT := & types.Type {
10321042 Name : types.Name {
@@ -1095,18 +1105,53 @@ func TestStructParse(t *testing.T) {
10951105 }
10961106 },
10971107 },
1108+ {
1109+ description : "comments on aliased type should not overwrite original type's comments" ,
1110+ testFiles : []string {
1111+ "k8s.io/gengo/v2/parser/testdata/type-alias/v1" ,
1112+ "k8s.io/gengo/v2/parser/testdata/type-alias/v2" ,
1113+ },
1114+ expected : func () * types.Type {
1115+ return & types.Type {
1116+ Name : types.Name {
1117+ Package : "k8s.io/gengo/v2/parser/testdata/type-alias/v1" ,
1118+ Name : "Blah" ,
1119+ },
1120+ Kind : types .Struct ,
1121+ CommentLines : []string {"Blah is a test." , "A test, I tell you." },
1122+ SecondClosestCommentLines : []string {"" },
1123+ Members : []types.Member {
1124+ {
1125+ Name : "A" ,
1126+ Embedded : false ,
1127+ CommentLines : []string {"A is the first field." },
1128+ Tags : `json:"a"` ,
1129+ Type : types .Int64 ,
1130+ },
1131+ {
1132+ Name : "B" ,
1133+ Embedded : false ,
1134+ CommentLines : []string {"B is the second field." , "Multiline comments work." },
1135+ Tags : `json:"b"` ,
1136+ Type : types .String ,
1137+ },
1138+ },
1139+ TypeParams : map [string ]* types.Type {},
1140+ }
1141+ },
1142+ },
10981143 }
10991144
11001145 for _ , tc := range testCases {
11011146 t .Run (tc .description , func (t * testing.T ) {
11021147 parser := New ()
11031148
1104- pkgs , err := parser .loadPackages (tc .testFile )
1149+ _ , err := parser .loadPackages (tc .testFiles ... )
11051150 if err != nil {
11061151 t .Errorf ("unexpected error: %v" , err )
11071152 }
1108- u := types. Universe {}
1109- if err := parser . addPkgToUniverse ( pkgs [ 0 ], & u ); err != nil {
1153+ u , err := parser . NewUniverse ()
1154+ if err != nil {
11101155 t .Errorf ("unexpected error: %v" , err )
11111156 }
11121157
@@ -1153,3 +1198,37 @@ func TestGoNameToName(t *testing.T) {
11531198 })
11541199 }
11551200}
1201+
1202+ func TestCommentsWithAliasedType (t * testing.T ) {
1203+ for i := 0 ; i < 50 ; i ++ {
1204+ parser := NewWithOptions (Options {BuildTags : []string {"ignore_autogenerated" }})
1205+ if _ , err := parser .loadPackages ("./testdata/type-alias/..." ); err != nil {
1206+ t .Fatalf ("unexpected error: %v" , err )
1207+ }
1208+
1209+ u , err := parser .NewUniverse ()
1210+ if err != nil {
1211+ t .Fatalf ("unexpected error: %v" , err )
1212+ }
1213+
1214+ for name , pkg := range u {
1215+ if name != "k8s.io/gengo/v2/parser/testdata/type-alias/v1" {
1216+ continue
1217+ }
1218+ for _ , typ := range pkg .Types {
1219+ if typ .Name .Name != "Blah" {
1220+ continue
1221+ }
1222+ if len (typ .CommentLines ) != 2 {
1223+ t .Fatalf ("expected 2 comments, got %d" , len (typ .CommentLines ))
1224+ }
1225+ if typ .CommentLines [0 ] != "Blah is a test." {
1226+ t .Errorf ("expected comment %q, got %q" , "Blah is a test." , typ .CommentLines [0 ])
1227+ }
1228+ if typ .CommentLines [1 ] != "A test, I tell you." {
1229+ t .Errorf ("expected comment %q, got %q" , "A test, I tell you." , typ .CommentLines [1 ])
1230+ }
1231+ }
1232+ }
1233+ }
1234+ }
0 commit comments