Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test/devux: helper function for assertions conditional on panics #1964

Merged
merged 3 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions osmoutils/test_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package osmoutils

import (
"testing"

"github.com/stretchr/testify/require"
)

// ConditionalPanic checks 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 does not
func ConditionalPanic(t *testing.T, expectPanic bool, sut func()) {
if expectPanic {
require.Panics(t, sut)
return
}
require.NotPanics(t, sut)
}
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)
}
}
9 changes: 2 additions & 7 deletions x/mint/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -111,17 +112,11 @@ func (suite *KeeperTestSuite) TestMintInitGenesis() {
originalVestingCoins := bankKeeper.GetBalance(ctx, developerAccount, tc.mintDenom)

// Test.
osmoutils.ConditionalPanic(suite.T(), tc.expectPanic, 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)
})

// Assertions.

// Module account was created.
Expand Down
2 changes: 1 addition & 1 deletion x/mint/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (suite *KeeperTestSuite) TestSetInitialSupplyOffsetDuringMigration() {
isDeveloperModuleAccountCreated: true,
},
"dev vesting module account does not exist": {
blockHeight: 1,
blockHeight: 1,
expectedError: keeper.ErrDevVestingModuleAccountNotCreated,
},
}
Expand Down