This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amino_codec_test.go
116 lines (98 loc) · 3.07 KB
/
amino_codec_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package codec_test
import (
"bytes"
"errors"
"testing"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
)
func createTestCodec() *codec.LegacyAmino {
cdc := codec.NewLegacyAmino()
cdc.RegisterInterface((*testdata.Animal)(nil), nil)
// NOTE: since we unmarshal interface using pointers, we need to register a pointer
// types here.
cdc.RegisterConcrete(&testdata.Dog{}, "testdata/Dog", nil)
cdc.RegisterConcrete(&testdata.Cat{}, "testdata/Cat", nil)
return cdc
}
func TestAminoMarsharlInterface(t *testing.T) {
cdc := codec.NewAminoCodec(createTestCodec())
m := interfaceMarshaler{cdc.MarshalInterface, cdc.UnmarshalInterface}
testInterfaceMarshaling(require.New(t), m, true)
m = interfaceMarshaler{cdc.MarshalInterfaceJSON, cdc.UnmarshalInterfaceJSON}
testInterfaceMarshaling(require.New(t), m, false)
}
func TestAminoCodec(t *testing.T) {
testMarshaling(t, codec.NewAminoCodec(createTestCodec()))
}
func TestAminoCodecMarshalJSONIndent(t *testing.T) {
any, err := types.NewAnyWithValue(&testdata.Dog{Name: "rufus"})
require.NoError(t, err)
testCases := []struct {
name string
input interface{}
marshalErr bool
wantJSON string
}{
{
name: "valid encoding and decoding",
input: &testdata.Dog{Name: "rufus"},
wantJSON: `{
"type": "testdata/Dog",
"value": {
"name": "rufus"
}
}`,
},
{
name: "any marshaling",
input: &testdata.HasAnimal{Animal: any},
wantJSON: `{
"animal": {
"type": "testdata/Dog",
"value": {
"name": "rufus"
}
}
}`,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
cdc := codec.NewAminoCodec(createTestCodec())
bz, err := cdc.MarshalJSONIndent(tc.input, "", " ")
if tc.marshalErr {
require.Error(t, err)
require.Panics(t, func() { codec.MustMarshalJSONIndent(cdc.LegacyAmino, tc.input) })
return
}
// Otherwise these are expected to pass.
require.NoError(t, err)
require.Equal(t, bz, []byte(tc.wantJSON))
var bz2 []byte
require.NotPanics(t, func() { bz2 = codec.MustMarshalJSONIndent(cdc.LegacyAmino, tc.input) })
require.Equal(t, bz2, []byte(tc.wantJSON))
})
}
}
func TestAminoCodecPrintTypes(t *testing.T) {
cdc := codec.NewAminoCodec(createTestCodec())
buf := new(bytes.Buffer)
require.NoError(t, cdc.PrintTypes(buf))
lines := bytes.Split(buf.Bytes(), []byte("\n"))
require.True(t, len(lines) > 1)
wantHeader := "| Type | Name | Prefix | Length | Notes |"
require.Equal(t, lines[0], []byte(wantHeader))
// Expecting the types to be listed in the order that they were registered.
require.True(t, bytes.HasPrefix(lines[2], []byte("| Dog | testdata/Dog |")))
require.True(t, bytes.HasPrefix(lines[3], []byte("| Cat | testdata/Cat |")))
}
func TestAminoCodecUnpackAnyFails(t *testing.T) {
cdc := codec.NewAminoCodec(createTestCodec())
err := cdc.UnpackAny(new(types.Any), &testdata.Cat{})
require.Error(t, err)
require.Equal(t, err, errors.New("AminoCodec can't handle unpack protobuf Any's"))
}