forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress_test.go
57 lines (51 loc) · 1.75 KB
/
address_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
package state
import (
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAddressMarshalling(t *testing.T) {
testCases := []struct {
name string
addressString string
addressFromStr func(string) (interface{}, error)
marshalJSON func(interface{}) ([]byte, error)
unmarshalJSON func([]byte) (interface{}, error)
}{
{
name: "Account Address",
addressString: "celestia1377k5an3f94v6wyaceu0cf4nq6gk2jtpc46g7h",
addressFromStr: func(s string) (interface{}, error) { return sdk.AccAddressFromBech32(s) },
marshalJSON: func(addr interface{}) ([]byte, error) { return addr.(sdk.AccAddress).MarshalJSON() },
unmarshalJSON: func(b []byte) (interface{}, error) {
var addr sdk.AccAddress
err := addr.UnmarshalJSON(b)
return addr, err
},
},
{
name: "Validator Address",
addressString: "celestiavaloper1q3v5cugc8cdpud87u4zwy0a74uxkk6u4q4gx4p",
addressFromStr: func(s string) (interface{}, error) { return sdk.ValAddressFromBech32(s) },
marshalJSON: func(addr interface{}) ([]byte, error) { return addr.(sdk.ValAddress).MarshalJSON() },
unmarshalJSON: func(b []byte) (interface{}, error) {
var addr sdk.ValAddress
err := addr.UnmarshalJSON(b)
return addr, err
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
addr, err := tc.addressFromStr(tc.addressString)
require.NoError(t, err)
addrBytes, err := tc.marshalJSON(addr)
assert.NoError(t, err)
assert.Equal(t, []byte("\""+tc.addressString+"\""), addrBytes)
addrUnmarshalled, err := tc.unmarshalJSON(addrBytes)
assert.NoError(t, err)
assert.Equal(t, addr, addrUnmarshalled)
})
}
}