...
1
2
3
4
5 package aliases_test
6
7 import (
8 "go/ast"
9 "go/parser"
10 "go/token"
11 "go/types"
12 "testing"
13
14 "golang.org/x/tools/internal/aliases"
15 "golang.org/x/tools/internal/testenv"
16 )
17
18
19 var _ func(*aliases.Alias) *types.TypeName = (*aliases.Alias).Obj
20
21
22
23
24
25 func TestNewAlias(t *testing.T) {
26 const source = `
27 package P
28
29 type Named int
30 `
31 fset := token.NewFileSet()
32 f, err := parser.ParseFile(fset, "hello.go", source, 0)
33 if err != nil {
34 t.Fatal(err)
35 }
36
37 var conf types.Config
38 pkg, err := conf.Check("P", fset, []*ast.File{f}, nil)
39 if err != nil {
40 t.Fatal(err)
41 }
42
43 expr := `*Named`
44 tv, err := types.Eval(fset, pkg, 0, expr)
45 if err != nil {
46 t.Fatalf("Eval(%s) failed: %v", expr, err)
47 }
48
49 for _, godebug := range []string{
50
51 "gotypesalias=0",
52 "gotypesalias=1"} {
53 t.Run(godebug, func(t *testing.T) {
54 t.Setenv("GODEBUG", godebug)
55
56 enabled := aliases.Enabled()
57
58 A := aliases.NewAlias(enabled, token.NoPos, pkg, "A", tv.Type)
59 if got, want := A.Name(), "A"; got != want {
60 t.Errorf("Expected A.Name()==%q. got %q", want, got)
61 }
62
63 if got, want := A.Type().Underlying(), tv.Type; got != want {
64 t.Errorf("Expected A.Type().Underlying()==%q. got %q", want, got)
65 }
66 if got, want := aliases.Unalias(A.Type()), tv.Type; got != want {
67 t.Errorf("Expected Unalias(A)==%q. got %q", want, got)
68 }
69
70 if testenv.Go1Point() >= 22 && godebug != "gotypesalias=0" {
71 if _, ok := A.Type().(*aliases.Alias); !ok {
72 t.Errorf("Expected A.Type() to be a types.Alias(). got %q", A.Type())
73 }
74 }
75 })
76 }
77 }
78
View as plain text