(t *testing.T)
| 348 | } |
| 349 | |
| 350 | func TestEnclosingFunction(t *testing.T) { |
| 351 | tests := []struct { |
| 352 | input string // the input file |
| 353 | substr string // first occurrence of this string denotes interval |
| 354 | fn string // name of expected containing function |
| 355 | }{ |
| 356 | // We use distinctive numbers as syntactic landmarks. |
| 357 | |
| 358 | // Ordinary function: |
| 359 | {`package main |
| 360 | func f() { println(1003) }`, |
| 361 | "100", "main.f"}, |
| 362 | // Methods: |
| 363 | {`package main |
| 364 | type T int |
| 365 | func (t T) f() { println(200) }`, |
| 366 | "200", "(main.T).f"}, |
| 367 | // Function literal: |
| 368 | {`package main |
| 369 | func f() { println(func() { print(300) }) }`, |
| 370 | "300", "main.f$1"}, |
| 371 | // Doubly nested |
| 372 | {`package main |
| 373 | func f() { println(func() { print(func() { print(350) })})}`, |
| 374 | "350", "main.f$1$1"}, |
| 375 | // Implicit init for package-level var initializer. |
| 376 | {"package main; var a = 400", "400", "main.init"}, |
| 377 | // No code for constants: |
| 378 | {"package main; const a = 500", "500", "(none)"}, |
| 379 | // Explicit init() |
| 380 | {"package main; func init() { println(600) }", "600", "main.init#1"}, |
| 381 | // Multiple explicit init functions: |
| 382 | {`package main |
| 383 | func init() { println("foo") } |
| 384 | func init() { println(800) }`, |
| 385 | "800", "main.init#2"}, |
| 386 | // init() containing FuncLit. |
| 387 | {`package main |
| 388 | func init() { println(func(){print(900)}) }`, |
| 389 | "900", "main.init#1$1"}, |
| 390 | } |
| 391 | for _, test := range tests { |
| 392 | conf := loader.Config{Fset: token.NewFileSet()} |
| 393 | f, start, end := findInterval(t, conf.Fset, test.input, test.substr) |
| 394 | if f == nil { |
| 395 | continue |
| 396 | } |
| 397 | path, exact := astutil.PathEnclosingInterval(f, start, end) |
| 398 | if !exact { |
| 399 | t.Errorf("EnclosingFunction(%q) not exact", test.substr) |
| 400 | continue |
| 401 | } |
| 402 | |
| 403 | conf.CreateFromFiles("main", f) |
| 404 | |
| 405 | iprog, err := conf.Load() |
| 406 | if err != nil { |
| 407 | t.Error(err) |
nothing calls this directly
no test coverage detected