Skip to content

Commit

Permalink
test/devux: helper function for assertions conditional on panics (#1964)
Browse files Browse the repository at this point in the history
* test: helper function for assertions conditional on panics

* added devs comments

* matts comment

(cherry picked from commit 298d7cf)

# Conflicts:
#	x/gamm/pool-models/balancer/pool_suite_test.go
#	x/mint/keeper/genesis_test.go
  • Loading branch information
stackman27 authored and mergify[bot] committed Jul 19, 2022
1 parent 355a41a commit 0a73938
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
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)
}
17 changes: 17 additions & 0 deletions x/gamm/pool-models/balancer/pool_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

<<<<<<< HEAD
"github.com/osmosis-labs/osmosis/v10/app/apptesting"
v10 "github.com/osmosis-labs/osmosis/v10/app/upgrades/v10"
"github.com/osmosis-labs/osmosis/v10/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v10/x/gamm/types"
=======
"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"
>>>>>>> 298d7cf5 (test/devux: helper function for assertions conditional on panics (#1964))
)

const (
Expand Down Expand Up @@ -716,11 +724,20 @@ func (suite *KeeperTestSuite) TestCalcJoinPoolShares() {
}
}

<<<<<<< HEAD
if tc.expectPanic {
require.Panics(t, sut)
} else {
require.NotPanics(t, sut)
}
=======
balancerPool, ok := pool.(*balancer.Pool)
require.True(t, ok)

assertPoolStateNotModified(t, balancerPool, func() {
osmoutils.ConditionalPanic(t, tc.expectPanic, sut)
})
>>>>>>> 298d7cf5 (test/devux: helper function for assertions conditional on panics (#1964))
})
}
}
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)
}
}
14 changes: 7 additions & 7 deletions x/mint/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ import (

"github.com/stretchr/testify/suite"

<<<<<<< HEAD
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/osmosis-labs/osmosis/v10/x/mint/keeper"
"github.com/osmosis-labs/osmosis/v10/x/mint/types"

sdk "github.com/cosmos/cosmos-sdk/types"
=======
"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"
>>>>>>> 298d7cf5 (test/devux: helper function for assertions conditional on panics (#1964))
)

var customGenesis = types.NewGenesisState(
Expand Down Expand Up @@ -113,17 +119,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

0 comments on commit 0a73938

Please sign in to comment.