github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/typeparams/normalize_test.go (about)

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