From 7678f8a913da409a0278171d181f175ddca1ce8e Mon Sep 17 00:00:00 2001 From: dauTT Date: Mon, 13 Jul 2020 21:54:43 +0200 Subject: [PATCH] x/ibc-transfer/simulation/genesis.go: add unit tests (#6709) --- x/ibc-transfer/simulation/genesis_test.go | 67 +++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 x/ibc-transfer/simulation/genesis_test.go diff --git a/x/ibc-transfer/simulation/genesis_test.go b/x/ibc-transfer/simulation/genesis_test.go new file mode 100644 index 000000000000..46ac79d2a421 --- /dev/null +++ b/x/ibc-transfer/simulation/genesis_test.go @@ -0,0 +1,67 @@ +package simulation_test + +import ( + "encoding/json" + "math/rand" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/types/module" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/cosmos/cosmos-sdk/x/ibc-transfer/simulation" + "github.com/cosmos/cosmos-sdk/x/ibc-transfer/types" +) + +// TestRandomizedGenState tests the normal scenario of applying RandomizedGenState. +// Abonormal scenarios are not tested here. +func TestRandomizedGenState(t *testing.T) { + cdc := codec.New() + s := rand.NewSource(1) + r := rand.New(s) + + simState := module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: cdc, + Rand: r, + NumBonded: 3, + Accounts: simtypes.RandomAccounts(r, 3), + InitialStake: 1000, + GenState: make(map[string]json.RawMessage), + } + + simulation.RandomizedGenState(&simState) + + var ibcTransferGenesis types.GenesisState + simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &ibcTransferGenesis) + + require.Equal(t, "euzxpfgkqegqiqwixnku", ibcTransferGenesis.PortID) + +} + +// TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState. +func TestRandomizedGenState1(t *testing.T) { + cdc := codec.New() + + s := rand.NewSource(1) + r := rand.New(s) + // all these tests will panic + tests := []struct { + simState module.SimulationState + panicMsg string + }{ + { // panic => reason: incomplete initialization of the simState + module.SimulationState{}, "invalid memory address or nil pointer dereference"}, + { // panic => reason: incomplete initialization of the simState + module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: cdc, + Rand: r, + }, "assignment to entry in nil map"}, + } + + for _, tt := range tests { + require.Panicsf(t, func() { simulation.RandomizedGenState(&tt.simState) }, tt.panicMsg) + } +}