Source file
src/go/types/termlist_test.go
1
2
3
4
5
6
7
8 package types
9
10 import (
11 "strings"
12 "testing"
13 )
14
15
16 func maketl(s string) termlist {
17 s = strings.ReplaceAll(s, " ", "")
18 names := strings.Split(s, "|")
19 r := make(termlist, len(names))
20 for i, n := range names {
21 r[i] = testTerm(n)
22 }
23 return r
24 }
25
26 func TestTermlistAll(t *testing.T) {
27 if !allTermlist.isAll() {
28 t.Errorf("allTermlist is not the set of all types")
29 }
30 }
31
32 func TestTermlistString(t *testing.T) {
33 for _, want := range []string{
34 "β
",
35 "π€",
36 "int",
37 "~int",
38 "myInt",
39 "β
| β
",
40 "π€ | π€",
41 "β
| π€ | int",
42 "β
| π€ | int | myInt",
43 } {
44 if got := maketl(want).String(); got != want {
45 t.Errorf("(%v).String() == %v", want, got)
46 }
47 }
48 }
49
50 func TestTermlistIsEmpty(t *testing.T) {
51 for test, want := range map[string]bool{
52 "β
": true,
53 "β
| β
": true,
54 "β
| β
| π€": false,
55 "β
| β
| myInt": false,
56 "π€": false,
57 "π€ | int": false,
58 "π€ | myInt | β
": false,
59 } {
60 xl := maketl(test)
61 got := xl.isEmpty()
62 if got != want {
63 t.Errorf("(%v).isEmpty() == %v; want %v", test, got, want)
64 }
65 }
66 }
67
68 func TestTermlistIsAll(t *testing.T) {
69 for test, want := range map[string]bool{
70 "β
": false,
71 "β
| β
": false,
72 "int | ~string": false,
73 "~int | myInt": false,
74 "β
| β
| π€": true,
75 "π€": true,
76 "π€ | int": true,
77 "myInt | π€": true,
78 } {
79 xl := maketl(test)
80 got := xl.isAll()
81 if got != want {
82 t.Errorf("(%v).isAll() == %v; want %v", test, got, want)
83 }
84 }
85 }
86
87 func TestTermlistNorm(t *testing.T) {
88 for _, test := range []struct {
89 xl, want string
90 }{
91 {"β
", "β
"},
92 {"β
| β
", "β
"},
93 {"β
| int", "int"},
94 {"β
| myInt", "myInt"},
95 {"π€ | int", "π€"},
96 {"π€ | myInt", "π€"},
97 {"int | myInt", "int | myInt"},
98 {"~int | int", "~int"},
99 {"~int | myInt", "~int"},
100 {"int | ~string | int", "int | ~string"},
101 {"~int | string | π€ | ~string | int", "π€"},
102 {"~int | string | myInt | ~string | int", "~int | ~string"},
103 } {
104 xl := maketl(test.xl)
105 got := maketl(test.xl).norm()
106 if got.String() != test.want {
107 t.Errorf("(%v).norm() = %v; want %v", xl, got, test.want)
108 }
109 }
110 }
111
112 func TestTermlistUnion(t *testing.T) {
113 for _, test := range []struct {
114 xl, yl, want string
115 }{
116
117 {"β
", "β
", "β
"},
118 {"β
", "π€", "π€"},
119 {"β
", "int", "int"},
120 {"π€", "~int", "π€"},
121 {"int", "~int", "~int"},
122 {"int", "string", "int | string"},
123 {"int", "myInt", "int | myInt"},
124 {"~int", "myInt", "~int"},
125 {"int | string", "~string", "int | ~string"},
126 {"~int | string", "~string | int", "~int | ~string"},
127 {"~int | string | β
", "~string | int", "~int | ~string"},
128 {"~int | myInt | β
", "~string | int", "~int | ~string"},
129 {"~int | string | π€", "~string | int", "π€"},
130 {"~int | string | myInt", "~string | int", "~int | ~string"},
131 } {
132 xl := maketl(test.xl)
133 yl := maketl(test.yl)
134 got := xl.union(yl).String()
135 if got != test.want {
136 t.Errorf("(%v).union(%v) = %v; want %v", test.xl, test.yl, got, test.want)
137 }
138 }
139 }
140
141 func TestTermlistIntersect(t *testing.T) {
142 for _, test := range []struct {
143 xl, yl, want string
144 }{
145
146 {"β
", "β
", "β
"},
147 {"β
", "π€", "β
"},
148 {"β
", "int", "β
"},
149 {"β
", "myInt", "β
"},
150 {"π€", "~int", "~int"},
151 {"π€", "myInt", "myInt"},
152 {"int", "~int", "int"},
153 {"int", "string", "β
"},
154 {"int", "myInt", "β
"},
155 {"~int", "myInt", "myInt"},
156 {"int | string", "~string", "string"},
157 {"~int | string", "~string | int", "int | string"},
158 {"~int | string | β
", "~string | int", "int | string"},
159 {"~int | myInt | β
", "~string | int", "int"},
160 {"~int | string | π€", "~string | int", "int | ~string"},
161 {"~int | string | myInt", "~string | int", "int | string"},
162 } {
163 xl := maketl(test.xl)
164 yl := maketl(test.yl)
165 got := xl.intersect(yl).String()
166 if got != test.want {
167 t.Errorf("(%v).intersect(%v) = %v; want %v", test.xl, test.yl, got, test.want)
168 }
169 }
170 }
171
172 func TestTermlistEqual(t *testing.T) {
173 for _, test := range []struct {
174 xl, yl string
175 want bool
176 }{
177 {"β
", "β
", true},
178 {"β
", "π€", false},
179 {"π€", "π€", true},
180 {"π€ | int", "π€", true},
181 {"π€ | int", "string | π€", true},
182 {"π€ | myInt", "string | π€", true},
183 {"int | ~string", "string | int", false},
184 {"~int | string", "string | myInt", false},
185 {"int | ~string | β
", "string | int | ~string", true},
186 } {
187 xl := maketl(test.xl)
188 yl := maketl(test.yl)
189 got := xl.equal(yl)
190 if got != test.want {
191 t.Errorf("(%v).equal(%v) = %v; want %v", test.xl, test.yl, got, test.want)
192 }
193 }
194 }
195
196 func TestTermlistIncludes(t *testing.T) {
197 for _, test := range []struct {
198 xl, typ string
199 want bool
200 }{
201 {"β
", "int", false},
202 {"π€", "int", true},
203 {"~int", "int", true},
204 {"int", "string", false},
205 {"~int", "string", false},
206 {"~int", "myInt", true},
207 {"int | string", "string", true},
208 {"~int | string", "int", true},
209 {"~int | string", "myInt", true},
210 {"~int | myInt | β
", "myInt", true},
211 {"myInt | β
| π€", "int", true},
212 } {
213 xl := maketl(test.xl)
214 yl := testTerm(test.typ).typ
215 got := xl.includes(yl)
216 if got != test.want {
217 t.Errorf("(%v).includes(%v) = %v; want %v", test.xl, yl, got, test.want)
218 }
219 }
220 }
221
222 func TestTermlistSupersetOf(t *testing.T) {
223 for _, test := range []struct {
224 xl, typ string
225 want bool
226 }{
227 {"β
", "β
", true},
228 {"β
", "π€", false},
229 {"β
", "int", false},
230 {"π€", "β
", true},
231 {"π€", "π€", true},
232 {"π€", "int", true},
233 {"π€", "~int", true},
234 {"π€", "myInt", true},
235 {"~int", "int", true},
236 {"~int", "~int", true},
237 {"~int", "myInt", true},
238 {"int", "~int", false},
239 {"myInt", "~int", false},
240 {"int", "string", false},
241 {"~int", "string", false},
242 {"int | string", "string", true},
243 {"int | string", "~string", false},
244 {"~int | string", "int", true},
245 {"~int | string", "myInt", true},
246 {"~int | string | β
", "string", true},
247 {"~string | β
| π€", "myInt", true},
248 } {
249 xl := maketl(test.xl)
250 y := testTerm(test.typ)
251 got := xl.supersetOf(y)
252 if got != test.want {
253 t.Errorf("(%v).supersetOf(%v) = %v; want %v", test.xl, y, got, test.want)
254 }
255 }
256 }
257
258 func TestTermlistSubsetOf(t *testing.T) {
259 for _, test := range []struct {
260 xl, yl string
261 want bool
262 }{
263 {"β
", "β
", true},
264 {"β
", "π€", true},
265 {"π€", "β
", false},
266 {"π€", "π€", true},
267 {"int", "int | string", true},
268 {"~int", "int | string", false},
269 {"~int", "myInt | string", false},
270 {"myInt", "~int | string", true},
271 {"~int", "string | string | int | ~int", true},
272 {"myInt", "string | string | ~int", true},
273 {"int | string", "string", false},
274 {"int | string", "string | int", true},
275 {"int | ~string", "string | int", false},
276 {"myInt | ~string", "string | int | π€", true},
277 {"int | ~string", "string | int | β
| string", false},
278 {"int | myInt", "string | ~int | β
| string", true},
279 } {
280 xl := maketl(test.xl)
281 yl := maketl(test.yl)
282 got := xl.subsetOf(yl)
283 if got != test.want {
284 t.Errorf("(%v).subsetOf(%v) = %v; want %v", test.xl, test.yl, got, test.want)
285 }
286 }
287 }
288
View as plain text