@@ -25,7 +25,7 @@ func TestNestedSubstInGenericFunction(t *testing.T) {
2525 `
2626
2727 fSet := token .NewFileSet ()
28- f , err := parser .ParseFile (fSet , " hello.go" , source , 0 )
28+ f , err := parser .ParseFile (fSet , ` hello.go` , source , 0 )
2929 if err != nil {
3030 t .Fatal (err )
3131 }
@@ -41,46 +41,59 @@ func TestNestedSubstInGenericFunction(t *testing.T) {
4141 args []string // type expressions of args for the named type
4242 }
4343
44- for _ , test := range []struct {
45- nesting []namedType
46- want string // expected underlying value after substitution
44+ for i , test := range []struct {
45+ nesting []namedType
46+ want string // expected underlying value after substitution
47+ substWant string // expected string value of the Subster
4748 }{
49+ // "Substituting types.Signatures with generic functions are currently unsupported."
50+ // since we should be getting back concrete functions from the type checker.
51+ //{
52+ // nesting: []namedType{
53+ // {name: `A`, args: []string{`int`}},
54+ // },
55+ //},
4856 {
4957 nesting : []namedType {
5058 {name : `A` , args : []string {`int` }},
59+ {name : `B` },
5160 },
52- want : `struct{X int}` ,
61+ want : `struct{X int}` ,
62+ substWant : `{T->int}` ,
5363 },
5464 {
5565 nesting : []namedType {
5666 {name : `A` , args : []string {`int` }},
57- {name : `B` },
67+ {name : `C` , args : [] string { `bool` } },
5868 },
59- want : `struct{X int}` ,
69+ want : `struct{X int; Y bool}` ,
70+ substWant : `{T->int}:{U->bool}` ,
6071 },
6172 {
6273 nesting : []namedType {
63- {name : `A` , args : []string {`int` }},
64- {name : `C` , args : []string {`bool` }},
74+ {name : `D` },
6575 },
66- want : "struct{X int; Y bool}" ,
76+ want : `func()` ,
77+ substWant : `{}` ,
6778 },
6879 {
6980 nesting : []namedType {
7081 {name : `D` },
7182 {name : `E` , args : []string {`int` }},
7283 },
73- want : "struct{X int}" ,
84+ want : `struct{X int}` ,
85+ substWant : `{V->int}` ,
7486 },
7587 {
7688 nesting : []namedType {
7789 {name : `F` , args : []string {`int` }},
7890 },
79- want : `struct{X int}` ,
91+ want : `struct{X int}` ,
92+ substWant : `{W->int}` ,
8093 },
8194 } {
8295 if len (test .nesting ) == 0 {
83- t .Fatal ( ` Must have at least one names type to instantiate` )
96+ t .Fatalf ( `Test %d: Must have at least one names type to instantiate`, i )
8497 }
8598
8699 ctxt := types .NewContext ()
@@ -90,7 +103,7 @@ func TestNestedSubstInGenericFunction(t *testing.T) {
90103 for _ , nt := range test .nesting {
91104 obj = scope .Lookup (nt .name )
92105 if obj == nil {
93- t .Fatalf (`Failed to find %s in package scope` , nt .name )
106+ t .Fatalf (`Test %d: Failed to find %s in package scope` , i , nt .name )
94107 }
95108 if fn , ok := obj .(* types.Func ); ok {
96109 scope = fn .Scope ()
@@ -100,25 +113,24 @@ func TestNestedSubstInGenericFunction(t *testing.T) {
100113 subst = New (ctxt , tp , args , subst )
101114 }
102115
103- shouldNotPanic (t , func () {
116+ func () {
117+ defer func () {
118+ if r := recover (); r != nil {
119+ t .Errorf (`Test %d: panicked: %v` , i , r )
120+ }
121+ }()
122+
104123 stInst := subst .Type (obj .Type ().Underlying ())
105124 if got := stInst .String (); got != test .want {
106- t .Errorf ("%s.typ(%s) = %v, want %v" , subst , obj .Type ().Underlying (), got , test .want )
125+ t .Errorf ("Test %d: %s.typ(%s): got %v, want %v" , i , subst , obj .Type ().Underlying (), got , test .want )
126+ }
127+ if got := subst .String (); got != test .substWant {
128+ t .Errorf ("Test %d: subst string got %v, want %v" , i , got , test .substWant )
107129 }
108- })
130+ }( )
109131 }
110132}
111133
112- func shouldNotPanic (t * testing.T , f func ()) {
113- t .Helper ()
114- defer func () {
115- if r := recover (); r != nil {
116- t .Errorf (`panicked: %v` , r )
117- }
118- }()
119- f ()
120- }
121-
122134func getTypeParams (t * testing.T , typ types.Type ) * types.TypeParamList {
123135 switch typ := typ .(type ) {
124136 case * types.Named :
@@ -140,7 +152,7 @@ func getTypeParams(t *testing.T, typ types.Type) *types.TypeParamList {
140152func evalType (t * testing.T , fSet * token.FileSet , pkg * types.Package , expr string ) types.Type {
141153 tv , err := types .Eval (fSet , pkg , 0 , expr )
142154 if err != nil {
143- t .Fatalf (" Eval(%s) failed: %v" , expr , err )
155+ t .Fatalf (` Eval(%s) failed: %v` , expr , err )
144156 }
145157 return tv .Type
146158}
0 commit comments