Debian Codegraph @

go/types/typeutil/typeparams_test.go

raw
1 //go:build go1.18
2
3 package typeutil
4
5 import (
6 "go/types"
7 "testing"
8 )
9
10 func simpleUnionIface(terms ...*types.Term) *types.Interface {
11 return types.NewInterfaceType(nil, []types.Type{types.NewUnion(terms)})
12 }
13
14 func newChannelIface(chans ...types.Type) *types.Interface {
15 var terms []*types.Term
16 for _, ch := range chans {
17 terms = append(terms, types.NewTerm(false, ch))
18 }
19 return simpleUnionIface(terms...)
20 }
21
22 func TestTypeSetCoreType(t *testing.T) {
23 pkg := types.NewPackage("pkg", "pkg")
24 TInt := types.Universe.Lookup("int").Type()
25 TUint := types.Universe.Lookup("uint").Type()
26 TMyInt1 := types.NewNamed(types.NewTypeName(0, pkg, "MyInt1", nil), TInt, nil)
27 TMyInt2 := types.NewNamed(types.NewTypeName(0, pkg, "MyInt2", nil), TInt, nil)
28 TChanInt := types.NewChan(types.SendRecv, TInt)
29 TChanIntRecv := types.NewChan(types.RecvOnly, TInt)
30 TChanIntSend := types.NewChan(types.SendOnly, TInt)
31 TNamedChanInt := types.NewNamed(types.NewTypeName(0, pkg, "NamedChan", nil), TChanInt, nil)
32
33 tt := []struct {
34 iface *types.Interface
35 want types.Type
36 }{
37 // same underlying type
38 {
39 simpleUnionIface(types.NewTerm(false, TMyInt1), types.NewTerm(false, TMyInt2)),
40 types.Universe.Lookup("int").Type(),
41 },
42 // different underlying types
43 {
44 simpleUnionIface(types.NewTerm(false, TInt), types.NewTerm(false, TUint)),
45 nil,
46 },
47 // empty type set
48 {
49 types.NewInterfaceType(nil, []types.Type{
50 types.NewUnion([]*types.Term{types.NewTerm(false, TInt)}),
51 types.NewUnion([]*types.Term{types.NewTerm(false, TUint)}),
52 }),
53 nil,
54 },
55 }
56 for _, tc := range tt {
57 ts := NewTypeSet(tc.iface)
58 if !types.Identical(ts.CoreType(), tc.want) {
59 t.Errorf("CoreType(%s) = %s, want %s", tc.iface, ts.CoreType(), tc.want)
60 }
61 }
62
63 tt2 := []struct {
64 iface *types.Interface
65 want types.ChanDir
66 }{
67 {
68 // named sr + unnamed sr = sr
69 // sr + sr = sr
70 newChannelIface(TNamedChanInt, TChanInt),
71 types.SendRecv,
72 },
73 {
74 // sr + sr = sr
75 newChannelIface(TChanInt, TChanInt),
76 types.SendRecv,
77 },
78 {
79 // s + s = s
80 newChannelIface(TChanIntSend, TChanIntSend),
81 types.SendOnly,
82 },
83 {
84 // r + r = r
85 newChannelIface(TChanIntRecv, TChanIntRecv),
86 types.RecvOnly,
87 },
88 {
89 // s + r = nil
90 newChannelIface(TChanIntSend, TChanIntRecv),
91 -1,
92 },
93 {
94 // sr + r = r
95 newChannelIface(TChanInt, TChanIntRecv),
96 types.RecvOnly,
97 },
98 {
99 // sr + s = s
100 newChannelIface(TChanInt, TChanIntSend),
101 types.SendOnly,
102 },
103 }
104 for _, tc := range tt2 {
105 ts := NewTypeSet(tc.iface)
106 core := ts.CoreType()
107 if (core == nil) != (tc.want == -1) {
108 t.Errorf("CoreType(%s) = %s, want %d", tc.iface, core, tc.want)
109 }
110 if core == nil {
111 continue
112 }
113 dir := core.(*types.Chan).Dir()
114 if dir != tc.want {
115 t.Errorf("direction of %s is %d, want %d", tc.iface, dir, tc.want)
116 }
117 }
118 }