diff --git a/x/evidence/simulation/genesis_test.go b/x/evidence/simulation/genesis_test.go new file mode 100644 index 000000000..aa1259b5d --- /dev/null +++ b/x/evidence/simulation/genesis_test.go @@ -0,0 +1,66 @@ +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/evidence/simulation" + "github.com/cosmos/cosmos-sdk/x/evidence/types" +) + +// TestRandomizedGenState tests the normal scenario of applying RandomizedGenState. +// Abonormal scenarios are not tested here. +func TestRandomizedGenState(t *testing.T) { + cdc := codec.New() + // Make sure to register cdc. + // Otherwise RandomizedGenState will panic! + types.RegisterCodec(cdc) + + 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 evidenceGenesis types.GenesisState + simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &evidenceGenesis) + + require.Len(t, evidenceGenesis.Evidence, 0) +} + +// TestRandomizedGenState tests the execution of RandomizedGenState +// without registering the evidence interfaces. +// We expect the test to panic. +func TestRandomizedGenState1(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), + } + + require.Panicsf(t, func() { simulation.RandomizedGenState(&simState) }, "failed to marshal JSON: Unregistered interface exported.Evidence") +}