| 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 types |
| 6 |
|
| 7 |
import ( |
| 8 |
"go/ast" |
| 9 |
"go/parser" |
| 10 |
"go/token" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestInvalidTypeSet(t *testing.T) { |
| 15 |
if !invalidTypeSet.IsEmpty() { |
| 16 |
t.Error("invalidTypeSet is not empty") |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
func TestTypeSetString(t *testing.T) { |
| 21 |
for body, want := range map[string]string{ |
| 22 |
"{}": "𝓤", |
| 23 |
"{int}": "{int}", |
| 24 |
"{~int}": "{~int}", |
| 25 |
"{int|string}": "{int | string}", |
| 26 |
"{int; string}": "∅", |
| 27 |
|
| 28 |
"{comparable}": "{comparable}", |
| 29 |
"{comparable; int}": "{int}", |
| 30 |
"{~int; comparable}": "{~int}", |
| 31 |
"{int|string; comparable}": "{int | string}", |
| 32 |
"{comparable; int; string}": "∅", |
| 33 |
|
| 34 |
"{m()}": "{func (p.T).m()}", |
| 35 |
"{m1(); m2() int }": "{func (p.T).m1(); func (p.T).m2() int}", |
| 36 |
"{error}": "{func (error).Error() string}", |
| 37 |
"{m(); comparable}": "{comparable; func (p.T).m()}", |
| 38 |
"{m1(); comparable; m2() int }": "{comparable; func (p.T).m1(); func (p.T).m2() int}", |
| 39 |
"{comparable; error}": "{comparable; func (error).Error() string}", |
| 40 |
|
| 41 |
"{m(); comparable; int|float32|string}": "{func (p.T).m(); int | float32 | string}", |
| 42 |
"{m1(); int; m2(); comparable }": "{func (p.T).m1(); func (p.T).m2(); int}", |
| 43 |
|
| 44 |
"{E}; type E interface{}": "𝓤", |
| 45 |
"{E}; type E interface{int;string}": "∅", |
| 46 |
"{E}; type E interface{comparable}": "{comparable}", |
| 47 |
} { |
| 48 |
// parse |
| 49 |
src := "package p; type T interface" + body |
| 50 |
fset := token.NewFileSet() |
| 51 |
file, err := parser.ParseFile(fset, "p.go", src, parser.AllErrors) |
| 52 |
if file == nil { |
| 53 |
t.Fatalf("%s: %v (invalid test case)", body, err) |
| 54 |
} |
| 55 |
|
| 56 |
// type check |
| 57 |
var conf Config |
| 58 |
pkg, err := conf.Check(file.Name.Name, fset, []*ast.File{file}, nil) |
| 59 |
if err != nil { |
| 60 |
t.Fatalf("%s: %v (invalid test case)", body, err) |
| 61 |
} |
| 62 |
|
| 63 |
// lookup T |
| 64 |
obj := pkg.scope.Lookup("T") |
| 65 |
if obj == nil { |
| 66 |
t.Fatalf("%s: T not found (invalid test case)", body) |
| 67 |
} |
| 68 |
T, ok := obj.Type().Underlying().(*Interface) |
| 69 |
if !ok { |
| 70 |
t.Fatalf("%s: %v is not an interface (invalid test case)", body, obj) |
| 71 |
} |
| 72 |
|
| 73 |
// verify test case |
| 74 |
got := T.typeSet().String() |
| 75 |
if got != want { |
| 76 |
t.Errorf("%s: got %s; want %s", body, got, want) |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// TODO(gri) add more tests |