Debian Codegraph @

common/types/unknown_test.go

raw symbols SCIP
1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package types
16
17 import (
18 "fmt"
19 "math"
20 "reflect"
21 "strings"
22 "testing"
23
24 "github.com/google/cel-go/common/types/ref"
25 )
26
27 func TestIsUnknown(t *testing.T) {
28 if IsUnknown(&Unknown{}) != true {
29 t.Error("IsUnknown(Unknown{}) returned false, wanted true")
30 }
31 if IsUnknown(Bool(true)) != false {
32 t.Error("IsUnknown(true) returned true, wanted false")
33 }
34 }
35
36 func TestNewAttribute(t *testing.T) {
37 if NewAttributeTrail("") != unspecifiedAttribute {
38 t.Error("An empty attribute must be the unspecified attribute")
39 }
40 if NewAttributeTrail("v").Equal(unspecifiedAttribute) {
41 t.Error("A non-empty attribute must not be equal to the unspecified attribute")
42 }
43 }
44
45 func TestAttributeEquals(t *testing.T) {
46 tests := []struct {
47 a *AttributeTrail
48 b *AttributeTrail
49 equal bool
50 }{
51 {
52 a: unspecifiedAttribute,
53 b: NewAttributeTrail(""),
54 equal: true,
55 },
56 {
57 a: NewAttributeTrail("a"),
58 b: NewAttributeTrail(""),
59 equal: false,
60 },
61 {
62 a: NewAttributeTrail("a"),
63 b: NewAttributeTrail("a"),
64 equal: true,
65 },
66 {
67 a: QualifyAttribute[string](NewAttributeTrail("a"), "b"),
68 b: NewAttributeTrail("a"),
69 equal: false,
70 },
71 {
72 a: QualifyAttribute[string](NewAttributeTrail("a"), "b"),
73 b: QualifyAttribute[int64](NewAttributeTrail("a"), 1),
74 equal: false,
75 },
76 {
77 a: QualifyAttribute[int64](NewAttributeTrail("a"), 1),
78 b: QualifyAttribute[string](NewAttributeTrail("a"), "1"),
79 equal: false,
80 },
81 {
82 a: QualifyAttribute[uint64](NewAttributeTrail("a"), 1),
83 b: QualifyAttribute[string](NewAttributeTrail("a"), "1"),
84 equal: false,
85 },
86 {
87 a: QualifyAttribute[string](NewAttributeTrail("a"), "b"),
88 b: QualifyAttribute[string](NewAttributeTrail("a"), "b"),
89 equal: true,
90 },
91 {
92 a: QualifyAttribute[int64](NewAttributeTrail("a"), 20),
93 b: QualifyAttribute[uint64](NewAttributeTrail("a"), 20),
94 equal: true,
95 },
96 {
97 a: QualifyAttribute[uint64](NewAttributeTrail("a"), 20),
98 b: QualifyAttribute[int64](NewAttributeTrail("a"), 20),
99 equal: true,
100 },
101 {
102 a: QualifyAttribute[uint64](NewAttributeTrail("a"), 21),
103 b: QualifyAttribute[int64](NewAttributeTrail("a"), 20),
104 equal: false,
105 },
106 {
107 a: QualifyAttribute[int64](NewAttributeTrail("a"), 20),
108 b: QualifyAttribute[uint64](NewAttributeTrail("a"), 21),
109 equal: false,
110 },
111 {
112 a: QualifyAttribute[int64](NewAttributeTrail("a"), -1),
113 b: QualifyAttribute[uint64](NewAttributeTrail("a"), 0),
114 equal: false,
115 },
116 {
117 a: QualifyAttribute[int64](NewAttributeTrail("a"), 1),
118 b: QualifyAttribute[uint64](NewAttributeTrail("a"), math.MaxInt64+1),
119 equal: false,
120 },
121 }
122 for i, tst := range tests {
123 tc := tst
124 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
125 out := tc.a.Equal(tc.b)
126 if out != tc.equal {
127 t.Errorf("%v.Equal(%v) got %v, wanted %v", tc.a, tc.b, out, tc.equal)
128 }
129 })
130 }
131 }
132
133 func TestAttributeString(t *testing.T) {
134 tests := []struct {
135 attr *AttributeTrail
136 out string
137 }{
138 {
139 attr: unspecifiedAttribute,
140 out: "<unspecified>",
141 },
142 {
143 attr: NewAttributeTrail("a"),
144 out: "a",
145 },
146 {
147 attr: QualifyAttribute[bool](NewAttributeTrail("a"), false),
148 out: "a[false]",
149 },
150 {
151 attr: QualifyAttribute[string](NewAttributeTrail("a"), "b"),
152 out: "a.b",
153 },
154 {
155 attr: QualifyAttribute(QualifyAttribute[string](NewAttributeTrail("a"), "b"), "$this"),
156 out: `a.b["$this"]`,
157 },
158 {
159 attr: QualifyAttribute[int64](NewAttributeTrail("a"), 12),
160 out: "a[12]",
161 },
162 {
163 attr: QualifyAttribute[uint64](NewAttributeTrail("a"), 24),
164 out: "a[24u]",
165 },
166 }
167 for i, tst := range tests {
168 tc := tst
169 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
170 out := tc.attr.String()
171 if out != tc.out {
172 t.Errorf("%v.String() got %v, wanted %v", tc.attr, out, tc.out)
173 }
174 })
175 }
176 }
177
178 func TestUnknownContains(t *testing.T) {
179 tests := []struct {
180 unk *Unknown
181 other *Unknown
182 out bool
183 }{
184 {
185 unk: NewUnknown(1, nil),
186 other: NewUnknown(1, unspecifiedAttribute),
187 out: true,
188 },
189 {
190 unk: NewUnknown(3, QualifyAttribute[bool](NewAttributeTrail("a"), true)),
191 other: NewUnknown(4, QualifyAttribute[string](NewAttributeTrail("a"), "b")),
192 out: false,
193 },
194 {
195 unk: NewUnknown(3, QualifyAttribute[string](NewAttributeTrail("a"), "b")),
196 other: NewUnknown(4, QualifyAttribute[string](NewAttributeTrail("a"), "b")),
197 out: false,
198 },
199 {
200 unk: NewUnknown(3, QualifyAttribute[string](NewAttributeTrail("a"), "c")),
201 other: NewUnknown(3, QualifyAttribute[string](NewAttributeTrail("a"), "b")),
202 out: false,
203 },
204 {
205 unk: MergeUnknowns(
206 NewUnknown(3, QualifyAttribute[bool](NewAttributeTrail("a"), true)),
207 NewUnknown(4, QualifyAttribute[string](NewAttributeTrail("a"), "b")),
208 ),
209 other: NewUnknown(3, QualifyAttribute[bool](NewAttributeTrail("a"), true)),
210 out: true,
211 },
212 {
213 unk: NewUnknown(3, QualifyAttribute[bool](NewAttributeTrail("a"), true)),
214 other: MergeUnknowns(
215 NewUnknown(3, QualifyAttribute[bool](NewAttributeTrail("a"), true)),
216 NewUnknown(4, QualifyAttribute[string](NewAttributeTrail("a"), "b")),
217 ),
218 out: false,
219 },
220 }
221 for i, tst := range tests {
222 tc := tst
223 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
224 out := tc.unk.Contains(tc.other)
225 if out != tc.out {
226 t.Errorf("%v.Contains(%v) got %v, wanted %v", tc.unk, tc.other, out, tc.out)
227 }
228 })
229 }
230 }
231
232 func TestUnknownIDs(t *testing.T) {
233 tests := []struct {
234 unk *Unknown
235 ids []int64
236 attrs []string
237 }{
238 {
239 unk: NewUnknown(1, nil),
240 ids: []int64{1},
241 attrs: []string{"<unspecified>"},
242 },
243 {
244 unk: NewUnknown(2, QualifyAttribute[bool](NewAttributeTrail("a"), true)),
245 ids: []int64{2},
246 attrs: []string{"a[true]"},
247 },
248 {
249 unk: NewUnknown(3, QualifyAttribute[string](NewAttributeTrail("a"), "b")),
250 ids: []int64{3},
251 attrs: []string{"a.b"},
252 },
253 {
254 unk: NewUnknown(4, QualifyAttribute[string](NewAttributeTrail("a"), "c")),
255 ids: []int64{4},
256 attrs: []string{"a.c"},
257 },
258 {
259 unk: MergeUnknowns(
260 NewUnknown(4, QualifyAttribute[string](NewAttributeTrail("a"), "b")),
261 NewUnknown(3, QualifyAttribute[bool](NewAttributeTrail("a"), true)),
262 ),
263 ids: []int64{3, 4},
264 attrs: []string{"a[true]", "a.b"},
265 },
266 }
267 for i, tst := range tests {
268 tc := tst
269 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
270 ids := tc.unk.IDs()
271 if !reflect.DeepEqual(ids, tc.ids) {
272 t.Errorf("%v.IDs() got %v, wanted %v", tc.unk, ids, tc.ids)
273 }
274 attrs := make([]string, len(ids))
275 idx := 0
276 for _, id := range ids {
277 trails, found := tc.unk.GetAttributeTrails(id)
278 if !found {
279 t.Fatalf("GetAttributeTrails(%d) not found", id)
280 }
281 if len(trails) != 1 {
282 t.Fatalf("GetAttributeTrails(%d) got %d trails, wanted 1", id, len(trails))
283 }
284 attrs[idx] = trails[0].String()
285 idx++
286 }
287 if !reflect.DeepEqual(attrs, tc.attrs) {
288 t.Errorf("%v.GetAttributeTrails() got %v, wanted %v", tc.unk, attrs, tc.attrs)
289 }
290 })
291 }
292 }
293
294 func TestUnknownString(t *testing.T) {
295 tests := []struct {
296 unk *Unknown
297 out any
298 }{
299 {
300 unk: NewUnknown(1, nil),
301 out: "<unspecified> (1)",
302 },
303 {
304 unk: NewUnknown(1, unspecifiedAttribute),
305 out: "<unspecified> (1)",
306 },
307 {
308 unk: NewUnknown(2, NewAttributeTrail("a")),
309 out: "a (2)",
310 },
311 {
312 unk: NewUnknown(3, QualifyAttribute[bool](NewAttributeTrail("a"), false)),
313 out: "a[false] (3)",
314 },
315 {
316 unk: MergeUnknowns(
317 NewUnknown(3, QualifyAttribute[bool](NewAttributeTrail("a"), true)),
318 NewUnknown(4, QualifyAttribute[string](NewAttributeTrail("a"), "b")),
319 ),
320 out: []string{"a[true] (3)", "a.b (4)"},
321 },
322 {
323 // this case might occur in a logical condition where the attributes are equal.
324 unk: MergeUnknowns(
325 NewUnknown(3, QualifyAttribute[int64](NewAttributeTrail("a"), 0)),
326 NewUnknown(3, QualifyAttribute[int64](NewAttributeTrail("a"), 0)),
327 ),
328 out: "a[0] (3)",
329 },
330 {
331 // this case might occur if attribute tracking through comprehensions is supported
332 unk: MergeUnknowns(
333 NewUnknown(3, QualifyAttribute[int64](NewAttributeTrail("a"), 0)),
334 NewUnknown(3, QualifyAttribute[int64](NewAttributeTrail("a"), 1)),
335 ),
336 out: "[a[0] a[1]] (3)",
337 },
338 }
339 for i, tst := range tests {
340 tc := tst
341 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
342 out := tc.unk.String()
343 switch want := tc.out.(type) {
344 case string:
345 if out != want {
346 t.Errorf("%v.String() got %v, wanted %v", tc.unk, out, want)
347 }
348 case []string:
349 for _, w := range want {
350 if !strings.Contains(out, w) {
351 t.Errorf("%v.String() got %v, wanted it to contain %v", tc.unk, out, w)
352 }
353 }
354 }
355
356 })
357 }
358 }
359
360 func TestMaybeMergeUnknowns(t *testing.T) {
361 tests := []struct {
362 in ref.Val
363 unk *Unknown
364 want *Unknown
365 isUnk bool
366 }{
367 // one of the unknowns is empty
368 {
369 in: String(""),
370 unk: nil,
371 isUnk: false,
372 },
373 // both unknowns are empty
374 {
375 in: String(""),
376 unk: &Unknown{},
377 want: &Unknown{},
378 isUnk: true,
379 },
380 {
381 in: newUnk(t, 2, "x"),
382 unk: newUnk(t, 1, "y"),
383 want: MergeUnknowns(newUnk(t, 2, "x"), newUnk(t, 1, "y")),
384 isUnk: true,
385 },
386 {
387 in: newUnk(t, 2, "x"),
388 want: newUnk(t, 2, "x"),
389 isUnk: true,
390 },
391 }
392
393 for i, tst := range tests {
394 tc := tst
395 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
396 got, isUnk := MaybeMergeUnknowns(tc.in, tc.unk)
397 if isUnk != tc.isUnk {
398 t.Errorf("MaybeMergeUnknowns(%v, %v) got %v, wanted %v", tc.in, tc.unk, got, tc.want)
399 }
400 })
401 }
402 }
403
404 func newUnk(t *testing.T, id int64, varName string) *Unknown {
405 t.Helper()
406 return NewUnknown(id, NewAttributeTrail(varName))
407 }