From be7dd7adf11098e264c0c2843d4d83de9d28fc39 Mon Sep 17 00:00:00 2001 From: stackman27 Date: Mon, 4 Jul 2022 15:58:02 -0700 Subject: [PATCH] added --- app/upgrades/v8/msg_filter_ante_test.go | 7 +++-- app/upgrades/v9/msg_filter_ante_test.go | 6 +++- osmoutils/binary_search_test.go | 6 +++- osmoutils/cli_helpers.go | 28 ++++++------------- wasmbinding/test/custom_msg_test.go | 19 +++++++------ wasmbinding/test/queries_test.go | 14 +++++----- .../pool-models/balancer/pool_suite_test.go | 3 +- x/gamm/pool-models/balancer/pool_test.go | 2 +- x/gamm/pool-models/balancer/util_test.go | 10 ------- x/mint/keeper/genesis_test.go | 4 ++- 10 files changed, 47 insertions(+), 52 deletions(-) diff --git a/app/upgrades/v8/msg_filter_ante_test.go b/app/upgrades/v8/msg_filter_ante_test.go index eda26003eaa..f3082ab59d8 100644 --- a/app/upgrades/v8/msg_filter_ante_test.go +++ b/app/upgrades/v8/msg_filter_ante_test.go @@ -1,7 +1,6 @@ package v8_test import ( - "github.com/osmosis-labs/osmosis/v7/osmoutils" "testing" sdk "github.com/cosmos/cosmos-sdk/types" @@ -68,7 +67,11 @@ func TestMsgFilterDecorator(t *testing.T) { require.NoError(t, txBuilder.SetMsgs(tc.msgs...)) _, err := handler.AnteHandle(tc.ctx, txBuilder.GetTx(), false, noOpAnteDecorator()) - osmoutils.ConditionalError(t, tc.expectErr, err)) + if tc.expectErr { + require.Error(t, err) + } else { + require.NoError(t, err) + } }) } } diff --git a/app/upgrades/v9/msg_filter_ante_test.go b/app/upgrades/v9/msg_filter_ante_test.go index 9e9e0ccad18..0a3d9b5fee5 100644 --- a/app/upgrades/v9/msg_filter_ante_test.go +++ b/app/upgrades/v9/msg_filter_ante_test.go @@ -60,7 +60,11 @@ func TestMsgFilterDecorator(t *testing.T) { require.NoError(t, txBuilder.SetMsgs(tc.msgs...)) _, err := handler.AnteHandle(tc.ctx, txBuilder.GetTx(), false, noOpAnteDecorator()) - osmoutils.ConditionalError(t, tc.expectErr, err)) + if tc.expectErr { + require.Error(t, err) + } else { + require.NoError(t, err) + } }) } } diff --git a/osmoutils/binary_search_test.go b/osmoutils/binary_search_test.go index 98032514f20..321fbda5b95 100644 --- a/osmoutils/binary_search_test.go +++ b/osmoutils/binary_search_test.go @@ -55,7 +55,11 @@ func TestBinarySearch(t *testing.T) { for _, tc := range tests { actualSolvedInput, err := BinarySearch(tc.f, tc.lowerbound, tc.upperbound, tc.targetOutput, tc.errTolerance, tc.maxIterations) - ConditionalError(t, tc.expectErr, err) + if tc.expectErr { + require.Error(t, err) + } else { + require.NoError(t, err) + } require.True(sdk.IntEq(t, tc.expectedSolvedInput, actualSolvedInput)) } } diff --git a/osmoutils/cli_helpers.go b/osmoutils/cli_helpers.go index 40bd380b742..9546dc1da31 100644 --- a/osmoutils/cli_helpers.go +++ b/osmoutils/cli_helpers.go @@ -10,7 +10,6 @@ import ( "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 { @@ -23,8 +22,6 @@ const ( bitlen = 64 ) -type PanicTestFunc func() - func ParseUint64SliceFromString(s string, separator string) ([]uint64, error) { var parsedInts []uint64 for _, s := range strings.Split(s, separator) { @@ -53,23 +50,16 @@ 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() - }) +// ConditionalPanic if expectPanic is true, asserts that sut (system under test) +// panics. If expectPanic is false, asserts that sut does not panic. +// returns true if sut panics and false it it doesnot +func ConditionalPanic(t *testing.T, expectPanic bool, sut func()) bool { + if expectPanic { + require.Panics(t, sut) + return true } - s.NotPanics(func() { - fn() - }) -} - -func ConditionalError(t *testing.T, expError bool, err error, msgs ...interface{}) { - if expError { - require.Error(t, err) - return - } + require.NotPanics(t, sut) + return false - require.NoError(t, err, msgs) } diff --git a/wasmbinding/test/custom_msg_test.go b/wasmbinding/test/custom_msg_test.go index 6989f9a2588..be4b81a8699 100644 --- a/wasmbinding/test/custom_msg_test.go +++ b/wasmbinding/test/custom_msg_test.go @@ -5,7 +5,6 @@ import ( "fmt" "testing" - "github.com/osmosis-labs/osmosis/v7/osmoutils" "github.com/osmosis-labs/osmosis/v7/x/tokenfactory/types" "github.com/stretchr/testify/require" @@ -562,14 +561,16 @@ func TestSwapMsg(t *testing.T) { msg := bindings.OsmosisMsg{Swap: tc.msg(state)} err := executeCustom(t, ctx, osmosis, reflect, trader, msg, tc.initFunds) - 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) - + 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) + } }) } } diff --git a/wasmbinding/test/queries_test.go b/wasmbinding/test/queries_test.go index e45b864f8ae..b87bcefef92 100644 --- a/wasmbinding/test/queries_test.go +++ b/wasmbinding/test/queries_test.go @@ -9,7 +9,6 @@ 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" ) @@ -105,12 +104,13 @@ func TestDenomAdmin(t *testing.T) { t.Run(tc.name, func(t *testing.T) { resp, err := queryPlugin.GetDenomAdmin(ctx, tc.denom) - - osmoutils.ConditionalError(t, tc.expectErr, err) - - require.NotNil(t, resp) - require.Equal(t, tc.expectAdmin, resp.Admin) - + if tc.expectErr { + require.Error(t, err) + } else { + require.NoError(t, err) + require.NotNil(t, resp) + require.Equal(t, tc.expectAdmin, resp.Admin) + } }) } } diff --git a/x/gamm/pool-models/balancer/pool_suite_test.go b/x/gamm/pool-models/balancer/pool_suite_test.go index afd071d0acb..851aa185f0e 100644 --- a/x/gamm/pool-models/balancer/pool_suite_test.go +++ b/x/gamm/pool-models/balancer/pool_suite_test.go @@ -13,6 +13,7 @@ import ( "github.com/osmosis-labs/osmosis/v7/app/apptesting" v10 "github.com/osmosis-labs/osmosis/v7/app/upgrades/v10" + "github.com/osmosis-labs/osmosis/v7/osmoutils" "github.com/osmosis-labs/osmosis/v7/x/gamm/pool-models/balancer" "github.com/osmosis-labs/osmosis/v7/x/gamm/types" ) @@ -720,7 +721,7 @@ func (suite *KeeperTestSuite) TestCalcJoinPoolShares() { require.True(t, ok) assertPoolStateNotModified(t, balancerPool, func() { - assertPanic(t, tc.expectPanic, sut) + osmoutils.ConditionalPanic(t, tc.expectPanic, sut) }) }) } diff --git a/x/gamm/pool-models/balancer/pool_test.go b/x/gamm/pool-models/balancer/pool_test.go index d4b3133672f..f02a6666d60 100644 --- a/x/gamm/pool-models/balancer/pool_test.go +++ b/x/gamm/pool-models/balancer/pool_test.go @@ -202,7 +202,7 @@ func TestCalcSingleAssetJoin(t *testing.T) { } assertPoolStateNotModified(t, balancerPool, func() { - assertPanic(t, tc.expectPanic, sut) + osmoutils.ConditionalPanic(t, tc.expectPanic, sut) }) }) } diff --git a/x/gamm/pool-models/balancer/util_test.go b/x/gamm/pool-models/balancer/util_test.go index 8a6f7f0afb2..c5e8405b696 100644 --- a/x/gamm/pool-models/balancer/util_test.go +++ b/x/gamm/pool-models/balancer/util_test.go @@ -76,13 +76,3 @@ func assertPoolStateNotModified(t *testing.T, pool *balancer.Pool, sut func()) { require.Equal(t, oldLiquidity, newLiquidity) require.Equal(t, oldShares, newShares) } - -// assertPanic if expectPanic is true, asserts that sut (system under test) -// panics. If expectPanic is false, asserts that sut does not panic. -func assertPanic(t *testing.T, expectPanic bool, sut func()) { - if expectPanic { - require.Panics(t, sut) - } else { - require.NotPanics(t, sut) - } -} diff --git a/x/mint/keeper/genesis_test.go b/x/mint/keeper/genesis_test.go index 67f10deb816..eace65cfbe6 100644 --- a/x/mint/keeper/genesis_test.go +++ b/x/mint/keeper/genesis_test.go @@ -123,7 +123,9 @@ func (suite *KeeperTestSuite) TestMintInitGenesis() { // mintKeeper.InitGenesis(ctx, tc.mintGenesis) // }) - osmoutils.ConditionalPanics(suite, tc.expectPanic, mintKeeper.InitGenesis(ctx, tc.mintGenesis)) + if osmoutils.ConditionalPanic(suite.T(), tc.expectPanic || false, func() { mintKeeper.InitGenesis(ctx, tc.mintGenesis) }) { + return + } // Assertions.