go.tools/go/types: added missing test for file scopes
R=adonovan, r CC=golang-dev https://golang.org/cl/12671044
This commit is contained in:
parent
28fe5aa8d3
commit
508ae115ae
|
|
@ -15,7 +15,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func pkgFor(path string, source string, info *Info) (*Package, error) {
|
func pkgFor(path, source string, info *Info) (*Package, error) {
|
||||||
fset = token.NewFileSet()
|
fset = token.NewFileSet()
|
||||||
f, err := parser.ParseFile(fset, path, source, 0)
|
f, err := parser.ParseFile(fset, path, source, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -31,6 +31,14 @@ func pkgFor(path string, source string, info *Info) (*Package, error) {
|
||||||
return pkg, nil
|
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) {
|
func TestCommaOkTypes(t *testing.T) {
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
src string
|
src string
|
||||||
|
|
@ -57,14 +65,8 @@ func TestCommaOkTypes(t *testing.T) {
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
path := fmt.Sprintf("CommaOkTypes%d", i)
|
path := fmt.Sprintf("CommaOkTypes%d", i)
|
||||||
|
|
||||||
// type-check
|
|
||||||
info := Info{Types: make(map[ast.Expr]Type)}
|
info := Info{Types: make(map[ast.Expr]Type)}
|
||||||
_, err := pkgFor(path, test.src, &info)
|
mustTypecheck(t, path, test.src, &info)
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// look for comma-ok expression type
|
// look for comma-ok expression type
|
||||||
var typ Type
|
var typ Type
|
||||||
|
|
@ -91,78 +93,75 @@ func TestScopesInfo(t *testing.T) {
|
||||||
src string
|
src string
|
||||||
scopes []string // list of scope descriptors of the form kind:varlist
|
scopes []string // list of scope descriptors of the form kind:varlist
|
||||||
}{
|
}{
|
||||||
{"package p", []string{
|
{`package p`, []string{
|
||||||
"file:",
|
"file:",
|
||||||
}},
|
}},
|
||||||
{"package p; func _() {}", []string{
|
{`package p; import ( "fmt"; m "math"; _ "os" )`, []string{
|
||||||
|
"file:fmt m",
|
||||||
|
}},
|
||||||
|
{`package p; func _() {}`, []string{
|
||||||
"file:", "func:",
|
"file:", "func:",
|
||||||
}},
|
}},
|
||||||
{"package p; func _(x, y int) {}", []string{
|
{`package p; func _(x, y int) {}`, []string{
|
||||||
"file:", "func:x y",
|
"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
|
"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",
|
"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",
|
"file:", "func:", "block:x",
|
||||||
}},
|
}},
|
||||||
{"package p; func _() { if true {} }", []string{
|
{`package p; func _() { if true {} }`, []string{
|
||||||
"file:", "func:", "if:", "block:",
|
"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",
|
"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",
|
"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:",
|
"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:",
|
"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",
|
"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
|
"file:", "func:t", "type switch:", "case:x", // x implicitly declared
|
||||||
}},
|
}},
|
||||||
{"package p; func _() { select{} }", []string{
|
{`package p; func _() { select{} }`, []string{
|
||||||
"file:", "func:",
|
"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:",
|
"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",
|
"file:", "func:c", "comm:i x",
|
||||||
}},
|
}},
|
||||||
{"package p; func _() { for{} }", []string{
|
{`package p; func _() { for{} }`, []string{
|
||||||
"file:", "func:", "for:", "block:",
|
"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:",
|
"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:",
|
"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:",
|
"file:", "func:a", "range:i x", "block:",
|
||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
path := fmt.Sprintf("ScopesInfo%d", i)
|
path := fmt.Sprintf("ScopesInfo%d", i)
|
||||||
|
|
||||||
// type-check
|
|
||||||
info := Info{Scopes: make(map[ast.Node]*Scope)}
|
info := Info{Scopes: make(map[ast.Node]*Scope)}
|
||||||
_, err := pkgFor(path, test.src, &info)
|
mustTypecheck(t, path, test.src, &info)
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// number of scopes must match
|
// number of scopes must match
|
||||||
if len(info.Scopes) != len(test.scopes) {
|
if len(info.Scopes) != len(test.scopes) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue