encode_test.go (1118B)
1 package msgpack 2 3 import ( 4 "bytes" 5 "reflect" 6 "testing" 7 "time" 8 ) 9 10 func TestEncodeNil(t *testing.T) { 11 e := NewEncoder() 12 e.Nil() 13 if !bytes.Equal(e.buf, []byte{0xc0}) { 14 t.Errorf("e.Nil() returns %v, want: [0xc0]", e.buf) 15 } 16 } 17 18 func TestEncode(t *testing.T) { 19 e := NewEncoder() 20 21 for _, test := range testCases { 22 e.Reset() 23 24 switch v := test.decoded.(type) { 25 case bool: 26 e.Bool(v) 27 case int: 28 e.Int(v) 29 case int8: 30 e.Int8(v) 31 case int16: 32 e.Int16(v) 33 case int32: 34 e.Int32(v) 35 case int64: 36 e.Int64(v) 37 case uint: 38 e.Uint(v) 39 case uint8: 40 e.Uint8(v) 41 case uint16: 42 e.Uint16(v) 43 case uint32: 44 e.Uint32(v) 45 case uint64: 46 e.Uint64(v) 47 case float32: 48 e.Float32(v) 49 case float64: 50 e.Float64(v) 51 case string: 52 e.String(v) 53 case []byte: 54 e.Binary(v) 55 case Array: 56 e.Array(v) 57 case Map: 58 e.Map(v) 59 case Extension: 60 e.Ext(v) 61 case time.Time: 62 e.Ext(NewTimestamp(v)) 63 } 64 65 if !bytes.Equal(e.buf, test.encoded) { 66 t.Errorf( 67 "e.%s(%v) returns %v, want: %v", 68 reflect.TypeOf(test.decoded).Name(), 69 test.decoded, 70 e.buf, 71 test.encoded, 72 ) 73 } 74 } 75 }