Skip to content

Commit

Permalink
added
Browse files Browse the repository at this point in the history
  • Loading branch information
stackman27 committed Jul 4, 2022
1 parent ea0464b commit be7dd7a
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 52 deletions.
7 changes: 5 additions & 2 deletions app/upgrades/v8/msg_filter_ante_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package v8_test

import (
"github.com/osmosis-labs/osmosis/v7/osmoutils"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -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)
}
})
}
}
6 changes: 5 additions & 1 deletion app/upgrades/v9/msg_filter_ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
6 changes: 5 additions & 1 deletion osmoutils/binary_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down
28 changes: 9 additions & 19 deletions osmoutils/cli_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
19 changes: 10 additions & 9 deletions wasmbinding/test/custom_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
})
}
}
Expand Down
14 changes: 7 additions & 7 deletions wasmbinding/test/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion x/gamm/pool-models/balancer/pool_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
})
})
}
Expand Down
2 changes: 1 addition & 1 deletion x/gamm/pool-models/balancer/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func TestCalcSingleAssetJoin(t *testing.T) {
}

assertPoolStateNotModified(t, balancerPool, func() {
assertPanic(t, tc.expectPanic, sut)
osmoutils.ConditionalPanic(t, tc.expectPanic, sut)
})
})
}
Expand Down
10 changes: 0 additions & 10 deletions x/gamm/pool-models/balancer/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 3 additions & 1 deletion x/mint/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit be7dd7a

Please sign in to comment.