meta/msgpack_writer_test.go

64 lines
1.2 KiB
Go
Raw Normal View History

package meta_test
import (
"bytes"
"encoding/hex"
"testing"
2023-07-03 22:09:39 +03:00
"git.bit5.ru/backend/meta/v3"
"github.com/stretchr/testify/require"
)
func TestMsgpackWriter(t *testing.T) {
t.Run("write struct", func(t *testing.T) {
var buf bytes.Buffer
wr := meta.NewMsgpackWriter(&buf)
s := TestParent{
Field1: "blabla",
Field2: TestFoo{
Field: 1,
},
Field3: []int8{2, 4, 6},
Field4: []TestFoo{
{Field: 10},
{Field: 1024},
},
}
err := s.Write(wr)
require.NoError(t, err)
2023-07-03 22:09:39 +03:00
expected := "94a6626c61626c6191019302040692910a91cd0400"
actual := hex.EncodeToString(buf.Bytes())
require.EqualValues(t, expected, actual)
})
t.Run("write child struct", func(t *testing.T) {
var buf bytes.Buffer
wr := meta.NewMsgpackWriter(&buf)
s := TestChild{
Field: "qwerty",
TestParent: TestParent{
Field1: "blabla",
Field2: TestFoo{
Field: 1,
},
Field3: []int8{2, 4, 6},
Field4: []TestFoo{
{Field: 10},
{Field: 1024},
},
},
}
err := s.Write(wr)
require.NoError(t, err)
2023-07-03 22:09:39 +03:00
expected := "95a6626c61626c6191019302040692910a91cd0400a6717765727479"
actual := hex.EncodeToString(buf.Bytes())
require.EqualValues(t, expected, actual)
})
}