feat(types): Implement .IsGT for types.Coin (#19281)

This commit is contained in:
Spoorthi
2024-01-30 21:37:32 +00:00
committed by GitHub
parent cc4ab17018
commit b2c26cdc4c
3 changed files with 35 additions and 0 deletions
+10
View File
@@ -68,6 +68,16 @@ func (coin Coin) IsZero() bool {
return coin.Amount.IsZero()
}
// IsGT returns true if they are the same type and the receiver is
// a greater value
func (coin Coin) IsGT(other Coin) bool {
if coin.Denom != other.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom))
}
return coin.Amount.GT(other.Amount)
}
// IsGTE returns true if they are the same type and the receiver is
// an equal or greater value
func (coin Coin) IsGTE(other Coin) bool {
+24
View File
@@ -302,6 +302,30 @@ func (s *coinTestSuite) TestQuoIntCoins() {
}
}
func (s *coinTestSuite) TestIsGTCoin() {
cases := []struct {
inputOne sdk.Coin
inputTwo sdk.Coin
expected bool
panics bool
}{
{sdk.NewInt64Coin(testDenom1, 2), sdk.NewInt64Coin(testDenom1, 1), true, false},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 1), false, false},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 2), false, false},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom2, 1), false, true},
}
for tcIndex, tc := range cases {
tc := tc
if tc.panics {
s.Require().Panics(func() { tc.inputOne.IsGT(tc.inputTwo) })
} else {
res := tc.inputOne.IsGT(tc.inputTwo)
s.Require().Equal(tc.expected, res, "coin GT relation is incorrect, tc #%d", tcIndex)
}
}
}
func (s *coinTestSuite) TestIsGTECoin() {
cases := []struct {
inputOne sdk.Coin