From cba0957a91b7f3ce16c06046b5c971e46ff806ef Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 31 Oct 2016 14:38:12 -0700 Subject: [PATCH] go/types: basic Defs/Uses test for aliases Change-Id: I113ea712838511ee823375d9ea059089195414f5 Reviewed-on: https://go-review.googlesource.com/32449 Run-TryBot: Robert Griesemer TryBot-Result: Gobot Gobot Reviewed-by: Alan Donovan --- src/go/types/api_test.go | 83 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/src/go/types/api_test.go b/src/go/types/api_test.go index 92c6d75e704..7c4fe575c84 100644 --- a/src/go/types/api_test.go +++ b/src/go/types/api_test.go @@ -1295,3 +1295,86 @@ func f(x int) { y := x; print(y) } } } } + +func TestAliases(t *testing.T) { + testenv.MustHaveGoBuild(t) + + const src = ` +package p + +import( + "go/build" + "go/types" +) + +// Defs +const Invalid => types.Invalid +type Struct => types.Struct +var Default => build.Default +func Implements => types.Implements + +// Uses +const _ = Invalid +var _ types.Struct = Struct{} // types must be identical +var _ build.Context = Default +var _ = Implements(nil, nil) +` + + info := Info{ + Defs: make(map[*ast.Ident]Object), + Uses: make(map[*ast.Ident]Object), + } + mustTypecheck(t, "Aliases", src, &info) + + // verify Defs + defs := map[string]string{ + "Invalid": "types.Invalid", + "Struct": "types.Struct", + "Default": "build.Default", + "Implements": "types.Implements", + } + + for ident, obj := range info.Defs { + if alias, ok := obj.(*Alias); ok { + if want := defs[ident.Name]; want != "" { + orig := alias.Orig() + if got := orig.Pkg().Name() + "." + orig.Name(); got != want { + t.Errorf("%v: got %v, want %v", ident, got, want) + } + delete(defs, ident.Name) // mark as found + } else { + t.Errorf("unexpected alias def of %v", ident) + } + } + } + + if len(defs) != 0 { + t.Errorf("missing aliases: %v", defs) + } + + // verify Uses + uses := map[string]string{ + "Invalid": "types.Invalid", + "Struct": "types.Struct", + "Default": "build.Default", + "Implements": "types.Implements", + } + + for ident, obj := range info.Uses { + if alias, ok := obj.(*Alias); ok { + if want := uses[ident.Name]; want != "" { + orig := alias.Orig() + if got := orig.Pkg().Name() + "." + orig.Name(); got != want { + t.Errorf("%v: got %v, want %v", ident, got, want) + } + delete(uses, ident.Name) // mark as found + } else { + t.Errorf("unexpected alias use of %v", ident) + } + } + } + + if len(uses) != 0 { + t.Errorf("missing aliases: %v", defs) + } +}