...

Source file src/go/types/typestring_test.go

Documentation: go/types

		 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  
		99  	// TODO(rFindley) uncomment this once this AST is accepted, and add more test
	 100  	// cases.
	 101  	// dup(`interface{type int, float32, complex128}`),
	 102  
	 103  	// maps
	 104  	dup("map[string]int"),
	 105  	{"map[struct{x, y int}][]byte", "map[struct{x int; y int}][]byte"},
	 106  
	 107  	// channels
	 108  	dup("chan<- chan int"),
	 109  	dup("chan<- <-chan int"),
	 110  	dup("<-chan <-chan int"),
	 111  	dup("chan (<-chan int)"),
	 112  	dup("chan<- func()"),
	 113  	dup("<-chan []func() int"),
	 114  }
	 115  
	 116  // types that depend on other type declarations (src in TestTypes)
	 117  var dependentTestTypes = []testEntry{
	 118  	// interfaces
	 119  	dup(`interface{io.Reader; io.Writer}`),
	 120  	dup(`interface{m() int; io.Writer}`),
	 121  	{`interface{m() interface{T}}`, `interface{m() interface{p.T}}`},
	 122  }
	 123  
	 124  func TestTypeString(t *testing.T) {
	 125  	testenv.MustHaveGoBuild(t)
	 126  
	 127  	var tests []testEntry
	 128  	tests = append(tests, independentTestTypes...)
	 129  	tests = append(tests, dependentTestTypes...)
	 130  
	 131  	for _, test := range tests {
	 132  		src := `package p; import "io"; type _ io.Writer; type T ` + test.src
	 133  		pkg, err := makePkg(src)
	 134  		if err != nil {
	 135  			t.Errorf("%s: %s", src, err)
	 136  			continue
	 137  		}
	 138  		typ := pkg.Scope().Lookup("T").Type().Underlying()
	 139  		if got := typ.String(); got != test.str {
	 140  			t.Errorf("%s: got %s, want %s", test.src, got, test.str)
	 141  		}
	 142  	}
	 143  }
	 144  
	 145  func TestIncompleteInterfaces(t *testing.T) {
	 146  	sig := NewSignature(nil, nil, nil, false)
	 147  	m := NewFunc(token.NoPos, nil, "m", sig)
	 148  	for _, test := range []struct {
	 149  		typ	*Interface
	 150  		want string
	 151  	}{
	 152  		{new(Interface), "interface{/* incomplete */}"},
	 153  		{new(Interface).Complete(), "interface{}"},
	 154  
	 155  		{NewInterface(nil, nil), "interface{}"},
	 156  		{NewInterface(nil, nil).Complete(), "interface{}"},
	 157  		{NewInterface([]*Func{}, nil), "interface{}"},
	 158  		{NewInterface([]*Func{}, nil).Complete(), "interface{}"},
	 159  		{NewInterface(nil, []*Named{}), "interface{}"},
	 160  		{NewInterface(nil, []*Named{}).Complete(), "interface{}"},
	 161  		{NewInterface([]*Func{m}, nil), "interface{m() /* incomplete */}"},
	 162  		{NewInterface([]*Func{m}, nil).Complete(), "interface{m()}"},
	 163  		{NewInterface(nil, []*Named{newDefined(new(Interface).Complete())}), "interface{T /* incomplete */}"},
	 164  		{NewInterface(nil, []*Named{newDefined(new(Interface).Complete())}).Complete(), "interface{T}"},
	 165  		{NewInterface(nil, []*Named{newDefined(NewInterface([]*Func{m}, nil))}), "interface{T /* incomplete */}"},
	 166  		{NewInterface(nil, []*Named{newDefined(NewInterface([]*Func{m}, nil).Complete())}), "interface{T /* incomplete */}"},
	 167  		{NewInterface(nil, []*Named{newDefined(NewInterface([]*Func{m}, nil).Complete())}).Complete(), "interface{T}"},
	 168  
	 169  		{NewInterfaceType(nil, nil), "interface{}"},
	 170  		{NewInterfaceType(nil, nil).Complete(), "interface{}"},
	 171  		{NewInterfaceType([]*Func{}, nil), "interface{}"},
	 172  		{NewInterfaceType([]*Func{}, nil).Complete(), "interface{}"},
	 173  		{NewInterfaceType(nil, []Type{}), "interface{}"},
	 174  		{NewInterfaceType(nil, []Type{}).Complete(), "interface{}"},
	 175  		{NewInterfaceType([]*Func{m}, nil), "interface{m() /* incomplete */}"},
	 176  		{NewInterfaceType([]*Func{m}, nil).Complete(), "interface{m()}"},
	 177  		{NewInterfaceType(nil, []Type{new(Interface).Complete()}), "interface{interface{} /* incomplete */}"},
	 178  		{NewInterfaceType(nil, []Type{new(Interface).Complete()}).Complete(), "interface{interface{}}"},
	 179  		{NewInterfaceType(nil, []Type{NewInterfaceType([]*Func{m}, nil)}), "interface{interface{m() /* incomplete */} /* incomplete */}"},
	 180  		{NewInterfaceType(nil, []Type{NewInterfaceType([]*Func{m}, nil).Complete()}), "interface{interface{m()} /* incomplete */}"},
	 181  		{NewInterfaceType(nil, []Type{NewInterfaceType([]*Func{m}, nil).Complete()}).Complete(), "interface{interface{m()}}"},
	 182  	} {
	 183  		got := test.typ.String()
	 184  		if got != test.want {
	 185  			t.Errorf("got: %s, want: %s", got, test.want)
	 186  		}
	 187  	}
	 188  }
	 189  
	 190  // newDefined creates a new defined type named T with the given underlying type.
	 191  // Helper function for use with TestIncompleteInterfaces only.
	 192  func newDefined(underlying Type) *Named {
	 193  	tname := NewTypeName(token.NoPos, nil, "T", nil)
	 194  	return NewNamed(tname, underlying, nil)
	 195  }
	 196  
	 197  func TestQualifiedTypeString(t *testing.T) {
	 198  	p, _ := pkgFor("p.go", "package p; type T int", nil)
	 199  	q, _ := pkgFor("q.go", "package q", nil)
	 200  
	 201  	pT := p.Scope().Lookup("T").Type()
	 202  	for _, test := range []struct {
	 203  		typ	Type
	 204  		this *Package
	 205  		want string
	 206  	}{
	 207  		{nil, nil, "<nil>"},
	 208  		{pT, nil, "p.T"},
	 209  		{pT, p, "T"},
	 210  		{pT, q, "p.T"},
	 211  		{NewPointer(pT), p, "*T"},
	 212  		{NewPointer(pT), q, "*p.T"},
	 213  	} {
	 214  		qualifier := func(pkg *Package) string {
	 215  			if pkg != test.this {
	 216  				return pkg.Name()
	 217  			}
	 218  			return ""
	 219  		}
	 220  		if got := TypeString(test.typ, qualifier); got != test.want {
	 221  			t.Errorf("TypeString(%s, %s) = %s, want %s",
	 222  				test.this, test.typ, got, test.want)
	 223  		}
	 224  	}
	 225  }
	 226  

View as plain text