Debian Codegraph @

typeparams/normalize_test.go

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