github.com/ava-labs/avalanchego@v1.11.11/vms/types/blob_data_test.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package types 5 6 import ( 7 "encoding/json" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestJSON(t *testing.T) { 14 tests := []struct { 15 name string 16 value JSONByteSlice 17 expectedJSON string 18 }{ 19 { 20 name: "nil", 21 value: nil, 22 expectedJSON: nullStr, 23 }, 24 { 25 name: "empty", 26 value: []byte{}, 27 expectedJSON: `"0x"`, 28 }, 29 { 30 name: "not empty", 31 value: []byte{0, 1, 2, 3}, 32 expectedJSON: `"0x00010203"`, 33 }, 34 } 35 for _, test := range tests { 36 t.Run(test.name, func(t *testing.T) { 37 require := require.New(t) 38 39 jsonBytes, err := json.Marshal(test.value) 40 require.NoError(err) 41 require.Equal(test.expectedJSON, string(jsonBytes)) 42 43 var unmarshaled JSONByteSlice 44 require.NoError(json.Unmarshal(jsonBytes, &unmarshaled)) 45 require.Equal(test.value, unmarshaled) 46 }) 47 } 48 } 49 50 func TestUnmarshalJSONNullKeepsInitialValue(t *testing.T) { 51 require := require.New(t) 52 53 unmarshaled := JSONByteSlice{1, 2, 3} 54 require.NoError(json.Unmarshal([]byte(nullStr), &unmarshaled)) 55 require.Equal(JSONByteSlice{1, 2, 3}, unmarshaled) 56 }