| 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 | errh := func(error) {} // dummy error handler so that parsing continues in presence of errors |
| 50 | src := "package p; type T interface" + body |
| 51 | file, err := Parse(nil, strings.NewReader(src), errh, nil, AllowGenerics) |
| 52 | if err != 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.PkgName.Value, []*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 := under(obj.Type()).(*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 | } |