encode_number_uint_test.go (17767B)
1 package gojay 2 3 import ( 4 "math" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestEncoderUint64(t *testing.T) { 12 var testCasesBasic = []struct { 13 name string 14 v uint64 15 expectedJSON string 16 }{ 17 { 18 name: "basic", 19 v: uint64(1), 20 expectedJSON: "[1,1]", 21 }, 22 { 23 name: "big", 24 v: math.MaxUint64, 25 expectedJSON: "[18446744073709551615,18446744073709551615]", 26 }, 27 { 28 name: "big", 29 v: uint64(0), 30 expectedJSON: "[0,0]", 31 }, 32 } 33 for _, testCase := range testCasesBasic { 34 t.Run(testCase.name, func(t *testing.T) { 35 var b = &strings.Builder{} 36 var enc = NewEncoder(b) 37 enc.Encode(EncodeArrayFunc(func(enc *Encoder) { 38 enc.Uint64(testCase.v) 39 enc.AddUint64(testCase.v) 40 })) 41 assert.Equal(t, testCase.expectedJSON, b.String()) 42 }) 43 } 44 var testCasesOmitEmpty = []struct { 45 name string 46 v uint64 47 expectedJSON string 48 }{ 49 { 50 name: "basic", 51 v: uint64(1), 52 expectedJSON: "[1,1]", 53 }, 54 { 55 name: "big", 56 v: math.MaxUint64, 57 expectedJSON: "[18446744073709551615,18446744073709551615]", 58 }, 59 { 60 name: "big", 61 v: uint64(0), 62 expectedJSON: "[]", 63 }, 64 } 65 for _, testCase := range testCasesOmitEmpty { 66 t.Run(testCase.name, func(t *testing.T) { 67 var b = &strings.Builder{} 68 var enc = NewEncoder(b) 69 enc.Encode(EncodeArrayFunc(func(enc *Encoder) { 70 enc.Uint64OmitEmpty(testCase.v) 71 enc.AddUint64OmitEmpty(testCase.v) 72 })) 73 assert.Equal(t, testCase.expectedJSON, b.String()) 74 }) 75 } 76 var testCasesKeyBasic = []struct { 77 name string 78 v uint64 79 expectedJSON string 80 }{ 81 { 82 name: "basic", 83 v: uint64(1), 84 expectedJSON: `{"foo":1,"bar":1}`, 85 }, 86 { 87 name: "big", 88 v: math.MaxUint64, 89 expectedJSON: `{"foo":18446744073709551615,"bar":18446744073709551615}`, 90 }, 91 { 92 name: "big", 93 v: uint64(0), 94 expectedJSON: `{"foo":0,"bar":0}`, 95 }, 96 } 97 for _, testCase := range testCasesKeyBasic { 98 t.Run(testCase.name, func(t *testing.T) { 99 var b = &strings.Builder{} 100 var enc = NewEncoder(b) 101 enc.Encode(EncodeObjectFunc(func(enc *Encoder) { 102 enc.Uint64Key("foo", testCase.v) 103 enc.AddUint64Key("bar", testCase.v) 104 })) 105 assert.Equal(t, testCase.expectedJSON, b.String()) 106 }) 107 } 108 var testCasesKeyOmitEmpty = []struct { 109 name string 110 v uint64 111 expectedJSON string 112 }{ 113 { 114 name: "basic", 115 v: uint64(1), 116 expectedJSON: `{"foo":1,"bar":1}`, 117 }, 118 { 119 name: "big", 120 v: math.MaxUint64, 121 expectedJSON: `{"foo":18446744073709551615,"bar":18446744073709551615}`, 122 }, 123 { 124 name: "big", 125 v: uint64(0), 126 expectedJSON: "{}", 127 }, 128 } 129 for _, testCase := range testCasesKeyOmitEmpty { 130 t.Run(testCase.name, func(t *testing.T) { 131 var b = &strings.Builder{} 132 var enc = NewEncoder(b) 133 enc.Encode(EncodeObjectFunc(func(enc *Encoder) { 134 enc.Uint64KeyOmitEmpty("foo", testCase.v) 135 enc.AddUint64KeyOmitEmpty("bar", testCase.v) 136 })) 137 assert.Equal(t, testCase.expectedJSON, b.String()) 138 }) 139 } 140 } 141 142 func TestEncoderUint32(t *testing.T) { 143 var testCasesBasic = []struct { 144 name string 145 v uint32 146 expectedJSON string 147 }{ 148 { 149 name: "basic", 150 v: uint32(1), 151 expectedJSON: "[1,1]", 152 }, 153 { 154 name: "big", 155 v: math.MaxUint32, 156 expectedJSON: "[4294967295,4294967295]", 157 }, 158 { 159 name: "big", 160 v: uint32(0), 161 expectedJSON: "[0,0]", 162 }, 163 } 164 for _, testCase := range testCasesBasic { 165 t.Run(testCase.name, func(t *testing.T) { 166 var b = &strings.Builder{} 167 var enc = NewEncoder(b) 168 enc.Encode(EncodeArrayFunc(func(enc *Encoder) { 169 enc.Uint32(testCase.v) 170 enc.AddUint32(testCase.v) 171 })) 172 assert.Equal(t, testCase.expectedJSON, b.String()) 173 }) 174 } 175 var testCasesOmitEmpty = []struct { 176 name string 177 v uint32 178 expectedJSON string 179 }{ 180 { 181 name: "basic", 182 v: uint32(1), 183 expectedJSON: "[1,1]", 184 }, 185 { 186 name: "big", 187 v: math.MaxUint32, 188 expectedJSON: "[4294967295,4294967295]", 189 }, 190 { 191 name: "big", 192 v: uint32(0), 193 expectedJSON: "[]", 194 }, 195 } 196 for _, testCase := range testCasesOmitEmpty { 197 t.Run(testCase.name, func(t *testing.T) { 198 var b = &strings.Builder{} 199 var enc = NewEncoder(b) 200 enc.Encode(EncodeArrayFunc(func(enc *Encoder) { 201 enc.Uint32OmitEmpty(testCase.v) 202 enc.AddUint32OmitEmpty(testCase.v) 203 })) 204 assert.Equal(t, testCase.expectedJSON, b.String()) 205 }) 206 } 207 var testCasesKeyBasic = []struct { 208 name string 209 v uint32 210 expectedJSON string 211 }{ 212 { 213 name: "basic", 214 v: uint32(1), 215 expectedJSON: `{"foo":1,"bar":1}`, 216 }, 217 { 218 name: "big", 219 v: math.MaxUint32, 220 expectedJSON: `{"foo":4294967295,"bar":4294967295}`, 221 }, 222 { 223 name: "big", 224 v: uint32(0), 225 expectedJSON: `{"foo":0,"bar":0}`, 226 }, 227 } 228 for _, testCase := range testCasesKeyBasic { 229 t.Run(testCase.name, func(t *testing.T) { 230 var b = &strings.Builder{} 231 var enc = NewEncoder(b) 232 enc.Encode(EncodeObjectFunc(func(enc *Encoder) { 233 enc.Uint32Key("foo", testCase.v) 234 enc.AddUint32Key("bar", testCase.v) 235 })) 236 assert.Equal(t, testCase.expectedJSON, b.String()) 237 }) 238 } 239 var testCasesKeyOmitEmpty = []struct { 240 name string 241 v uint32 242 expectedJSON string 243 }{ 244 { 245 name: "basic", 246 v: uint32(1), 247 expectedJSON: `{"foo":1,"bar":1}`, 248 }, 249 { 250 name: "big", 251 v: math.MaxUint32, 252 expectedJSON: `{"foo":4294967295,"bar":4294967295}`, 253 }, 254 { 255 name: "big", 256 v: uint32(0), 257 expectedJSON: `{}`, 258 }, 259 } 260 for _, testCase := range testCasesKeyOmitEmpty { 261 t.Run(testCase.name, func(t *testing.T) { 262 var b = &strings.Builder{} 263 var enc = NewEncoder(b) 264 enc.Encode(EncodeObjectFunc(func(enc *Encoder) { 265 enc.Uint32KeyOmitEmpty("foo", testCase.v) 266 enc.AddUint32KeyOmitEmpty("bar", testCase.v) 267 })) 268 assert.Equal(t, testCase.expectedJSON, b.String()) 269 }) 270 } 271 } 272 273 func TestEncoderUint16(t *testing.T) { 274 var testCasesBasic = []struct { 275 name string 276 v uint16 277 expectedJSON string 278 }{ 279 { 280 name: "basic", 281 v: uint16(1), 282 expectedJSON: "[1,1]", 283 }, 284 { 285 name: "big", 286 v: math.MaxUint16, 287 expectedJSON: "[65535,65535]", 288 }, 289 { 290 name: "big", 291 v: uint16(0), 292 expectedJSON: "[0,0]", 293 }, 294 } 295 for _, testCase := range testCasesBasic { 296 t.Run(testCase.name, func(t *testing.T) { 297 var b = &strings.Builder{} 298 var enc = NewEncoder(b) 299 enc.Encode(EncodeArrayFunc(func(enc *Encoder) { 300 enc.Uint16(testCase.v) 301 enc.AddUint16(testCase.v) 302 })) 303 assert.Equal(t, testCase.expectedJSON, b.String()) 304 }) 305 } 306 var testCasesOmitEmpty = []struct { 307 name string 308 v uint16 309 expectedJSON string 310 }{ 311 { 312 name: "basic", 313 v: uint16(1), 314 expectedJSON: "[1,1]", 315 }, 316 { 317 name: "big", 318 v: math.MaxUint16, 319 expectedJSON: "[65535,65535]", 320 }, 321 { 322 name: "big", 323 v: uint16(0), 324 expectedJSON: "[]", 325 }, 326 } 327 for _, testCase := range testCasesOmitEmpty { 328 t.Run(testCase.name, func(t *testing.T) { 329 var b = &strings.Builder{} 330 var enc = NewEncoder(b) 331 enc.Encode(EncodeArrayFunc(func(enc *Encoder) { 332 enc.Uint16OmitEmpty(testCase.v) 333 enc.AddUint16OmitEmpty(testCase.v) 334 })) 335 assert.Equal(t, testCase.expectedJSON, b.String()) 336 }) 337 } 338 var testCasesKeyBasic = []struct { 339 name string 340 v uint16 341 expectedJSON string 342 }{ 343 { 344 name: "basic", 345 v: uint16(1), 346 expectedJSON: `{"foo":1,"bar":1}`, 347 }, 348 { 349 name: "big", 350 v: math.MaxUint16, 351 expectedJSON: `{"foo":65535,"bar":65535}`, 352 }, 353 { 354 name: "big", 355 v: uint16(0), 356 expectedJSON: `{"foo":0,"bar":0}`, 357 }, 358 } 359 for _, testCase := range testCasesKeyBasic { 360 t.Run(testCase.name, func(t *testing.T) { 361 var b = &strings.Builder{} 362 var enc = NewEncoder(b) 363 enc.Encode(EncodeObjectFunc(func(enc *Encoder) { 364 enc.Uint16Key("foo", testCase.v) 365 enc.AddUint16Key("bar", testCase.v) 366 })) 367 assert.Equal(t, testCase.expectedJSON, b.String()) 368 }) 369 } 370 var testCasesKeyOmitEmpty = []struct { 371 name string 372 v uint16 373 expectedJSON string 374 }{ 375 { 376 name: "basic", 377 v: uint16(1), 378 expectedJSON: `{"foo":1,"bar":1}`, 379 }, 380 { 381 name: "big", 382 v: math.MaxUint16, 383 expectedJSON: `{"foo":65535,"bar":65535}`, 384 }, 385 { 386 name: "big", 387 v: uint16(0), 388 expectedJSON: `{}`, 389 }, 390 } 391 for _, testCase := range testCasesKeyOmitEmpty { 392 t.Run(testCase.name, func(t *testing.T) { 393 var b = &strings.Builder{} 394 var enc = NewEncoder(b) 395 enc.Encode(EncodeObjectFunc(func(enc *Encoder) { 396 enc.Uint16KeyOmitEmpty("foo", testCase.v) 397 enc.AddUint16KeyOmitEmpty("bar", testCase.v) 398 })) 399 assert.Equal(t, testCase.expectedJSON, b.String()) 400 }) 401 } 402 } 403 404 func TestEncoderUint8(t *testing.T) { 405 var testCasesBasic = []struct { 406 name string 407 v uint8 408 expectedJSON string 409 }{ 410 { 411 name: "basic", 412 v: uint8(1), 413 expectedJSON: "[1,1]", 414 }, 415 { 416 name: "big", 417 v: math.MaxUint8, 418 expectedJSON: "[255,255]", 419 }, 420 { 421 name: "big", 422 v: uint8(0), 423 expectedJSON: "[0,0]", 424 }, 425 } 426 for _, testCase := range testCasesBasic { 427 t.Run(testCase.name, func(t *testing.T) { 428 var b = &strings.Builder{} 429 var enc = NewEncoder(b) 430 enc.Encode(EncodeArrayFunc(func(enc *Encoder) { 431 enc.Uint8(testCase.v) 432 enc.AddUint8(testCase.v) 433 })) 434 assert.Equal(t, testCase.expectedJSON, b.String()) 435 }) 436 } 437 var testCasesOmitEmpty = []struct { 438 name string 439 v uint8 440 expectedJSON string 441 }{ 442 { 443 name: "basic", 444 v: uint8(1), 445 expectedJSON: "[1,1]", 446 }, 447 { 448 name: "big", 449 v: math.MaxUint8, 450 expectedJSON: "[255,255]", 451 }, 452 { 453 name: "big", 454 v: uint8(0), 455 expectedJSON: "[]", 456 }, 457 } 458 for _, testCase := range testCasesOmitEmpty { 459 t.Run(testCase.name, func(t *testing.T) { 460 var b = &strings.Builder{} 461 var enc = NewEncoder(b) 462 enc.Encode(EncodeArrayFunc(func(enc *Encoder) { 463 enc.Uint8OmitEmpty(testCase.v) 464 enc.AddUint8OmitEmpty(testCase.v) 465 })) 466 assert.Equal(t, testCase.expectedJSON, b.String()) 467 }) 468 } 469 var testCasesKeyBasic = []struct { 470 name string 471 v uint8 472 expectedJSON string 473 }{ 474 { 475 name: "basic", 476 v: uint8(1), 477 expectedJSON: `{"foo":1,"bar":1}`, 478 }, 479 { 480 name: "big", 481 v: math.MaxUint8, 482 expectedJSON: `{"foo":255,"bar":255}`, 483 }, 484 { 485 name: "big", 486 v: uint8(0), 487 expectedJSON: `{"foo":0,"bar":0}`, 488 }, 489 } 490 for _, testCase := range testCasesKeyBasic { 491 t.Run(testCase.name, func(t *testing.T) { 492 var b = &strings.Builder{} 493 var enc = NewEncoder(b) 494 enc.Encode(EncodeObjectFunc(func(enc *Encoder) { 495 enc.Uint8Key("foo", testCase.v) 496 enc.AddUint8Key("bar", testCase.v) 497 })) 498 assert.Equal(t, testCase.expectedJSON, b.String()) 499 }) 500 } 501 var testCasesKeyOmitEmpty = []struct { 502 name string 503 v uint8 504 expectedJSON string 505 }{ 506 { 507 name: "basic", 508 v: uint8(1), 509 expectedJSON: `{"foo":1,"bar":1}`, 510 }, 511 { 512 name: "big", 513 v: math.MaxUint8, 514 expectedJSON: `{"foo":255,"bar":255}`, 515 }, 516 { 517 name: "big", 518 v: uint8(0), 519 expectedJSON: `{}`, 520 }, 521 } 522 for _, testCase := range testCasesKeyOmitEmpty { 523 t.Run(testCase.name, func(t *testing.T) { 524 var b = &strings.Builder{} 525 var enc = NewEncoder(b) 526 enc.Encode(EncodeObjectFunc(func(enc *Encoder) { 527 enc.Uint8KeyOmitEmpty("foo", testCase.v) 528 enc.AddUint8KeyOmitEmpty("bar", testCase.v) 529 })) 530 assert.Equal(t, testCase.expectedJSON, b.String()) 531 }) 532 } 533 } 534 535 func TestEncoderUint64NullEmpty(t *testing.T) { 536 var testCases = []struct { 537 name string 538 baseJSON string 539 expectedJSON string 540 }{ 541 { 542 name: "basic 1st elem", 543 baseJSON: "[", 544 expectedJSON: `[null,1`, 545 }, 546 { 547 name: "basic 2nd elem", 548 baseJSON: `["test"`, 549 expectedJSON: `["test",null,1`, 550 }, 551 } 552 for _, testCase := range testCases { 553 t.Run("true", func(t *testing.T) { 554 var b strings.Builder 555 var enc = NewEncoder(&b) 556 enc.writeString(testCase.baseJSON) 557 enc.Uint64NullEmpty(0) 558 enc.AddUint64NullEmpty(1) 559 enc.Write() 560 assert.Equal(t, testCase.expectedJSON, b.String()) 561 }) 562 } 563 } 564 565 func TestEncoderUint64KeyNullEmpty(t *testing.T) { 566 var testCases = []struct { 567 name string 568 baseJSON string 569 expectedJSON string 570 }{ 571 { 572 name: "basic 1st elem", 573 baseJSON: "{", 574 expectedJSON: `{"foo":null,"bar":1`, 575 }, 576 { 577 name: "basic 2nd elem", 578 baseJSON: `{"test":"test"`, 579 expectedJSON: `{"test":"test","foo":null,"bar":1`, 580 }, 581 } 582 for _, testCase := range testCases { 583 t.Run(testCase.name, func(t *testing.T) { 584 var b strings.Builder 585 var enc = NewEncoder(&b) 586 enc.writeString(testCase.baseJSON) 587 enc.Uint64KeyNullEmpty("foo", 0) 588 enc.AddUint64KeyNullEmpty("bar", 1) 589 enc.Write() 590 assert.Equal(t, testCase.expectedJSON, b.String()) 591 }) 592 } 593 } 594 595 func TestEncoderUint32NullEmpty(t *testing.T) { 596 var testCases = []struct { 597 name string 598 baseJSON string 599 expectedJSON string 600 }{ 601 { 602 name: "basic 1st elem", 603 baseJSON: "[", 604 expectedJSON: `[null,1`, 605 }, 606 { 607 name: "basic 2nd elem", 608 baseJSON: `["test"`, 609 expectedJSON: `["test",null,1`, 610 }, 611 } 612 for _, testCase := range testCases { 613 t.Run("true", func(t *testing.T) { 614 var b strings.Builder 615 var enc = NewEncoder(&b) 616 enc.writeString(testCase.baseJSON) 617 enc.Uint32NullEmpty(0) 618 enc.AddUint32NullEmpty(1) 619 enc.Write() 620 assert.Equal(t, testCase.expectedJSON, b.String()) 621 }) 622 } 623 } 624 625 func TestEncoderUint32KeyNullEmpty(t *testing.T) { 626 var testCases = []struct { 627 name string 628 baseJSON string 629 expectedJSON string 630 }{ 631 { 632 name: "basic 1st elem", 633 baseJSON: "{", 634 expectedJSON: `{"foo":null,"bar":1`, 635 }, 636 { 637 name: "basic 2nd elem", 638 baseJSON: `{"test":"test"`, 639 expectedJSON: `{"test":"test","foo":null,"bar":1`, 640 }, 641 } 642 for _, testCase := range testCases { 643 t.Run(testCase.name, func(t *testing.T) { 644 var b strings.Builder 645 var enc = NewEncoder(&b) 646 enc.writeString(testCase.baseJSON) 647 enc.AddUint32KeyNullEmpty("foo", 0) 648 enc.Uint32KeyNullEmpty("bar", uint32(1)) 649 enc.Write() 650 assert.Equal(t, testCase.expectedJSON, b.String()) 651 }) 652 } 653 } 654 655 func TestEncoderUint16NullEmpty(t *testing.T) { 656 var testCases = []struct { 657 name string 658 baseJSON string 659 expectedJSON string 660 }{ 661 { 662 name: "basic 1st elem", 663 baseJSON: "[", 664 expectedJSON: `[null,1`, 665 }, 666 { 667 name: "basic 2nd elem", 668 baseJSON: `["test"`, 669 expectedJSON: `["test",null,1`, 670 }, 671 } 672 for _, testCase := range testCases { 673 t.Run("true", func(t *testing.T) { 674 var b strings.Builder 675 var enc = NewEncoder(&b) 676 enc.writeString(testCase.baseJSON) 677 enc.AddUint16NullEmpty(0) 678 enc.Uint16NullEmpty(1) 679 enc.Write() 680 assert.Equal(t, testCase.expectedJSON, b.String()) 681 }) 682 } 683 } 684 685 func TestEncoderUint16KeyNullEmpty(t *testing.T) { 686 var testCases = []struct { 687 name string 688 baseJSON string 689 expectedJSON string 690 }{ 691 { 692 name: "basic 1st elem", 693 baseJSON: "{", 694 expectedJSON: `{"foo":null,"bar":1`, 695 }, 696 { 697 name: "basic 2nd elem", 698 baseJSON: `{"test":"test"`, 699 expectedJSON: `{"test":"test","foo":null,"bar":1`, 700 }, 701 } 702 for _, testCase := range testCases { 703 t.Run(testCase.name, func(t *testing.T) { 704 var b strings.Builder 705 var enc = NewEncoder(&b) 706 enc.writeString(testCase.baseJSON) 707 enc.AddUint16KeyNullEmpty("foo", 0) 708 enc.Uint16KeyNullEmpty("bar", uint16(1)) 709 enc.Write() 710 assert.Equal(t, testCase.expectedJSON, b.String()) 711 }) 712 } 713 } 714 715 func TestEncoderUint8NullEmpty(t *testing.T) { 716 var testCases = []struct { 717 name string 718 baseJSON string 719 expectedJSON string 720 }{ 721 { 722 name: "basic 1st elem", 723 baseJSON: "[", 724 expectedJSON: `[null,1`, 725 }, 726 { 727 name: "basic 2nd elem", 728 baseJSON: `["test"`, 729 expectedJSON: `["test",null,1`, 730 }, 731 } 732 for _, testCase := range testCases { 733 t.Run("true", func(t *testing.T) { 734 var b strings.Builder 735 var enc = NewEncoder(&b) 736 enc.writeString(testCase.baseJSON) 737 enc.AddUint8NullEmpty(0) 738 enc.Uint8NullEmpty(1) 739 enc.Write() 740 assert.Equal(t, testCase.expectedJSON, b.String()) 741 }) 742 } 743 } 744 745 func TestEncoderUint8KeyNullEmpty(t *testing.T) { 746 var testCases = []struct { 747 name string 748 baseJSON string 749 expectedJSON string 750 }{ 751 { 752 name: "basic 1st elem", 753 baseJSON: "{", 754 expectedJSON: `{"foo":null,"bar":1`, 755 }, 756 { 757 name: "basic 2nd elem", 758 baseJSON: `{"test":"test"`, 759 expectedJSON: `{"test":"test","foo":null,"bar":1`, 760 }, 761 } 762 for _, testCase := range testCases { 763 t.Run(testCase.name, func(t *testing.T) { 764 var b strings.Builder 765 var enc = NewEncoder(&b) 766 enc.writeString(testCase.baseJSON) 767 enc.AddUint8KeyNullEmpty("foo", 0) 768 enc.Uint8KeyNullEmpty("bar", uint8(1)) 769 enc.Write() 770 assert.Equal(t, testCase.expectedJSON, b.String()) 771 }) 772 } 773 }