From 508ae115ae81ced77301e3fa8cad86b776ae16b9 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 8 Aug 2013 20:54:28 -0700 Subject: [PATCH] go.tools/go/types: added missing test for file scopes R=adonovan, r CC=golang-dev https://golang.org/cl/12671044 --- go/types/api_test.go | 69 ++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/go/types/api_test.go b/go/types/api_test.go index e116066f..056dbcdf 100644 --- a/go/types/api_test.go +++ b/go/types/api_test.go @@ -15,7 +15,7 @@ import ( "testing" ) -func pkgFor(path string, source string, info *Info) (*Package, error) { +func pkgFor(path, source string, info *Info) (*Package, error) { fset = token.NewFileSet() f, err := parser.ParseFile(fset, path, source, 0) if err != nil { @@ -31,6 +31,14 @@ func pkgFor(path string, source string, info *Info) (*Package, error) { return pkg, nil } +func mustTypecheck(t *testing.T, path, source string, info *Info) *Package { + pkg, err := pkgFor(path, source, info) + if err != nil { + t.Fatalf("%s: didn't type-check (%s)", path, err) + } + return pkg +} + func TestCommaOkTypes(t *testing.T) { var tests = []struct { src string @@ -57,14 +65,8 @@ func TestCommaOkTypes(t *testing.T) { for i, test := range tests { path := fmt.Sprintf("CommaOkTypes%d", i) - - // type-check info := Info{Types: make(map[ast.Expr]Type)} - _, err := pkgFor(path, test.src, &info) - if err != nil { - t.Error(err) - continue - } + mustTypecheck(t, path, test.src, &info) // look for comma-ok expression type var typ Type @@ -91,78 +93,75 @@ func TestScopesInfo(t *testing.T) { src string scopes []string // list of scope descriptors of the form kind:varlist }{ - {"package p", []string{ + {`package p`, []string{ "file:", }}, - {"package p; func _() {}", []string{ + {`package p; import ( "fmt"; m "math"; _ "os" )`, []string{ + "file:fmt m", + }}, + {`package p; func _() {}`, []string{ "file:", "func:", }}, - {"package p; func _(x, y int) {}", []string{ + {`package p; func _(x, y int) {}`, []string{ "file:", "func:x y", }}, - {"package p; func _(x, y int) { x, z := 1, 2; _ = z }", []string{ + {`package p; func _(x, y int) { x, z := 1, 2; _ = z }`, []string{ "file:", "func:x y z", // redeclaration of x }}, - {"package p; func _(x, y int) (u, _ int) { return }", []string{ + {`package p; func _(x, y int) (u, _ int) { return }`, []string{ "file:", "func:u x y", }}, - {"package p; func _() { { var x int; _ = x } }", []string{ + {`package p; func _() { { var x int; _ = x } }`, []string{ "file:", "func:", "block:x", }}, - {"package p; func _() { if true {} }", []string{ + {`package p; func _() { if true {} }`, []string{ "file:", "func:", "if:", "block:", }}, - {"package p; func _() { if x := 0; x < 0 { y := x; _ = y } }", []string{ + {`package p; func _() { if x := 0; x < 0 { y := x; _ = y } }`, []string{ "file:", "func:", "if:x", "block:y", }}, - {"package p; func _() { switch x := 0; x {} }", []string{ + {`package p; func _() { switch x := 0; x {} }`, []string{ "file:", "func:", "switch:x", }}, - {"package p; func _() { switch x := 0; x { case 1: y := x; _ = y; default: }}", []string{ + {`package p; func _() { switch x := 0; x { case 1: y := x; _ = y; default: }}`, []string{ "file:", "func:", "switch:x", "case:y", "case:", }}, - {"package p; func _(t interface{}) { switch t.(type) {} }", []string{ + {`package p; func _(t interface{}) { switch t.(type) {} }`, []string{ "file:", "func:t", "type switch:", }}, - {"package p; func _(t interface{}) { switch t := t; t.(type) {} }", []string{ + {`package p; func _(t interface{}) { switch t := t; t.(type) {} }`, []string{ "file:", "func:t", "type switch:t", }}, - {"package p; func _(t interface{}) { switch x := t.(type) { case int: } }", []string{ + {`package p; func _(t interface{}) { switch x := t.(type) { case int: } }`, []string{ "file:", "func:t", "type switch:", "case:x", // x implicitly declared }}, - {"package p; func _() { select{} }", []string{ + {`package p; func _() { select{} }`, []string{ "file:", "func:", }}, - {"package p; func _(c chan int) { select{ case <-c: } }", []string{ + {`package p; func _(c chan int) { select{ case <-c: } }`, []string{ "file:", "func:c", "comm:", }}, - {"package p; func _(c chan int) { select{ case i := <-c: x := i; _ = x} }", []string{ + {`package p; func _(c chan int) { select{ case i := <-c: x := i; _ = x} }`, []string{ "file:", "func:c", "comm:i x", }}, - {"package p; func _() { for{} }", []string{ + {`package p; func _() { for{} }`, []string{ "file:", "func:", "for:", "block:", }}, - {"package p; func _(n int) { for i := 0; i < n; i++ { _ = i } }", []string{ + {`package p; func _(n int) { for i := 0; i < n; i++ { _ = i } }`, []string{ "file:", "func:n", "for:i", "block:", }}, - {"package p; func _(a []int) { for i := range a { _ = i} }", []string{ + {`package p; func _(a []int) { for i := range a { _ = i} }`, []string{ "file:", "func:a", "range:i", "block:", }}, - {"package p; var s int; func _(a []int) { for i, x := range a { s += x; _ = i } }", []string{ + {`package p; var s int; func _(a []int) { for i, x := range a { s += x; _ = i } }`, []string{ "file:", "func:a", "range:i x", "block:", }}, } for i, test := range tests { path := fmt.Sprintf("ScopesInfo%d", i) - - // type-check info := Info{Scopes: make(map[ast.Node]*Scope)} - _, err := pkgFor(path, test.src, &info) - if err != nil { - t.Error(err) - continue - } + mustTypecheck(t, path, test.src, &info) // number of scopes must match if len(info.Scopes) != len(test.scopes) {