github.com/coveo/gotemplate@v2.7.7+incompatible/collections/implementation/generic_test.go (about) 1 package implementation 2 3 import ( 4 "fmt" 5 "reflect" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 11 "github.com/coveo/gotemplate/errors" 12 ) 13 14 var strFixture = baseList(baseListHelper.NewStringList(strings.Split("Hello World, I'm Foo Bar!", " ")...).AsArray()) 15 16 func Test_list_Append(t *testing.T) { 17 t.Parallel() 18 19 tests := []struct { 20 name string 21 l baseIList 22 values []interface{} 23 want baseIList 24 }{ 25 {"Empty", baseList{}, []interface{}{1, 2, 3}, baseList{1, 2, 3}}, 26 {"List of int", baseList{1, 2, 3}, []interface{}{4, 5}, baseList{1, 2, 3, 4, 5}}, 27 {"List of string", strFixture, []interface{}{"That's all folks!"}, baseList{"Hello", "World,", "I'm", "Foo", "Bar!", "That's all folks!"}}, 28 } 29 30 for _, tt := range tests { 31 t.Run(tt.name, func(t *testing.T) { 32 if got := tt.l.Append(tt.values...); !reflect.DeepEqual(got, tt.want) { 33 t.Errorf("baseList.Append():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 34 } 35 }) 36 } 37 } 38 39 func Test_list_Prepend(t *testing.T) { 40 t.Parallel() 41 42 tests := []struct { 43 name string 44 l baseIList 45 values []interface{} 46 want baseIList 47 }{ 48 {"Empty", baseList{}, []interface{}{1, 2, 3}, baseList{1, 2, 3}}, 49 {"List of int", baseList{1, 2, 3}, []interface{}{4, 5}, baseList{4, 5, 1, 2, 3}}, 50 {"List of string", strFixture, []interface{}{"That's all folks!"}, baseList{"That's all folks!", "Hello", "World,", "I'm", "Foo", "Bar!"}}, 51 } 52 53 for _, tt := range tests { 54 t.Run(tt.name, func(t *testing.T) { 55 if got := tt.l.Prepend(tt.values...); !reflect.DeepEqual(got, tt.want) { 56 t.Errorf("baseList.Prepend():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 57 } 58 }) 59 } 60 } 61 62 func Test_list_AsArray(t *testing.T) { 63 t.Parallel() 64 65 tests := []struct { 66 name string 67 l baseList 68 want []interface{} 69 }{ 70 {"Empty List", baseList{}, []interface{}{}}, 71 {"List of int", baseList{1, 2, 3}, []interface{}{1, 2, 3}}, 72 {"List of string", strFixture, []interface{}{"Hello", "World,", "I'm", "Foo", "Bar!"}}, 73 } 74 for _, tt := range tests { 75 t.Run(tt.name, func(t *testing.T) { 76 if got := tt.l.AsArray(); !reflect.DeepEqual(got, tt.want) { 77 t.Errorf("baseList.AsList():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 78 } 79 }) 80 } 81 } 82 83 func Test_baseList_Strings(t *testing.T) { 84 t.Parallel() 85 86 tests := []struct { 87 name string 88 l baseList 89 want []string 90 }{ 91 {"Empty List", baseList{}, []string{}}, 92 {"List of int", baseList{1, 2, 3}, []string{"1", "2", "3"}}, 93 {"List of string", strFixture, []string{"Hello", "World,", "I'm", "Foo", "Bar!"}}, 94 } 95 for _, tt := range tests { 96 t.Run(tt.name, func(t *testing.T) { 97 if got := tt.l.Strings(); !reflect.DeepEqual(got, tt.want) { 98 t.Errorf("baseList.Strings() = %v, want %v", got, tt.want) 99 } 100 }) 101 } 102 } 103 104 func Test_list_Capacity(t *testing.T) { 105 t.Parallel() 106 107 tests := []struct { 108 name string 109 l baseIList 110 want int 111 }{ 112 {"Empty List with 100 spaces", baseListHelper.CreateList(0, 100), 100}, 113 } 114 for _, tt := range tests { 115 t.Run(tt.name, func(t *testing.T) { 116 if got := tt.l.Capacity(); got != tt.want { 117 t.Errorf("baseList.Capacity() = %v, want %v", got, tt.want) 118 } 119 if tt.l.Capacity() != tt.l.Cap() { 120 t.Errorf("Cap and Capacity return different values") 121 } 122 }) 123 } 124 } 125 126 func Test_list_Clone(t *testing.T) { 127 t.Parallel() 128 129 tests := []struct { 130 name string 131 l baseList 132 want baseIList 133 }{ 134 {"Empty List", baseList{}, baseList{}}, 135 {"List of int", baseList{1, 2, 3}, baseList{1, 2, 3}}, 136 {"List of string", strFixture, baseList{"Hello", "World,", "I'm", "Foo", "Bar!"}}, 137 } 138 for _, tt := range tests { 139 t.Run(tt.name, func(t *testing.T) { 140 if got := tt.l.Clone(); !reflect.DeepEqual(got, tt.want) { 141 t.Errorf("baseList.Clone():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 142 } 143 }) 144 } 145 } 146 147 func Test_list_Get(t *testing.T) { 148 t.Parallel() 149 150 tests := []struct { 151 name string 152 l baseList 153 indexes []int 154 want interface{} 155 }{ 156 {"Empty List", baseList{}, []int{0}, nil}, 157 {"Negative index", baseList{}, []int{-1}, nil}, 158 {"List of int", baseList{1, 2, 3}, []int{0}, 1}, 159 {"List of string", strFixture, []int{1}, "World,"}, 160 {"Get last", strFixture, []int{-1}, "Bar!"}, 161 {"Get before last", strFixture, []int{-2}, "Foo"}, 162 {"A way to before last", strFixture, []int{-12}, nil}, 163 {"Get nothing", strFixture, nil, nil}, 164 } 165 for _, tt := range tests { 166 t.Run(tt.name, func(t *testing.T) { 167 if got := tt.l.Get(tt.indexes...); !reflect.DeepEqual(got, tt.want) { 168 t.Errorf("baseList.Get() = %v, want %v", got, tt.want) 169 } 170 }) 171 } 172 } 173 174 func Test_list_Len(t *testing.T) { 175 t.Parallel() 176 177 tests := []struct { 178 name string 179 l baseList 180 want int 181 }{ 182 {"Empty List", baseList{}, 0}, 183 {"List of int", baseList{1, 2, 3}, 3}, 184 {"List of string", strFixture, 5}, 185 } 186 for _, tt := range tests { 187 t.Run(tt.name, func(t *testing.T) { 188 if got := tt.l.Len(); got != tt.want { 189 t.Errorf("baseList.Len() = %v, want %v", got, tt.want) 190 } 191 if tt.l.Len() != tt.l.Count() { 192 t.Errorf("Len and Count return different values") 193 } 194 }) 195 } 196 } 197 198 func Test_CreateList(t *testing.T) { 199 t.Parallel() 200 201 tests := []struct { 202 name string 203 args []int 204 want baseIList 205 wantErr bool 206 }{ 207 {"Empty", nil, baseList{}, false}, 208 {"With nil elements", []int{10}, make(baseList, 10), false}, 209 {"With capacity", []int{0, 10}, make(baseList, 0, 10), false}, 210 {"Too much args", []int{0, 10, 1}, nil, true}, 211 } 212 for _, tt := range tests { 213 var err error 214 t.Run(tt.name, func(t *testing.T) { 215 defer func() { err = errors.Trap(err, recover()) }() 216 got := baseListHelper.CreateList(tt.args...) 217 if !reflect.DeepEqual(got, tt.want) { 218 t.Errorf("CreateList():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 219 } 220 if got.Capacity() != tt.want.Cap() { 221 t.Errorf("CreateList() capacity:\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got.Cap(), tt.want.Capacity()) 222 } 223 }) 224 if (err != nil) != tt.wantErr { 225 t.Errorf("CreateList() error = %v, wantErr %v", err, tt.wantErr) 226 } 227 } 228 } 229 230 func Test_list_Create(t *testing.T) { 231 t.Parallel() 232 233 tests := []struct { 234 name string 235 l baseList 236 args []int 237 want baseIList 238 }{ 239 {"Empty", nil, nil, baseList{}}, 240 {"Existing List", baseList{1, 2}, nil, baseList{}}, 241 {"With Empty spaces", baseList{1, 2}, []int{5}, baseList{nil, nil, nil, nil, nil}}, 242 {"With Capacity", baseList{1, 2}, []int{0, 5}, baseListHelper.CreateList(0, 5)}, 243 } 244 for _, tt := range tests { 245 t.Run(tt.name, func(t *testing.T) { 246 got := tt.l.Create(tt.args...) 247 if !reflect.DeepEqual(got, tt.want) { 248 t.Errorf("baseList.Create():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 249 } 250 if got.Capacity() != tt.want.Capacity() { 251 t.Errorf("baseList.Create() capacity:\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got.Capacity(), tt.want.Capacity()) 252 } 253 }) 254 } 255 } 256 257 func Test_list_New(t *testing.T) { 258 t.Parallel() 259 260 tests := []struct { 261 name string 262 l baseList 263 args []interface{} 264 want baseIList 265 }{ 266 {"Empty", nil, nil, baseList{}}, 267 {"Existing List", baseList{1, 2}, nil, baseList{}}, 268 {"With elements", baseList{1, 2}, []interface{}{3, 4, 5}, baseList{3, 4, 5}}, 269 {"With strings", baseList{1, 2}, []interface{}{"Hello", "World"}, baseList{"Hello", "World"}}, 270 {"With nothing", baseList{1, 2}, []interface{}{}, baseList{}}, 271 {"With nil", baseList{1, 2}, nil, baseList{}}, 272 {"Adding array", baseList{1, 2}, []interface{}{baseList{3, 4}}, baseList{3, 4}}, 273 } 274 for _, tt := range tests { 275 t.Run(tt.name, func(t *testing.T) { 276 if got := tt.l.New(tt.args...); !reflect.DeepEqual(got, tt.want) { 277 t.Errorf("baseList.Create():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 278 } 279 }) 280 } 281 } 282 283 func Test_list_CreateDict(t *testing.T) { 284 t.Parallel() 285 286 tests := []struct { 287 name string 288 l baseList 289 args []int 290 want baseIDict 291 wantErr bool 292 }{ 293 {"Empty", nil, nil, baseDict{}, false}, 294 {"With capacity", nil, []int{10}, baseDict{}, false}, 295 {"With too much parameter", nil, []int{10, 1}, nil, true}, 296 } 297 for _, tt := range tests { 298 var err error 299 t.Run(tt.name, func(t *testing.T) { 300 defer func() { err = errors.Trap(err, recover()) }() 301 got := tt.l.CreateDict(tt.args...) 302 if !reflect.DeepEqual(got, tt.want) { 303 t.Errorf("baseList.CreateDict():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 304 } 305 }) 306 if (err != nil) != tt.wantErr { 307 t.Errorf("baseList.CreateDict() error = %v, wantErr %v", err, tt.wantErr) 308 return 309 } 310 } 311 } 312 313 func Test_list_Contains(t *testing.T) { 314 t.Parallel() 315 316 tests := []struct { 317 name string 318 l baseList 319 args []interface{} 320 want bool 321 }{ 322 {"Empty List", nil, []interface{}{}, false}, 323 {"Search nothing", baseList{1}, nil, true}, 324 {"Search nothing 2", baseList{1}, []interface{}{}, true}, 325 {"Not there", baseList{1}, []interface{}{2}, false}, 326 {"Included", baseList{1, 2}, []interface{}{2}, true}, 327 {"Partially there", baseList{1, 2}, []interface{}{2, 3}, false}, 328 } 329 for _, tt := range tests { 330 t.Run(tt.name, func(t *testing.T) { 331 if got := tt.l.Contains(tt.args...); !reflect.DeepEqual(got, tt.want) { 332 t.Errorf("baseList.Contains():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 333 } 334 if got := tt.l.Has(tt.args...); !reflect.DeepEqual(got, tt.want) { 335 t.Errorf("baseList.Has():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 336 } 337 }) 338 } 339 } 340 341 func Test_list_First_Last(t *testing.T) { 342 t.Parallel() 343 344 tests := []struct { 345 name string 346 l baseList 347 wantFirst interface{} 348 wantLast interface{} 349 }{ 350 {"Nil", nil, nil, nil}, 351 {"Empty", baseList{}, nil, nil}, 352 {"One element", baseList{1}, 1, 1}, 353 {"Many element ", baseList{1, "two", 3.1415, "four"}, 1, "four"}, 354 } 355 for _, tt := range tests { 356 t.Run(tt.name, func(t *testing.T) { 357 if got := tt.l.First(); !reflect.DeepEqual(got, tt.wantFirst) { 358 t.Errorf("baseList.First():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.wantFirst) 359 } 360 if got := tt.l.Last(); !reflect.DeepEqual(got, tt.wantLast) { 361 t.Errorf("baseList.Last():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.wantLast) 362 } 363 }) 364 } 365 } 366 367 func Test_list_Pop(t *testing.T) { 368 t.Parallel() 369 370 tests := []struct { 371 name string 372 l baseList 373 args []int 374 want interface{} 375 wantList baseList 376 }{ 377 {"Nil", nil, nil, nil, baseList{}}, 378 {"Empty", baseList{}, nil, nil, baseList{}}, 379 {"Non existent", baseList{}, []int{1}, nil, baseList{}}, 380 {"Empty with args", baseList{}, []int{1, 3}, baseList{nil, nil}, baseList{}}, 381 {"List with bad index", baseList{0, 1, 2, 3, 4, 5}, []int{1, 3, 8}, baseList{1, 3, nil}, baseList{0, 2, 4, 5}}, 382 {"Pop last element", baseList{0, 1, 2, 3, 4, 5}, nil, 5, baseList{0, 1, 2, 3, 4}}, 383 {"Pop before last", baseList{0, 1, 2, 3, 4, 5}, []int{-2}, 4, baseList{0, 1, 2, 3, 5}}, 384 {"Pop first element", baseList{0, 1, 2, 3, 4, 5}, []int{0}, 0, baseList{1, 2, 3, 4, 5}}, 385 {"Pop all", baseList{0, 1, 2, 3}, []int{0, 1, 2, 3}, baseList{0, 1, 2, 3}, baseList{}}, 386 {"Pop same element many time", baseList{0, 1, 2, 3}, []int{1, 1, 2, 2}, baseList{1, 1, 2, 2}, baseList{0, 3}}, 387 } 388 for _, tt := range tests { 389 t.Run(tt.name, func(t *testing.T) { 390 got, gotL := tt.l.Pop(tt.args...) 391 if !reflect.DeepEqual(got, tt.want) { 392 t.Errorf("baseList.Pop():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 393 } 394 if !reflect.DeepEqual(gotL, tt.wantList) { 395 t.Errorf("baseList.Pop():\ngotList %[1]v (%[1]T)\n want %[2]v (%[2]T)", gotL, tt.wantList) 396 } 397 }) 398 } 399 } 400 401 func Test_list_Intersect(t *testing.T) { 402 t.Parallel() 403 404 tests := []struct { 405 name string 406 l baseList 407 args []interface{} 408 want baseList 409 }{ 410 {"Empty List", nil, []interface{}{}, baseList{}}, 411 {"Intersect nothing", baseList{1}, nil, baseList{}}, 412 {"Intersect nothing 2", baseList{1}, []interface{}{}, baseList{}}, 413 {"Not there", baseList{1}, []interface{}{2}, baseList{}}, 414 {"Included", baseList{1, 2}, []interface{}{2}, baseList{2}}, 415 {"Partially there", baseList{1, 2}, []interface{}{2, 3}, baseList{2}}, 416 {"With duplicates", baseList{1, 2, 3, 4, 5, 4, 3, 2, 1}, []interface{}{3, 4, 5, 6, 7, 8, 7, 6, 5, 5, 4, 3}, baseList{3, 4, 5}}, 417 } 418 for _, tt := range tests { 419 t.Run(tt.name, func(t *testing.T) { 420 if got := tt.l.Intersect(tt.args...); !reflect.DeepEqual(got, tt.want) { 421 t.Errorf("baseList.Intersect():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 422 } 423 }) 424 } 425 } 426 427 func Test_list_Union(t *testing.T) { 428 t.Parallel() 429 430 tests := []struct { 431 name string 432 l baseList 433 args []interface{} 434 want baseList 435 }{ 436 {"Empty List", nil, []interface{}{}, baseList{}}, 437 {"Intersect nothing", baseList{1}, nil, baseList{1}}, 438 {"Intersect nothing 2", baseList{1}, []interface{}{}, baseList{1}}, 439 {"Not there", baseList{1}, []interface{}{2}, baseList{1, 2}}, 440 {"Included", baseList{1, 2}, []interface{}{2}, baseList{1, 2}}, 441 {"Partially there", baseList{1, 2}, []interface{}{2, 3}, baseList{1, 2, 3}}, 442 {"With duplicates", baseList{1, 2, 3, 4, 5, 4, 3, 2, 1}, []interface{}{8, 7, 6, 5, 6, 7, 8}, baseList{1, 2, 3, 4, 5, 8, 7, 6}}, 443 } 444 for _, tt := range tests { 445 t.Run(tt.name, func(t *testing.T) { 446 if got := tt.l.Union(tt.args...); !reflect.DeepEqual(got, tt.want) { 447 t.Errorf("baseList.Union():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 448 } 449 }) 450 } 451 } 452 453 func Test_list_Without(t *testing.T) { 454 t.Parallel() 455 456 tests := []struct { 457 name string 458 l baseList 459 args []interface{} 460 want baseList 461 }{ 462 {"Empty List", nil, []interface{}{}, baseList{}}, 463 {"Remove nothing", baseList{1}, nil, baseList{1}}, 464 {"Remove nothing 2", baseList{1}, []interface{}{}, baseList{1}}, 465 {"Not there", baseList{1}, []interface{}{2}, baseList{1}}, 466 {"Included", baseList{1, 2}, []interface{}{2}, baseList{1}}, 467 {"Partially there", baseList{1, 2}, []interface{}{2, 3}, baseList{1}}, 468 } 469 for _, tt := range tests { 470 t.Run(tt.name, func(t *testing.T) { 471 if got := tt.l.Without(tt.args...); !reflect.DeepEqual(got, tt.want) { 472 t.Errorf("baseList.Without():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 473 } 474 }) 475 } 476 } 477 478 func Test_list_Unique(t *testing.T) { 479 t.Parallel() 480 481 tests := []struct { 482 name string 483 l baseList 484 want baseList 485 }{ 486 {"Empty List", nil, baseList{}}, 487 {"Remove nothing", baseList{1}, baseList{1}}, 488 {"Duplicates following", baseList{1, 1, 2, 3}, baseList{1, 2, 3}}, 489 {"Duplicates not following", baseList{1, 2, 3, 1, 2, 3, 4}, baseList{1, 2, 3, 4}}, 490 } 491 for _, tt := range tests { 492 t.Run(tt.name, func(t *testing.T) { 493 if got := tt.l.Unique(); !reflect.DeepEqual(got, tt.want) { 494 t.Errorf("baseList.Unique():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 495 } 496 }) 497 } 498 } 499 func Test_list_Reverse(t *testing.T) { 500 t.Parallel() 501 502 tests := []struct { 503 name string 504 l baseList 505 want baseIList 506 }{ 507 {"Empty List", baseList{}, baseList{}}, 508 {"List of int", baseList{1, 2, 3}, baseList{3, 2, 1}}, 509 {"List of string", strFixture, baseList{"Bar!", "Foo", "I'm", "World,", "Hello"}}, 510 } 511 for _, tt := range tests { 512 t.Run(tt.name, func(t *testing.T) { 513 l := tt.l.Clone() 514 if got := l.Reverse(); !reflect.DeepEqual(got, tt.want) { 515 t.Errorf("baseList.Reverse():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 516 } 517 }) 518 } 519 } 520 521 func Test_list_Set(t *testing.T) { 522 t.Parallel() 523 524 type args struct { 525 i int 526 v interface{} 527 } 528 tests := []struct { 529 name string 530 l baseIList 531 args args 532 want baseIList 533 wantErr bool 534 }{ 535 {"Empty", baseList{}, args{2, 1}, baseList{nil, nil, 1}, false}, 536 {"List of int", baseList{1, 2, 3}, args{0, 10}, baseList{10, 2, 3}, false}, 537 {"List of string", strFixture, args{2, "You're"}, baseList{"Hello", "World,", "You're", "Foo", "Bar!"}, false}, 538 {"Negative", baseList{}, args{-1, "negative value"}, nil, true}, 539 } 540 for _, tt := range tests { 541 t.Run(tt.name, func(t *testing.T) { 542 got, err := tt.l.Clone().Set(tt.args.i, tt.args.v) 543 if (err != nil) != tt.wantErr { 544 t.Errorf("baseList.Set() error = %v, wantErr %v", err, tt.wantErr) 545 return 546 } 547 if !reflect.DeepEqual(got, tt.want) { 548 t.Errorf("baseList.Set():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 549 } 550 }) 551 } 552 } 553 554 var mapFixture = map[string]interface{}{ 555 "int": 123, 556 "float": 1.23, 557 "string": "Foo bar", 558 "list": []interface{}{1, "two"}, 559 "listInt": []int{1, 2, 3}, 560 "map": map[string]interface{}{ 561 "sub1": 1, 562 "sub2": "two", 563 }, 564 "mapInt": map[int]interface{}{ 565 1: 1, 566 2: "two", 567 }, 568 } 569 570 var dictFixture = baseDict(baseDictHelper.AsDictionary(mapFixture).AsMap()) 571 572 func dumpKeys(t *testing.T, d1, d2 baseIDict) { 573 t.Parallel() 574 575 for key := range d1.AsMap() { 576 v1, v2 := d1.Get(key), d2.Get(key) 577 if reflect.DeepEqual(v1, v2) { 578 continue 579 } 580 t.Logf("'%[1]v' = %[2]v (%[2]T) vs %[3]v (%[3]T)", key, v1, v2) 581 } 582 } 583 584 func Test_dict_AsMap(t *testing.T) { 585 t.Parallel() 586 587 tests := []struct { 588 name string 589 d baseDict 590 want map[string]interface{} 591 }{ 592 {"Nil", nil, nil}, 593 {"Empty", baseDict{}, map[string]interface{}{}}, 594 {"Map", dictFixture, map[string]interface{}(dictFixture)}, 595 } 596 for _, tt := range tests { 597 t.Run(tt.name, func(t *testing.T) { 598 if got := tt.d.AsMap(); !reflect.DeepEqual(got, tt.want) { 599 t.Errorf("baseDict.AsMap():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 600 } 601 }) 602 } 603 } 604 605 func Test_dict_Clone(t *testing.T) { 606 t.Parallel() 607 608 tests := []struct { 609 name string 610 d baseDict 611 keys []interface{} 612 want baseIDict 613 }{ 614 {"Nil", nil, nil, baseDict{}}, 615 {"Empty", baseDict{}, nil, baseDict{}}, 616 {"Map", dictFixture, nil, dictFixture}, 617 {"Map with Fields", dictFixture, []interface{}{"int", "list"}, baseDict(dictFixture).Omit("float", "string", "listInt", "map", "mapInt")}, 618 } 619 for _, tt := range tests { 620 t.Run(tt.name, func(t *testing.T) { 621 got := tt.d.Clone(tt.keys...) 622 if !reflect.DeepEqual(got, tt.want) { 623 t.Errorf("baseDict.Clone():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 624 dumpKeys(t, got, tt.want) 625 } 626 627 // Ensure that the copy is distinct from the original 628 got.Set("NewFields", "Test") 629 if reflect.DeepEqual(got, tt.want) { 630 t.Errorf("Should be different:\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 631 } 632 if !got.Has("NewFields") || !reflect.DeepEqual(got.Get("NewFields"), "Test") { 633 t.Errorf("Element has not been added") 634 } 635 if got.Len() != tt.want.Count()+1 { 636 t.Errorf("Len and Count don't return the same value") 637 } 638 }) 639 } 640 } 641 642 func Test_baseDict_CreateList(t *testing.T) { 643 t.Parallel() 644 645 tests := []struct { 646 name string 647 d baseDict 648 args []int 649 want baseIList 650 wantLen int 651 wantCapacity int 652 }{ 653 {"Nil", nil, nil, baseList{}, 0, 0}, 654 {"Empty", baseDict{}, nil, baseList{}, 0, 0}, 655 {"Map", dictFixture, nil, baseList{}, 0, 0}, 656 {"Map with size", dictFixture, []int{3}, baseList{nil, nil, nil}, 3, 3}, 657 {"Map with capacity", dictFixture, []int{0, 10}, baseList{}, 0, 10}, 658 {"Map with size&capacity", dictFixture, []int{3, 10}, baseList{nil, nil, nil}, 3, 10}, 659 } 660 for _, tt := range tests { 661 t.Run(tt.name, func(t *testing.T) { 662 got := tt.d.CreateList(tt.args...) 663 if !reflect.DeepEqual(got, tt.want) { 664 t.Errorf("baseDict.CreateList() = %v, want %v", got, tt.want) 665 } 666 if got.Len() != tt.wantLen || got.Cap() != tt.wantCapacity { 667 t.Errorf("baseDict.CreateList() size: %d, %d vs %d, %d", got.Len(), got.Cap(), tt.wantLen, tt.wantCapacity) 668 } 669 }) 670 } 671 } 672 673 func Test_dict_Create(t *testing.T) { 674 t.Parallel() 675 676 tests := []struct { 677 name string 678 d baseDict 679 args []int 680 want baseIDict 681 wantErr bool 682 }{ 683 {"Empty", nil, nil, baseDict{}, false}, 684 {"With capacity", nil, []int{10}, baseDict{}, false}, 685 {"With too much parameter", nil, []int{10, 1}, nil, true}, 686 } 687 for _, tt := range tests { 688 var err error 689 t.Run(tt.name, func(t *testing.T) { 690 defer func() { err = errors.Trap(err, recover()) }() 691 got := tt.d.Create(tt.args...) 692 if !reflect.DeepEqual(got, tt.want) { 693 t.Errorf("baseDict.Create():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 694 } 695 }) 696 if (err != nil) != tt.wantErr { 697 t.Errorf("baseList.Create() error = %v, wantErr %v", err, tt.wantErr) 698 return 699 } 700 } 701 } 702 703 func Test_dict_Default(t *testing.T) { 704 t.Parallel() 705 706 type args struct { 707 key interface{} 708 defVal interface{} 709 } 710 tests := []struct { 711 name string 712 d baseDict 713 args args 714 want interface{} 715 }{ 716 {"Empty", nil, args{"Foo", "Bar"}, "Bar"}, 717 {"Map int", dictFixture, args{"int", 1}, 123}, 718 {"Map float", dictFixture, args{"float", 1}, 1.23}, 719 {"Map Non existant", dictFixture, args{"Foo", "Bar"}, "Bar"}, 720 } 721 for _, tt := range tests { 722 t.Run(tt.name, func(t *testing.T) { 723 if got := tt.d.Default(tt.args.key, tt.args.defVal); !reflect.DeepEqual(got, tt.want) { 724 t.Errorf("baseDict.Default() = %v, want %v", got, tt.want) 725 } 726 }) 727 } 728 } 729 730 func Test_dict_Delete(t *testing.T) { 731 t.Parallel() 732 733 type args struct { 734 key interface{} 735 keys []interface{} 736 } 737 tests := []struct { 738 name string 739 d baseDict 740 args args 741 want baseIDict 742 wantErr bool 743 }{ 744 {"Empty", nil, args{}, baseDict{}, true}, 745 {"Map", dictFixture, args{}, dictFixture, true}, 746 {"Non existant key", dictFixture, args{"Test", nil}, dictFixture, true}, 747 {"Map with keys", dictFixture, args{"int", []interface{}{"list"}}, dictFixture.Clone("float", "string", "listInt", "map", "mapInt"), false}, 748 {"Map with keys + non existant", dictFixture, args{"int", []interface{}{"list", "Test"}}, dictFixture.Clone("float", "string", "listInt", "map", "mapInt"), true}, 749 } 750 for _, tt := range tests { 751 t.Run(tt.name, func(t *testing.T) { 752 d := tt.d.Clone() 753 got, err := d.Delete(tt.args.key, tt.args.keys...) 754 if (err != nil) != tt.wantErr { 755 t.Errorf("baseDict.Delete() error = %v, wantErr %v", err, tt.wantErr) 756 return 757 } 758 if !reflect.DeepEqual(got, tt.want) { 759 t.Errorf("baseDict.Delete():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 760 dumpKeys(t, got, tt.want) 761 } 762 }) 763 } 764 } 765 766 func Test_dict_Flush(t *testing.T) { 767 t.Parallel() 768 769 tests := []struct { 770 name string 771 d baseDict 772 keys []interface{} 773 want baseIDict 774 }{ 775 {"Empty", nil, nil, baseDict{}}, 776 {"Map", dictFixture, nil, baseDict{}}, 777 {"Non existant key", dictFixture, []interface{}{"Test"}, dictFixture}, 778 {"Map with keys", dictFixture, []interface{}{"int", "list"}, dictFixture.Clone("float", "string", "listInt", "map", "mapInt")}, 779 {"Map with keys + non existant", dictFixture, []interface{}{"int", "list", "Test"}, dictFixture.Clone("float", "string", "listInt", "map", "mapInt")}, 780 } 781 for _, tt := range tests { 782 t.Run(tt.name, func(t *testing.T) { 783 d := tt.d.Clone() 784 got := d.Flush(tt.keys...) 785 if !reflect.DeepEqual(got, tt.want) { 786 t.Errorf("baseDict.Flush():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 787 dumpKeys(t, got, tt.want) 788 } 789 if !reflect.DeepEqual(d, got) { 790 t.Errorf("Should be equal after: %v, want %v", d, got) 791 dumpKeys(t, d, got) 792 } 793 }) 794 } 795 } 796 797 func Test_dict_Keys(t *testing.T) { 798 t.Parallel() 799 800 tests := []struct { 801 name string 802 d baseDict 803 want baseIList 804 }{ 805 {"Empty", nil, baseList{}}, 806 {"Map", dictFixture, baseList{str("float"), str("int"), str("list"), str("listInt"), str("map"), str("mapInt"), str("string")}}, 807 } 808 for _, tt := range tests { 809 t.Run(tt.name, func(t *testing.T) { 810 if got := tt.d.GetKeys(); !reflect.DeepEqual(got, tt.want) { 811 t.Errorf("baseDict.GetKeys():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 812 } 813 }) 814 } 815 } 816 817 func Test_dict_KeysAsString(t *testing.T) { 818 t.Parallel() 819 820 tests := []struct { 821 name string 822 d baseDict 823 want strArray 824 }{ 825 {"Empty", nil, strArray{}}, 826 {"Map", dictFixture, strArray{"float", "int", "list", "listInt", "map", "mapInt", "string"}}, 827 } 828 for _, tt := range tests { 829 t.Run(tt.name, func(t *testing.T) { 830 if got := tt.d.KeysAsString(); !reflect.DeepEqual(got, tt.want) { 831 t.Errorf("baseDict.KeysAsString():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 832 } 833 }) 834 } 835 } 836 837 func Test_dict_Merge(t *testing.T) { 838 t.Parallel() 839 840 adding1 := baseDict{ 841 "int": 1000, 842 "Add1Int": 1, 843 "Add1String": "string", 844 } 845 adding2 := baseDict{ 846 "Add2Int": 1, 847 "Add2String": "string", 848 "map": map[string]interface{}{ 849 "sub1": 2, 850 "newVal": "NewValue", 851 }, 852 } 853 type args struct { 854 baseDict baseIDict 855 dicts []baseIDict 856 } 857 tests := []struct { 858 name string 859 d baseDict 860 args args 861 want baseIDict 862 }{ 863 {"Empty", nil, args{nil, []baseIDict{}}, baseDict{}}, 864 {"Add map to empty", nil, args{dictFixture, []baseIDict{}}, dictFixture}, 865 {"Add map to same map", dictFixture, args{dictFixture, []baseIDict{}}, dictFixture}, 866 {"Add empty to map", dictFixture, args{nil, []baseIDict{}}, dictFixture}, 867 {"Add new1 to map", dictFixture, args{adding1, []baseIDict{}}, dictFixture.Clone().Merge(adding1)}, 868 {"Add new2 to map", dictFixture, args{adding2, []baseIDict{}}, dictFixture.Clone().Merge(adding2)}, 869 {"Add new1 & new2 to map", dictFixture, args{adding1, []baseIDict{adding2}}, dictFixture.Clone().Merge(adding1, adding2)}, 870 {"Add new1 & new2 to map", dictFixture, args{adding1, []baseIDict{adding2}}, dictFixture.Clone().Merge(adding1).Merge(adding2)}, 871 } 872 for _, tt := range tests { 873 go t.Run(tt.name, func(t *testing.T) { 874 d := tt.d.Clone() 875 got := d.Merge(tt.args.baseDict, tt.args.dicts...) 876 if !reflect.DeepEqual(got, tt.want) { 877 t.Errorf("baseDict.Merge():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 878 dumpKeys(t, got, tt.want) 879 } 880 }) 881 } 882 } 883 884 func Test_dict_Values(t *testing.T) { 885 t.Parallel() 886 887 tests := []struct { 888 name string 889 d baseDict 890 want baseIList 891 }{ 892 {"Empty", nil, baseList{}}, 893 {"Map", dictFixture, baseList{1.23, 123, baseList{1, "two"}, baseList{1, 2, 3}, baseDict{"sub1": 1, "sub2": "two"}, baseDict{"1": 1, "2": "two"}, "Foo bar"}}, 894 } 895 for _, tt := range tests { 896 t.Run(tt.name, func(t *testing.T) { 897 if got := tt.d.GetValues(); !reflect.DeepEqual(got, tt.want) { 898 t.Errorf("baseDict.GetValues():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 899 } 900 }) 901 } 902 } 903 904 func Test_dict_Pop(t *testing.T) { 905 t.Parallel() 906 907 tests := []struct { 908 name string 909 d baseDict 910 args []interface{} 911 want interface{} 912 wantObject baseIDict 913 }{ 914 {"Nil", dictFixture, nil, nil, dictFixture}, 915 {"Pop one element", dictFixture, []interface{}{"float"}, 1.23, dictFixture.Omit("float")}, 916 {"Pop missing element", dictFixture, []interface{}{"undefined"}, nil, dictFixture}, 917 {"Pop element twice", dictFixture, []interface{}{"int", "int", "string"}, baseList{123, 123, "Foo bar"}, dictFixture.Omit("int", "string")}, 918 } 919 for _, tt := range tests { 920 t.Run(tt.name, func(t *testing.T) { 921 d := tt.d.Clone() 922 got := d.Pop(tt.args...) 923 if !reflect.DeepEqual(got, tt.want) { 924 t.Errorf("baseDict.Pop():\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", got, tt.want) 925 } 926 if !reflect.DeepEqual(d, tt.wantObject) { 927 t.Errorf("baseDict.Pop() object:\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", d, tt.wantObject) 928 } 929 }) 930 } 931 } 932 933 func Test_dict_Add(t *testing.T) { 934 t.Parallel() 935 936 type args struct { 937 key interface{} 938 v interface{} 939 } 940 tests := []struct { 941 name string 942 d baseDict 943 args args 944 want baseIDict 945 }{ 946 {"Empty", nil, args{"A", 1}, baseDict{"A": 1}}, 947 {"With element", baseDict{"A": 1}, args{"A", 2}, baseDict{"A": baseList{1, 2}}}, 948 {"With element, another value", baseDict{"A": 1}, args{"B", 2}, baseDict{"A": 1, "B": 2}}, 949 {"With list element", baseDict{"A": baseList{1, 2}}, args{"A", 3}, baseDict{"A": baseList{1, 2, 3}}}, 950 } 951 for _, tt := range tests { 952 t.Run(tt.name, func(t *testing.T) { 953 if got := tt.d.Add(tt.args.key, tt.args.v); !reflect.DeepEqual(got, tt.want) { 954 t.Errorf("baseDict.Add() = %v, want %v", got, tt.want) 955 } 956 }) 957 } 958 } 959 960 func Test_dict_Set(t *testing.T) { 961 t.Parallel() 962 963 type args struct { 964 key interface{} 965 v interface{} 966 } 967 tests := []struct { 968 name string 969 d baseDict 970 args args 971 want baseIDict 972 }{ 973 {"Empty", nil, args{"A", 1}, baseDict{"A": 1}}, 974 {"With element", baseDict{"A": 1}, args{"A", 2}, baseDict{"A": 2}}, 975 {"With element, another value", baseDict{"A": 1}, args{"B", 2}, baseDict{"A": 1, "B": 2}}, 976 } 977 for _, tt := range tests { 978 t.Run(tt.name, func(t *testing.T) { 979 if got := tt.d.Set(tt.args.key, tt.args.v); !reflect.DeepEqual(got, tt.want) { 980 t.Errorf("baseDict.Set() = %v, want %v", got, tt.want) 981 } 982 }) 983 } 984 } 985 986 func Test_dict_Transpose(t *testing.T) { 987 t.Parallel() 988 989 tests := []struct { 990 name string 991 d baseDict 992 want baseIDict 993 }{ 994 {"Empty", nil, baseDict{}}, 995 {"Base", baseDict{"A": 1}, baseDict{"1": str("A")}}, 996 {"Multiple", baseDict{"A": 1, "B": 2, "C": 1}, baseDict{"1": baseList{str("A"), str("C")}, "2": str("B")}}, 997 {"List", baseDict{"A": []int{1, 2, 3}, "B": 2, "C": 3}, baseDict{"1": str("A"), "2": baseList{str("A"), str("B")}, "3": baseList{str("A"), str("C")}}}, 998 {"Complex", baseDict{"A": baseDict{"1": 1, "2": 2}, "B": 2, "C": 3}, baseDict{"2": str("B"), "3": str("C"), fmt.Sprint(baseDict{"1": 1, "2": 2}): str("A")}}, 999 } 1000 for _, tt := range tests { 1001 t.Run(tt.name, func(t *testing.T) { 1002 if got := tt.d.Transpose(); !reflect.DeepEqual(got, tt.want) { 1003 t.Errorf("baseDict.Transpose() = %v, want %v", got, tt.want) 1004 } 1005 }) 1006 } 1007 } 1008 1009 func Test_baseList_Get(t *testing.T) { 1010 type args struct { 1011 indexes []int 1012 } 1013 tests := []struct { 1014 name string 1015 l baseList 1016 args args 1017 want interface{} 1018 }{ 1019 // TODO: Add test cases. 1020 } 1021 for _, tt := range tests { 1022 t.Run(tt.name, func(t *testing.T) { 1023 if got := tt.l.Get(tt.args.indexes...); !reflect.DeepEqual(got, tt.want) { 1024 t.Errorf("baseList.Get() = %v, want %v", got, tt.want) 1025 } 1026 }) 1027 } 1028 } 1029 1030 func Test_baseList_TypeName(t *testing.T) { 1031 tests := []struct { 1032 name string 1033 l baseList 1034 want str 1035 }{ 1036 // TODO: Add test cases. 1037 } 1038 for _, tt := range tests { 1039 t.Run(tt.name, func(t *testing.T) { 1040 if got := tt.l.TypeName(); !reflect.DeepEqual(got, tt.want) { 1041 t.Errorf("baseList.TypeName() = %v, want %v", got, tt.want) 1042 } 1043 }) 1044 } 1045 } 1046 1047 func Test_base_TypeName(t *testing.T) { 1048 t.Run("list", func(t *testing.T) { assert.Equal(t, baseList{}.TypeName(), str("base")) }) 1049 t.Run("dict", func(t *testing.T) { assert.Equal(t, baseDict{}.TypeName(), str("base")) }) 1050 } 1051 1052 func Test_base_GetHelper(t *testing.T) { 1053 t.Run("list", func(t *testing.T) { 1054 gotD, gotL := baseList{}.GetHelpers() 1055 assert.Equal(t, gotD.CreateDictionary().TypeName(), baseDictHelper.CreateDictionary().TypeName()) 1056 assert.Equal(t, gotL.CreateList().TypeName(), baseListHelper.CreateList().TypeName()) 1057 }) 1058 t.Run("dict", func(t *testing.T) { 1059 gotD, gotL := baseDict{}.GetHelpers() 1060 assert.Equal(t, gotD.CreateDictionary().TypeName(), baseDictHelper.CreateDictionary().TypeName()) 1061 assert.Equal(t, gotL.CreateList().TypeName(), baseListHelper.CreateList().TypeName()) 1062 }) 1063 }