@@ -823,12 +823,14 @@ func TestAddOnePkgToUniverse(t *testing.T) {
823
823
func TestStructParse (t * testing.T ) {
824
824
testCases := []struct {
825
825
description string
826
- testFile string
826
+ testFiles [] string
827
827
expected func () * types.Type
828
828
}{
829
829
{
830
830
description : "basic comments" ,
831
- testFile : "k8s.io/gengo/v2/parser/testdata/basic" ,
831
+ testFiles : []string {
832
+ "k8s.io/gengo/v2/parser/testdata/basic" ,
833
+ },
832
834
expected : func () * types.Type {
833
835
return & types.Type {
834
836
Name : types.Name {
@@ -860,7 +862,9 @@ func TestStructParse(t *testing.T) {
860
862
},
861
863
{
862
864
description : "generic" ,
863
- testFile : "./testdata/generic" ,
865
+ testFiles : []string {
866
+ "./testdata/generic" ,
867
+ },
864
868
expected : func () * types.Type {
865
869
return & types.Type {
866
870
Name : types.Name {
@@ -897,7 +901,9 @@ func TestStructParse(t *testing.T) {
897
901
},
898
902
{
899
903
description : "generic on field" ,
900
- testFile : "./testdata/generic-field" ,
904
+ testFiles : []string {
905
+ "./testdata/generic-field" ,
906
+ },
901
907
expected : func () * types.Type {
902
908
fieldType := & types.Type {
903
909
Name : types.Name {
@@ -953,7 +959,9 @@ func TestStructParse(t *testing.T) {
953
959
},
954
960
{
955
961
description : "generic multiple" ,
956
- testFile : "./testdata/generic-multi" ,
962
+ testFiles : []string {
963
+ "./testdata/generic-multi" ,
964
+ },
957
965
expected : func () * types.Type {
958
966
return & types.Type {
959
967
Name : types.Name {
@@ -1026,7 +1034,9 @@ func TestStructParse(t *testing.T) {
1026
1034
},
1027
1035
{
1028
1036
description : "generic recursive" ,
1029
- testFile : "./testdata/generic-recursive" ,
1037
+ testFiles : []string {
1038
+ "./testdata/generic-recursive" ,
1039
+ },
1030
1040
expected : func () * types.Type {
1031
1041
recursiveT := & types.Type {
1032
1042
Name : types.Name {
@@ -1095,18 +1105,53 @@ func TestStructParse(t *testing.T) {
1095
1105
}
1096
1106
},
1097
1107
},
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
+ },
1098
1143
}
1099
1144
1100
1145
for _ , tc := range testCases {
1101
1146
t .Run (tc .description , func (t * testing.T ) {
1102
1147
parser := New ()
1103
1148
1104
- pkgs , err := parser .loadPackages (tc .testFile )
1149
+ _ , err := parser .loadPackages (tc .testFiles ... )
1105
1150
if err != nil {
1106
1151
t .Errorf ("unexpected error: %v" , err )
1107
1152
}
1108
- u := types. Universe {}
1109
- if err := parser . addPkgToUniverse ( pkgs [ 0 ], & u ); err != nil {
1153
+ u , err := parser . NewUniverse ()
1154
+ if err != nil {
1110
1155
t .Errorf ("unexpected error: %v" , err )
1111
1156
}
1112
1157
@@ -1153,3 +1198,37 @@ func TestGoNameToName(t *testing.T) {
1153
1198
})
1154
1199
}
1155
1200
}
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