| 1 |
// Copyright 2021 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 typeparams_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"go/ast" |
| 9 |
"go/parser" |
| 10 |
"go/token" |
| 11 |
"go/types" |
| 12 |
"regexp" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
|
| 16 |
. "honnef.co/go/tools/internal/xtools-internal/typeparams" |
| 17 |
) |
| 18 |
|
| 19 |
func TestStructuralTerms(t *testing.T) { |
| 20 |
// In the following tests, src must define a type T with (at least) one type |
| 21 |
// parameter. We will compute the structural terms of the first type |
| 22 |
// parameter. |
| 23 |
tests := []struct { |
| 24 |
src string |
| 25 |
want string |
| 26 |
wantError string |
| 27 |
}{ |
| 28 |
{"package emptyinterface0; type T[P interface{}] int", "all", ""}, |
| 29 |
{"package emptyinterface1; type T[P interface{ int | interface{} }] int", "all", ""}, |
| 30 |
{"package singleton; type T[P interface{ int }] int", "int", ""}, |
| 31 |
{"package under; type T[P interface{~int}] int", "~int", ""}, |
| 32 |
{"package superset; type T[P interface{ ~int | int }] int", "~int", ""}, |
| 33 |
{"package overlap; type T[P interface{ ~int; int }] int", "int", ""}, |
| 34 |
{"package emptyintersection; type T[P interface{ ~int; string }] int", "", "empty type set"}, |
| 35 |
|
| 36 |
{"package embedded0; type T[P interface{ I }] int; type I interface { int }", "int", ""}, |
| 37 |
{"package embedded1; type T[P interface{ I | string }] int; type I interface{ int | ~string }", "int ?\\| ?~string", ""}, |
| 38 |
{"package embedded2; type T[P interface{ I; string }] int; type I interface{ int | ~string }", "string", ""}, |
| 39 |
|
| 40 |
{"package named; type T[P C] int; type C interface{ ~int|int }", "~int", ""}, |
| 41 |
{`// package example is taken from the docstring for StructuralTerms |
| 42 |
package example |
| 43 |
|
| 44 |
type A interface{ ~string|~[]byte } |
| 45 |
|
| 46 |
type B interface{ int|string } |
| 47 |
|
| 48 |
type C interface { ~string|~int } |
| 49 |
|
| 50 |
type T[P interface{ A|B; C }] int |
| 51 |
`, "~string ?\\| ?int", ""}, |
| 52 |
} |
| 53 |
|
| 54 |
for _, test := range tests { |
| 55 |
fset := token.NewFileSet() |
| 56 |
f, err := parser.ParseFile(fset, "p.go", test.src, 0) |
| 57 |
if err != nil { |
| 58 |
t.Fatal(err) |
| 59 |
} |
| 60 |
t.Run(f.Name.Name, func(t *testing.T) { |
| 61 |
conf := types.Config{ |
| 62 |
Error: func(error) {}, // keep going on errors |
| 63 |
} |
| 64 |
pkg, err := conf.Check("", fset, []*ast.File{f}, nil) |
| 65 |
if err != nil { |
| 66 |
t.Logf("types.Config.Check: %v", err) |
| 67 |
// keep going on type checker errors: we want to assert on behavior of |
| 68 |
// invalid code as well. |
| 69 |
} |
| 70 |
obj := pkg.Scope().Lookup("T") |
| 71 |
if obj == nil { |
| 72 |
t.Fatal("type T not found") |
| 73 |
} |
| 74 |
T := obj.Type().(*types.Named).TypeParams().At(0) |
| 75 |
terms, err := StructuralTerms(T) |
| 76 |
if test.wantError != "" { |
| 77 |
if err == nil { |
| 78 |
t.Fatalf("StructuralTerms(%s): nil error, want %q", T, test.wantError) |
| 79 |
} |
| 80 |
if !strings.Contains(err.Error(), test.wantError) { |
| 81 |
t.Errorf("StructuralTerms(%s): err = %q, want %q", T, err, test.wantError) |
| 82 |
} |
| 83 |
return |
| 84 |
} |
| 85 |
if err != nil { |
| 86 |
t.Fatal(err) |
| 87 |
} |
| 88 |
var got string |
| 89 |
if len(terms) == 0 { |
| 90 |
got = "all" |
| 91 |
} else { |
| 92 |
qual := types.RelativeTo(pkg) |
| 93 |
got = types.TypeString(types.NewUnion(terms), qual) |
| 94 |
} |
| 95 |
want := regexp.MustCompile(test.want) |
| 96 |
if !want.MatchString(got) { |
| 97 |
t.Errorf("StructuralTerms(%s) = %q, want %q", T, got, test.want) |
| 98 |
} |
| 99 |
}) |
| 100 |
} |
| 101 |
} |