@@ -3,6 +3,8 @@ package msgp
3
3
import (
4
4
"bytes"
5
5
"math"
6
+ "reflect"
7
+ "strings"
6
8
"testing"
7
9
"time"
8
10
)
@@ -348,3 +350,134 @@ func BenchmarkAppendTime(b *testing.B) {
348
350
AppendTime (buf [0 :0 ], t )
349
351
}
350
352
}
353
+
354
+ // TestEncodeDecode does a back-and-forth test of encoding and decoding and compare the value with a given output.
355
+ func TestEncodeDecode (t * testing.T ) {
356
+ for _ , tc := range []struct {
357
+ name string
358
+ input interface {}
359
+ output interface {}
360
+ encodeError string
361
+ }{
362
+ {
363
+ name : "nil" ,
364
+ input : nil ,
365
+ },
366
+ {
367
+ name : "bool" ,
368
+ input : true ,
369
+ },
370
+ {
371
+ name : "int" ,
372
+ input : int64 (42 ),
373
+ },
374
+ {
375
+ name : "float" ,
376
+ input : 3.14159 ,
377
+ },
378
+ {
379
+ name : "string" ,
380
+ input : "hello" ,
381
+ },
382
+ {
383
+ name : "bytes" ,
384
+ input : []byte ("hello" ),
385
+ },
386
+ {
387
+ name : "array-empty" ,
388
+ input : []interface {}{},
389
+ },
390
+ {
391
+ name : "array" ,
392
+ input : []interface {}{int64 (1 ), int64 (2 ), int64 (3 )},
393
+ },
394
+ {
395
+ name : "map-empty" ,
396
+ input : map [string ]interface {}{},
397
+ },
398
+ {
399
+ name : "map" ,
400
+ input : map [string ]interface {}{"a" : int64 (1 ), "b" : int64 (2 )},
401
+ },
402
+ {
403
+ name : "map-interface" ,
404
+ input : map [string ]interface {}{"a" : int64 (1 ), "b" : "2" },
405
+ },
406
+ {
407
+ name : "map-string" ,
408
+ input : map [string ]string {"a" : "1" , "b" : "2" },
409
+ output : map [string ]interface {}{"a" : "1" , "b" : "2" },
410
+ },
411
+ {
412
+ name : "map-array" ,
413
+ input : map [string ][]int64 {"a" : {1 , 2 }, "b" : {3 }},
414
+ output : map [string ]interface {}{"a" : []interface {}{int64 (1 ), int64 (2 )}, "b" : []interface {}{int64 (3 )}},
415
+ },
416
+ {
417
+ name : "map-map" ,
418
+ input : map [string ]map [string ]int64 {"a" : {"a" : 1 , "b" : 2 }, "b" : {"c" : 3 }},
419
+ output : map [string ]interface {}{"a" : map [string ]interface {}{"a" : int64 (1 ), "b" : int64 (2 )}, "b" : map [string ]interface {}{"c" : int64 (3 )}},
420
+ },
421
+ {
422
+ name : "array-map" ,
423
+ input : []interface {}{map [string ]interface {}{"a" : int64 (1 ), "b" : "2" }, map [string ]int64 {"c" : 3 }},
424
+ output : []interface {}{map [string ]interface {}{"a" : int64 (1 ), "b" : "2" }, map [string ]interface {}{"c" : int64 (3 )}},
425
+ },
426
+ {
427
+ name : "array-array" ,
428
+ input : []interface {}{[]int64 {1 , 2 }, []interface {}{int64 (3 )}},
429
+ output : []interface {}{[]interface {}{int64 (1 ), int64 (2 )}, []interface {}{int64 (3 )}},
430
+ },
431
+ {
432
+ name : "array-array-map" ,
433
+ input : []interface {}{[]interface {}{int64 (1 ), int64 (2 )}, map [string ]interface {}{"c" : int64 (3 )}},
434
+ },
435
+ {
436
+ name : "map-array-map" ,
437
+ input : map [string ]interface {}{"a" : []interface {}{int64 (1 ), int64 (2 )}, "b" : map [string ]interface {}{"c" : int64 (3 )}},
438
+ },
439
+ {
440
+ name : "map-invalid-keys" ,
441
+ input : map [interface {}]interface {}{int64 (1 ): int64 (2 )},
442
+ encodeError : "msgp: map keys must be strings" ,
443
+ },
444
+ {
445
+ name : "map-nested-invalid-keys" ,
446
+ input : map [string ]interface {}{"a" : map [int64 ]string {1 : "2" }},
447
+ encodeError : "msgp: map keys must be strings" ,
448
+ },
449
+ {
450
+ name : "invalid-type" ,
451
+ input : struct {}{},
452
+ encodeError : "msgp: type \" struct {}\" not supported" ,
453
+ },
454
+ } {
455
+ t .Run (tc .name , func (t * testing.T ) {
456
+ // If no output is given, use the input as output
457
+ if tc .output == nil {
458
+ tc .output = tc .input
459
+ }
460
+
461
+ buf , err := AppendIntf (nil , tc .input )
462
+ if tc .encodeError != "" {
463
+ if err == nil || ! strings .Contains (err .Error (), tc .encodeError ) {
464
+ t .Fatalf ("expected encode error '%s' but got '%s'" , tc .encodeError , err )
465
+ }
466
+ return
467
+ }
468
+
469
+ if tc .encodeError == "" && err != nil {
470
+ t .Fatalf ("expected no encode error but got '%s'" , err .Error ())
471
+ }
472
+
473
+ out , _ , _ := ReadIntfBytes (buf )
474
+ if err != nil {
475
+ t .Fatalf ("expected no decode error but got '%s'" , err .Error ())
476
+ }
477
+
478
+ if ! reflect .DeepEqual (tc .output , out ) {
479
+ t .Fatalf ("expected '%v' but got '%v'" , tc .input , out )
480
+ }
481
+ })
482
+ }
483
+ }
0 commit comments