2023-06-06 15:14:33 +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-06-06 15:14:33 +03:00
|
|
|
"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"
|
2023-06-06 15:14:33 +03:00
|
|
|
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"
|
2023-06-06 15:14:33 +03:00
|
|
|
actual := hex.EncodeToString(buf.Bytes())
|
|
|
|
require.EqualValues(t, expected, actual)
|
|
|
|
})
|
|
|
|
}
|