-
Notifications
You must be signed in to change notification settings - Fork 78
/
repr_test.go
99 lines (80 loc) · 1.92 KB
/
repr_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
package amino
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type Foo struct {
a string
b int
c []*Foo
D string // exposed
}
type pair struct {
Key string
Value interface{}
}
func (pr pair) get(key string) (value interface{}) {
if pr.Key != key {
panic(fmt.Sprintf("wanted %v but is %v", key, pr.Key))
}
return pr.Value
}
func (f Foo) MarshalAmino() ([]pair, error) { // nolint: golint
return []pair{
{"a", f.a},
{"b", f.b},
{"c", f.c},
{"D", f.D},
}, nil
}
func (f *Foo) UnmarshalAmino(repr []pair) error {
f.a = repr[0].get("a").(string)
f.b = repr[1].get("b").(int)
f.c = repr[2].get("c").([]*Foo)
f.D = repr[3].get("D").(string)
return nil
}
func TestMarshalAminoBinary(t *testing.T) {
cdc := NewCodec()
cdc.RegisterInterface((*interface{})(nil), nil)
// register a bunch of concrete "implementations" which are type aliases:
cdc.RegisterConcrete(string(""), "string", nil)
cdc.RegisterConcrete(int(0), "int", nil)
cdc.RegisterConcrete(([]*Foo)(nil), "[]*Foo", nil)
var f = Foo{
a: "K",
b: 2,
c: []*Foo{nil, nil, nil},
D: "J",
}
bz, err := cdc.MarshalBinaryLengthPrefixed(f)
assert.NoError(t, err)
t.Logf("bz %#v", bz)
var f2 Foo
err = cdc.UnmarshalBinaryLengthPrefixed(bz, &f2)
assert.NoError(t, err)
assert.Equal(t, f, f2)
assert.Equal(t, f.a, f2.a) // In case the above doesn't check private fields?
}
func TestMarshalAminoJSON(t *testing.T) {
cdc := NewCodec()
cdc.RegisterInterface((*interface{})(nil), nil)
cdc.RegisterConcrete(string(""), "string", nil)
cdc.RegisterConcrete(int(0), "int", nil)
cdc.RegisterConcrete(([]*Foo)(nil), "[]*Foo", nil)
var f = Foo{
a: "K",
b: 2,
c: []*Foo{nil, nil, nil},
D: "J",
}
bz, err := cdc.MarshalJSON(f)
assert.Nil(t, err)
t.Logf("bz %X", bz)
var f2 Foo
err = cdc.UnmarshalJSON(bz, &f2)
assert.Nil(t, err)
assert.Equal(t, f, f2)
assert.Equal(t, f.a, f2.a) // In case the above doesn't check private fields?
}