fix: replace IsEqual with Equal (#14739)

Co-authored-by: Marko <marko@baricevic.me>
Co-authored-by: Marko <marbar3778@yahoo.com>
Closes https://github.com/cosmos/cosmos-sdk/issues/3246
This commit is contained in:
Emil Georgiev
2023-01-24 09:50:34 +00:00
committed by GitHub
parent 6674402216
commit e9fbb01f3a
28 changed files with 99 additions and 126 deletions
+7 -10
View File
@@ -92,12 +92,9 @@ func (coin Coin) IsLTE(other Coin) bool {
}
// IsEqual returns true if the two sets of Coins have the same value
// Deprecated: Use Coin.Equal instead.
func (coin Coin) IsEqual(other Coin) bool {
if coin.Denom != other.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom))
}
return coin.Amount.Equal(other.Amount)
return coin.Equal(other)
}
// Add adds amounts of two coins with same denom. If the coins differ in denom then
@@ -463,7 +460,7 @@ func (coins Coins) SafeQuoInt(x Int) (Coins, bool) {
// a.IsAllLTE(a.Max(b))
// b.IsAllLTE(a.Max(b))
// a.IsAllLTE(c) && b.IsAllLTE(c) == a.Max(b).IsAllLTE(c)
// a.Add(b...).IsEqual(a.Min(b).Add(a.Max(b)...))
// a.Add(b...).Equal(a.Min(b).Add(a.Max(b)...))
//
// E.g.
// {1A, 3B, 2C}.Max({4A, 2B, 2C} == {4A, 3B, 2C})
@@ -509,7 +506,7 @@ func (coins Coins) Max(coinsB Coins) Coins {
// a.Min(b).IsAllLTE(a)
// a.Min(b).IsAllLTE(b)
// c.IsAllLTE(a) && c.IsAllLTE(b) == c.IsAllLTE(a.Min(b))
// a.Add(b...).IsEqual(a.Min(b).Add(a.Max(b)...))
// a.Add(b...).Equal(a.Min(b).Add(a.Max(b)...))
//
// E.g.
// {1A, 3B, 2C}.Min({4A, 2B, 2C} == {1A, 2B, 2C})
@@ -652,8 +649,8 @@ func (coins Coins) IsZero() bool {
return true
}
// IsEqual returns true if the two sets of Coins have the same value
func (coins Coins) IsEqual(coinsB Coins) bool {
// Equal returns true if the two sets of Coins have the same value
func (coins Coins) Equal(coinsB Coins) bool {
if len(coins) != len(coinsB) {
return false
}
@@ -662,7 +659,7 @@ func (coins Coins) IsEqual(coinsB Coins) bool {
coinsB = coinsB.Sort()
for i := 0; i < len(coins); i++ {
if !coins[i].IsEqual(coinsB[i]) {
if !coins[i].Equal(coinsB[i]) {
return false
}
}
+18 -30
View File
@@ -59,21 +59,15 @@ func (s *coinTestSuite) TestIsEqualCoin() {
inputOne sdk.Coin
inputTwo sdk.Coin
expected bool
panics bool
}{
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 1), true, false},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom2, 1), false, true},
{sdk.NewInt64Coin("stake", 1), sdk.NewInt64Coin("stake", 10), false, false},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 1), true},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom2, 1), false},
{sdk.NewInt64Coin("stake", 1), sdk.NewInt64Coin("stake", 10), false},
}
for tcIndex, tc := range cases {
tc := tc
if tc.panics {
s.Require().Panics(func() { tc.inputOne.IsEqual(tc.inputTwo) })
} else {
res := tc.inputOne.IsEqual(tc.inputTwo)
s.Require().Equal(tc.expected, res, "coin equality relation is incorrect, tc #%d", tcIndex)
}
res := tc.inputOne.IsEqual(tc.inputTwo)
s.Require().Equal(tc.expected, res, "coin equality relation is incorrect, tc #%d", tcIndex)
}
}
@@ -513,25 +507,19 @@ func (s *coinTestSuite) TestEqualCoins() {
inputOne sdk.Coins
inputTwo sdk.Coins
expected bool
panics bool
}{
{sdk.Coins{}, sdk.Coins{}, true, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, true, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, true, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom2, 0)}, false, true},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 1)}, false, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, false, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, true, false},
{sdk.Coins{}, sdk.Coins{}, true},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, true},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, true},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom2, 0)}, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 1)}, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, true},
}
for tcnum, tc := range cases {
tc := tc
if tc.panics {
s.Require().Panics(func() { tc.inputOne.IsEqual(tc.inputTwo) })
} else {
res := tc.inputOne.IsEqual(tc.inputTwo)
s.Require().Equal(tc.expected, res, "Equality is differed from exported. tc #%d, expected %b, actual %b.", tcnum, tc.expected, res)
}
res := tc.inputOne.Equal(tc.inputTwo)
s.Require().Equal(tc.expected, res, "Equality is differed from exported. tc #%d, expected %b, actual %b.", tcnum, tc.expected, res)
}
}
@@ -579,7 +567,7 @@ func TestCoinsAddCoalescesDuplicateDenominations(t *testing.T) {
{"den", sdk.NewInt(11)},
}
if !got.IsEqual(want) {
if !got.Equal(want) {
t.Fatalf("Wrong result\n\tGot: %s\n\tWant: %s", got, want)
}
}
@@ -871,8 +859,8 @@ func (s *coinTestSuite) TestMinMax() {
for _, tc := range cases {
min := tc.input1.Min(tc.input2)
max := tc.input1.Max(tc.input2)
s.Require().True(min.IsEqual(tc.min), tc.name)
s.Require().True(max.IsEqual(tc.max), tc.name)
s.Require().True(min.Equal(tc.min), tc.name)
s.Require().True(max.Equal(tc.max), tc.name)
}
}
@@ -1174,7 +1162,7 @@ func (s *coinTestSuite) TestNewCoins() {
continue
}
got := sdk.NewCoins(tt.coins...)
s.Require().True(got.IsEqual(tt.want))
s.Require().True(got.Equal(tt.want))
}
}
+4 -7
View File
@@ -79,12 +79,9 @@ func (coin DecCoin) IsLT(other DecCoin) bool {
}
// IsEqual returns true if the two sets of Coins have the same value.
// Deprecated: Use DecCoin.Equal instead.
func (coin DecCoin) IsEqual(other DecCoin) bool {
if coin.Denom != other.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom))
}
return coin.Amount.Equal(other.Amount)
return coin.Equal(other)
}
// Add adds amounts of two decimal coins with same denom.
@@ -483,8 +480,8 @@ func (coins DecCoins) AmountOf(denom string) Dec {
}
}
// IsEqual returns true if the two sets of DecCoins have the same value.
func (coins DecCoins) IsEqual(coinsB DecCoins) bool {
// Equal returns true if the two sets of DecCoins have the same value.
func (coins DecCoins) Equal(coinsB DecCoins) bool {
if len(coins) != len(coinsB) {
return false
}
+17 -27
View File
@@ -468,7 +468,7 @@ func (s *decCoinTestSuite) TestDecCoinsIntersect() {
s.Require().NoError(err, "unexpected parse error in %v", i)
exr, err := sdk.ParseDecCoins(tc.expectedResult)
s.Require().NoError(err, "unexpected parse error in %v", i)
s.Require().True(in1.Intersect(in2).IsEqual(exr), "in1.cap(in2) != exr in %v", i)
s.Require().True(in1.Intersect(in2).Equal(exr), "in1.cap(in2) != exr in %v", i)
}
}
@@ -1006,48 +1006,43 @@ func (s *decCoinTestSuite) TestDecCoin_IsEqual() {
coin sdk.DecCoin
otherCoin sdk.DecCoin
expectedResult bool
expectedPanic bool
}{
{
"Different Denom Same Amount",
sdk.DecCoin{testDenom1, math.LegacyNewDec(20)},
sdk.DecCoin{testDenom2, math.LegacyNewDec(20)},
false, true,
false,
},
{
"Different Denom Different Amount",
sdk.DecCoin{testDenom1, math.LegacyNewDec(20)},
sdk.DecCoin{testDenom2, math.LegacyNewDec(10)},
false, true,
false,
},
{
"Same Denom Different Amount",
sdk.DecCoin{testDenom1, math.LegacyNewDec(20)},
sdk.DecCoin{testDenom1, math.LegacyNewDec(10)},
false, false,
false,
},
{
"Same Denom Same Amount",
sdk.DecCoin{testDenom1, math.LegacyNewDec(20)},
sdk.DecCoin{testDenom1, math.LegacyNewDec(20)},
true, false,
true,
},
}
for i, tc := range testCases {
s.T().Run(tc.name, func(t *testing.T) {
if tc.expectedPanic {
s.Require().Panics(func() { tc.coin.IsEqual(tc.otherCoin) }, "Test case #%d: %s", i, tc.name)
res := tc.coin.IsEqual(tc.otherCoin)
if tc.expectedResult {
s.Require().True(res, "Test case #%d: %s", i, tc.name)
} else {
res := tc.coin.IsEqual(tc.otherCoin)
if tc.expectedResult {
s.Require().True(res, "Test case #%d: %s", i, tc.name)
} else {
s.Require().False(res, "Test case #%d: %s", i, tc.name)
}
s.Require().False(res, "Test case #%d: %s", i, tc.name)
}
})
}
@@ -1059,14 +1054,13 @@ func (s *decCoinTestSuite) TestDecCoins_IsEqual() {
coinsA sdk.DecCoins
coinsB sdk.DecCoins
expectedResult bool
expectedPanic bool
}{
{"Different length sets", sdk.DecCoins{
sdk.DecCoin{testDenom1, math.LegacyNewDec(3)},
sdk.DecCoin{testDenom1, math.LegacyNewDec(4)},
}, sdk.DecCoins{
sdk.DecCoin{testDenom1, math.LegacyNewDec(35)},
}, false, false},
}, false},
{"Same length - different denoms", sdk.DecCoins{
sdk.DecCoin{testDenom1, math.LegacyNewDec(3)},
@@ -1074,7 +1068,7 @@ func (s *decCoinTestSuite) TestDecCoins_IsEqual() {
}, sdk.DecCoins{
sdk.DecCoin{testDenom2, math.LegacyNewDec(3)},
sdk.DecCoin{testDenom2, math.LegacyNewDec(4)},
}, false, true},
}, false},
{"Same length - different amounts", sdk.DecCoins{
sdk.DecCoin{testDenom1, math.LegacyNewDec(3)},
@@ -1082,7 +1076,7 @@ func (s *decCoinTestSuite) TestDecCoins_IsEqual() {
}, sdk.DecCoins{
sdk.DecCoin{testDenom1, math.LegacyNewDec(41)},
sdk.DecCoin{testDenom1, math.LegacyNewDec(343)},
}, false, false},
}, false},
{"Same length - same amounts", sdk.DecCoins{
sdk.DecCoin{testDenom1, math.LegacyNewDec(33)},
@@ -1090,20 +1084,16 @@ func (s *decCoinTestSuite) TestDecCoins_IsEqual() {
}, sdk.DecCoins{
sdk.DecCoin{testDenom1, math.LegacyNewDec(33)},
sdk.DecCoin{testDenom1, math.LegacyNewDec(344)},
}, true, false},
}, true},
}
for i, tc := range testCases {
s.T().Run(tc.name, func(t *testing.T) {
if tc.expectedPanic {
s.Require().Panics(func() { tc.coinsA.IsEqual(tc.coinsB) }, "Test case #%d: %s", i, tc.name)
res := tc.coinsA.Equal(tc.coinsB)
if tc.expectedResult {
s.Require().True(res, "Test case #%d: %s", i, tc.name)
} else {
res := tc.coinsA.IsEqual(tc.coinsB)
if tc.expectedResult {
s.Require().True(res, "Test case #%d: %s", i, tc.name)
} else {
s.Require().False(res, "Test case #%d: %s", i, tc.name)
}
s.Require().False(res, "Test case #%d: %s", i, tc.name)
}
})
}