Source file src/go/types/typestring_test.go

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package types_test
     6  
     7  import (
     8  	"go/ast"
     9  	"go/importer"
    10  	"go/parser"
    11  	"go/token"
    12  	"internal/testenv"
    13  	"testing"
    14  
    15  	. "go/types"
    16  )
    17  
    18  const filename = "<src>"
    19  
    20  func makePkg(src string) (*Package, error) {
    21  	fset := token.NewFileSet()
    22  	file, err := parser.ParseFile(fset, filename, src, parser.DeclarationErrors)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	// use the package name as package path
    27  	conf := Config{Importer: importer.Default()}
    28  	return conf.Check(file.Name.Name, fset, []*ast.File{file}, nil)
    29  }
    30  
    31  type testEntry struct {
    32  	src, str string
    33  }
    34  
    35  // dup returns a testEntry where both src and str are the same.
    36  func dup(s string) testEntry {
    37  	return testEntry{s, s}
    38  }
    39  
    40  // types that don't depend on any other type declarations
    41  var independentTestTypes = []testEntry{
    42  	// basic types
    43  	dup("int"),
    44  	dup("float32"),
    45  	dup("string"),
    46  
    47  	// arrays
    48  	dup("[10]int"),
    49  
    50  	// slices
    51  	dup("[]int"),
    52  	dup("[][]int"),
    53  
    54  	// structs
    55  	dup("struct{}"),
    56  	dup("struct{x int}"),
    57  	{`struct {
    58  		x, y int
    59  		z float32 "foo"
    60  	}`, `struct{x int; y int; z float32 "foo"}`},
    61  	{`struct {
    62  		string
    63  		elems []complex128
    64  	}`, `struct{string; elems []complex128}`},
    65  
    66  	// pointers
    67  	dup("*int"),
    68  	dup("***struct{}"),
    69  	dup("*struct{a int; b float32}"),
    70  
    71  	// functions
    72  	dup("func()"),
    73  	dup("func(x int)"),
    74  	{"func(x, y int)", "func(x int, y int)"},
    75  	{"func(x, y int, z string)", "func(x int, y int, z string)"},
    76  	dup("func(int)"),
    77  	{"func(int, string, byte)", "func(int, string, byte)"},
    78  
    79  	dup("func() int"),
    80  	{"func() (string)", "func() string"},
    81  	dup("func() (u int)"),
    82  	{"func() (u, v int, w string)", "func() (u int, v int, w string)"},
    83  
    84  	dup("func(int) string"),
    85  	dup("func(x int) string"),
    86  	dup("func(x int) (u string)"),
    87  	{"func(x, y int) (u string)", "func(x int, y int) (u string)"},
    88  
    89  	dup("func(...int) string"),
    90  	dup("func(x ...int) string"),
    91  	dup("func(x ...int) (u string)"),
    92  	{"func(x int, y ...int) (u string)", "func(x int, y ...int) (u string)"},
    93  
    94  	// interfaces
    95  	dup("interface{}"),
    96  	dup("interface{m()}"),
    97  	dup(`interface{String() string; m(int) float32}`),
    98  	dup("interface{int|float32|complex128}"),
    99  	dup("interface{int|~float32|~complex128}"),
   100  	dup("any"),
   101  	dup("interface{comparable}"),
   102  	// TODO(gri) adjust test for EvalCompositeTest
   103  	// {"comparable", "interface{comparable}"},
   104  	// {"error", "interface{Error() string}"},
   105  
   106  	// maps
   107  	dup("map[string]int"),
   108  	{"map[struct{x, y int}][]byte", "map[struct{x int; y int}][]byte"},
   109  
   110  	// channels
   111  	dup("chan<- chan int"),
   112  	dup("chan<- <-chan int"),
   113  	dup("<-chan <-chan int"),
   114  	dup("chan (<-chan int)"),
   115  	dup("chan<- func()"),
   116  	dup("<-chan []func() int"),
   117  }
   118  
   119  // types that depend on other type declarations (src in TestTypes)
   120  var dependentTestTypes = []testEntry{
   121  	// interfaces
   122  	dup(`interface{io.Reader; io.Writer}`),
   123  	dup(`interface{m() int; io.Writer}`),
   124  	{`interface{m() interface{T}}`, `interface{m() interface{p.T}}`},
   125  }
   126  
   127  func TestTypeString(t *testing.T) {
   128  	testenv.MustHaveGoBuild(t)
   129  
   130  	var tests []testEntry
   131  	tests = append(tests, independentTestTypes...)
   132  	tests = append(tests, dependentTestTypes...)
   133  
   134  	for _, test := range tests {
   135  		src := `package p; import "io"; type _ io.Writer; type T ` + test.src
   136  		pkg, err := makePkg(src)
   137  		if err != nil {
   138  			t.Errorf("%s: %s", src, err)
   139  			continue
   140  		}
   141  		obj := pkg.Scope().Lookup("T")
   142  		if obj == nil {
   143  			t.Errorf("%s: T not found", test.src)
   144  			continue
   145  		}
   146  		typ := obj.Type().Underlying()
   147  		if got := typ.String(); got != test.str {
   148  			t.Errorf("%s: got %s, want %s", test.src, got, test.str)
   149  		}
   150  	}
   151  }
   152  
   153  func TestQualifiedTypeString(t *testing.T) {
   154  	p, _ := pkgFor("p.go", "package p; type T int", nil)
   155  	q, _ := pkgFor("q.go", "package q", nil)
   156  
   157  	pT := p.Scope().Lookup("T").Type()
   158  	for _, test := range []struct {
   159  		typ  Type
   160  		this *Package
   161  		want string
   162  	}{
   163  		{nil, nil, "<nil>"},
   164  		{pT, nil, "p.T"},
   165  		{pT, p, "T"},
   166  		{pT, q, "p.T"},
   167  		{NewPointer(pT), p, "*T"},
   168  		{NewPointer(pT), q, "*p.T"},
   169  	} {
   170  		qualifier := func(pkg *Package) string {
   171  			if pkg != test.this {
   172  				return pkg.Name()
   173  			}
   174  			return ""
   175  		}
   176  		if got := TypeString(test.typ, qualifier); got != test.want {
   177  			t.Errorf("TypeString(%s, %s) = %s, want %s",
   178  				test.this, test.typ, got, test.want)
   179  		}
   180  	}
   181  }
   182  

View as plain text