From ea0464b49b28d99624381a1eb35ab4b1450c5251 Mon Sep 17 00:00:00 2001 From: stackman27 Date: Sun, 3 Jul 2022 17:44:46 -0700 Subject: [PATCH] still work --- app/upgrades/v8/msg_filter_ante_test.go | 7 ++----- app/upgrades/v9/msg_filter_ante_test.go | 6 +----- osmoutils/binary_search_test.go | 8 ++------ osmoutils/cli_helpers.go | 26 +++++++++++++++++++++++++ wasmbinding/test/custom_msg_test.go | 19 +++++++++--------- wasmbinding/test/queries_test.go | 14 ++++++------- x/epochs/client/cli/cli_test.go | 1 + x/mint/keeper/genesis_test.go | 23 ++++++++++++---------- x/mint/keeper/keeper_test.go | 2 +- 9 files changed, 62 insertions(+), 44 deletions(-) diff --git a/app/upgrades/v8/msg_filter_ante_test.go b/app/upgrades/v8/msg_filter_ante_test.go index f3082ab59d8..eda26003eaa 100644 --- a/app/upgrades/v8/msg_filter_ante_test.go +++ b/app/upgrades/v8/msg_filter_ante_test.go @@ -1,6 +1,7 @@ package v8_test import ( + "github.com/osmosis-labs/osmosis/v7/osmoutils" "testing" sdk "github.com/cosmos/cosmos-sdk/types" @@ -67,11 +68,7 @@ func TestMsgFilterDecorator(t *testing.T) { require.NoError(t, txBuilder.SetMsgs(tc.msgs...)) _, err := handler.AnteHandle(tc.ctx, txBuilder.GetTx(), false, noOpAnteDecorator()) - if tc.expectErr { - require.Error(t, err) - } else { - require.NoError(t, err) - } + osmoutils.ConditionalError(t, tc.expectErr, err)) }) } } diff --git a/app/upgrades/v9/msg_filter_ante_test.go b/app/upgrades/v9/msg_filter_ante_test.go index 0a3d9b5fee5..9e9e0ccad18 100644 --- a/app/upgrades/v9/msg_filter_ante_test.go +++ b/app/upgrades/v9/msg_filter_ante_test.go @@ -60,11 +60,7 @@ func TestMsgFilterDecorator(t *testing.T) { require.NoError(t, txBuilder.SetMsgs(tc.msgs...)) _, err := handler.AnteHandle(tc.ctx, txBuilder.GetTx(), false, noOpAnteDecorator()) - if tc.expectErr { - require.Error(t, err) - } else { - require.NoError(t, err) - } + osmoutils.ConditionalError(t, tc.expectErr, err)) }) } } diff --git a/osmoutils/binary_search_test.go b/osmoutils/binary_search_test.go index 365ccb6d482..98032514f20 100644 --- a/osmoutils/binary_search_test.go +++ b/osmoutils/binary_search_test.go @@ -55,12 +55,8 @@ func TestBinarySearch(t *testing.T) { for _, tc := range tests { actualSolvedInput, err := BinarySearch(tc.f, tc.lowerbound, tc.upperbound, tc.targetOutput, tc.errTolerance, tc.maxIterations) - if tc.expectErr { - require.Error(t, err) - } else { - require.NoError(t, err) - require.True(sdk.IntEq(t, tc.expectedSolvedInput, actualSolvedInput)) - } + ConditionalError(t, tc.expectErr, err) + require.True(sdk.IntEq(t, tc.expectedSolvedInput, actualSolvedInput)) } } diff --git a/osmoutils/cli_helpers.go b/osmoutils/cli_helpers.go index 88b2a25a0dd..40bd380b742 100644 --- a/osmoutils/cli_helpers.go +++ b/osmoutils/cli_helpers.go @@ -4,10 +4,13 @@ import ( "fmt" "strconv" "strings" + "testing" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" ) func DefaultFeeString(cfg network.Config) string { @@ -20,6 +23,8 @@ const ( bitlen = 64 ) +type PanicTestFunc func() + func ParseUint64SliceFromString(s string, separator string) ([]uint64, error) { var parsedInts []uint64 for _, s := range strings.Split(s, separator) { @@ -47,3 +52,24 @@ func ParseSdkIntFromString(s string, separator string) ([]sdk.Int, error) { } return parsedInts, nil } + +func ConditionalPanics(s suite.Suite, expPanic bool, fn PanicTestFunc) { + if expPanic { + s.Panics(func() { + fn() + }) + } + + s.NotPanics(func() { + fn() + }) +} + +func ConditionalError(t *testing.T, expError bool, err error, msgs ...interface{}) { + if expError { + require.Error(t, err) + return + } + + require.NoError(t, err, msgs) +} diff --git a/wasmbinding/test/custom_msg_test.go b/wasmbinding/test/custom_msg_test.go index be4b81a8699..6989f9a2588 100644 --- a/wasmbinding/test/custom_msg_test.go +++ b/wasmbinding/test/custom_msg_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/osmosis-labs/osmosis/v7/osmoutils" "github.com/osmosis-labs/osmosis/v7/x/tokenfactory/types" "github.com/stretchr/testify/require" @@ -561,16 +562,14 @@ func TestSwapMsg(t *testing.T) { msg := bindings.OsmosisMsg{Swap: tc.msg(state)} err := executeCustom(t, ctx, osmosis, reflect, trader, msg, tc.initFunds) - if tc.expectErr { - require.Error(t, err) - } else { - require.NoError(t, err) - balances := osmosis.BankKeeper.GetAllBalances(ctx, reflect) - // uncomment these to debug any confusing results (show balances, not (*big.Int)(0x140005e51e0)) - // fmt.Printf("Expected: %s\n", tc.finalFunds) - // fmt.Printf("Got: %s\n", balances) - require.EqualValues(t, tc.finalFunds, balances) - } + osmoutils.ConditionalError(t, tc.expectErr, err) + + balances := osmosis.BankKeeper.GetAllBalances(ctx, reflect) + // uncomment these to debug any confusing results (show balances, not (*big.Int)(0x140005e51e0)) + // fmt.Printf("Expected: %s\n", tc.finalFunds) + // fmt.Printf("Got: %s\n", balances) + require.EqualValues(t, tc.finalFunds, balances) + }) } } diff --git a/wasmbinding/test/queries_test.go b/wasmbinding/test/queries_test.go index b87bcefef92..e45b864f8ae 100644 --- a/wasmbinding/test/queries_test.go +++ b/wasmbinding/test/queries_test.go @@ -9,6 +9,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/osmosis-labs/osmosis/v7/osmoutils" "github.com/osmosis-labs/osmosis/v7/wasmbinding" "github.com/osmosis-labs/osmosis/v7/wasmbinding/bindings" ) @@ -104,13 +105,12 @@ func TestDenomAdmin(t *testing.T) { t.Run(tc.name, func(t *testing.T) { resp, err := queryPlugin.GetDenomAdmin(ctx, tc.denom) - if tc.expectErr { - require.Error(t, err) - } else { - require.NoError(t, err) - require.NotNil(t, resp) - require.Equal(t, tc.expectAdmin, resp.Admin) - } + + osmoutils.ConditionalError(t, tc.expectErr, err) + + require.NotNil(t, resp) + require.Equal(t, tc.expectAdmin, resp.Admin) + }) } } diff --git a/x/epochs/client/cli/cli_test.go b/x/epochs/client/cli/cli_test.go index c30a1d8d20a..97af5dd4541 100644 --- a/x/epochs/client/cli/cli_test.go +++ b/x/epochs/client/cli/cli_test.go @@ -68,6 +68,7 @@ func (s *IntegrationTestSuite) TestGetCmdCurrentEpoch() { } out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args) + if tc.expectErr { s.Require().Error(err) } else { diff --git a/x/mint/keeper/genesis_test.go b/x/mint/keeper/genesis_test.go index 7aa8ddcf01f..67f10deb816 100644 --- a/x/mint/keeper/genesis_test.go +++ b/x/mint/keeper/genesis_test.go @@ -7,6 +7,7 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/stretchr/testify/suite" + "github.com/osmosis-labs/osmosis/v7/osmoutils" "github.com/osmosis-labs/osmosis/v7/x/mint/keeper" "github.com/osmosis-labs/osmosis/v7/x/mint/types" ) @@ -111,16 +112,18 @@ func (suite *KeeperTestSuite) TestMintInitGenesis() { originalVestingCoins := bankKeeper.GetBalance(ctx, developerAccount, tc.mintDenom) // Test. - if tc.expectPanic { - suite.Panics(func() { - mintKeeper.InitGenesis(ctx, tc.mintGenesis) - }) - return - } - - suite.NotPanics(func() { - mintKeeper.InitGenesis(ctx, tc.mintGenesis) - }) + // if tc.expectPanic { + // suite.Panics(func() { + // mintKeeper.InitGenesis(ctx, tc.mintGenesis) + // }) + // return + // } + + // suite.NotPanics(func() { + // mintKeeper.InitGenesis(ctx, tc.mintGenesis) + // }) + + osmoutils.ConditionalPanics(suite, tc.expectPanic, mintKeeper.InitGenesis(ctx, tc.mintGenesis)) // Assertions. diff --git a/x/mint/keeper/keeper_test.go b/x/mint/keeper/keeper_test.go index 802cfd98268..a7c65186a10 100644 --- a/x/mint/keeper/keeper_test.go +++ b/x/mint/keeper/keeper_test.go @@ -268,7 +268,7 @@ func (suite *KeeperTestSuite) TestSetInitialSupplyOffsetDuringMigration() { isDeveloperModuleAccountCreated: true, }, "dev vesting module account does not exist": { - blockHeight: 1, + blockHeight: 1, expectedError: keeper.ErrDevVestingModuleAccountNotCreated, }, }