github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/internal/testserdes/testing.go (about) 1 package testserdes 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/nspcc-dev/neo-go/pkg/io" 8 "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" 9 "github.com/stretchr/testify/require" 10 "gopkg.in/yaml.v3" 11 ) 12 13 // MarshalUnmarshalJSON checks if the expected stays the same after 14 // marshal/unmarshal via JSON. 15 func MarshalUnmarshalJSON(t *testing.T, expected, actual any) { 16 data, err := json.Marshal(expected) 17 require.NoError(t, err) 18 require.NoError(t, json.Unmarshal(data, actual)) 19 require.Equal(t, expected, actual) 20 } 21 22 // MarshalUnmarshalYAML checks if the expected stays the same after 23 // marshal/unmarshal via YAML. 24 func MarshalUnmarshalYAML(t *testing.T, expected, actual any) { 25 data, err := yaml.Marshal(expected) 26 require.NoError(t, err) 27 require.NoError(t, yaml.Unmarshal(data, actual)) 28 require.Equal(t, expected, actual) 29 } 30 31 // EncodeDecodeBinary checks if the expected stays the same after 32 // serializing/deserializing via io.Serializable methods. 33 func EncodeDecodeBinary(t *testing.T, expected, actual io.Serializable) { 34 data, err := EncodeBinary(expected) 35 require.NoError(t, err) 36 require.NoError(t, DecodeBinary(data, actual)) 37 require.Equal(t, expected, actual) 38 } 39 40 // ToFromStackItem checks if the expected stays the same after converting to/from 41 // StackItem. 42 func ToFromStackItem(t *testing.T, expected, actual stackitem.Convertible) { 43 item, err := expected.ToStackItem() 44 require.NoError(t, err) 45 require.NoError(t, actual.FromStackItem(item)) 46 require.Equal(t, expected, actual) 47 } 48 49 // EncodeBinary serializes a to a byte slice. 50 func EncodeBinary(a io.Serializable) ([]byte, error) { 51 w := io.NewBufBinWriter() 52 a.EncodeBinary(w.BinWriter) 53 if w.Err != nil { 54 return nil, w.Err 55 } 56 return w.Bytes(), nil 57 } 58 59 // DecodeBinary deserializes a from a byte slice. 60 func DecodeBinary(data []byte, a io.Serializable) error { 61 r := io.NewBinReaderFromBuf(data) 62 a.DecodeBinary(r) 63 return r.Err 64 } 65 66 type encodable interface { 67 Encode(*io.BinWriter) error 68 Decode(*io.BinReader) error 69 } 70 71 // EncodeDecode checks if the expected stays the same after 72 // serializing/deserializing via encodable methods. 73 func EncodeDecode(t *testing.T, expected, actual encodable) { 74 data, err := Encode(expected) 75 require.NoError(t, err) 76 require.NoError(t, Decode(data, actual)) 77 require.Equal(t, expected, actual) 78 } 79 80 // Encode serializes a to a byte slice. 81 func Encode(a encodable) ([]byte, error) { 82 w := io.NewBufBinWriter() 83 err := a.Encode(w.BinWriter) 84 if err != nil { 85 return nil, err 86 } 87 return w.Bytes(), nil 88 } 89 90 // Decode deserializes a from a byte slice. 91 func Decode(data []byte, a encodable) error { 92 r := io.NewBinReaderFromBuf(data) 93 err := a.Decode(r) 94 if r.Err != nil { 95 return r.Err 96 } 97 return err 98 }