forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollections_test.go
54 lines (43 loc) · 1.64 KB
/
collections_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
package codec_test
import (
"testing"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
"github.com/stretchr/testify/require"
"cosmossdk.io/collections/colltest"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
gogotypes "github.com/cosmos/gogoproto/types"
)
func TestCollectionsCorrectness(t *testing.T) {
t.Run("CollValue", func(t *testing.T) {
cdc := codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
colltest.TestValueCodec(t, codec.CollValue[gogotypes.UInt64Value](cdc), gogotypes.UInt64Value{
Value: 500,
})
})
t.Run("BoolValue", func(t *testing.T) {
colltest.TestValueCodec(t, codec.BoolValue, true)
colltest.TestValueCodec(t, codec.BoolValue, false)
// asserts produced bytes are equal
valueAssert := func(b bool) {
wantBytes, err := (&gogotypes.BoolValue{Value: b}).Marshal()
require.NoError(t, err)
gotBytes, err := codec.BoolValue.Encode(b)
require.NoError(t, err)
require.Equal(t, wantBytes, gotBytes)
}
valueAssert(true)
valueAssert(false)
})
t.Run("CollInterfaceValue", func(t *testing.T) {
cdc := codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
cdc.InterfaceRegistry().RegisterInterface("animal", (*testdata.Animal)(nil), &testdata.Dog{}, &testdata.Cat{})
valueCodec := codec.CollInterfaceValue[testdata.Animal](cdc)
colltest.TestValueCodec[testdata.Animal](t, valueCodec, &testdata.Dog{Name: "Doggo"})
colltest.TestValueCodec[testdata.Animal](t, valueCodec, &testdata.Cat{Moniker: "Kitty"})
// assert if used with a non interface type it yields a panic.
require.Panics(t, func() {
codec.CollInterfaceValue[*testdata.Dog](cdc)
})
})
}