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