| 1 |
/* |
| 2 |
Copyright 2015 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package parser_test |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"path" |
| 22 |
"path/filepath" |
| 23 |
"reflect" |
| 24 |
"testing" |
| 25 |
"text/template" |
| 26 |
|
| 27 |
"github.com/davecgh/go-spew/spew" |
| 28 |
"github.com/google/go-cmp/cmp" |
| 29 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 30 |
|
| 31 |
"k8s.io/gengo/args" |
| 32 |
"k8s.io/gengo/namer" |
| 33 |
"k8s.io/gengo/parser" |
| 34 |
"k8s.io/gengo/types" |
| 35 |
) |
| 36 |
|
| 37 |
func TestRecursive(t *testing.T) { |
| 38 |
dir := "k8s.io/gengo/testdata/a" |
| 39 |
d := args.Default() |
| 40 |
d.InputDirs = []string{dir + "/..."} |
| 41 |
b, err := d.NewBuilder() |
| 42 |
if err != nil { |
| 43 |
t.Fatalf("Fail making builder: %v", err) |
| 44 |
} |
| 45 |
findTypes, err := b.FindTypes() |
| 46 |
if err != nil { |
| 47 |
t.Fatalf("Fail finding types: %v", err) |
| 48 |
} |
| 49 |
foundB := false |
| 50 |
foundC := false |
| 51 |
for _, p := range b.FindPackages() { |
| 52 |
t.Logf("Package: %v", p) |
| 53 |
if p == "k8s.io/gengo/testdata/a/b" { |
| 54 |
foundB = true |
| 55 |
} |
| 56 |
if p == "k8s.io/gengo/testdata/a/c" { |
| 57 |
foundC = true |
| 58 |
} |
| 59 |
} |
| 60 |
if !foundB { |
| 61 |
t.Errorf("Expected to find packages a and b") |
| 62 |
} |
| 63 |
if foundC { |
| 64 |
t.Error("Did not expect to find package c") |
| 65 |
} |
| 66 |
if name := findTypes[dir].Types["AA"].Methods["AFunc"].Name.Name; |
| 67 |
name != "func (*k8s.io/gengo/testdata/a.AA).AFunc(i *int, j int) (*k8s.io/gengo/testdata/a.A, k8s.io/gengo/testdata/a/b.ITest, error)" { |
| 68 |
t.Errorf("Parse method type error, got name: %s", name) |
| 69 |
} |
| 70 |
// only has three package: package "a", package "b", and package "" for all |
| 71 |
if len(findTypes) != 3 { |
| 72 |
t.Error("Parse type error, and take type path as package") |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestRecursiveWithTestGoFiles(t *testing.T) { |
| 77 |
d := args.Default() |
| 78 |
d.IncludeTestFiles = true |
| 79 |
d.InputDirs = []string{"k8s.io/gengo/testdata/a/..."} |
| 80 |
b, err := d.NewBuilder() |
| 81 |
if err != nil { |
| 82 |
t.Fatalf("Fail making builder: %v", err) |
| 83 |
} |
| 84 |
_, err = b.FindTypes() |
| 85 |
if err != nil { |
| 86 |
t.Fatalf("Fail finding types: %v", err) |
| 87 |
} |
| 88 |
foundB := false |
| 89 |
foundC := false |
| 90 |
for _, p := range b.FindPackages() { |
| 91 |
t.Logf("Package: %v", p) |
| 92 |
if p == "k8s.io/gengo/testdata/a/b" { |
| 93 |
foundB = true |
| 94 |
} |
| 95 |
if p == "k8s.io/gengo/testdata/a/c" { |
| 96 |
foundC = true |
| 97 |
} |
| 98 |
} |
| 99 |
if !foundC || !foundB { |
| 100 |
t.Errorf("Expected to find packages a, b and c") |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
type file struct { |
| 105 |
path string |
| 106 |
contents string |
| 107 |
} |
| 108 |
|
| 109 |
// Pass files in topological order - deps first! |
| 110 |
func construct(t *testing.T, files []file, testNamer namer.Namer) (*parser.Builder, types.Universe, []*types.Type) { |
| 111 |
b := parser.New() |
| 112 |
for _, f := range files { |
| 113 |
if err := b.AddFileForTest(path.Dir(f.path), filepath.FromSlash(f.path), []byte(f.contents)); err != nil { |
| 114 |
t.Fatal(err) |
| 115 |
} |
| 116 |
} |
| 117 |
u, err := b.FindTypes() |
| 118 |
if err != nil { |
| 119 |
t.Fatal(err) |
| 120 |
} |
| 121 |
orderer := namer.Orderer{Namer: testNamer} |
| 122 |
o := orderer.OrderUniverse(u) |
| 123 |
return b, u, o |
| 124 |
} |
| 125 |
|
| 126 |
func TestBuilder(t *testing.T) { |
| 127 |
var testFiles = []file{ |
| 128 |
{ |
| 129 |
path: "base/common/proto/common.go", contents: ` |
| 130 |
package common |
| 131 |
|
| 132 |
type Object struct { |
| 133 |
ID int64 |
| 134 |
} |
| 135 |
`, |
| 136 |
}, { |
| 137 |
path: "base/foo/proto/foo.go", contents: ` |
| 138 |
package foo |
| 139 |
|
| 140 |
import ( |
| 141 |
"base/common/proto" |
| 142 |
) |
| 143 |
|
| 144 |
type Blah struct { |
| 145 |
common.Object |
| 146 |
Count int64 |
| 147 |
Frobbers map[string]*Frobber |
| 148 |
Baz []Object |
| 149 |
Nickname *string |
| 150 |
NumberIsAFavorite map[int]bool |
| 151 |
} |
| 152 |
|
| 153 |
type Frobber struct { |
| 154 |
Name string |
| 155 |
Amount int64 |
| 156 |
} |
| 157 |
|
| 158 |
type Object struct { |
| 159 |
common.Object |
| 160 |
} |
| 161 |
|
| 162 |
func AFunc(obj1 common.Object, obj2 Object) Frobber { |
| 163 |
} |
| 164 |
|
| 165 |
var AVar Frobber |
| 166 |
|
| 167 |
var ( |
| 168 |
AnotherVar = Frobber{} |
| 169 |
) |
| 170 |
|
| 171 |
type Enumeration string |
| 172 |
const ( |
| 173 |
EnumSymbol Enumeration = "enumSymbolValue" |
| 174 |
) |
| 175 |
|
| 176 |
type Degrees int |
| 177 |
const ( |
| 178 |
FirstDegree Degrees = iota |
| 179 |
SecondDegree |
| 180 |
ThirdDegree |
| 181 |
) |
| 182 |
|
| 183 |
const ConstNineNine = 99 |
| 184 |
const ConstHundred = ConstNineNine + 1 |
| 185 |
|
| 186 |
const ConstExpr = 1 - 0.707i * 9.3 |
| 187 |
const ConstFloat = float64(7.8) |
| 188 |
const ConstString = "constant string" |
| 189 |
`, |
| 190 |
}, |
| 191 |
} |
| 192 |
|
| 193 |
var tmplText = ` |
| 194 |
package o |
| 195 |
{{define "Struct"}}type {{Name .}} interface { {{range $m := .Members}}{{$n := Name $m.Type}} |
| 196 |
{{if $m.Embedded}}{{$n}}{{else}}{{$m.Name}}() {{$n}}{{if $m.Type.Elem}}{{else}} |
| 197 |
Set{{$m.Name}}({{$n}}){{end}}{{end}}{{end}} |
| 198 |
} |
| 199 |
|
| 200 |
{{end}} |
| 201 |
{{define "Func"}}{{$s := .Underlying.Signature}}var {{Name .}} func({{range $index,$elem := $s.Parameters}}{{if $index}}, {{end}}{{Raw $elem}}{{end}}) {{if $s.Results|len |gt 1}}({{end}}{{range $index,$elem := $s.Results}}{{if $index}}, {{end}}{{Raw .}}{{end}}{{if $s.Results|len |gt 1}}){{end}} = {{Raw .}} |
| 202 |
|
| 203 |
{{end}} |
| 204 |
{{define "Var"}}{{$t := .Underlying}}var {{Name .}} {{Raw $t}} = {{Raw .}} |
| 205 |
|
| 206 |
{{end}} |
| 207 |
{{define "Const"}}{{$t := .Underlying}}const {{Name .}} {{Raw $t}} = {{Raw .}}({{ .ConstValue }}) |
| 208 |
|
| 209 |
{{end}} |
| 210 |
{{range $t := .}}{{if eq $t.Kind "Struct"}}{{template "Struct" $t}}{{end}}{{end}} |
| 211 |
{{range $t := .}}{{if eq $t.Kind "DeclarationOf"}}{{if eq $t.Underlying.Kind "Func"}}{{template "Func" $t}}{{end}}{{end}}{{end}} |
| 212 |
{{range $t := .}}{{if eq $t.Kind "DeclarationOf"}}{{if eq $t.Underlying.Kind "Struct"}}{{template "Var" $t}}{{end}}{{end}}{{end}} |
| 213 |
{{range $t := .}}{{if eq $t.Kind "DeclarationOf"}}{{if eq $t.Underlying.Kind "Alias"}}{{template "Const" $t}}{{end}}{{end}}{{end}}` |
| 214 |
|
| 215 |
var expect = ` |
| 216 |
package o |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
type CommonObject interface { |
| 222 |
ID() Int64 |
| 223 |
SetID(Int64) |
| 224 |
} |
| 225 |
|
| 226 |
type FooBlah interface { |
| 227 |
CommonObject |
| 228 |
Count() Int64 |
| 229 |
SetCount(Int64) |
| 230 |
Frobbers() MapStringToPointerFooFrobber |
| 231 |
Baz() SliceFooObject |
| 232 |
Nickname() PointerString |
| 233 |
NumberIsAFavorite() MapIntToBool |
| 234 |
} |
| 235 |
|
| 236 |
type FooFrobber interface { |
| 237 |
Name() String |
| 238 |
SetName(String) |
| 239 |
Amount() Int64 |
| 240 |
SetAmount(Int64) |
| 241 |
} |
| 242 |
|
| 243 |
type FooObject interface { |
| 244 |
CommonObject |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
var FooAFunc func(proto.Object, proto.Object) proto.Frobber = proto.AFunc |
| 249 |
|
| 250 |
|
| 251 |
var FooAVar proto.Frobber = proto.AVar |
| 252 |
|
| 253 |
var FooAnotherVar proto.Frobber = proto.AnotherVar |
| 254 |
|
| 255 |
|
| 256 |
const FooEnumSymbol proto.Enumeration = proto.EnumSymbol(enumSymbolValue) |
| 257 |
|
| 258 |
const FooFirstDegree proto.Degrees = proto.FirstDegree(0) |
| 259 |
|
| 260 |
const FooSecondDegree proto.Degrees = proto.SecondDegree(1) |
| 261 |
|
| 262 |
const FooThirdDegree proto.Degrees = proto.ThirdDegree(2) |
| 263 |
|
| 264 |
` |
| 265 |
testNamer := namer.NewPublicNamer(1, "proto") |
| 266 |
rawNamer := namer.NewRawNamer("o", nil) |
| 267 |
_, u, o := construct(t, testFiles, testNamer) |
| 268 |
t.Logf("\n%v\n\n", o) |
| 269 |
args := map[string]interface{}{ |
| 270 |
"Name": testNamer.Name, |
| 271 |
"Raw": rawNamer.Name, |
| 272 |
} |
| 273 |
tmpl := template.Must( |
| 274 |
template.New(""). |
| 275 |
Funcs(args). |
| 276 |
Parse(tmplText), |
| 277 |
) |
| 278 |
buf := &bytes.Buffer{} |
| 279 |
tmpl.Execute(buf, o) |
| 280 |
if e, a := expect, buf.String(); e != a { |
| 281 |
cmp.Diff(e, a) |
| 282 |
t.Errorf("Wanted, got:\n%v\n-----\n%v\nDiff:\n%s", e, a, cmp.Diff(e, a)) |
| 283 |
} |
| 284 |
if p := u.Package("base/foo/proto"); !p.HasImport("base/common/proto") { |
| 285 |
t.Errorf("Unexpected lack of import line: %#v", p.Imports) |
| 286 |
} |
| 287 |
|
| 288 |
strPtr := func(s string) *string { return &s } |
| 289 |
|
| 290 |
expectedConst := map[string]*types.Type{ |
| 291 |
"EnumSymbol": &types.Type{ |
| 292 |
Name: types.Name{Package: "base/foo/proto", Name: "EnumSymbol"}, |
| 293 |
Kind: types.DeclarationOf, |
| 294 |
ConstValue: strPtr("enumSymbolValue"), |
| 295 |
}, |
| 296 |
"FirstDegree": &types.Type{ |
| 297 |
Name: types.Name{Package: "base/foo/proto", Name: "FirstDegree"}, |
| 298 |
Kind: types.DeclarationOf, |
| 299 |
ConstValue: strPtr("0"), |
| 300 |
}, |
| 301 |
"SecondDegree": &types.Type{ |
| 302 |
Name: types.Name{Package: "base/foo/proto", Name: "SecondDegree"}, |
| 303 |
Kind: types.DeclarationOf, |
| 304 |
ConstValue: strPtr("1"), |
| 305 |
}, |
| 306 |
"ThirdDegree": &types.Type{ |
| 307 |
Name: types.Name{Package: "base/foo/proto", Name: "ThirdDegree"}, |
| 308 |
Kind: types.DeclarationOf, |
| 309 |
ConstValue: strPtr("2"), |
| 310 |
}, |
| 311 |
"ConstNineNine": &types.Type{ |
| 312 |
Name: types.Name{Package: "base/foo/proto", Name: "ConstNineNine"}, |
| 313 |
Kind: types.DeclarationOf, |
| 314 |
ConstValue: strPtr("99"), |
| 315 |
}, |
| 316 |
"ConstHundred": &types.Type{ |
| 317 |
Name: types.Name{Package: "base/foo/proto", Name: "ConstHundred"}, |
| 318 |
Kind: types.DeclarationOf, |
| 319 |
ConstValue: strPtr("100"), |
| 320 |
}, |
| 321 |
"ConstFloat": &types.Type{ |
| 322 |
Name: types.Name{Package: "base/foo/proto", Name: "ConstFloat"}, |
| 323 |
Kind: types.DeclarationOf, |
| 324 |
ConstValue: strPtr("7.8"), |
| 325 |
}, |
| 326 |
"ConstExpr": &types.Type{ |
| 327 |
Name: types.Name{Package: "base/foo/proto", Name: "ConstExpr"}, |
| 328 |
Kind: types.DeclarationOf, |
| 329 |
ConstValue: strPtr("(1 + -6.5751i)"), |
| 330 |
}, |
| 331 |
"ConstString": &types.Type{ |
| 332 |
Name: types.Name{Package: "base/foo/proto", Name: "ConstString"}, |
| 333 |
Kind: types.DeclarationOf, |
| 334 |
ConstValue: strPtr("constant string"), |
| 335 |
}, |
| 336 |
} |
| 337 |
|
| 338 |
if diff := cmp.Diff( |
| 339 |
u.Package("base/foo/proto").Constants, expectedConst, |
| 340 |
cmpopts.IgnoreFields(types.Type{}, "Underlying"), |
| 341 |
cmpopts.IgnoreFields(types.Type{}, "CommentLines"), |
| 342 |
cmpopts.IgnoreFields(types.Type{}, "SecondClosestCommentLines"), |
| 343 |
); diff != "" { |
| 344 |
t.Errorf("Constant mismatch: %s", diff) |
| 345 |
} |
| 346 |
|
| 347 |
if len(u.Package("base/foo/proto").Constants) != len(expectedConst) { |
| 348 |
t.Errorf("Wanted %d constants, got: %s", |
| 349 |
len(expectedConst), spew.Sdump(u.Package("base/foo/proto").Constants)) |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
func TestStructParse(t *testing.T) { |
| 354 |
var structTest = file{ |
| 355 |
path: "base/foo/proto/foo.go", |
| 356 |
contents: ` |
| 357 |
package foo |
| 358 |
|
| 359 |
// Blah is a test. |
| 360 |
// A test, I tell you. |
| 361 |
type Blah struct { |
| 362 |
// A is the first field. |
| 363 |
A int64 ` + "`" + `json:"a"` + "`" + ` |
| 364 |
|
| 365 |
// B is the second field. |
| 366 |
// Multiline comments work. |
| 367 |
B string ` + "`" + `json:"b"` + "`" + ` |
| 368 |
} |
| 369 |
`, |
| 370 |
} |
| 371 |
|
| 372 |
_, u, o := construct(t, []file{structTest}, namer.NewPublicNamer(0)) |
| 373 |
t.Logf("%#v", o) |
| 374 |
blahT := u.Type(types.Name{Package: "base/foo/proto", Name: "Blah"}) |
| 375 |
if blahT == nil { |
| 376 |
t.Fatal("type not found") |
| 377 |
} |
| 378 |
if e, a := types.Struct, blahT.Kind; e != a { |
| 379 |
t.Errorf("struct kind wrong, wanted %v, got %v", e, a) |
| 380 |
} |
| 381 |
if e, a := []string{"Blah is a test.", "A test, I tell you."}, blahT.CommentLines; !reflect.DeepEqual(e, a) { |
| 382 |
t.Errorf("struct comment wrong, wanted %q, got %q", e, a) |
| 383 |
} |
| 384 |
m := types.Member{ |
| 385 |
Name: "B", |
| 386 |
Embedded: false, |
| 387 |
CommentLines: []string{"B is the second field.", "Multiline comments work."}, |
| 388 |
Tags: `json:"b"`, |
| 389 |
Type: types.String, |
| 390 |
} |
| 391 |
if e, a := m, blahT.Members[1]; !reflect.DeepEqual(e, a) { |
| 392 |
t.Errorf("wanted, got:\n%#v\n%#v", e, a) |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
func TestParseSecondClosestCommentLines(t *testing.T) { |
| 397 |
const fileName = "base/foo/proto/foo.go" |
| 398 |
testCases := []struct { |
| 399 |
testFile file |
| 400 |
expected []string |
| 401 |
}{ |
| 402 |
{ |
| 403 |
testFile: file{ |
| 404 |
path: fileName, contents: ` |
| 405 |
package foo |
| 406 |
// Blah's SecondClosestCommentLines. |
| 407 |
// Another line. |
| 408 |
|
| 409 |
// Blah is a test. |
| 410 |
// A test, I tell you. |
| 411 |
type Blah struct { |
| 412 |
a int |
| 413 |
} |
| 414 |
`}, |
| 415 |
expected: []string{"Blah's SecondClosestCommentLines.", "Another line."}, |
| 416 |
}, |
| 417 |
{ |
| 418 |
testFile: file{ |
| 419 |
path: fileName, contents: ` |
| 420 |
package foo |
| 421 |
// Blah's SecondClosestCommentLines. |
| 422 |
// Another line. |
| 423 |
|
| 424 |
type Blah struct { |
| 425 |
a int |
| 426 |
} |
| 427 |
`}, |
| 428 |
expected: []string{"Blah's SecondClosestCommentLines.", "Another line."}, |
| 429 |
}, |
| 430 |
} |
| 431 |
for _, test := range testCases { |
| 432 |
_, u, o := construct(t, []file{test.testFile}, namer.NewPublicNamer(0)) |
| 433 |
t.Logf("%#v", o) |
| 434 |
blahT := u.Type(types.Name{Package: "base/foo/proto", Name: "Blah"}) |
| 435 |
if e, a := test.expected, blahT.SecondClosestCommentLines; !reflect.DeepEqual(e, a) { |
| 436 |
t.Errorf("struct second closest comment wrong, wanted %q, got %q", e, a) |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
func TestParseMethodParameterAndResultNames(t *testing.T) { |
| 442 |
const fileName = "base/foo/proto/foo.go" |
| 443 |
testCases := []struct { |
| 444 |
testFile file |
| 445 |
expectedParamNames map[string][]string |
| 446 |
expectedResultNames map[string][]string |
| 447 |
}{ |
| 448 |
{ |
| 449 |
testFile: file{ |
| 450 |
path: fileName, contents: ` |
| 451 |
package foo |
| 452 |
|
| 453 |
type bar struct{} |
| 454 |
|
| 455 |
func (b *bar) SingleParam(param1 int) {} |
| 456 |
func (b *bar) MultipleParams(param1, param2 int) {} |
| 457 |
func (b *bar) SingleParamSingleResult(param1 int) (out1 bool) {} |
| 458 |
func (b *bar) MultipleParamsMultipleResults(param1 bool, param2 int) (out1 bool, out2 string) {} |
| 459 |
func (b *bar) NoParamsMultipleResults() (out1 bool, out2 string) {} |
| 460 |
func (b *bar) NoParamsSingleResults() (out1 bool) {} |
| 461 |
func (b *bar) NoParamsNoResults() {} |
| 462 |
func (b *bar) UnnamedSingleParamNoResults(int) {} |
| 463 |
func (b *bar) UnnamedMultipleParamsNoResult(int, bool) {} |
| 464 |
func (b *bar) NoParamsSingleUnnamedResults() int {} |
| 465 |
func (b *bar) NoParamsMultipleUnnamedResults() (int, string) {} |
| 466 |
`}, |
| 467 |
expectedParamNames: map[string][]string{ |
| 468 |
"SingleParam": {"param1"}, |
| 469 |
"MultipleParams": {"param1", "param2"}, |
| 470 |
"SingleParamSingleResult": {"param1"}, |
| 471 |
"MultipleParamsMultipleResults": {"param1", "param2"}, |
| 472 |
"NoParamsMultipleResults": nil, |
| 473 |
"NoParamsSingleResults": nil, |
| 474 |
"NoParamsNoResults": nil, |
| 475 |
"UnnamedSingleParamNoResults": {""}, |
| 476 |
"UnnamedMultipleParamsNoResult": {"", ""}, |
| 477 |
"NoParamsSingleUnnamedResults": nil, |
| 478 |
"NoParamsMultipleUnnamedResults": nil, |
| 479 |
}, |
| 480 |
expectedResultNames: map[string][]string{ |
| 481 |
"SingleParam": nil, |
| 482 |
"MultipleParams": nil, |
| 483 |
"SingleParamSingleResult": {"out1"}, |
| 484 |
"MultipleParamsMultipleResults": {"out1", "out2"}, |
| 485 |
"NoParamsMultipleResults": {"out1", "out2"}, |
| 486 |
"NoParamsSingleResults": {"out1"}, |
| 487 |
"NoParamsNoResults": nil, |
| 488 |
"UnnamedSingleParamNoResults": nil, |
| 489 |
"UnnamedMultipleParamsNoResult": nil, |
| 490 |
"NoParamsSingleUnnamedResults": {""}, |
| 491 |
"NoParamsMultipleUnnamedResults": {"", ""}, |
| 492 |
}, |
| 493 |
}, |
| 494 |
} |
| 495 |
for _, test := range testCases { |
| 496 |
_, u, o := construct(t, []file{test.testFile}, namer.NewPublicNamer(0)) |
| 497 |
t.Logf("%#v", o) |
| 498 |
blahT := u.Type(types.Name{Package: "base/foo/proto", Name: "bar"}) |
| 499 |
|
| 500 |
for methodName, methodType := range blahT.Methods { |
| 501 |
expectedParamNames := test.expectedParamNames[methodName] |
| 502 |
actualParamNames := methodType.Signature.ParameterNames |
| 503 |
if !reflect.DeepEqual(expectedParamNames, actualParamNames) { |
| 504 |
t.Errorf("%s param names parsed incorrectly wrong, wanted %v, got %v", methodName, expectedParamNames, |
| 505 |
actualParamNames) |
| 506 |
} |
| 507 |
|
| 508 |
expectedResultNames := test.expectedResultNames[methodName] |
| 509 |
actualResultNames := methodType.Signature.ResultNames |
| 510 |
if !reflect.DeepEqual(expectedResultNames, actualResultNames) { |
| 511 |
t.Errorf("%s result names parsed incorrectly wrong, wanted %v, got %v", methodName, expectedResultNames, |
| 512 |
actualResultNames) |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
func TestParseMethodCommentLines(t *testing.T) { |
| 520 |
const fileName = "base/foo/proto/foo.go" |
| 521 |
testCases := []struct { |
| 522 |
testFile file |
| 523 |
expected []string |
| 524 |
}{ |
| 525 |
{ |
| 526 |
testFile: file{ |
| 527 |
path: fileName, contents: ` |
| 528 |
package foo |
| 529 |
|
| 530 |
type Blah struct { |
| 531 |
a int |
| 532 |
} |
| 533 |
|
| 534 |
// BlahFunc's CommentLines. |
| 535 |
// Another line. |
| 536 |
func (b *Blah) BlahFunc() {} |
| 537 |
`}, |
| 538 |
expected: []string{"BlahFunc's CommentLines.", "Another line."}, |
| 539 |
}, |
| 540 |
{ |
| 541 |
testFile: file{ |
| 542 |
path: fileName, contents: ` |
| 543 |
package foo |
| 544 |
|
| 545 |
type Blah interface { |
| 546 |
// BlahFunc's CommentLines. |
| 547 |
// Another line. |
| 548 |
BlahFunc() |
| 549 |
} |
| 550 |
`}, |
| 551 |
expected: []string{"BlahFunc's CommentLines.", "Another line."}, |
| 552 |
}, |
| 553 |
} |
| 554 |
for _, test := range testCases { |
| 555 |
_, u, o := construct(t, []file{test.testFile}, namer.NewPublicNamer(0)) |
| 556 |
t.Logf("%#v", o) |
| 557 |
blahT := u.Type(types.Name{Package: "base/foo/proto", Name: "Blah"}) |
| 558 |
blahM := blahT.Methods["BlahFunc"] |
| 559 |
if e, a := test.expected, blahM.CommentLines; !reflect.DeepEqual(e, a) { |
| 560 |
t.Errorf("method comment wrong, wanted %q, got %q", e, a) |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
signatureTestCases := []struct { |
| 565 |
testFile file |
| 566 |
expected []string |
| 567 |
}{ |
| 568 |
{ |
| 569 |
testFile: file{ |
| 570 |
path: fileName, contents: ` |
| 571 |
package foo |
| 572 |
|
| 573 |
type Blah struct { |
| 574 |
a int |
| 575 |
} |
| 576 |
|
| 577 |
// Method1 CommentLines. |
| 578 |
func (b *Blah) Method1(sameArg int) {} |
| 579 |
|
| 580 |
// Method2 CommentLines. |
| 581 |
func (b *Blah) Method2(sameArg int) {} |
| 582 |
`}, |
| 583 |
}, |
| 584 |
{ |
| 585 |
testFile: file{ |
| 586 |
path: fileName, contents: ` |
| 587 |
package foo |
| 588 |
|
| 589 |
type Blah interface { |
| 590 |
// Method1 CommentLines. |
| 591 |
Method1(sameArg int) error |
| 592 |
|
| 593 |
// Method2 CommentLines. |
| 594 |
Method2(sameArg int) error |
| 595 |
} |
| 596 |
`}, |
| 597 |
}, |
| 598 |
} |
| 599 |
for _, test := range signatureTestCases { |
| 600 |
_, u, o := construct(t, []file{test.testFile}, namer.NewPublicNamer(0)) |
| 601 |
t.Logf("%#v", o) |
| 602 |
blahT := u.Type(types.Name{Package: "base/foo/proto", Name: "Blah"}) |
| 603 |
blahM1 := blahT.Methods["Method1"] |
| 604 |
blahM2 := blahT.Methods["Method2"] |
| 605 |
c1 := blahM1.CommentLines |
| 606 |
c2 := blahM2.CommentLines |
| 607 |
if reflect.DeepEqual(c1, c2) { |
| 608 |
t.Errorf("same signature method comment got equal, %v == %v", c1, c2) |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
func TestParseConstantCommentLines(t *testing.T) { |
| 614 |
testFile := file{ |
| 615 |
path: "base/foo/proto/foo.go", |
| 616 |
contents: ` |
| 617 |
package foo |
| 618 |
|
| 619 |
// FooString is a string of foo. |
| 620 |
type FooString string |
| 621 |
|
| 622 |
// FooStringOne is one foo. |
| 623 |
const FooStringOne FooString = "One" |
| 624 |
|
| 625 |
// An important integer. |
| 626 |
// This one is nine. |
| 627 |
const OtherInt = 9 |
| 628 |
`, |
| 629 |
} |
| 630 |
|
| 631 |
expectComment := func(obj *types.Type, lines []string) { |
| 632 |
t.Helper() |
| 633 |
if !reflect.DeepEqual(obj.CommentLines, lines) { |
| 634 |
t.Errorf("wrong const comment for %q: wanted %q, got %q", |
| 635 |
obj.Name, |
| 636 |
lines, obj.CommentLines, |
| 637 |
) |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
_, u, _ := construct(t, []file{testFile}, namer.NewPublicNamer(0)) |
| 642 |
|
| 643 |
expectComment( |
| 644 |
u.Constant(types.Name{Package: "base/foo/proto", Name: "FooStringOne"}), |
| 645 |
[]string{"FooStringOne is one foo."}, |
| 646 |
) |
| 647 |
|
| 648 |
expectComment( |
| 649 |
u.Constant(types.Name{Package: "base/foo/proto", Name: "OtherInt"}), |
| 650 |
[]string{"An important integer.", "This one is nine."}, |
| 651 |
) |
| 652 |
} |
| 653 |
|
| 654 |
func TestTypeKindParse(t *testing.T) { |
| 655 |
var testFiles = []file{ |
| 656 |
{path: "a/foo.go", contents: "package a\ntype Test string\n"}, |
| 657 |
{path: "b/foo.go", contents: "package b\ntype Test map[int]string\n"}, |
| 658 |
{path: "c/foo.go", contents: "package c\ntype Test []string\n"}, |
| 659 |
{path: "d/foo.go", contents: "package d\ntype Test struct{a int; b struct{a int}; c map[int]string; d *string}\n"}, |
| 660 |
{path: "e/foo.go", contents: "package e\ntype Test *string\n"}, |
| 661 |
{path: "f/foo.go", contents: ` |
| 662 |
package f |
| 663 |
import ( |
| 664 |
"a" |
| 665 |
"b" |
| 666 |
) |
| 667 |
type Test []a.Test |
| 668 |
type Test2 *a.Test |
| 669 |
type Test3 map[a.Test]b.Test |
| 670 |
type Test4 struct { |
| 671 |
a struct {a a.Test; b b.Test} |
| 672 |
b map[a.Test]b.Test |
| 673 |
c *a.Test |
| 674 |
d []a.Test |
| 675 |
e []string |
| 676 |
} |
| 677 |
`}, |
| 678 |
{path: "g/foo.go", contents: ` |
| 679 |
package g |
| 680 |
type Test func(a, b string) (c, d string) |
| 681 |
func (t Test) Method(a, b string) (c, d string) { return t(a, b) } |
| 682 |
type Interface interface{Method(a, b string) (c, d string)} |
| 683 |
`}, |
| 684 |
{path: "h/foo.go", contents: ` |
| 685 |
package h |
| 686 |
import "a" |
| 687 |
type Test [1]a.Test |
| 688 |
`}, |
| 689 |
} |
| 690 |
|
| 691 |
// Check that the right types are found, and the namers give the expected names. |
| 692 |
|
| 693 |
assertions := []struct { |
| 694 |
Package, Name string |
| 695 |
k types.Kind |
| 696 |
names []string |
| 697 |
}{ |
| 698 |
{ |
| 699 |
Package: "a", Name: "Test", k: types.Alias, |
| 700 |
names: []string{"Test", "ATest", "test", "aTest", "a.Test"}, |
| 701 |
}, |
| 702 |
{ |
| 703 |
Package: "b", Name: "Test", k: types.Map, |
| 704 |
names: []string{"Test", "BTest", "test", "bTest", "b.Test"}, |
| 705 |
}, |
| 706 |
{ |
| 707 |
Package: "c", Name: "Test", k: types.Slice, |
| 708 |
names: []string{"Test", "CTest", "test", "cTest", "c.Test"}, |
| 709 |
}, |
| 710 |
{ |
| 711 |
Package: "d", Name: "Test", k: types.Struct, |
| 712 |
names: []string{"Test", "DTest", "test", "dTest", "d.Test"}, |
| 713 |
}, |
| 714 |
{ |
| 715 |
Package: "e", Name: "Test", k: types.Pointer, |
| 716 |
names: []string{"Test", "ETest", "test", "eTest", "e.Test"}, |
| 717 |
}, |
| 718 |
{ |
| 719 |
Package: "f", Name: "Test", k: types.Slice, |
| 720 |
names: []string{"Test", "FTest", "test", "fTest", "f.Test"}, |
| 721 |
}, |
| 722 |
{ |
| 723 |
Package: "g", Name: "Test", k: types.Func, |
| 724 |
names: []string{"Test", "GTest", "test", "gTest", "g.Test"}, |
| 725 |
}, |
| 726 |
{ |
| 727 |
Package: "g", Name: "Interface", k: types.Interface, |
| 728 |
names: []string{"Interface", "GInterface", "interface", "gInterface", "g.Interface"}, |
| 729 |
}, |
| 730 |
{ |
| 731 |
Package: "h", Name: "Test", k: types.Array, |
| 732 |
names: []string{"Test", "HTest", "test", "hTest", "h.Test"}, |
| 733 |
}, |
| 734 |
{ |
| 735 |
Package: "", Name: "string", k: types.Builtin, |
| 736 |
names: []string{"String", "String", "string", "string", "string"}, |
| 737 |
}, |
| 738 |
{ |
| 739 |
Package: "", Name: "int", k: types.Builtin, |
| 740 |
names: []string{"Int", "Int", "int", "int", "int"}, |
| 741 |
}, |
| 742 |
{ |
| 743 |
Package: "", Name: "struct{a int}", k: types.Struct, |
| 744 |
names: []string{"StructInt", "StructInt", "structInt", "structInt", "struct{a int}"}, |
| 745 |
}, |
| 746 |
{ |
| 747 |
Package: "", Name: "struct{a a.Test; b b.Test}", k: types.Struct, |
| 748 |
names: []string{"StructTestTest", "StructATestBTest", "structTestTest", "structATestBTest", "struct{a a.Test; b b.Test}"}, |
| 749 |
}, |
| 750 |
{ |
| 751 |
Package: "", Name: "map[int]string", k: types.Map, |
| 752 |
names: []string{"MapIntToString", "MapIntToString", "mapIntToString", "mapIntToString", "map[int]string"}, |
| 753 |
}, |
| 754 |
{ |
| 755 |
Package: "", Name: "map[a.Test]b.Test", k: types.Map, |
| 756 |
names: []string{"MapTestToTest", "MapATestToBTest", "mapTestToTest", "mapATestToBTest", "map[a.Test]b.Test"}, |
| 757 |
}, |
| 758 |
{ |
| 759 |
Package: "", Name: "[]string", k: types.Slice, |
| 760 |
names: []string{"SliceString", "SliceString", "sliceString", "sliceString", "[]string"}, |
| 761 |
}, |
| 762 |
{ |
| 763 |
Package: "", Name: "[]a.Test", k: types.Slice, |
| 764 |
names: []string{"SliceTest", "SliceATest", "sliceTest", "sliceATest", "[]a.Test"}, |
| 765 |
}, |
| 766 |
{ |
| 767 |
Package: "", Name: "*string", k: types.Pointer, |
| 768 |
names: []string{"PointerString", "PointerString", "pointerString", "pointerString", "*string"}, |
| 769 |
}, |
| 770 |
{ |
| 771 |
Package: "", Name: "*a.Test", k: types.Pointer, |
| 772 |
names: []string{"PointerTest", "PointerATest", "pointerTest", "pointerATest", "*a.Test"}, |
| 773 |
}, |
| 774 |
} |
| 775 |
|
| 776 |
namers := []namer.Namer{ |
| 777 |
namer.NewPublicNamer(0), |
| 778 |
namer.NewPublicNamer(1), |
| 779 |
namer.NewPrivateNamer(0), |
| 780 |
namer.NewPrivateNamer(1), |
| 781 |
namer.NewRawNamer("", nil), |
| 782 |
} |
| 783 |
|
| 784 |
for nameIndex, namer := range namers { |
| 785 |
_, u, _ := construct(t, testFiles, namer) |
| 786 |
t.Logf("Found types:\n") |
| 787 |
for pkgName, pkg := range u { |
| 788 |
for typeName, cur := range pkg.Types { |
| 789 |
t.Logf("%q-%q: %s %s", pkgName, typeName, cur.Name, cur.Kind) |
| 790 |
} |
| 791 |
} |
| 792 |
t.Logf("\n\n") |
| 793 |
|
| 794 |
for _, item := range assertions { |
| 795 |
n := types.Name{Package: item.Package, Name: item.Name} |
| 796 |
thisType := u.Type(n) |
| 797 |
if thisType == nil { |
| 798 |
t.Errorf("type %s not found", n) |
| 799 |
continue |
| 800 |
} |
| 801 |
underlyingType := thisType |
| 802 |
if item.k != types.Alias && thisType.Kind == types.Alias { |
| 803 |
underlyingType = thisType.Underlying |
| 804 |
if underlyingType == nil { |
| 805 |
t.Errorf("underlying type %s not found", n) |
| 806 |
continue |
| 807 |
} |
| 808 |
} |
| 809 |
if e, a := item.k, underlyingType.Kind; e != a { |
| 810 |
t.Errorf("%v-%s: type kind wrong, wanted %v, got %v (%#v)", nameIndex, n, e, a, underlyingType) |
| 811 |
} |
| 812 |
if e, a := item.names[nameIndex], namer.Name(thisType); e != a { |
| 813 |
t.Errorf("%v-%s: Expected %q, got %q", nameIndex, n, e, a) |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
// Also do some one-off checks |
| 818 |
gtest := u.Type(types.Name{Package: "g", Name: "Test"}) |
| 819 |
if e, a := 1, len(gtest.Methods); e != a { |
| 820 |
t.Errorf("expected %v but found %v methods: %#v", e, a, gtest) |
| 821 |
} |
| 822 |
iface := u.Type(types.Name{Package: "g", Name: "Interface"}) |
| 823 |
if e, a := 1, len(iface.Methods); e != a { |
| 824 |
t.Errorf("expected %v but found %v methods: %#v", e, a, iface) |
| 825 |
} |
| 826 |
} |
| 827 |
} |