Some checks are pending
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
79 lines
2.8 KiB
Go
79 lines
2.8 KiB
Go
package codec_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
gogotypes "github.com/cosmos/gogoproto/types"
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/protobuf/testing/protocmp"
|
|
"google.golang.org/protobuf/types/known/wrapperspb"
|
|
|
|
"cosmossdk.io/collections/colltest"
|
|
|
|
"git.cw.tr/mukan-network/mukan-sdk/codec"
|
|
codectypes "git.cw.tr/mukan-network/mukan-sdk/codec/types"
|
|
"git.cw.tr/mukan-network/mukan-sdk/testutil/testdata"
|
|
)
|
|
|
|
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("CollValueV2", func(t *testing.T) {
|
|
// NOTE: we cannot use colltest.TestValueCodec because protov2 has different
|
|
// compare semantics than protov1. We need to use protocmp.Transform() alongside
|
|
// cmp to ensure equality.
|
|
encoder := codec.CollValueV2[wrapperspb.UInt64Value]()
|
|
value := &wrapperspb.UInt64Value{Value: 500}
|
|
encodedValue, err := encoder.Encode(value)
|
|
require.NoError(t, err)
|
|
decodedValue, err := encoder.Decode(encodedValue)
|
|
require.NoError(t, err)
|
|
require.True(t, cmp.Equal(value, decodedValue, protocmp.Transform()), "encoding and decoding produces different values")
|
|
|
|
encodedJSONValue, err := encoder.EncodeJSON(value)
|
|
require.NoError(t, err)
|
|
decodedJSONValue, err := encoder.DecodeJSON(encodedJSONValue)
|
|
require.NoError(t, err)
|
|
require.True(t, cmp.Equal(value, decodedJSONValue, protocmp.Transform()), "encoding and decoding produces different values")
|
|
require.NotEmpty(t, encoder.ValueType())
|
|
|
|
_ = encoder.Stringify(value)
|
|
})
|
|
|
|
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)
|
|
})
|
|
})
|
|
}
|