meta/msgpack_assoc_writer_test.go

64 lines
1.3 KiB
Go
Raw Permalink Normal View History

2023-07-03 22:09:39 +03:00
package meta_test
import (
"bytes"
"encoding/hex"
"testing"
2023-09-11 17:41:16 +03:00
"git.bit5.ru/backend/meta/v5"
2023-07-03 22:09:39 +03:00
"github.com/stretchr/testify/require"
)
func TestMsgpackAssocWriter(t *testing.T) {
t.Run("write struct", func(t *testing.T) {
var buf bytes.Buffer
wr := meta.NewMsgpackAssocWriter(&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)
expected := "84a26631a6626c61626c61a2663281a56669656c6401a2663393020406a266349281a56669656c640a81a56669656c64cd0400"
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.NewMsgpackAssocWriter(&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)
expected := "85a26631a6626c61626c61a2663281a56669656c6401a2663393020406a266349281a56669656c640a81a56669656c64cd0400a166a6717765727479"
actual := hex.EncodeToString(buf.Bytes())
require.EqualValues(t, expected, actual)
})
}