2020-03-26 14:43:24 +00:00
|
|
|
package testserdes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2021-07-17 15:37:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-03-26 14:43:24 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-11-24 13:03:08 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2020-03-26 14:43:24 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalUnmarshalJSON checks if the expected stays the same after
|
2020-03-26 14:43:24 +00:00
|
|
|
// marshal/unmarshal via JSON.
|
2023-04-03 10:34:24 +00:00
|
|
|
func MarshalUnmarshalJSON(t *testing.T, expected, actual any) {
|
2020-03-26 14:43:24 +00:00
|
|
|
data, err := json.Marshal(expected)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, json.Unmarshal(data, actual))
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:03:08 +00:00
|
|
|
// MarshalUnmarshalYAML checks if the expected stays the same after
|
|
|
|
// marshal/unmarshal via YAML.
|
2023-04-03 10:34:24 +00:00
|
|
|
func MarshalUnmarshalYAML(t *testing.T, expected, actual any) {
|
2022-11-24 13:03:08 +00:00
|
|
|
data, err := yaml.Marshal(expected)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, yaml.Unmarshal(data, actual))
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// EncodeDecodeBinary checks if the expected stays the same after
|
2020-03-26 14:43:24 +00:00
|
|
|
// serializing/deserializing via io.Serializable methods.
|
|
|
|
func EncodeDecodeBinary(t *testing.T, expected, actual io.Serializable) {
|
|
|
|
data, err := EncodeBinary(expected)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, DecodeBinary(data, actual))
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ToFromStackItem checks if the expected stays the same after converting to/from
|
2021-07-17 15:37:33 +00:00
|
|
|
// StackItem.
|
|
|
|
func ToFromStackItem(t *testing.T, expected, actual stackitem.Convertible) {
|
|
|
|
item, err := expected.ToStackItem()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, actual.FromStackItem(item))
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:43:24 +00:00
|
|
|
// EncodeBinary serializes a to a byte slice.
|
|
|
|
func EncodeBinary(a io.Serializable) ([]byte, error) {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
a.EncodeBinary(w.BinWriter)
|
|
|
|
if w.Err != nil {
|
|
|
|
return nil, w.Err
|
|
|
|
}
|
|
|
|
return w.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary deserializes a from a byte slice.
|
|
|
|
func DecodeBinary(data []byte, a io.Serializable) error {
|
|
|
|
r := io.NewBinReaderFromBuf(data)
|
|
|
|
a.DecodeBinary(r)
|
|
|
|
return r.Err
|
|
|
|
}
|
2020-05-25 07:09:21 +00:00
|
|
|
|
|
|
|
type encodable interface {
|
|
|
|
Encode(*io.BinWriter) error
|
|
|
|
Decode(*io.BinReader) error
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// EncodeDecode checks if the expected stays the same after
|
2020-05-25 07:09:21 +00:00
|
|
|
// serializing/deserializing via encodable methods.
|
|
|
|
func EncodeDecode(t *testing.T, expected, actual encodable) {
|
|
|
|
data, err := Encode(expected)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, Decode(data, actual))
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode serializes a to a byte slice.
|
|
|
|
func Encode(a encodable) ([]byte, error) {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
err := a.Encode(w.BinWriter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return w.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode deserializes a from a byte slice.
|
|
|
|
func Decode(data []byte, a encodable) error {
|
|
|
|
r := io.NewBinReaderFromBuf(data)
|
|
|
|
err := a.Decode(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return r.Err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|