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