gitlab.com/beacon-software/gadget@v0.0.0-20181217202115-54565ea1ed5e/generics/valueholder_test.go (about) 1 package generics 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 9 "gitlab.com/beacon-software/gadget/errors" 10 ) 11 12 type fields struct { 13 value string 14 Error errors.TracerError 15 } 16 17 func TestValueHolder_clean(t *testing.T) { 18 type args struct { 19 prefix string 20 } 21 tests := []struct { 22 name string 23 fields fields 24 args args 25 want string 26 want1 bool 27 }{ 28 { 29 name: "empty value", 30 fields: fields{value: ""}, 31 args: args{prefix: "prefix"}, 32 want: "", 33 want1: false, 34 }, 35 { 36 name: "value does not contain prefix", 37 fields: fields{value: "asdf"}, 38 args: args{prefix: "prefix"}, 39 want: "", 40 want1: false, 41 }, 42 { 43 name: "values contains prefix exactly", 44 fields: fields{value: "prefix"}, 45 args: args{prefix: "prefix"}, 46 want: "", 47 want1: true, 48 }, 49 { 50 name: "works correctly", 51 fields: fields{value: "prefix1234"}, 52 args: args{prefix: "prefix"}, 53 want: "1234", 54 want1: true, 55 }, 56 } 57 for _, tt := range tests { 58 t.Run(tt.name, func(t *testing.T) { 59 vh := &ValueHolder{ 60 Value: tt.fields.value, 61 Error: tt.fields.Error, 62 } 63 got, got1 := vh.clean(tt.args.prefix) 64 if got != tt.want { 65 t.Errorf("ValueHolder.clean() got = %v, want %v", got, tt.want) 66 } 67 if got1 != tt.want1 { 68 t.Errorf("ValueHolder.clean() got1 = %v, want %v", got1, tt.want1) 69 } 70 }) 71 } 72 } 73 74 func TestNewValueHolder(t *testing.T) { 75 type args struct { 76 value interface{} 77 } 78 tests := []struct { 79 name string 80 args args 81 want *ValueHolder 82 want1 bool 83 }{ 84 { 85 name: "string", 86 args: args{value: "string"}, 87 want: &ValueHolder{Value: stringPrefix + "string"}, 88 want1: true, 89 }, 90 { 91 name: "empty string", 92 args: args{value: ""}, 93 want: &ValueHolder{Value: stringPrefix}, 94 want1: true, 95 }, 96 { 97 name: "int", 98 args: args{value: 10}, 99 want: &ValueHolder{Value: intPrefix + "10"}, 100 want1: true, 101 }, 102 { 103 name: "int32", 104 args: args{value: int32(10)}, 105 want: &ValueHolder{Value: int32Prefix + "10"}, 106 want1: true, 107 }, 108 { 109 name: "int64", 110 args: args{value: int64(10)}, 111 want: &ValueHolder{Value: int64Prefix + "10"}, 112 want1: true, 113 }, 114 { 115 name: "uint", 116 args: args{value: uint(10)}, 117 want: &ValueHolder{Value: uIntPrefix + "10"}, 118 want1: true, 119 }, 120 { 121 name: "uint32", 122 args: args{value: uint32(10)}, 123 want: &ValueHolder{Value: uInt32Prefix + "10"}, 124 want1: true, 125 }, 126 { 127 name: "uint64", 128 args: args{value: uint64(10)}, 129 want: &ValueHolder{Value: uInt64Prefix + "10"}, 130 want1: true, 131 }, 132 { 133 name: "float32", 134 args: args{value: float32(10.01)}, 135 want: &ValueHolder{Value: float32Prefix + "10.01"}, 136 want1: true, 137 }, 138 { 139 name: "float64", 140 args: args{value: float64(10.1234)}, 141 want: &ValueHolder{Value: float64Prefix + "10.1234"}, 142 want1: true, 143 }, 144 { 145 name: "bool", 146 args: args{value: true}, 147 want: &ValueHolder{Value: boolPrefix + "true"}, 148 want1: true, 149 }, 150 { 151 name: "error", 152 args: args{value: nil}, 153 want: &ValueHolder{Value: "", Error: NewUnsupportedTypeError(nil)}, 154 want1: false, 155 }, 156 } 157 for _, tt := range tests { 158 t.Run(tt.name, func(t *testing.T) { 159 got, got1 := NewValueHolder(tt.args.value) 160 if !reflect.DeepEqual(got.Value, tt.want.Value) { 161 t.Errorf("NewValueHolder().value got = %v, want %v", got.Value, tt.want.Value) 162 } 163 if !(reflect.DeepEqual(got.Error, tt.want.Error) || reflect.DeepEqual(got.Error.Error(), tt.want.Error.Error())) { 164 t.Errorf("NewValueHolder().Error got = %v, want %v", got.Error, tt.want.Error) 165 } 166 if got1 != tt.want1 { 167 t.Errorf("NewValueHolder() got1 = %v, want %v", got1, tt.want1) 168 } 169 }) 170 } 171 } 172 173 func TestValueHolder_GetRaw(t *testing.T) { 174 tests := []struct { 175 name string 176 fields fields 177 want string 178 }{ 179 { 180 fields: fields{value: "asdf", Error: nil}, 181 want: "asdf", 182 }, 183 } 184 for _, tt := range tests { 185 t.Run(tt.name, func(t *testing.T) { 186 vh := &ValueHolder{ 187 Value: tt.fields.value, 188 Error: tt.fields.Error, 189 } 190 if got := vh.GetRaw(); got != tt.want { 191 t.Errorf("ValueHolder.GetRaw() = %v, want %v", got, tt.want) 192 } 193 }) 194 } 195 } 196 197 func TestValueHolder_SetRaw(t *testing.T) { 198 type args struct { 199 value string 200 } 201 tests := []struct { 202 name string 203 fields fields 204 args args 205 }{ 206 { 207 fields: fields{value: "awef"}, 208 args: args{value: "awef"}, 209 }, 210 } 211 for _, tt := range tests { 212 t.Run(tt.name, func(t *testing.T) { 213 vh := &ValueHolder{ 214 Value: tt.fields.value, 215 Error: tt.fields.Error, 216 } 217 vh.SetRaw(tt.args.value) 218 }) 219 } 220 } 221 222 func TestValueHolder_Set(t *testing.T) { 223 type args struct { 224 value interface{} 225 } 226 tests := []struct { 227 name string 228 fields fields 229 args args 230 want bool 231 }{ 232 { 233 name: "int", 234 fields: fields{value: intPrefix + "10", Error: nil}, 235 args: args{value: int(10)}, 236 want: true, 237 }, 238 { 239 name: "int32", 240 fields: fields{value: int32Prefix + "10", Error: nil}, 241 args: args{value: int32(10)}, 242 want: true, 243 }, 244 { 245 name: "int64", 246 fields: fields{value: int64Prefix + "10", Error: nil}, 247 args: args{value: int64(10)}, 248 want: true, 249 }, 250 { 251 name: "uInt", 252 fields: fields{value: uIntPrefix + "10", Error: nil}, 253 args: args{value: uint(10)}, 254 want: true, 255 }, 256 { 257 name: "uInt32", 258 fields: fields{value: uInt32Prefix + "10", Error: nil}, 259 args: args{value: uint32(10)}, 260 want: true, 261 }, 262 { 263 name: "uInt64", 264 fields: fields{value: uInt64Prefix + "10", Error: nil}, 265 args: args{value: uint64(10)}, 266 want: true, 267 }, 268 { 269 name: "float32", 270 fields: fields{value: float32Prefix + "10.1234", Error: nil}, 271 args: args{value: float32(10.1234)}, 272 want: true, 273 }, 274 { 275 name: "float64", 276 fields: fields{value: float64Prefix + "10.1234", Error: nil}, 277 args: args{value: float64(10.1234)}, 278 want: true, 279 }, 280 { 281 name: "string", 282 fields: fields{value: stringPrefix + "asdf", Error: nil}, 283 args: args{value: "asdf"}, 284 want: true, 285 }, 286 { 287 name: "bool", 288 fields: fields{value: boolPrefix + "true", Error: nil}, 289 args: args{value: true}, 290 want: true, 291 }, 292 { 293 name: "error", 294 fields: fields{value: "monkeys", Error: NewCorruptValueError("monkeys")}, 295 args: args{value: nil}, 296 want: false, 297 }, 298 } 299 for _, tt := range tests { 300 t.Run(tt.name, func(t *testing.T) { 301 vh := &ValueHolder{ 302 Value: tt.fields.value, 303 Error: tt.fields.Error, 304 } 305 if got := vh.Set(tt.args.value); got != tt.want { 306 t.Errorf("ValueHolder.Set() = %v, want %v", got, tt.want) 307 } 308 }) 309 } 310 } 311 312 func TestValueHolder_Get(t *testing.T) { 313 tests := []struct { 314 name string 315 fields fields 316 want interface{} 317 want1 bool 318 }{ 319 { 320 name: "int", 321 fields: fields{value: intPrefix + "10", Error: nil}, 322 want: int(10), 323 want1: true, 324 }, 325 { 326 name: "int32", 327 fields: fields{value: int32Prefix + "10", Error: nil}, 328 want: int32(10), 329 want1: true, 330 }, 331 { 332 name: "int64", 333 fields: fields{value: int64Prefix + "10", Error: nil}, 334 want: int64(10), 335 want1: true, 336 }, 337 { 338 name: "uInt", 339 fields: fields{value: uIntPrefix + "10", Error: nil}, 340 want: uint(10), 341 want1: true, 342 }, 343 { 344 name: "uInt32", 345 fields: fields{value: uInt32Prefix + "10", Error: nil}, 346 want: uint32(10), 347 want1: true, 348 }, 349 { 350 name: "uInt64", 351 fields: fields{value: uInt64Prefix + "10", Error: nil}, 352 want: uint64(10), 353 want1: true, 354 }, 355 { 356 name: "float32", 357 fields: fields{value: float32Prefix + "10.1234", Error: nil}, 358 want: float32(10.1234), 359 want1: true, 360 }, 361 { 362 name: "float64", 363 fields: fields{value: float64Prefix + "10.1234", Error: nil}, 364 want: float64(10.1234), 365 want1: true, 366 }, 367 { 368 name: "string", 369 fields: fields{value: stringPrefix + "asdf", Error: nil}, 370 want: "asdf", 371 want1: true, 372 }, 373 { 374 name: "bool", 375 fields: fields{value: boolPrefix + "true", Error: nil}, 376 want: true, 377 want1: true, 378 }, 379 { 380 name: "error", 381 fields: fields{value: "monkeys"}, 382 want: nil, 383 want1: false, 384 }, 385 } 386 for _, tt := range tests { 387 t.Run(tt.name, func(t *testing.T) { 388 vh := &ValueHolder{ 389 Value: tt.fields.value, 390 Error: tt.fields.Error, 391 } 392 got, got1 := vh.Get() 393 if !reflect.DeepEqual(got, tt.want) { 394 t.Errorf("ValueHolder.Get() got = %v, want %v", got, tt.want) 395 } 396 if got1 != tt.want1 { 397 t.Errorf("ValueHolder.Get() got1 = %v, want %v", got1, tt.want1) 398 } 399 }) 400 } 401 } 402 403 func TestValueHolder_getInt(t *testing.T) { 404 type args struct { 405 prefix string 406 size int 407 } 408 tests := []struct { 409 name string 410 fields fields 411 args args 412 want int 413 want1 bool 414 }{ 415 { 416 name: "fail no prefix", 417 fields: fields{value: "10"}, 418 want: 0, 419 want1: false, 420 }, 421 { 422 name: "fail bad parse", 423 fields: fields{value: intPrefix + "asdf"}, 424 want: 0, 425 want1: false, 426 }, 427 { 428 name: "int prefix", 429 fields: fields{value: intPrefix + "10"}, 430 want: 10, 431 want1: true, 432 }, 433 { 434 name: "int32 prefix", 435 fields: fields{value: int32Prefix + "10"}, 436 want: 10, 437 want1: true, 438 }, 439 { 440 name: "int64 prefix", 441 fields: fields{value: int64Prefix + "10"}, 442 want: 10, 443 want1: true, 444 }, 445 } 446 for _, tt := range tests { 447 t.Run(tt.name, func(t *testing.T) { 448 vh := &ValueHolder{ 449 Value: tt.fields.value, 450 Error: tt.fields.Error, 451 } 452 got, got1 := vh.GetInt() 453 if got != tt.want { 454 t.Errorf("ValueHolder.getInt() got = %v, want %v", got, tt.want) 455 } 456 if got1 != tt.want1 { 457 t.Errorf("ValueHolder.getInt() got1 = %v, want %v", got1, tt.want1) 458 } 459 }) 460 } 461 } 462 463 func TestValueHolder_getUInt(t *testing.T) { 464 tests := []struct { 465 name string 466 fields fields 467 want uint 468 want1 bool 469 }{ 470 { 471 name: "fail no prefix", 472 fields: fields{value: "10"}, 473 want: 0, 474 want1: false, 475 }, 476 { 477 name: "fail bad parse", 478 fields: fields{value: uIntPrefix + "asdf"}, 479 want: 0, 480 want1: false, 481 }, 482 { 483 name: "works", 484 fields: fields{value: uIntPrefix + "10"}, 485 want: 10, 486 want1: true, 487 }, 488 { 489 name: "uInt32", 490 fields: fields{value: uInt32Prefix + "10"}, 491 want: 10, 492 want1: true, 493 }, 494 { 495 name: "uInt64", 496 fields: fields{value: uInt64Prefix + "10"}, 497 want: 10, 498 want1: true, 499 }, 500 } 501 for _, tt := range tests { 502 t.Run(tt.name, func(t *testing.T) { 503 vh := &ValueHolder{ 504 Value: tt.fields.value, 505 Error: tt.fields.Error, 506 } 507 got, got1 := vh.GetUint() 508 assert.Equal(t, tt.want, got) 509 assert.Equal(t, tt.want1, got1) 510 }) 511 } 512 } 513 514 func TestValueHolder_GetBool(t *testing.T) { 515 tests := []struct { 516 name string 517 fields fields 518 want bool 519 want1 bool 520 }{ 521 { 522 name: "works", 523 fields: fields{value: boolPrefix + "true"}, 524 want: true, 525 want1: true, 526 }, 527 { 528 name: "bad prefix", 529 fields: fields{value: uIntPrefix + "true"}, 530 want: false, 531 want1: false, 532 }, 533 { 534 name: "bad parse", 535 fields: fields{value: boolPrefix + "tralse"}, 536 want: false, 537 want1: false, 538 }, 539 } 540 for _, tt := range tests { 541 t.Run(tt.name, func(t *testing.T) { 542 vh := &ValueHolder{ 543 Value: tt.fields.value, 544 Error: tt.fields.Error, 545 } 546 got, got1 := vh.GetBool() 547 if got != tt.want { 548 t.Errorf("ValueHolder.GetBool() got = %v, want %v", got, tt.want) 549 } 550 if got1 != tt.want1 { 551 t.Errorf("ValueHolder.GetBool() got1 = %v, want %v", got1, tt.want1) 552 } 553 }) 554 } 555 } 556 557 func TestValueHolder_getFloat(t *testing.T) { 558 type args struct { 559 prefix string 560 size int 561 } 562 tests := []struct { 563 name string 564 fields fields 565 args args 566 want float64 567 want1 bool 568 }{ 569 { 570 name: "fail no prefix", 571 fields: fields{value: "10"}, 572 args: args{prefix: float32Prefix, size: 32}, 573 want: 0, 574 want1: false, 575 }, 576 { 577 name: "fail bad parse", 578 fields: fields{value: float32Prefix + "asdf"}, 579 args: args{prefix: float32Prefix, size: 32}, 580 want: 0, 581 want1: false, 582 }, 583 { 584 name: "works", 585 fields: fields{value: float64Prefix + "10.1"}, 586 args: args{prefix: float64Prefix, size: 64}, 587 want: 10.1, 588 want1: true, 589 }, 590 } 591 for _, tt := range tests { 592 t.Run(tt.name, func(t *testing.T) { 593 vh := &ValueHolder{ 594 Value: tt.fields.value, 595 Error: tt.fields.Error, 596 } 597 got, got1 := vh.getFloat(tt.args.prefix, tt.args.size) 598 if got != tt.want { 599 t.Errorf("ValueHolder.getFloat() got = %v, want %v", got, tt.want) 600 } 601 if got1 != tt.want1 { 602 t.Errorf("ValueHolder.getFloat() got1 = %v, want %v", got1, tt.want1) 603 } 604 }) 605 } 606 } 607 608 func TestValueHolder_ParseSet(t *testing.T) { 609 type args struct { 610 value string 611 fieldType string 612 } 613 tests := []struct { 614 name string 615 args args 616 want string 617 wantErr bool 618 }{ 619 { 620 name: "string", 621 args: args{ 622 value: "foo", 623 fieldType: TypeString, 624 }, 625 want: "str:foo", 626 wantErr: false, 627 }, 628 { 629 name: "bool", 630 args: args{ 631 value: "true", 632 fieldType: TypeBoolean, 633 }, 634 want: "bln:true", 635 wantErr: false, 636 }, 637 { 638 name: "int", 639 args: args{ 640 value: "12", 641 fieldType: TypeInteger, 642 }, 643 want: "i64:12", 644 wantErr: false, 645 }, 646 { 647 name: "JSON", 648 args: args{ 649 value: `{"h":0,"i":1}`, 650 fieldType: TypeJSON, 651 }, 652 want: `str:{"h":0,"i":1}`, 653 wantErr: false, 654 }, 655 { 656 name: "float", 657 args: args{ 658 value: "10", 659 fieldType: TypeFloat, 660 }, 661 want: "f64:10", 662 wantErr: false, 663 }, 664 { 665 name: "error", 666 args: args{ 667 value: "10", 668 fieldType: "unsupported", 669 }, 670 want: "", 671 wantErr: true, 672 }, 673 } 674 for _, tt := range tests { 675 t.Run(tt.name, func(t *testing.T) { 676 vh, _ := NewValueHolder(nil) 677 err := vh.ParseSet(tt.args.value, tt.args.fieldType) 678 if (err != nil) != tt.wantErr { 679 t.Errorf("GetDatabaseValue() error = %v, wantErr %v", err, tt.wantErr) 680 return 681 } 682 if vh.GetRaw() != tt.want { 683 t.Errorf("GetDatabaseValue() = %v, want %v", vh.GetRaw(), tt.want) 684 } 685 }) 686 } 687 }