@@ -10,6 +10,7 @@ import (
10
10
"bytes"
11
11
"encoding/binary"
12
12
"errors"
13
+ "fmt"
13
14
"io"
14
15
"testing"
15
16
@@ -348,236 +349,77 @@ func TestArray(t *testing.T) {
348
349
})
349
350
}
350
351
351
- func TestArray_StringN (t * testing.T ) {
352
+ func TestArray_Stringer (t * testing.T ) {
352
353
testCases := []struct {
353
354
description string
354
- n int
355
- values []Value
355
+ array Array
356
356
want string
357
357
}{
358
- // n = 0 cases
359
358
{
360
- description : "n=0, array with 1 element" ,
361
- n : 0 ,
362
- values : []Value {
363
- {
364
- Type : TypeString ,
365
- Data : AppendString (nil , "abc" ),
366
- },
367
- },
368
- want : "" ,
369
- },
370
- {
371
- description : "n=0, empty array" ,
372
- n : 0 ,
373
- values : []Value {},
374
- want : "" ,
359
+ description : "empty array" ,
360
+ array : BuildArray (nil ),
361
+ want : `[]` ,
375
362
},
376
363
{
377
- description : "n=0, nested array" ,
378
- n : 0 ,
379
- values : []Value {
380
- {
381
- Type : TypeArray ,
382
- Data : BuildArray (nil , Value {
383
- Type : TypeString ,
384
- Data : AppendString (nil , "abc" ),
385
- }),
386
- },
387
- },
388
- want : "" ,
389
- },
390
- {
391
- description : "n=0, array with mixed types" ,
392
- n : 0 ,
393
- values : []Value {
394
- {
395
- Type : TypeString ,
396
- Data : AppendString (nil , "abc" ),
397
- },
398
- {
399
- Type : TypeInt32 ,
400
- Data : AppendInt32 (nil , 123 ),
401
- },
402
- {
403
- Type : TypeBoolean ,
404
- Data : AppendBoolean (nil , true ),
405
- },
406
- },
407
- want : "" ,
364
+ description : "array with 1 element" ,
365
+ array : BuildArray (nil , Value {
366
+ Type : TypeInt32 ,
367
+ Data : AppendInt32 (nil , 123 ),
368
+ }),
369
+ want : `[{"$numberInt":"123"}]` ,
408
370
},
409
-
410
- // n < 0 cases
411
371
{
412
- description : "n<0, array with 1 element " ,
413
- n : - 1 ,
414
- values : [] Value {
415
- {
372
+ description : "nested array" ,
373
+ array : BuildArray ( nil , Value {
374
+ Type : TypeArray ,
375
+ Data : BuildArray ( nil , Value {
416
376
Type : TypeString ,
417
377
Data : AppendString (nil , "abc" ),
418
- },
419
- },
420
- want : "" ,
421
- },
422
- {
423
- description : "n<0, empty array" ,
424
- n : - 1 ,
425
- values : []Value {},
426
- want : "" ,
427
- },
428
- {
429
- description : "n<0, nested array" ,
430
- n : - 1 ,
431
- values : []Value {
432
- {
433
- Type : TypeArray ,
434
- Data : BuildArray (nil , Value {
435
- Type : TypeString ,
436
- Data : AppendString (nil , "abc" ),
437
- }),
438
- },
439
- },
440
- want : "" ,
441
- },
442
- {
443
- description : "n<0, array with mixed types" ,
444
- n : - 1 ,
445
- values : []Value {
446
- {
447
- Type : TypeString ,
448
- Data : AppendString (nil , "abc" ),
449
- },
450
- {
451
- Type : TypeInt32 ,
452
- Data : AppendInt32 (nil , 123 ),
453
- },
454
- {
455
- Type : TypeBoolean ,
456
- Data : AppendBoolean (nil , true ),
457
- },
458
- },
459
- want : "" ,
460
- },
461
-
462
- // n > 0 cases
463
- {
464
- description : "n>0, array LT n" ,
465
- n : 1 ,
466
- values : []Value {
467
- {
468
- Type : TypeInt32 ,
469
- Data : AppendInt32 (nil , 2 ),
470
- },
471
- },
472
- want : "[" ,
473
- },
474
- {
475
- description : "n>0, array LT n" ,
476
- n : 2 ,
477
- values : []Value {
478
- {
479
- Type : TypeInt32 ,
480
- Data : AppendInt32 (nil , 2 ),
481
- },
482
- },
483
- want : "[{" ,
484
- },
485
- {
486
- description : "n>0, array LT n" ,
487
- n : 14 ,
488
- values : []Value {
489
- {
490
- Type : TypeInt32 ,
491
- Data : AppendInt32 (nil , 2 ),
492
- },
493
- },
494
- want : `[{"$numberInt"` ,
495
- },
496
- {
497
- description : "n>0, array GT n" ,
498
- n : 30 ,
499
- values : []Value {
500
- {
501
- Type : TypeInt32 ,
502
- Data : AppendInt32 (nil , 2 ),
503
- },
504
- },
505
- want : `[{"$numberInt":"2"}]` ,
506
- },
507
- {
508
- description : "n>0, array EQ n" ,
509
- n : 20 ,
510
- values : []Value {
511
- {
512
- Type : TypeInt32 ,
513
- Data : AppendInt32 (nil , 2 ),
514
- },
515
- },
516
- want : `[{"$numberInt":"2"}]` ,
517
- },
518
- {
519
- description : "n>0, mixed array" ,
520
- n : 24 ,
521
- values : []Value {
522
- {
523
- Type : TypeInt32 ,
524
- Data : AppendInt32 (nil , 1 ),
525
- },
526
- {
527
- Type : TypeString ,
528
- Data : AppendString (nil , "foo" ),
529
- },
530
- },
531
- want : `[{"$numberInt":"1"},"foo` ,
532
- },
533
- {
534
- description : "n>0, empty array" ,
535
- n : 10 ,
536
- values : []Value {},
537
- want : "[]" ,
538
- },
539
- {
540
- description : "n>0, nested array" ,
541
- n : 10 ,
542
- values : []Value {
543
- {
544
- Type : TypeArray ,
545
- Data : BuildArray (nil , Value {
546
- Type : TypeString ,
547
- Data : AppendString (nil , "abc" ),
548
- }),
549
- },
550
- },
378
+ }),
379
+ }),
551
380
want : `[["abc"]]` ,
552
381
},
553
382
{
554
- description : "n>0, array with mixed types" ,
555
- n : 32 ,
556
- values : []Value {
557
- {
383
+ description : "array with mixed types" ,
384
+ array : BuildArray (nil ,
385
+ Value {
558
386
Type : TypeString ,
559
387
Data : AppendString (nil , "abc" ),
560
388
},
561
- {
389
+ Value {
562
390
Type : TypeInt32 ,
563
391
Data : AppendInt32 (nil , 123 ),
564
392
},
565
- {
393
+ Value {
566
394
Type : TypeBoolean ,
567
395
Data : AppendBoolean (nil , true ),
568
396
},
569
- } ,
570
- want : `["abc",{"$numberInt":"123"},true` ,
397
+ ) ,
398
+ want : `["abc",{"$numberInt":"123"},true] ` ,
571
399
},
572
400
}
573
401
574
402
for _ , tc := range testCases {
575
- t .Run (tc .description , func (t * testing.T ) {
576
- got , _ := Array ( BuildArray ( nil , tc .values ... )). StringN ( tc . n )
403
+ t .Run (fmt . Sprintf ( "String %s" , tc .description ) , func (t * testing.T ) {
404
+ got := tc .array . String ( )
577
405
assert .Equal (t , tc .want , got )
578
- if tc .n >= 0 {
579
- assert .LessOrEqual (t , len (got ), tc .n , "got %v, want %v" , got , tc .want )
580
- }
581
406
})
582
407
}
408
+
409
+ for _ , tc := range testCases {
410
+ for n := - 1 ; n <= len (tc .want )+ 1 ; n ++ {
411
+ t .Run (fmt .Sprintf ("StringN %s n==%d" , tc .description , n ), func (t * testing.T ) {
412
+ got , _ := tc .array .StringN (n )
413
+ l := n
414
+ if l < 0 {
415
+ l = 0
416
+ }
417
+ if l > len (tc .want ) {
418
+ l = len (tc .want )
419
+ }
420
+ want := tc .want [:l ]
421
+ assert .Equal (t , want , got , "got %v, want %v" , got , want )
422
+ })
423
+ }
424
+ }
583
425
}
0 commit comments