| 1 | // Copyright 2018 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 | "encoding/json" |
| 19 | "fmt" |
| 20 | "math" |
| 21 | "reflect" |
| 22 | "testing" |
| 23 | |
| 24 | "google.golang.org/protobuf/encoding/protojson" |
| 25 | "google.golang.org/protobuf/proto" |
| 26 | |
| 27 | "github.com/google/cel-go/common/types/ref" |
| 28 | "github.com/google/cel-go/common/types/traits" |
| 29 | |
| 30 | anypb "google.golang.org/protobuf/types/known/anypb" |
| 31 | dpb "google.golang.org/protobuf/types/known/durationpb" |
| 32 | structpb "google.golang.org/protobuf/types/known/structpb" |
| 33 | ) |
| 34 | |
| 35 | func TestBaseListAdd_Empty(t *testing.T) { |
| 36 | reg := newTestRegistry(t) |
| 37 | list := NewDynamicList(reg, []bool{true}) |
| 38 | if list.Add(NewDynamicList(reg, []bool{})) != list { |
| 39 | t.Error("Adding an empty list created new list.") |
| 40 | } |
| 41 | if NewDynamicList(reg, []string{}).Add(list) != list { |
| 42 | t.Error("Adding list to empty created a new list.") |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestBaseListAdd_Error(t *testing.T) { |
| 47 | if !IsError(NewDynamicList(newTestRegistry(t), []bool{}).Add(String("error"))) { |
| 48 | t.Error("Addind a non-list value to a list unexpected succeeds.") |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestBaseListContains(t *testing.T) { |
| 53 | list := NewDynamicList(newTestRegistry(t), []float32{1.0, 2.0, 3.0}) |
| 54 | tests := []struct { |
| 55 | in ref.Val |
| 56 | out ref.Val |
| 57 | }{ |
| 58 | { |
| 59 | in: Double(math.NaN()), |
| 60 | out: False, |
| 61 | }, |
| 62 | { |
| 63 | in: Double(5), |
| 64 | out: False, |
| 65 | }, |
| 66 | { |
| 67 | in: Double(3), |
| 68 | out: True, |
| 69 | }, |
| 70 | { |
| 71 | in: Uint(3), |
| 72 | out: True, |
| 73 | }, |
| 74 | { |
| 75 | in: Int(3), |
| 76 | out: True, |
| 77 | }, |
| 78 | { |
| 79 | in: Int(3), |
| 80 | out: True, |
| 81 | }, |
| 82 | { |
| 83 | in: Int(0), |
| 84 | out: False, |
| 85 | }, |
| 86 | { |
| 87 | in: String("3"), |
| 88 | out: False, |
| 89 | }, |
| 90 | } |
| 91 | for _, tc := range tests { |
| 92 | got := list.Contains(tc.in) |
| 93 | if !reflect.DeepEqual(got, tc.out) { |
| 94 | t.Errorf("list.Contains(%v) returned %v, wanted %v", tc.in, got, tc.out) |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestBaseListConvertToNative(t *testing.T) { |
| 100 | list := NewDynamicList(newTestRegistry(t), []float64{1.0, 2.0}) |
| 101 | if protoList, err := list.ConvertToNative(reflect.TypeOf([]float32{})); err != nil { |
| 102 | t.Error(err) |
| 103 | } else if !reflect.DeepEqual(protoList, []float32{1.0, 2.0}) { |
| 104 | t.Errorf("Could not convert to []float32: %v", protoList) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestBaseListConvertToNative_Any(t *testing.T) { |
| 109 | list := NewDynamicList(newTestRegistry(t), []float64{1.0, 2.0}) |
| 110 | val, err := list.ConvertToNative(anyValueType) |
| 111 | if err != nil { |
| 112 | t.Error(err) |
| 113 | } |
| 114 | jsonVal := &structpb.ListValue{} |
| 115 | err = protojson.Unmarshal([]byte("[1.0, 2.0]"), jsonVal) |
| 116 | if err != nil { |
| 117 | t.Fatalf("protojson.Unmarshal() failed: %v", err) |
| 118 | } |
| 119 | want, err := anypb.New(jsonVal) |
| 120 | if err != nil { |
| 121 | t.Fatalf("anypb.New() failed: %v", err) |
| 122 | } |
| 123 | if !proto.Equal(val.(proto.Message), want) { |
| 124 | t.Errorf("Got %v, wanted %v", val, want) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func TestBaseListConvertToNative_Json(t *testing.T) { |
| 129 | list := NewDynamicList(newTestRegistry(t), []float64{1.0, 2.0}) |
| 130 | val, err := list.ConvertToNative(JSONListType) |
| 131 | if err != nil { |
| 132 | t.Error(err) |
| 133 | } |
| 134 | want := &structpb.ListValue{} |
| 135 | err = protojson.Unmarshal([]byte("[1.0, 2.0]"), want) |
| 136 | if err != nil { |
| 137 | t.Fatalf("protojson.Unmarshal() failed: %v", err) |
| 138 | } |
| 139 | if !proto.Equal(val.(proto.Message), want) { |
| 140 | t.Errorf("Got %v, wanted %v", val, want) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func TestBaseListConvertToType(t *testing.T) { |
| 145 | list := NewDynamicList(newTestRegistry(t), []string{"h", "e", "l", "l", "o"}) |
| 146 | if list.ConvertToType(ListType) != list { |
| 147 | t.Error("List was not convertible to itself.") |
| 148 | } |
| 149 | if list.ConvertToType(TypeType) != ListType { |
| 150 | t.Error("Unable to obtain the proper type from the list.") |
| 151 | } |
| 152 | if !IsError(list.ConvertToType(MapType)) { |
| 153 | t.Error("List was able to convert to unexpected type.") |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | func TestBaseListEqual(t *testing.T) { |
| 158 | reg := newTestRegistry(t) |
| 159 | listA := NewDynamicList(reg, []string{"h", "e", "l", "l", "o"}) |
| 160 | if listA.Equal(listA) != True { |
| 161 | t.Error("listA.Equal(listA) did not return true.") |
| 162 | } |
| 163 | listB := NewDynamicList(reg, []string{"h", "e", "l", "p", "!"}) |
| 164 | if listA.Equal(listB) != False { |
| 165 | t.Error("listA.Equal(listB) did not return false.") |
| 166 | } |
| 167 | listC := reg.NativeToValue([]any{"h", "e", "l", "l", String("o")}) |
| 168 | if listA.Equal(listC) != True { |
| 169 | t.Error("listA.Equal(listC) did not return true.") |
| 170 | } |
| 171 | listD := reg.NativeToValue([]any{"h", "e", 1, "p", "!"}) |
| 172 | if listA.Equal(listD) != False { |
| 173 | t.Error("listA.Equal(listD) did not return true") |
| 174 | } |
| 175 | if IsError(listB.Equal(listD)) { |
| 176 | t.Error("listA.Equal(listD) errored, wanted 'false'") |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | func TestBaseListGet(t *testing.T) { |
| 181 | validateList123(t, NewDynamicList(newTestRegistry(t), []int32{1, 2, 3})) |
| 182 | } |
| 183 | |
| 184 | func TestBaseListString(t *testing.T) { |
| 185 | l := DefaultTypeAdapter.NativeToValue([]any{1, "hello", 2.1, true, []string{"world"}}) |
| 186 | want := `[1, hello, 2.1, true, [world]]` |
| 187 | if fmt.Sprintf("%v", l) != want { |
| 188 | t.Errorf("l.String() got %v, wanted %v", l, want) |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | func TestConcatListString(t *testing.T) { |
| 193 | l := DefaultTypeAdapter.NativeToValue([]any{1, "hello", 2.1, true}).(traits.Lister) |
| 194 | c := l.Add(DefaultTypeAdapter.NativeToValue([]string{"world"})) |
| 195 | want := `[1, hello, 2.1, true, world]` |
| 196 | if fmt.Sprintf("%v", c) != want { |
| 197 | t.Errorf("c.String() got %v, wanted %v", c, want) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | func TestListIsZeroValue(t *testing.T) { |
| 202 | tests := []struct { |
| 203 | val any |
| 204 | isZeroValue bool |
| 205 | }{ |
| 206 | { |
| 207 | val: []string{}, |
| 208 | isZeroValue: true, |
| 209 | }, |
| 210 | { |
| 211 | val: []int{}, |
| 212 | isZeroValue: true, |
| 213 | }, |
| 214 | { |
| 215 | val: []any{}, |
| 216 | isZeroValue: true, |
| 217 | }, |
| 218 | { |
| 219 | val: &structpb.ListValue{}, |
| 220 | isZeroValue: true, |
| 221 | }, |
| 222 | { |
| 223 | val: []ref.Val{}, |
| 224 | isZeroValue: true, |
| 225 | }, |
| 226 | { |
| 227 | val: DefaultTypeAdapter.NativeToValue([]ref.Val{}).(traits.Lister).Add(DefaultTypeAdapter.NativeToValue([]ref.Val{})), |
| 228 | isZeroValue: true, |
| 229 | }, |
| 230 | { |
| 231 | val: []string{""}, |
| 232 | isZeroValue: false, |
| 233 | }, |
| 234 | { |
| 235 | val: []bool{false}, |
| 236 | isZeroValue: false, |
| 237 | }, |
| 238 | { |
| 239 | val: []any{0}, |
| 240 | isZeroValue: false, |
| 241 | }, |
| 242 | { |
| 243 | val: &structpb.ListValue{Values: []*structpb.Value{structpb.NewBoolValue(false)}}, |
| 244 | isZeroValue: false, |
| 245 | }, |
| 246 | { |
| 247 | val: []ref.Val{Double(0.0)}, |
| 248 | isZeroValue: false, |
| 249 | }, |
| 250 | { |
| 251 | val: DefaultTypeAdapter.NativeToValue([]ref.Val{IntOne}).(traits.Lister).Add(DefaultTypeAdapter.NativeToValue([]ref.Val{})), |
| 252 | isZeroValue: false, |
| 253 | }, |
| 254 | } |
| 255 | for i, tst := range tests { |
| 256 | tc := tst |
| 257 | t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 258 | v := DefaultTypeAdapter.NativeToValue(tc.val) |
| 259 | zv, ok := v.(traits.Zeroer) |
| 260 | if !ok { |
| 261 | t.Fatalf("%v could not be converted to a zero-valuer type", tc.val) |
| 262 | } |
| 263 | if zv.IsZeroValue() != tc.isZeroValue { |
| 264 | t.Errorf("%v.IsZeroValue() got %t, wanted %t", v, zv.IsZeroValue(), tc.isZeroValue) |
| 265 | } |
| 266 | }) |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | func TestValueListGet(t *testing.T) { |
| 271 | validateList123(t, NewRefValList(newTestRegistry(t), []ref.Val{Int(1), Int(2), Int(3)})) |
| 272 | } |
| 273 | |
| 274 | func TestBaseListIterator(t *testing.T) { |
| 275 | validateIterator123(t, NewDynamicList(newTestRegistry(t), []int32{1, 2, 3})) |
| 276 | } |
| 277 | |
| 278 | func TestValueListValue_Iterator(t *testing.T) { |
| 279 | validateIterator123(t, NewRefValList(newTestRegistry(t), []ref.Val{Int(1), Int(2), Int(3)})) |
| 280 | } |
| 281 | |
| 282 | func TestBaseListNestedList(t *testing.T) { |
| 283 | reg := newTestRegistry(t) |
| 284 | listUint32 := []uint32{1, 2} |
| 285 | nestedUint32 := NewDynamicList(reg, []any{listUint32}) |
| 286 | listUint64 := []uint64{1, 2} |
| 287 | nestedUint64 := NewDynamicList(reg, []any{listUint64}) |
| 288 | if nestedUint32.Equal(nestedUint64) != True { |
| 289 | t.Error("Could not find nested list") |
| 290 | } |
| 291 | if nestedUint32.Contains(NewDynamicList(reg, listUint64)) != True || |
| 292 | nestedUint64.Contains(NewDynamicList(reg, listUint32)) != True { |
| 293 | t.Error("Could not find type compatible nested lists") |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | func TestBaseListSize(t *testing.T) { |
| 298 | reg := newTestRegistry(t) |
| 299 | listUint32 := []uint32{1, 2} |
| 300 | nestedUint32 := NewDynamicList(reg, []any{listUint32}) |
| 301 | if nestedUint32.Size() != IntOne { |
| 302 | t.Error("List indicates the incorrect size.") |
| 303 | } |
| 304 | if nestedUint32.Get(IntZero).(traits.Sizer).Size() != Int(2) { |
| 305 | t.Error("Nested list indicates the incorrect size.") |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | func TestMutableListGet(t *testing.T) { |
| 310 | reg := newTestRegistry(t) |
| 311 | listA := NewMutableList(reg) |
| 312 | listB := NewStringList(reg, []string{"item"}) |
| 313 | listA = listA.Add(listB).(*mutableList) |
| 314 | |
| 315 | itemVal := listA.Get(Int(0)) |
| 316 | |
| 317 | if itemVal.Value().(string) != "item" { |
| 318 | t.Error("MutableList get returned invalid item.") |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | func TestConcatListAdd(t *testing.T) { |
| 323 | reg := newTestRegistry(t) |
| 324 | listA := NewDynamicList(reg, []float32{1.0, 2.0}) |
| 325 | listB := NewStringList(reg, []string{"3"}) |
| 326 | list := listA.Add(listB).(traits.Lister).Add(listA). |
| 327 | Value().([]any) |
| 328 | expected := []any{ |
| 329 | float64(1.0), |
| 330 | float64(2.0), |
| 331 | string("3"), |
| 332 | float64(1.0), |
| 333 | float64(2.0)} |
| 334 | if len(list) != len(expected) { |
| 335 | t.Errorf("Got '%v', expected '%v'", list, expected) |
| 336 | } else { |
| 337 | for i := 0; i < len(list); i++ { |
| 338 | if expected[i] != list[i] { |
| 339 | t.Errorf("elem[%d] Got '%v', expected '%v'", |
| 340 | i, list[i], expected[i]) |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | // Zero length input list |
| 345 | listConcat := listA.Add(listB).(traits.Lister) |
| 346 | same := listConcat.Add(NewStringList(reg, []string{})) |
| 347 | if !reflect.DeepEqual(listConcat, same) { |
| 348 | t.Error("Adding an empty list to a concat list did not return the concat list") |
| 349 | } |
| 350 | // Zero length operand list |
| 351 | same = NewDynamicList(reg, []bool{}).Add(listConcat) |
| 352 | if !reflect.DeepEqual(listConcat, same) { |
| 353 | t.Error("Adding a concat list to an empty list did not return the concat list") |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | func TestConcatListConvertToNative_Json(t *testing.T) { |
| 358 | reg := newTestRegistry(t) |
| 359 | listA := NewDynamicList(reg, []float32{1.0, 2.0}) |
| 360 | listB := NewDynamicList(reg, []string{"3"}) |
| 361 | list := listA.Add(listB) |
| 362 | jsonVal, err := list.ConvertToNative(JSONValueType) |
| 363 | if err != nil { |
| 364 | t.Fatalf("Got error '%v', expected value", err) |
| 365 | } |
| 366 | jsonBytes, err := protojson.Marshal(jsonVal.(proto.Message)) |
| 367 | if err != nil { |
| 368 | t.Fatalf("protojson.Marshal(%v) failed: %v", jsonVal, err) |
| 369 | } |
| 370 | jsonTxt := string(jsonBytes) |
| 371 | outList := []any{} |
| 372 | err = json.Unmarshal(jsonBytes, &outList) |
| 373 | if err != nil { |
| 374 | t.Fatalf("json.Unmarshal(%q) failed: %v", jsonTxt, err) |
| 375 | } |
| 376 | if !reflect.DeepEqual(outList, []any{1.0, 2.0, "3"}) { |
| 377 | t.Errorf("got json '%v', expected %v", outList, []any{1.0, 2.0, "3"}) |
| 378 | } |
| 379 | // Test proto3 to JSON conversion. |
| 380 | listC := NewDynamicList(reg, []*dpb.Duration{{Seconds: 100}}) |
| 381 | listConcat := listA.Add(listC) |
| 382 | jsonVal, err = listConcat.ConvertToNative(JSONValueType) |
| 383 | if err != nil { |
| 384 | t.Fatal(err) |
| 385 | } |
| 386 | jsonBytes, err = protojson.Marshal(jsonVal.(proto.Message)) |
| 387 | if err != nil { |
| 388 | t.Fatalf("protojson.Marshal(%v) failed: %v", jsonVal, err) |
| 389 | } |
| 390 | jsonTxt = string(jsonBytes) |
| 391 | outList = []any{} |
| 392 | err = json.Unmarshal(jsonBytes, &outList) |
| 393 | if err != nil { |
| 394 | t.Fatalf("json.Unmarshal(%q) failed: %v", jsonTxt, err) |
| 395 | } |
| 396 | if !reflect.DeepEqual(outList, []any{1.0, 2.0, "100s"}) { |
| 397 | t.Errorf("got json '%v', expected %v", outList, []any{1.0, 2.0, "100s"}) |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | func TestConcatListConvertToNativeListInterface(t *testing.T) { |
| 402 | reg := newTestRegistry(t) |
| 403 | listA := NewDynamicList(reg, []float32{1.0, 2.0}) |
| 404 | listB := NewStringList(reg, []string{"3.0"}) |
| 405 | list := listA.Add(listB) |
| 406 | iface, err := list.ConvertToNative(reflect.TypeOf([]any{})) |
| 407 | if err != nil { |
| 408 | t.Errorf("Got '%v', expected '%v'", err, list) |
| 409 | } |
| 410 | want := []any{1.0, 2.0, "3.0"} |
| 411 | if !reflect.DeepEqual(iface, want) { |
| 412 | t.Errorf("Got '%v', expected '%v'", iface, want) |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | func TestConcatListConvertToType(t *testing.T) { |
| 417 | reg := newTestRegistry(t) |
| 418 | listA := NewDynamicList(reg, []float32{1.0, 2.0}) |
| 419 | listB := NewDynamicList(reg, []*dpb.Duration{{Seconds: 100}}) |
| 420 | list := listA.Add(listB) |
| 421 | if list.ConvertToType(ListType) != list { |
| 422 | t.Error("List conversion to list failed.") |
| 423 | } |
| 424 | if list.ConvertToType(TypeType) != ListType { |
| 425 | t.Error("List conversion to type failed.") |
| 426 | } |
| 427 | if !IsError(list.ConvertToType(MapType)) { |
| 428 | t.Error("List conversion to map unexpectedly succeeded.") |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | func TestConcatListContains(t *testing.T) { |
| 433 | reg := newTestRegistry(t) |
| 434 | listA := NewDynamicList(reg, []float32{1.0, 2.0}) |
| 435 | listB := NewDynamicList(reg, []string{"3"}) |
| 436 | listConcat := listA.Add(listB).(traits.Lister) |
| 437 | if listConcat.Contains(String("3")) != True { |
| 438 | t.Error("Concatenated list did not contain value in 'next' list.") |
| 439 | } |
| 440 | if listConcat.Contains(Double(2.0)) != True { |
| 441 | t.Error("Concatenated list did not contain value in 'prev' list.") |
| 442 | } |
| 443 | homogList := NewDynamicList(reg, []string{"3"}).Add( |
| 444 | NewStringList(reg, []string{"2", "1"})).(traits.Lister) |
| 445 | if homogList.Contains(String("4")) != False { |
| 446 | t.Error("Concatenated homogeneous list did not return false.") |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | func TestConcatListContainsNonBool(t *testing.T) { |
| 451 | reg := newTestRegistry(t) |
| 452 | listA := NewDynamicList(reg, []float32{1.0, 2.0}) |
| 453 | listB := NewDynamicList(reg, []string{"3"}) |
| 454 | listConcat := listA.Add(listB).(traits.Lister) |
| 455 | if IsError(listConcat.Contains(String("4"))) { |
| 456 | t.Error("Contains errored with a not-found element, wanted 'false'") |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | func TestConcatListEqual(t *testing.T) { |
| 461 | reg := newTestRegistry(t) |
| 462 | listA := NewDynamicList(reg, []float32{1.0, 2.0}) |
| 463 | listB := NewDynamicList(reg, []float64{3.0}) |
| 464 | list := listA.Add(listB) |
| 465 | // Note the internal type of list raw and concat list are slightly different. |
| 466 | listRaw := NewDynamicList(reg, []any{float32(1.0), float64(2.0), float64(3.0)}) |
| 467 | if listRaw.Equal(list) != True || list.Equal(listRaw) != True { |
| 468 | t.Errorf("listRaw.Equal(list) not true, got '%v', expected '%v'", list.Value(), listRaw.Value()) |
| 469 | } |
| 470 | if list.Equal(listA) == True || listRaw.Equal(listA) == True { |
| 471 | t.Error("lists of unequal length considered equal") |
| 472 | } |
| 473 | listC := reg.NativeToValue([]any{1.0, 3.0, 2.0}) |
| 474 | if list.Equal(listC) != False { |
| 475 | t.Errorf("list.Equal(listC) got %v, wanted false", list.Equal(listC)) |
| 476 | } |
| 477 | listD := reg.NativeToValue([]any{1, 2.0, 3.0}) |
| 478 | if list.Equal(listD) != True { |
| 479 | t.Errorf("list.Equal(listD) got %v, wanted true", list.Equal(listD)) |
| 480 | } |
| 481 | if list.Equal(NullValue) != False { |
| 482 | t.Errorf("list.Equal(NullValue) got %v, wanted false", list.Equal(NullValue)) |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | func TestConcatListGet(t *testing.T) { |
| 487 | reg := newTestRegistry(t) |
| 488 | listA := NewDynamicList(reg, []float32{1.0, 2.0}) |
| 489 | listB := NewDynamicList(reg, []float64{3.0}) |
| 490 | list := listA.Add(listB).(traits.Lister) |
| 491 | if getElem(t, list, Int(0)) != Double(1.0) || |
| 492 | getElem(t, list, Uint(1)) != Double(2.0) || |
| 493 | getElem(t, list, Double(2.0)) != Double(3.0) { |
| 494 | t.Errorf("List values by index did not match expectations") |
| 495 | } |
| 496 | if val := list.Get(Int(-1)); !IsError(val) { |
| 497 | t.Errorf("Should not have been able to read a negative index") |
| 498 | } |
| 499 | if val := list.Get(Int(3)); !IsError(val) { |
| 500 | t.Errorf("Should not have been able to read beyond end of list") |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | func TestConcatListIterator(t *testing.T) { |
| 505 | reg := newTestRegistry(t) |
| 506 | listA := NewDynamicList(reg, []float32{1.0, 2.0}) |
| 507 | listB := NewDynamicList(reg, []float64{3.0}) |
| 508 | list := listA.Add(listB).(traits.Lister) |
| 509 | it := list.Iterator() |
| 510 | var i = int64(0) |
| 511 | for ; it.HasNext() == True; i++ { |
| 512 | elem := it.Next() |
| 513 | if getElem(t, list, Int(i)) != elem { |
| 514 | t.Errorf( |
| 515 | "List iterator returned incorrect value: list[%d]: %v", i, elem) |
| 516 | } |
| 517 | } |
| 518 | if it.Next() != nil { |
| 519 | t.Errorf("List iterator attempted to continue beyond list size") |
| 520 | } |
| 521 | if i != 3 { |
| 522 | t.Errorf("Iterator did not iterate until last value") |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | func TestStringListAdd_Empty(t *testing.T) { |
| 527 | reg := newTestRegistry(t) |
| 528 | list := NewStringList(reg, []string{"hello"}) |
| 529 | if list.Add(NewStringList(reg, []string{})) != list { |
| 530 | t.Error("Adding empty lists resulted in new list creation.") |
| 531 | } |
| 532 | if NewStringList(reg, []string{}).Add(list) != list { |
| 533 | t.Error("Adding empty lists resulted in new list creation.") |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | func TestStringListAdd_Error(t *testing.T) { |
| 538 | reg := newTestRegistry(t) |
| 539 | if !IsError(NewStringList(reg, []string{}).Add(True)) { |
| 540 | t.Error("Got list, expected error.") |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | func TestStringListAdd_Heterogenous(t *testing.T) { |
| 545 | reg := newTestRegistry(t) |
| 546 | listA := NewStringList(reg, []string{"hello"}) |
| 547 | listB := NewDynamicList(reg, []int32{1, 2, 3}) |
| 548 | list := listA.Add(listB).(traits.Lister).Value().([]any) |
| 549 | expected := []any{"hello", int64(1), int64(2), int64(3)} |
| 550 | if len(list) != len(expected) { |
| 551 | t.Errorf("Unexpected list size. Got '%d', expected 4", len(list)) |
| 552 | } |
| 553 | for i, v := range expected { |
| 554 | if list[i] != v { |
| 555 | t.Errorf("elem[%d] Got '%v', expected '%v'", i, list[i], v) |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | func TestStringListAdd_StringLists(t *testing.T) { |
| 561 | reg := newTestRegistry(t) |
| 562 | listA := NewStringList(reg, []string{"hello"}) |
| 563 | listB := NewStringList(reg, []string{"world", "!"}) |
| 564 | list := listA.Add(listB).(traits.Lister) |
| 565 | if list.Size() != Int(3) { |
| 566 | t.Error("Combined list did not have correct size.") |
| 567 | } |
| 568 | expected := []string{"hello", "world", "!"} |
| 569 | for i, v := range expected { |
| 570 | if list.Get(Int(i)).Equal(String(v)) != True { |
| 571 | t.Errorf("elem[%d] Got '%v', expected '%v'", i, list.Get(Int(i)), v) |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | func TestStringListConvertToNative(t *testing.T) { |
| 577 | reg := newTestRegistry(t) |
| 578 | list := NewStringList(reg, []string{"h", "e", "l", "p"}) |
| 579 | val, err := list.ConvertToNative(reflect.TypeOf([]string{})) |
| 580 | if err != nil { |
| 581 | t.Error("Unable to convert string list to itself.") |
| 582 | } |
| 583 | if !reflect.DeepEqual(val, []string{"h", "e", "l", "p"}) { |
| 584 | t.Errorf(`Got %v, expected ["h", "e", "l", "p"]`, val) |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | func TestStringListConvertToNative_ListInterface(t *testing.T) { |
| 589 | reg := newTestRegistry(t) |
| 590 | list := NewStringList(reg, []string{"h", "e", "l", "p"}) |
| 591 | val, err := list.ConvertToNative(reflect.TypeOf([]any{})) |
| 592 | if err != nil { |
| 593 | t.Error("Unable to convert string list to itself.") |
| 594 | } |
| 595 | want := []any{"h", "e", "l", "p"} |
| 596 | if !reflect.DeepEqual(val.([]any), want) { |
| 597 | for i, e := range val.([]any) { |
| 598 | t.Logf("val[%d] %v(%T)", i, e, e) |
| 599 | } |
| 600 | for i, e := range want { |
| 601 | t.Logf("want[%d] %v(%T)", i, e, e) |
| 602 | } |
| 603 | t.Errorf(`Got %v(%T), expected %v(%T)`, val, val, want, want) |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | func TestStringListConvertToNative_Error(t *testing.T) { |
| 608 | reg := newTestRegistry(t) |
| 609 | list := NewStringList(reg, []string{"h", "e", "l", "p"}) |
| 610 | _, err := list.ConvertToNative(JSONStructType) |
| 611 | if err == nil { |
| 612 | t.Error("Conversion of list to unsupported type did not error.") |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | func TestStringListConvertToNative_Json(t *testing.T) { |
| 617 | reg := newTestRegistry(t) |
| 618 | list := NewStringList(reg, []string{"h", "e", "l", "p"}) |
| 619 | jsonVal, err := list.ConvertToNative(JSONValueType) |
| 620 | if err != nil { |
| 621 | t.Errorf("Got '%v', expected '%v'", err, jsonVal) |
| 622 | } |
| 623 | jsonBytes, err := protojson.Marshal(jsonVal.(proto.Message)) |
| 624 | if err != nil { |
| 625 | t.Fatalf("protojson.Marshal(%v) failed: %v", jsonVal, err) |
| 626 | } |
| 627 | jsonTxt := string(jsonBytes) |
| 628 | outList := []any{} |
| 629 | err = json.Unmarshal(jsonBytes, &outList) |
| 630 | if err != nil { |
| 631 | t.Fatalf("json.Unmarshal(%q) failed: %v", jsonTxt, err) |
| 632 | } |
| 633 | if !reflect.DeepEqual(outList, []any{"h", "e", "l", "p"}) { |
| 634 | t.Errorf("got json '%v', expected %v", jsonTxt, outList) |
| 635 | } |
| 636 | |
| 637 | jsonList, err := list.ConvertToNative(JSONListType) |
| 638 | if err != nil { |
| 639 | t.Errorf("Got '%v', expected '%v'", err, jsonList) |
| 640 | } |
| 641 | jsonListBytes, err := protojson.Marshal(jsonList.(proto.Message)) |
| 642 | if err != nil { |
| 643 | t.Fatalf("protojson.Marshal(%v) failed: %v", jsonVal, err) |
| 644 | } |
| 645 | jsonListTxt := string(jsonListBytes) |
| 646 | if jsonTxt != jsonListTxt { |
| 647 | t.Errorf("Json value and list value not equal.") |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | func TestStringListGet_OutOfRange(t *testing.T) { |
| 652 | reg := newTestRegistry(t) |
| 653 | list := NewStringList(reg, []string{"hello", "world"}) |
| 654 | if !IsError(list.Get(Int(-1))) { |
| 655 | t.Error("Negative index did not return error.") |
| 656 | } |
| 657 | if !IsError(list.Get(Int(2))) { |
| 658 | t.Error("Index out of range did not return error.") |
| 659 | } |
| 660 | if !IsError(list.Get(Double(0.9))) { |
| 661 | t.Error("Index out of range did not return error.") |
| 662 | } |
| 663 | if !IsError(list.Get(String("1"))) { |
| 664 | t.Error("Invalid index type did not return error.") |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | func TestValueListAdd(t *testing.T) { |
| 669 | reg := newTestRegistry(t) |
| 670 | listA := NewRefValList(reg, []ref.Val{String("hello")}) |
| 671 | listB := NewRefValList(reg, []ref.Val{String("world")}) |
| 672 | listConcat := listA.Add(listB).(traits.Lister) |
| 673 | if listConcat.Contains(String("goodbye")) != False { |
| 674 | t.Error("Homogeneous concatenated value list did not return false on missing input") |
| 675 | } |
| 676 | if listConcat.Contains(String("hello")) != True { |
| 677 | t.Error("Homogeneous concatenated value list did not return true on valid input") |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | func TestValueListConvertToNative_Json(t *testing.T) { |
| 682 | reg := newTestRegistry(t) |
| 683 | list := NewRefValList(reg, []ref.Val{String("hello"), String("world")}) |
| 684 | jsonVal, err := list.ConvertToNative(JSONListType) |
| 685 | if err != nil { |
| 686 | t.Errorf("Got '%v', expected '%v'", err, jsonVal) |
| 687 | } |
| 688 | jsonBytes, err := protojson.Marshal(jsonVal.(proto.Message)) |
| 689 | if err != nil { |
| 690 | t.Fatalf("protojson.Marshal(%v) failed: %v", jsonVal, err) |
| 691 | } |
| 692 | jsonTxt := string(jsonBytes) |
| 693 | outList := []any{} |
| 694 | err = json.Unmarshal(jsonBytes, &outList) |
| 695 | if err != nil { |
| 696 | t.Fatalf("json.Unmarshal(%q) failed: %v", jsonTxt, err) |
| 697 | } |
| 698 | if !reflect.DeepEqual(outList, []any{"hello", "world"}) { |
| 699 | t.Errorf("got json '%v', expected %v", jsonTxt, outList) |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | func TestMutableList(t *testing.T) { |
| 704 | l := NewMutableList(DefaultTypeAdapter) |
| 705 | l.Add(NewRefValList(DefaultTypeAdapter, []ref.Val{String("hello")})) |
| 706 | l.Add(NewRefValList(DefaultTypeAdapter, []ref.Val{String("world")})) |
| 707 | il := l.ToImmutableList() |
| 708 | if il.Size() != Int(2) { |
| 709 | t.Errorf("il.Size() got %d, wanted size 2", il.Size()) |
| 710 | } |
| 711 | l.Add(NewRefValList(DefaultTypeAdapter, []ref.Val{String("!")})) |
| 712 | if il.Size() != Int(2) { |
| 713 | t.Errorf("il.Size() got %d, wanted size 2", il.Size()) |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | func TestListFold(t *testing.T) { |
| 718 | |
| 719 | tests := []struct { |
| 720 | l any |
| 721 | folds int |
| 722 | foldLimit int |
| 723 | }{ |
| 724 | { |
| 725 | l: []string{"hello", "world"}, |
| 726 | folds: 2, |
| 727 | foldLimit: 2, |
| 728 | }, |
| 729 | { |
| 730 | l: []string{"hello", "world"}, |
| 731 | folds: 1, |
| 732 | foldLimit: 1, |
| 733 | }, |
| 734 | { |
| 735 | l: []string{"hello"}, |
| 736 | folds: 1, |
| 737 | foldLimit: 2, |
| 738 | }, |
| 739 | { |
| 740 | l: []ref.Val{}, |
| 741 | folds: 0, |
| 742 | foldLimit: 20, |
| 743 | }, |
| 744 | { |
| 745 | l: []ref.Val{ |
| 746 | String("hello"), |
| 747 | String("world"), |
| 748 | String("goodbye"), |
| 749 | String("cruel world"), |
| 750 | }, |
| 751 | folds: 1, |
| 752 | foldLimit: 1, |
| 753 | }, |
| 754 | { |
| 755 | l: []ref.Val{ |
| 756 | String("hello"), |
| 757 | String("world"), |
| 758 | String("goodbye"), |
| 759 | String("cruel world"), |
| 760 | }, |
| 761 | folds: 4, |
| 762 | foldLimit: 10, |
| 763 | }, |
| 764 | { |
| 765 | l: DefaultTypeAdapter.NativeToValue([]ref.Val{ |
| 766 | String("hello"), |
| 767 | String("world"), |
| 768 | }).(traits.Lister).Add(DefaultTypeAdapter.NativeToValue([]ref.Val{ |
| 769 | String("goodbye"), |
| 770 | String("cruel world"), |
| 771 | })), |
| 772 | folds: 4, |
| 773 | foldLimit: 10, |
| 774 | }, |
| 775 | { |
| 776 | l: DefaultTypeAdapter.NativeToValue([]ref.Val{ |
| 777 | String("hello"), |
| 778 | String("world"), |
| 779 | }).(traits.Lister).Add(DefaultTypeAdapter.NativeToValue([]ref.Val{ |
| 780 | String("goodbye"), |
| 781 | String("cruel world"), |
| 782 | })), |
| 783 | folds: 3, |
| 784 | foldLimit: 3, |
| 785 | }, |
| 786 | } |
| 787 | reg := NewEmptyRegistry() |
| 788 | for i, tst := range tests { |
| 789 | tc := tst |
| 790 | l := reg.NativeToValue(tc.l).(traits.Lister) |
| 791 | foldKinds := map[string]traits.Foldable{ |
| 792 | "modern": ToFoldableList(l), |
| 793 | "legacy": ToFoldableList(proxyLegacyList{proxy: l}), |
| 794 | } |
| 795 | for foldKind, foldable := range foldKinds { |
| 796 | t.Run(fmt.Sprintf("[%d]%s", i, foldKind), func(t *testing.T) { |
| 797 | f := &testListFolder{foldLimit: tc.foldLimit} |
| 798 | foldable.Fold(f) |
| 799 | if f.folds != tc.folds { |
| 800 | t.Errorf("m.Fold(f) got %d, wanted %d folds", f.folds, tc.folds) |
| 801 | } |
| 802 | }) |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | type testListFolder struct { |
| 808 | foldLimit int |
| 809 | folds int |
| 810 | } |
| 811 | |
| 812 | func (f *testListFolder) FoldEntry(k, v any) bool { |
| 813 | if f.foldLimit != 0 { |
| 814 | if f.folds >= f.foldLimit { |
| 815 | return false |
| 816 | } |
| 817 | } |
| 818 | f.folds++ |
| 819 | return true |
| 820 | } |
| 821 | |
| 822 | // proxyLegacyList omits the foldable interfaces associated with all core Lister implementations |
| 823 | type proxyLegacyList struct { |
| 824 | proxy traits.Lister |
| 825 | } |
| 826 | |
| 827 | func (m proxyLegacyList) ConvertToNative(typeDesc reflect.Type) (any, error) { |
| 828 | return m.proxy.ConvertToNative(typeDesc) |
| 829 | } |
| 830 | |
| 831 | func (m proxyLegacyList) ConvertToType(typeValue ref.Type) ref.Val { |
| 832 | return m.proxy.ConvertToType(typeValue) |
| 833 | } |
| 834 | |
| 835 | func (m proxyLegacyList) Equal(other ref.Val) ref.Val { |
| 836 | return m.proxy.Equal(other) |
| 837 | } |
| 838 | |
| 839 | func (m proxyLegacyList) Type() ref.Type { |
| 840 | return m.proxy.Type() |
| 841 | } |
| 842 | |
| 843 | func (m proxyLegacyList) Value() any { |
| 844 | return m.proxy.Value() |
| 845 | } |
| 846 | |
| 847 | func (m proxyLegacyList) Add(other ref.Val) ref.Val { |
| 848 | return m.proxy.Add(other) |
| 849 | } |
| 850 | |
| 851 | func (m proxyLegacyList) Contains(value ref.Val) ref.Val { |
| 852 | return m.proxy.Contains(value) |
| 853 | } |
| 854 | |
| 855 | func (m proxyLegacyList) Get(index ref.Val) ref.Val { |
| 856 | return m.proxy.Get(index) |
| 857 | } |
| 858 | |
| 859 | func (m proxyLegacyList) Iterator() traits.Iterator { |
| 860 | return m.proxy.Iterator() |
| 861 | } |
| 862 | |
| 863 | func (m proxyLegacyList) Size() ref.Val { |
| 864 | return m.proxy.Size() |
| 865 | } |
| 866 | |
| 867 | func getElem(t *testing.T, list traits.Indexer, index ref.Val) any { |
| 868 | t.Helper() |
| 869 | val := list.Get(index) |
| 870 | if IsError(val) { |
| 871 | t.Errorf("Error reading list index %d, %v", index, val) |
| 872 | return nil |
| 873 | } |
| 874 | return val |
| 875 | } |
| 876 | |
| 877 | func validateList123(t *testing.T, list traits.Lister) { |
| 878 | t.Helper() |
| 879 | if getElem(t, list, Int(0)) != Int(1) || |
| 880 | getElem(t, list, Uint(1)) != Int(2) || |
| 881 | getElem(t, list, Double(2.0)) != Int(3) { |
| 882 | t.Errorf("List values by index did not match expectations") |
| 883 | } |
| 884 | if val := list.Get(Int(-1)); !IsError(val) { |
| 885 | t.Errorf("Should not have been able to read a negative index") |
| 886 | } |
| 887 | if val := list.Get(Int(3)); !IsError(val) { |
| 888 | t.Errorf("Should not have been able to read beyond end of list") |
| 889 | } |
| 890 | if !IsError(list.Get(Uint(3))) { |
| 891 | t.Error("Invalid index type did not result in error") |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | func validateIterator123(t *testing.T, list traits.Lister) { |
| 896 | t.Helper() |
| 897 | it := list.Iterator() |
| 898 | var i = int64(0) |
| 899 | for ; it.HasNext() == True; i++ { |
| 900 | elem := it.Next() |
| 901 | if getElem(t, list, Int(i)) != elem { |
| 902 | t.Errorf( |
| 903 | "List iterator returned incorrect value: list[%d]: %v", i, elem) |
| 904 | } |
| 905 | } |
| 906 | if it.Next() != nil { |
| 907 | t.Errorf("List iterator attempted to continue beyond list size") |
| 908 | } |
| 909 | if i != 3 { |
| 910 | t.Errorf("Iterator did not iterate until last value") |
| 911 | } |
| 912 | } |