2020-09-27 14:51:14 -07:00
|
|
|
package types_test
|
2018-10-05 03:41:38 -07:00
|
|
|
|
|
|
|
import (
|
2019-02-15 07:33:23 -08:00
|
|
|
"strings"
|
2018-10-05 03:41:38 -07:00
|
|
|
"testing"
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
"github.com/stretchr/testify/suite"
|
2020-09-27 14:51:14 -07:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-10-05 03:41:38 -07:00
|
|
|
)
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
type decCoinTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
2020-10-08 04:42:32 -07:00
|
|
|
func TestDecCoinTestSuite(t *testing.T) {
|
2020-09-29 04:29:10 -07:00
|
|
|
suite.Run(t, new(decCoinTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *decCoinTestSuite) TestNewDecCoin() {
|
|
|
|
s.Require().NotPanics(func() {
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewInt64DecCoin(testDenom1, 5)
|
2019-01-18 08:45:20 -08:00
|
|
|
})
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().NotPanics(func() {
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewInt64DecCoin(testDenom1, 0)
|
2019-01-18 08:45:20 -08:00
|
|
|
})
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().NotPanics(func() {
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewInt64DecCoin(strings.ToUpper(testDenom1), 5)
|
2019-01-18 08:45:20 -08:00
|
|
|
})
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Panics(func() {
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewInt64DecCoin(testDenom1, -5)
|
2019-01-18 08:45:20 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestNewDecCoinFromDec() {
|
|
|
|
s.Require().NotPanics(func() {
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoinFromDec(testDenom1, sdk.NewDec(5))
|
2019-01-18 08:45:20 -08:00
|
|
|
})
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().NotPanics(func() {
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoinFromDec(testDenom1, sdk.ZeroDec())
|
2019-01-18 08:45:20 -08:00
|
|
|
})
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().NotPanics(func() {
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoinFromDec(strings.ToUpper(testDenom1), sdk.NewDec(5))
|
2019-01-18 08:45:20 -08:00
|
|
|
})
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Panics(func() {
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoinFromDec(testDenom1, sdk.NewDec(-5))
|
2019-01-18 08:45:20 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestNewDecCoinFromCoin() {
|
|
|
|
s.Require().NotPanics(func() {
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoinFromCoin(sdk.Coin{testDenom1, sdk.NewInt(5)})
|
2019-01-18 08:45:20 -08:00
|
|
|
})
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().NotPanics(func() {
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoinFromCoin(sdk.Coin{testDenom1, sdk.NewInt(0)})
|
2019-01-18 08:45:20 -08:00
|
|
|
})
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().NotPanics(func() {
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoinFromCoin(sdk.Coin{strings.ToUpper(testDenom1), sdk.NewInt(5)})
|
2019-01-18 08:45:20 -08:00
|
|
|
})
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Panics(func() {
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoinFromCoin(sdk.Coin{testDenom1, sdk.NewInt(-5)})
|
2019-01-18 08:45:20 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestDecCoinIsPositive() {
|
2020-09-27 14:51:14 -07:00
|
|
|
dc := sdk.NewInt64DecCoin(testDenom1, 5)
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().True(dc.IsPositive())
|
2019-01-18 08:45:20 -08:00
|
|
|
|
2020-09-27 14:51:14 -07:00
|
|
|
dc = sdk.NewInt64DecCoin(testDenom1, 0)
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().False(dc.IsPositive())
|
2019-01-18 08:45:20 -08:00
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestAddDecCoin() {
|
2020-09-27 14:51:14 -07:00
|
|
|
decCoinA1 := sdk.NewDecCoinFromDec(testDenom1, sdk.NewDecWithPrec(11, 1))
|
|
|
|
decCoinA2 := sdk.NewDecCoinFromDec(testDenom1, sdk.NewDecWithPrec(22, 1))
|
|
|
|
decCoinB1 := sdk.NewDecCoinFromDec(testDenom2, sdk.NewDecWithPrec(11, 1))
|
2018-10-05 03:41:38 -07:00
|
|
|
|
|
|
|
// regular add
|
2019-02-21 09:35:55 -08:00
|
|
|
res := decCoinA1.Add(decCoinA1)
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Equal(decCoinA2, res, "sum of coins is incorrect")
|
2018-10-05 03:41:38 -07:00
|
|
|
|
|
|
|
// bad denom add
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Panics(func() {
|
2019-02-21 09:35:55 -08:00
|
|
|
decCoinA1.Add(decCoinB1)
|
2018-10-05 03:41:38 -07:00
|
|
|
}, "expected panic on sum of different denoms")
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestAddDecCoins() {
|
2020-09-27 14:51:14 -07:00
|
|
|
one := sdk.NewDec(1)
|
|
|
|
zero := sdk.NewDec(0)
|
|
|
|
two := sdk.NewDec(2)
|
2018-10-05 03:41:38 -07:00
|
|
|
|
|
|
|
cases := []struct {
|
2020-09-27 14:51:14 -07:00
|
|
|
inputOne sdk.DecCoins
|
|
|
|
inputTwo sdk.DecCoins
|
|
|
|
expected sdk.DecCoins
|
2018-10-05 03:41:38 -07:00
|
|
|
}{
|
2020-09-27 14:51:14 -07:00
|
|
|
{sdk.DecCoins{{testDenom1, one}, {testDenom2, one}}, sdk.DecCoins{{testDenom1, one}, {testDenom2, one}}, sdk.DecCoins{{testDenom1, two}, {testDenom2, two}}},
|
|
|
|
{sdk.DecCoins{{testDenom1, zero}, {testDenom2, one}}, sdk.DecCoins{{testDenom1, zero}, {testDenom2, zero}}, sdk.DecCoins{{testDenom2, one}}},
|
|
|
|
{sdk.DecCoins{{testDenom1, zero}, {testDenom2, zero}}, sdk.DecCoins{{testDenom1, zero}, {testDenom2, zero}}, sdk.DecCoins(nil)},
|
2018-10-05 03:41:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for tcIndex, tc := range cases {
|
2020-01-03 12:44:53 -08:00
|
|
|
res := tc.inputOne.Add(tc.inputTwo...)
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Equal(tc.expected, res, "sum of coins is incorrect, tc #%d", tcIndex)
|
2018-10-05 03:41:38 -07:00
|
|
|
}
|
|
|
|
}
|
2019-01-18 08:45:20 -08:00
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestFilteredZeroDecCoins() {
|
2020-07-28 03:17:58 -07:00
|
|
|
cases := []struct {
|
|
|
|
name string
|
2020-09-27 14:51:14 -07:00
|
|
|
input sdk.DecCoins
|
2020-07-28 03:17:58 -07:00
|
|
|
original string
|
|
|
|
expected string
|
2021-07-26 13:51:22 -07:00
|
|
|
panic bool
|
2020-07-28 03:17:58 -07:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "all greater than zero",
|
2020-09-27 14:51:14 -07:00
|
|
|
input: sdk.DecCoins{
|
|
|
|
{"testa", sdk.NewDec(1)},
|
|
|
|
{"testb", sdk.NewDec(2)},
|
|
|
|
{"testc", sdk.NewDec(3)},
|
|
|
|
{"testd", sdk.NewDec(4)},
|
|
|
|
{"teste", sdk.NewDec(5)},
|
2020-07-28 03:17:58 -07:00
|
|
|
},
|
|
|
|
original: "1.000000000000000000testa,2.000000000000000000testb,3.000000000000000000testc,4.000000000000000000testd,5.000000000000000000teste",
|
|
|
|
expected: "1.000000000000000000testa,2.000000000000000000testb,3.000000000000000000testc,4.000000000000000000testd,5.000000000000000000teste",
|
2021-07-26 13:51:22 -07:00
|
|
|
panic: false,
|
2020-07-28 03:17:58 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "zero coin in middle",
|
2020-09-27 14:51:14 -07:00
|
|
|
input: sdk.DecCoins{
|
|
|
|
{"testa", sdk.NewDec(1)},
|
|
|
|
{"testb", sdk.NewDec(2)},
|
|
|
|
{"testc", sdk.NewDec(0)},
|
|
|
|
{"testd", sdk.NewDec(4)},
|
|
|
|
{"teste", sdk.NewDec(5)},
|
2020-07-28 03:17:58 -07:00
|
|
|
},
|
|
|
|
original: "1.000000000000000000testa,2.000000000000000000testb,0.000000000000000000testc,4.000000000000000000testd,5.000000000000000000teste",
|
|
|
|
expected: "1.000000000000000000testa,2.000000000000000000testb,4.000000000000000000testd,5.000000000000000000teste",
|
2021-07-26 13:51:22 -07:00
|
|
|
panic: false,
|
2020-07-28 03:17:58 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "zero coin end (unordered)",
|
2020-09-27 14:51:14 -07:00
|
|
|
input: sdk.DecCoins{
|
|
|
|
{"teste", sdk.NewDec(5)},
|
|
|
|
{"testc", sdk.NewDec(3)},
|
|
|
|
{"testa", sdk.NewDec(1)},
|
|
|
|
{"testd", sdk.NewDec(4)},
|
|
|
|
{"testb", sdk.NewDec(0)},
|
2020-07-28 03:17:58 -07:00
|
|
|
},
|
|
|
|
original: "5.000000000000000000teste,3.000000000000000000testc,1.000000000000000000testa,4.000000000000000000testd,0.000000000000000000testb",
|
|
|
|
expected: "1.000000000000000000testa,3.000000000000000000testc,4.000000000000000000testd,5.000000000000000000teste",
|
2021-07-26 13:51:22 -07:00
|
|
|
panic: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "panic when same denoms in multiple coins",
|
|
|
|
input: sdk.DecCoins{
|
|
|
|
{"testa", sdk.NewDec(5)},
|
|
|
|
{"testa", sdk.NewDec(3)},
|
|
|
|
{"testa", sdk.NewDec(1)},
|
|
|
|
{"testd", sdk.NewDec(4)},
|
|
|
|
{"testb", sdk.NewDec(2)},
|
|
|
|
},
|
|
|
|
original: "5.000000000000000000teste,3.000000000000000000testc,1.000000000000000000testa,4.000000000000000000testd,0.000000000000000000testb",
|
|
|
|
expected: "1.000000000000000000testa,3.000000000000000000testc,4.000000000000000000testd,5.000000000000000000teste",
|
|
|
|
panic: true,
|
2020-07-28 03:17:58 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range cases {
|
2021-07-26 13:51:22 -07:00
|
|
|
if tt.panic {
|
|
|
|
s.Require().Panics(func() { sdk.NewDecCoins(tt.input...) }, "Should panic due to multiple coins with same denom")
|
|
|
|
} else {
|
|
|
|
undertest := sdk.NewDecCoins(tt.input...)
|
|
|
|
s.Require().Equal(tt.expected, undertest.String(), "NewDecCoins must return expected results")
|
|
|
|
s.Require().Equal(tt.original, tt.input.String(), "input must be unmodified and match original")
|
|
|
|
}
|
2020-07-28 03:17:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestIsValid() {
|
2019-12-16 14:10:18 -08:00
|
|
|
tests := []struct {
|
2020-09-27 14:51:14 -07:00
|
|
|
coin sdk.DecCoin
|
2019-12-16 14:10:18 -08:00
|
|
|
expectPass bool
|
|
|
|
msg string
|
|
|
|
}{
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoin("mytoken", sdk.NewInt(10)),
|
2019-12-16 14:10:18 -08:00
|
|
|
true,
|
|
|
|
"valid coins should have passed",
|
|
|
|
},
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.DecCoin{Denom: "BTC", Amount: sdk.NewDec(10)},
|
2020-08-14 02:09:53 -07:00
|
|
|
true,
|
|
|
|
"valid uppercase denom",
|
2019-12-16 14:10:18 -08:00
|
|
|
},
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.DecCoin{Denom: "Bitcoin", Amount: sdk.NewDec(10)},
|
2020-08-14 02:09:53 -07:00
|
|
|
true,
|
|
|
|
"valid mixed case denom",
|
|
|
|
},
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.DecCoin{Denom: "btc", Amount: sdk.NewDec(-10)},
|
2019-12-16 14:10:18 -08:00
|
|
|
false,
|
|
|
|
"negative amount",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
if tc.expectPass {
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().True(tc.coin.IsValid(), tc.msg)
|
2019-12-16 14:10:18 -08:00
|
|
|
} else {
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().False(tc.coin.IsValid(), tc.msg)
|
2019-12-16 14:10:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestSubDecCoin() {
|
2019-12-16 14:10:18 -08:00
|
|
|
tests := []struct {
|
2020-09-27 14:51:14 -07:00
|
|
|
coin sdk.DecCoin
|
2019-12-16 14:10:18 -08:00
|
|
|
expectPass bool
|
|
|
|
msg string
|
|
|
|
}{
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoin("mytoken", sdk.NewInt(20)),
|
2019-12-16 14:10:18 -08:00
|
|
|
true,
|
|
|
|
"valid coins should have passed",
|
|
|
|
},
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoin("othertoken", sdk.NewInt(20)),
|
2019-12-16 14:10:18 -08:00
|
|
|
false,
|
|
|
|
"denom mismatch",
|
|
|
|
},
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoin("mytoken", sdk.NewInt(9)),
|
2019-12-16 14:10:18 -08:00
|
|
|
false,
|
|
|
|
"negative amount",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-27 14:51:14 -07:00
|
|
|
decCoin := sdk.NewDecCoin("mytoken", sdk.NewInt(10))
|
2019-12-16 14:10:18 -08:00
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
if tc.expectPass {
|
|
|
|
equal := tc.coin.Sub(decCoin)
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Equal(equal, decCoin, tc.msg)
|
2019-12-16 14:10:18 -08:00
|
|
|
} else {
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Panics(func() { tc.coin.Sub(decCoin) }, tc.msg)
|
2019-12-16 14:10:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestSubDecCoins() {
|
2019-12-16 14:10:18 -08:00
|
|
|
tests := []struct {
|
2020-09-27 14:51:14 -07:00
|
|
|
coins sdk.DecCoins
|
2019-12-16 14:10:18 -08:00
|
|
|
expectPass bool
|
|
|
|
msg string
|
|
|
|
}{
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoinsFromCoins(sdk.NewCoin("mytoken", sdk.NewInt(10)), sdk.NewCoin("btc", sdk.NewInt(20)), sdk.NewCoin("eth", sdk.NewInt(30))),
|
2019-12-16 14:10:18 -08:00
|
|
|
true,
|
|
|
|
"sorted coins should have passed",
|
|
|
|
},
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.DecCoins{sdk.NewDecCoin("mytoken", sdk.NewInt(10)), sdk.NewDecCoin("btc", sdk.NewInt(20)), sdk.NewDecCoin("eth", sdk.NewInt(30))},
|
2019-12-16 14:10:18 -08:00
|
|
|
false,
|
|
|
|
"unorted coins should panic",
|
|
|
|
},
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.DecCoins{sdk.DecCoin{Denom: "BTC", Amount: sdk.NewDec(10)}, sdk.NewDecCoin("eth", sdk.NewInt(15)), sdk.NewDecCoin("mytoken", sdk.NewInt(5))},
|
2019-12-16 14:10:18 -08:00
|
|
|
false,
|
|
|
|
"invalid denoms",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-27 14:51:14 -07:00
|
|
|
decCoins := sdk.NewDecCoinsFromCoins(sdk.NewCoin("btc", sdk.NewInt(10)), sdk.NewCoin("eth", sdk.NewInt(15)), sdk.NewCoin("mytoken", sdk.NewInt(5)))
|
2019-12-16 14:10:18 -08:00
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
if tc.expectPass {
|
|
|
|
equal := tc.coins.Sub(decCoins)
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Equal(equal, decCoins, tc.msg)
|
2019-12-16 14:10:18 -08:00
|
|
|
} else {
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Panics(func() { tc.coins.Sub(decCoins) }, tc.msg)
|
2019-12-16 14:10:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestSortDecCoins() {
|
2020-09-27 14:51:14 -07:00
|
|
|
good := sdk.DecCoins{
|
|
|
|
sdk.NewInt64DecCoin("gas", 1),
|
|
|
|
sdk.NewInt64DecCoin("mineral", 1),
|
|
|
|
sdk.NewInt64DecCoin("tree", 1),
|
2019-01-18 08:45:20 -08:00
|
|
|
}
|
2020-09-27 14:51:14 -07:00
|
|
|
empty := sdk.DecCoins{
|
|
|
|
sdk.NewInt64DecCoin("gold", 0),
|
2019-01-18 08:45:20 -08:00
|
|
|
}
|
2020-09-27 14:51:14 -07:00
|
|
|
badSort1 := sdk.DecCoins{
|
|
|
|
sdk.NewInt64DecCoin("tree", 1),
|
|
|
|
sdk.NewInt64DecCoin("gas", 1),
|
|
|
|
sdk.NewInt64DecCoin("mineral", 1),
|
2019-01-18 08:45:20 -08:00
|
|
|
}
|
2020-09-27 14:51:14 -07:00
|
|
|
badSort2 := sdk.DecCoins{ // both are after the first one, but the second and third are in the wrong order
|
|
|
|
sdk.NewInt64DecCoin("gas", 1),
|
|
|
|
sdk.NewInt64DecCoin("tree", 1),
|
|
|
|
sdk.NewInt64DecCoin("mineral", 1),
|
2019-01-18 08:45:20 -08:00
|
|
|
}
|
2020-09-27 14:51:14 -07:00
|
|
|
badAmt := sdk.DecCoins{
|
|
|
|
sdk.NewInt64DecCoin("gas", 1),
|
|
|
|
sdk.NewInt64DecCoin("tree", 0),
|
|
|
|
sdk.NewInt64DecCoin("mineral", 1),
|
2019-01-18 08:45:20 -08:00
|
|
|
}
|
2020-09-27 14:51:14 -07:00
|
|
|
dup := sdk.DecCoins{
|
|
|
|
sdk.NewInt64DecCoin("gas", 1),
|
|
|
|
sdk.NewInt64DecCoin("gas", 1),
|
|
|
|
sdk.NewInt64DecCoin("mineral", 1),
|
2019-01-18 08:45:20 -08:00
|
|
|
}
|
|
|
|
cases := []struct {
|
2020-08-14 02:09:53 -07:00
|
|
|
name string
|
2020-09-27 14:51:14 -07:00
|
|
|
coins sdk.DecCoins
|
2019-01-18 08:45:20 -08:00
|
|
|
before, after bool // valid before/after sort
|
|
|
|
}{
|
2020-08-14 02:09:53 -07:00
|
|
|
{"valid coins", good, true, true},
|
|
|
|
{"empty coins", empty, false, false},
|
|
|
|
{"unsorted coins (1)", badSort1, false, true},
|
|
|
|
{"unsorted coins (2)", badSort2, false, true},
|
|
|
|
{"zero amount coins", badAmt, false, false},
|
|
|
|
{"duplicate coins", dup, false, false},
|
2019-01-18 08:45:20 -08:00
|
|
|
}
|
|
|
|
|
2020-08-14 02:09:53 -07:00
|
|
|
for _, tc := range cases {
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Equal(tc.before, tc.coins.IsValid(), "coin validity is incorrect before sorting; %s", tc.name)
|
2019-01-18 08:45:20 -08:00
|
|
|
tc.coins.Sort()
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Equal(tc.after, tc.coins.IsValid(), "coin validity is incorrect after sorting; %s", tc.name)
|
2019-01-18 08:45:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestDecCoinsValidate() {
|
2019-01-18 08:45:20 -08:00
|
|
|
testCases := []struct {
|
2020-09-27 14:51:14 -07:00
|
|
|
input sdk.DecCoins
|
2020-08-14 02:09:53 -07:00
|
|
|
expectedPass bool
|
2019-01-18 08:45:20 -08:00
|
|
|
}{
|
2020-09-27 14:51:14 -07:00
|
|
|
{sdk.DecCoins{}, true},
|
|
|
|
{sdk.DecCoins{sdk.DecCoin{testDenom1, sdk.NewDec(5)}}, true},
|
|
|
|
{sdk.DecCoins{sdk.DecCoin{testDenom1, sdk.NewDec(5)}, sdk.DecCoin{testDenom2, sdk.NewDec(100000)}}, true},
|
|
|
|
{sdk.DecCoins{sdk.DecCoin{testDenom1, sdk.NewDec(-5)}}, false},
|
|
|
|
{sdk.DecCoins{sdk.DecCoin{"BTC", sdk.NewDec(5)}}, true},
|
|
|
|
{sdk.DecCoins{sdk.DecCoin{"0BTC", sdk.NewDec(5)}}, false},
|
|
|
|
{sdk.DecCoins{sdk.DecCoin{testDenom1, sdk.NewDec(5)}, sdk.DecCoin{"B", sdk.NewDec(100000)}}, false},
|
|
|
|
{sdk.DecCoins{sdk.DecCoin{testDenom1, sdk.NewDec(5)}, sdk.DecCoin{testDenom2, sdk.NewDec(-100000)}}, false},
|
|
|
|
{sdk.DecCoins{sdk.DecCoin{testDenom1, sdk.NewDec(-5)}, sdk.DecCoin{testDenom2, sdk.NewDec(100000)}}, false},
|
|
|
|
{sdk.DecCoins{sdk.DecCoin{"BTC", sdk.NewDec(5)}, sdk.DecCoin{testDenom2, sdk.NewDec(100000)}}, true},
|
|
|
|
{sdk.DecCoins{sdk.DecCoin{"0BTC", sdk.NewDec(5)}, sdk.DecCoin{testDenom2, sdk.NewDec(100000)}}, false},
|
2019-01-18 08:45:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
2020-08-14 02:09:53 -07:00
|
|
|
err := tc.input.Validate()
|
|
|
|
if tc.expectedPass {
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().NoError(err, "unexpected result for test case #%d, input: %v", i, tc.input)
|
2020-08-14 02:09:53 -07:00
|
|
|
} else {
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Error(err, "unexpected result for test case #%d, input: %v", i, tc.input)
|
2020-08-14 02:09:53 -07:00
|
|
|
}
|
2019-01-18 08:45:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestParseDecCoins() {
|
2019-01-18 08:45:20 -08:00
|
|
|
testCases := []struct {
|
|
|
|
input string
|
2020-09-27 14:51:14 -07:00
|
|
|
expectedResult sdk.DecCoins
|
2019-01-18 08:45:20 -08:00
|
|
|
expectedErr bool
|
|
|
|
}{
|
|
|
|
{"", nil, false},
|
2020-11-16 03:34:54 -08:00
|
|
|
{"4stake", sdk.DecCoins{sdk.NewDecCoinFromDec("stake", sdk.NewDecFromInt(sdk.NewInt(4)))}, false},
|
|
|
|
{"5.5atom,4stake", sdk.DecCoins{
|
|
|
|
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5500000000000000000, sdk.Precision)),
|
|
|
|
sdk.NewDecCoinFromDec("stake", sdk.NewDec(4)),
|
|
|
|
}, false},
|
2020-09-27 14:51:14 -07:00
|
|
|
{"0.0stake", sdk.DecCoins{}, false}, // remove zero coins
|
2020-09-25 01:31:31 -07:00
|
|
|
{"10.0btc,1.0atom,20.0btc", nil, true},
|
2020-08-14 02:09:53 -07:00
|
|
|
{
|
|
|
|
"0.004STAKE",
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.DecCoins{sdk.NewDecCoinFromDec("STAKE", sdk.NewDecWithPrec(4000000000000000, sdk.Precision))},
|
2020-08-14 02:09:53 -07:00
|
|
|
false,
|
|
|
|
},
|
2019-01-18 08:45:20 -08:00
|
|
|
{
|
|
|
|
"0.004stake",
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.DecCoins{sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, sdk.Precision))},
|
2019-01-18 08:45:20 -08:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"5.04atom,0.004stake",
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.DecCoins{
|
|
|
|
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5040000000000000000, sdk.Precision)),
|
|
|
|
sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, sdk.Precision)),
|
2019-01-18 08:45:20 -08:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2020-09-25 01:31:31 -07:00
|
|
|
{"0.0stake,0.004stake,5.04atom", // remove zero coins
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.DecCoins{
|
|
|
|
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5040000000000000000, sdk.Precision)),
|
|
|
|
sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, sdk.Precision)),
|
2020-09-25 01:31:31 -07:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2019-01-18 08:45:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
2020-09-27 14:51:14 -07:00
|
|
|
res, err := sdk.ParseDecCoins(tc.input)
|
2019-01-18 08:45:20 -08:00
|
|
|
if tc.expectedErr {
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Error(err, "expected error for test case #%d, input: %v", i, tc.input)
|
2019-01-18 08:45:20 -08:00
|
|
|
} else {
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().NoError(err, "unexpected error for test case #%d, input: %v", i, tc.input)
|
|
|
|
s.Require().Equal(tc.expectedResult, res, "unexpected result for test case #%d, input: %v", i, tc.input)
|
2019-01-18 08:45:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestDecCoinsString() {
|
2019-01-18 08:45:20 -08:00
|
|
|
testCases := []struct {
|
2020-09-27 14:51:14 -07:00
|
|
|
input sdk.DecCoins
|
2019-01-18 08:45:20 -08:00
|
|
|
expected string
|
|
|
|
}{
|
2020-09-27 14:51:14 -07:00
|
|
|
{sdk.DecCoins{}, ""},
|
2019-01-18 08:45:20 -08:00
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.DecCoins{
|
|
|
|
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5040000000000000000, sdk.Precision)),
|
|
|
|
sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, sdk.Precision)),
|
2019-01-18 08:45:20 -08:00
|
|
|
},
|
|
|
|
"5.040000000000000000atom,0.004000000000000000stake",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
out := tc.input.String()
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Equal(tc.expected, out, "unexpected result for test case #%d, input: %v", i, tc.input)
|
2019-01-18 08:45:20 -08:00
|
|
|
}
|
|
|
|
}
|
2019-02-27 12:38:57 -08:00
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestDecCoinsIntersect() {
|
2019-02-27 12:38:57 -08:00
|
|
|
testCases := []struct {
|
|
|
|
input1 string
|
|
|
|
input2 string
|
|
|
|
expectedResult string
|
|
|
|
}{
|
|
|
|
{"", "", ""},
|
|
|
|
{"1.0stake", "", ""},
|
|
|
|
{"1.0stake", "1.0stake", "1.0stake"},
|
|
|
|
{"", "1.0stake", ""},
|
|
|
|
{"1.0stake", "", ""},
|
|
|
|
{"2.0stake,1.0trope", "1.9stake", "1.9stake"},
|
|
|
|
{"2.0stake,1.0trope", "2.1stake", "2.0stake"},
|
|
|
|
{"2.0stake,1.0trope", "0.9trope", "0.9trope"},
|
|
|
|
{"2.0stake,1.0trope", "1.9stake,0.9trope", "1.9stake,0.9trope"},
|
|
|
|
{"2.0stake,1.0trope", "1.9stake,0.9trope,20.0other", "1.9stake,0.9trope"},
|
|
|
|
{"2.0stake,1.0trope", "1.0other", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
2020-09-27 14:51:14 -07:00
|
|
|
in1, err := sdk.ParseDecCoins(tc.input1)
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().NoError(err, "unexpected parse error in %v", i)
|
2020-09-27 14:51:14 -07:00
|
|
|
in2, err := sdk.ParseDecCoins(tc.input2)
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().NoError(err, "unexpected parse error in %v", i)
|
2020-09-27 14:51:14 -07:00
|
|
|
exr, err := sdk.ParseDecCoins(tc.expectedResult)
|
2020-09-29 04:29:10 -07:00
|
|
|
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)
|
2019-03-16 16:14:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestDecCoinsTruncateDecimal() {
|
2020-09-27 14:51:14 -07:00
|
|
|
decCoinA := sdk.NewDecCoinFromDec("bar", sdk.MustNewDecFromStr("5.41"))
|
|
|
|
decCoinB := sdk.NewDecCoinFromDec("foo", sdk.MustNewDecFromStr("6.00"))
|
2019-03-16 16:14:37 -07:00
|
|
|
|
|
|
|
testCases := []struct {
|
2020-09-27 14:51:14 -07:00
|
|
|
input sdk.DecCoins
|
|
|
|
truncatedCoins sdk.Coins
|
|
|
|
changeCoins sdk.DecCoins
|
2019-03-16 16:14:37 -07:00
|
|
|
}{
|
2020-09-27 14:51:14 -07:00
|
|
|
{sdk.DecCoins{}, sdk.Coins(nil), sdk.DecCoins(nil)},
|
2019-03-16 16:14:37 -07:00
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.DecCoins{decCoinA, decCoinB},
|
|
|
|
sdk.Coins{sdk.NewInt64Coin(decCoinA.Denom, 5), sdk.NewInt64Coin(decCoinB.Denom, 6)},
|
|
|
|
sdk.DecCoins{sdk.NewDecCoinFromDec(decCoinA.Denom, sdk.MustNewDecFromStr("0.41"))},
|
2019-03-16 16:14:37 -07:00
|
|
|
},
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.DecCoins{decCoinB},
|
|
|
|
sdk.Coins{sdk.NewInt64Coin(decCoinB.Denom, 6)},
|
|
|
|
sdk.DecCoins(nil),
|
2019-03-16 16:14:37 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
truncatedCoins, changeCoins := tc.input.TruncateDecimal()
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Equal(
|
|
|
|
tc.truncatedCoins, truncatedCoins,
|
2019-03-16 16:14:37 -07:00
|
|
|
"unexpected truncated coins; tc #%d, input: %s", i, tc.input,
|
|
|
|
)
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Equal(
|
|
|
|
tc.changeCoins, changeCoins,
|
2019-03-16 16:14:37 -07:00
|
|
|
"unexpected change coins; tc #%d, input: %s", i, tc.input,
|
|
|
|
)
|
2019-02-27 12:38:57 -08:00
|
|
|
}
|
|
|
|
}
|
2019-04-05 11:13:22 -07:00
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestDecCoinsQuoDecTruncate() {
|
2020-09-27 14:51:14 -07:00
|
|
|
x := sdk.MustNewDecFromStr("1.00")
|
|
|
|
y := sdk.MustNewDecFromStr("10000000000000000000.00")
|
2019-04-05 11:13:22 -07:00
|
|
|
|
|
|
|
testCases := []struct {
|
2020-09-27 14:51:14 -07:00
|
|
|
coins sdk.DecCoins
|
|
|
|
input sdk.Dec
|
|
|
|
result sdk.DecCoins
|
2019-04-05 11:13:22 -07:00
|
|
|
panics bool
|
|
|
|
}{
|
2020-09-27 14:51:14 -07:00
|
|
|
{sdk.DecCoins{}, sdk.ZeroDec(), sdk.DecCoins(nil), true},
|
|
|
|
{sdk.DecCoins{sdk.NewDecCoinFromDec("foo", x)}, y, sdk.DecCoins(nil), false},
|
|
|
|
{sdk.DecCoins{sdk.NewInt64DecCoin("foo", 5)}, sdk.NewDec(2), sdk.DecCoins{sdk.NewDecCoinFromDec("foo", sdk.MustNewDecFromStr("2.5"))}, false},
|
2019-04-05 11:13:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
2019-10-17 06:47:35 -07:00
|
|
|
tc := tc
|
2019-04-05 11:13:22 -07:00
|
|
|
if tc.panics {
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Panics(func() { tc.coins.QuoDecTruncate(tc.input) })
|
2019-04-05 11:13:22 -07:00
|
|
|
} else {
|
|
|
|
res := tc.coins.QuoDecTruncate(tc.input)
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Equal(tc.result, res, "unexpected result; tc #%d, coins: %s, input: %s", i, tc.coins, tc.input)
|
2019-04-05 11:13:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-03 12:44:53 -08:00
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestNewDecCoinsWithIsValid() {
|
2020-09-27 14:51:14 -07:00
|
|
|
fake1 := append(sdk.NewDecCoins(sdk.NewDecCoin("mytoken", sdk.NewInt(10))), sdk.DecCoin{Denom: "10BTC", Amount: sdk.NewDec(10)})
|
|
|
|
fake2 := append(sdk.NewDecCoins(sdk.NewDecCoin("mytoken", sdk.NewInt(10))), sdk.DecCoin{Denom: "BTC", Amount: sdk.NewDec(-10)})
|
2020-01-03 12:44:53 -08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-09-27 14:51:14 -07:00
|
|
|
coin sdk.DecCoins
|
2020-01-03 12:44:53 -08:00
|
|
|
expectPass bool
|
|
|
|
msg string
|
|
|
|
}{
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoins(sdk.NewDecCoin("mytoken", sdk.NewInt(10))),
|
2020-01-03 12:44:53 -08:00
|
|
|
true,
|
|
|
|
"valid coins should have passed",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fake1,
|
|
|
|
false,
|
|
|
|
"invalid denoms",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fake2,
|
|
|
|
false,
|
|
|
|
"negative amount",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
if tc.expectPass {
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().True(tc.coin.IsValid(), tc.msg)
|
2020-01-03 12:44:53 -08:00
|
|
|
} else {
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().False(tc.coin.IsValid(), tc.msg)
|
2020-01-03 12:44:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *decCoinTestSuite) TestDecCoins_AddDecCoinWithIsValid() {
|
2020-09-27 14:51:14 -07:00
|
|
|
lengthTestDecCoins := sdk.NewDecCoins().Add(sdk.NewDecCoin("mytoken", sdk.NewInt(10))).Add(sdk.DecCoin{Denom: "BTC", Amount: sdk.NewDec(10)})
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().Equal(2, len(lengthTestDecCoins), "should be 2")
|
2020-01-03 12:44:53 -08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-09-27 14:51:14 -07:00
|
|
|
coin sdk.DecCoins
|
2020-01-03 12:44:53 -08:00
|
|
|
expectPass bool
|
|
|
|
msg string
|
|
|
|
}{
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoins().Add(sdk.NewDecCoin("mytoken", sdk.NewInt(10))),
|
2020-01-03 12:44:53 -08:00
|
|
|
true,
|
|
|
|
"valid coins should have passed",
|
|
|
|
},
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoins().Add(sdk.NewDecCoin("mytoken", sdk.NewInt(10))).Add(sdk.DecCoin{Denom: "0BTC", Amount: sdk.NewDec(10)}),
|
2020-01-03 12:44:53 -08:00
|
|
|
false,
|
|
|
|
"invalid denoms",
|
|
|
|
},
|
|
|
|
{
|
2020-09-27 14:51:14 -07:00
|
|
|
sdk.NewDecCoins().Add(sdk.NewDecCoin("mytoken", sdk.NewInt(10))).Add(sdk.DecCoin{Denom: "BTC", Amount: sdk.NewDec(-10)}),
|
2020-01-03 12:44:53 -08:00
|
|
|
false,
|
|
|
|
"negative amount",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
if tc.expectPass {
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().True(tc.coin.IsValid(), tc.msg)
|
2020-01-03 12:44:53 -08:00
|
|
|
} else {
|
2020-09-29 04:29:10 -07:00
|
|
|
s.Require().False(tc.coin.IsValid(), tc.msg)
|
2020-01-03 12:44:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-26 13:51:22 -07:00
|
|
|
|
|
|
|
func (s *decCoinTestSuite) TestDecCoins_Empty() {
|
|
|
|
testCases := []struct {
|
|
|
|
input sdk.DecCoins
|
|
|
|
expectedResult bool
|
|
|
|
msg string
|
|
|
|
}{
|
|
|
|
{sdk.DecCoins{}, true, "No coins as expected."},
|
|
|
|
{sdk.DecCoins{sdk.DecCoin{testDenom1, sdk.NewDec(5)}}, false, "DecCoins is not empty"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
if tc.expectedResult {
|
|
|
|
s.Require().True(tc.input.Empty(), tc.msg)
|
|
|
|
} else {
|
|
|
|
s.Require().False(tc.input.Empty(), tc.msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *decCoinTestSuite) TestDecCoins_GetDenomByIndex() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
input sdk.DecCoins
|
|
|
|
index int
|
|
|
|
expectedResult string
|
|
|
|
expectedErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"No DecCoins in Slice",
|
|
|
|
sdk.DecCoins{},
|
|
|
|
0,
|
|
|
|
"",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{"When index out of bounds", sdk.DecCoins{sdk.DecCoin{testDenom1, sdk.NewDec(5)}}, 2, "", true},
|
|
|
|
{"When negative index", sdk.DecCoins{sdk.DecCoin{testDenom1, sdk.NewDec(5)}}, -1, "", true},
|
|
|
|
{
|
|
|
|
"Appropriate index case",
|
|
|
|
sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(5)},
|
|
|
|
sdk.DecCoin{testDenom2, sdk.NewDec(57)},
|
|
|
|
}, 1, testDenom2, false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
s.T().Run(tc.name, func(t *testing.T) {
|
|
|
|
if tc.expectedErr {
|
|
|
|
s.Require().Panics(func() { tc.input.GetDenomByIndex(tc.index) }, "Test should have panicked")
|
|
|
|
} else {
|
|
|
|
res := tc.input.GetDenomByIndex(tc.index)
|
|
|
|
s.Require().Equal(tc.expectedResult, res, "Unexpected result for test case #%d, expected output: %s, input: %v", i, tc.expectedResult, tc.input)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *decCoinTestSuite) TestDecCoins_IsAllPositive() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
input sdk.DecCoins
|
|
|
|
expectedResult bool
|
|
|
|
}{
|
|
|
|
{"No Coins", sdk.DecCoins{}, false},
|
|
|
|
|
|
|
|
{"One Coin - Zero value", sdk.DecCoins{sdk.DecCoin{testDenom1, sdk.NewDec(0)}}, false},
|
|
|
|
|
2021-09-14 07:43:12 -07:00
|
|
|
{"One Coin - Positive value", sdk.DecCoins{sdk.DecCoin{testDenom1, sdk.NewDec(5)}}, true},
|
2021-07-26 13:51:22 -07:00
|
|
|
|
|
|
|
{"One Coin - Negative value", sdk.DecCoins{sdk.DecCoin{testDenom1, sdk.NewDec(-15)}}, false},
|
|
|
|
|
|
|
|
{"Multiple Coins - All positive value", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(51)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(123)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(50)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(92233720)},
|
|
|
|
}, true},
|
|
|
|
|
|
|
|
{"Multiple Coins - Some negative value", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(51)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(-123)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(0)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(92233720)},
|
|
|
|
}, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
s.T().Run(tc.name, func(t *testing.T) {
|
|
|
|
if tc.expectedResult {
|
|
|
|
s.Require().True(tc.input.IsAllPositive(), "Test case #%d: %s", i, tc.name)
|
|
|
|
} else {
|
|
|
|
s.Require().False(tc.input.IsAllPositive(), "Test case #%d: %s", i, tc.name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *decCoinTestSuite) TestDecCoin_IsLT() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
coin sdk.DecCoin
|
|
|
|
otherCoin sdk.DecCoin
|
|
|
|
expectedResult bool
|
|
|
|
expectedPanic bool
|
|
|
|
}{
|
|
|
|
|
|
|
|
{"Same Denom - Less than other coin", sdk.DecCoin{testDenom1, sdk.NewDec(3)}, sdk.DecCoin{testDenom1, sdk.NewDec(19)}, true, false},
|
|
|
|
|
|
|
|
{"Same Denom - Greater than other coin", sdk.DecCoin{testDenom1, sdk.NewDec(343340)}, sdk.DecCoin{testDenom1, sdk.NewDec(14)}, false, false},
|
|
|
|
|
|
|
|
{"Same Denom - Same as other coin", sdk.DecCoin{testDenom1, sdk.NewDec(20)}, sdk.DecCoin{testDenom1, sdk.NewDec(20)}, false, false},
|
|
|
|
|
|
|
|
{"Different Denom - Less than other coin", sdk.DecCoin{testDenom1, sdk.NewDec(3)}, sdk.DecCoin{testDenom2, sdk.NewDec(19)}, true, true},
|
|
|
|
|
|
|
|
{"Different Denom - Greater than other coin", sdk.DecCoin{testDenom1, sdk.NewDec(343340)}, sdk.DecCoin{testDenom2, sdk.NewDec(14)}, true, true},
|
|
|
|
|
|
|
|
{"Different Denom - Same as other coin", sdk.DecCoin{testDenom1, sdk.NewDec(20)}, sdk.DecCoin{testDenom2, sdk.NewDec(20)}, true, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
s.T().Run(tc.name, func(t *testing.T) {
|
|
|
|
if tc.expectedPanic {
|
|
|
|
s.Require().Panics(func() { tc.coin.IsLT(tc.otherCoin) }, "Test case #%d: %s", i, tc.name)
|
|
|
|
} else {
|
|
|
|
res := tc.coin.IsLT(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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *decCoinTestSuite) TestDecCoin_IsGTE() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
coin sdk.DecCoin
|
|
|
|
otherCoin sdk.DecCoin
|
|
|
|
expectedResult bool
|
|
|
|
expectedPanic bool
|
|
|
|
}{
|
|
|
|
|
|
|
|
{"Same Denom - Less than other coin", sdk.DecCoin{testDenom1, sdk.NewDec(3)}, sdk.DecCoin{testDenom1, sdk.NewDec(19)}, false, false},
|
|
|
|
|
|
|
|
{"Same Denom - Greater than other coin", sdk.DecCoin{testDenom1, sdk.NewDec(343340)}, sdk.DecCoin{testDenom1, sdk.NewDec(14)}, true, false},
|
|
|
|
|
|
|
|
{"Same Denom - Same as other coin", sdk.DecCoin{testDenom1, sdk.NewDec(20)}, sdk.DecCoin{testDenom1, sdk.NewDec(20)}, true, false},
|
|
|
|
|
|
|
|
{"Different Denom - Less than other coin", sdk.DecCoin{testDenom1, sdk.NewDec(3)}, sdk.DecCoin{testDenom2, sdk.NewDec(19)}, true, true},
|
|
|
|
|
|
|
|
{"Different Denom - Greater than other coin", sdk.DecCoin{testDenom1, sdk.NewDec(343340)}, sdk.DecCoin{testDenom2, sdk.NewDec(14)}, true, true},
|
|
|
|
|
|
|
|
{"Different Denom - Same as other coin", sdk.DecCoin{testDenom1, sdk.NewDec(20)}, sdk.DecCoin{testDenom2, sdk.NewDec(20)}, true, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
s.T().Run(tc.name, func(t *testing.T) {
|
|
|
|
if tc.expectedPanic {
|
|
|
|
s.Require().Panics(func() { tc.coin.IsGTE(tc.otherCoin) }, "Test case #%d: %s", i, tc.name)
|
|
|
|
} else {
|
|
|
|
res := tc.coin.IsGTE(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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *decCoinTestSuite) TestDecCoins_IsZero() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
coins sdk.DecCoins
|
|
|
|
expectedResult bool
|
|
|
|
}{
|
|
|
|
{"No Coins", sdk.DecCoins{}, true},
|
|
|
|
|
|
|
|
{"One Coin - Zero value", sdk.DecCoins{sdk.DecCoin{testDenom1, sdk.NewDec(0)}}, true},
|
|
|
|
|
2021-09-14 07:43:12 -07:00
|
|
|
{"One Coin - Positive value", sdk.DecCoins{sdk.DecCoin{testDenom1, sdk.NewDec(5)}}, false},
|
2021-07-26 13:51:22 -07:00
|
|
|
|
|
|
|
{"Multiple Coins - All zero value", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(0)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(0)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(0)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(0)},
|
|
|
|
}, true},
|
|
|
|
|
|
|
|
{"Multiple Coins - Some positive value", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(0)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(0)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(0)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(92233720)},
|
|
|
|
}, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
s.T().Run(tc.name, func(t *testing.T) {
|
|
|
|
if tc.expectedResult {
|
|
|
|
s.Require().True(tc.coins.IsZero(), "Test case #%d: %s", i, tc.name)
|
|
|
|
} else {
|
|
|
|
s.Require().False(tc.coins.IsZero(), "Test case #%d: %s", i, tc.name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *decCoinTestSuite) TestDecCoins_MulDec() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
coins sdk.DecCoins
|
|
|
|
multiplier sdk.Dec
|
|
|
|
expectedResult sdk.DecCoins
|
|
|
|
}{
|
|
|
|
{"No Coins", sdk.DecCoins{}, sdk.NewDec(1), sdk.DecCoins(nil)},
|
|
|
|
|
|
|
|
{"Multiple coins - zero multiplier", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(10)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(30)},
|
|
|
|
}, sdk.NewDec(0), sdk.DecCoins(nil)},
|
|
|
|
|
|
|
|
{"Multiple coins - positive multiplier", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(1)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(2)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(3)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(4)},
|
|
|
|
}, sdk.NewDec(2), sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(20)},
|
|
|
|
}},
|
|
|
|
|
|
|
|
{"Multiple coins - negative multiplier", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(1)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(2)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(3)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(4)},
|
|
|
|
}, sdk.NewDec(-2), sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(-20)},
|
|
|
|
}},
|
|
|
|
|
|
|
|
{"Multiple coins - Different denom", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(1)},
|
|
|
|
sdk.DecCoin{testDenom2, sdk.NewDec(2)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(3)},
|
|
|
|
sdk.DecCoin{testDenom2, sdk.NewDec(4)},
|
|
|
|
}, sdk.NewDec(2), sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(8)},
|
|
|
|
sdk.DecCoin{testDenom2, sdk.NewDec(12)},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
s.T().Run(tc.name, func(t *testing.T) {
|
|
|
|
res := tc.coins.MulDec(tc.multiplier)
|
|
|
|
s.Require().Equal(tc.expectedResult, res, "Test case #%d: %s", i, tc.name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *decCoinTestSuite) TestDecCoins_MulDecTruncate() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
coins sdk.DecCoins
|
|
|
|
multiplier sdk.Dec
|
|
|
|
expectedResult sdk.DecCoins
|
|
|
|
expectedPanic bool
|
|
|
|
}{
|
|
|
|
{"No Coins", sdk.DecCoins{}, sdk.NewDec(1), sdk.DecCoins(nil), false},
|
|
|
|
|
|
|
|
{"Multiple coins - zero multiplier", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(10, 3)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(30, 2)},
|
2022-01-12 10:26:09 -08:00
|
|
|
}, sdk.NewDec(0), sdk.DecCoins{}, false},
|
2021-07-26 13:51:22 -07:00
|
|
|
|
|
|
|
{"Multiple coins - positive multiplier", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)},
|
|
|
|
}, sdk.NewDec(1), sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(3, 0)},
|
|
|
|
}, false},
|
|
|
|
|
|
|
|
{"Multiple coins - positive multiplier", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)},
|
|
|
|
}, sdk.NewDec(-2), sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(-6, 0)},
|
|
|
|
}, false},
|
|
|
|
|
|
|
|
{"Multiple coins - Different denom", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)},
|
|
|
|
sdk.DecCoin{testDenom2, sdk.NewDecWithPrec(3333, 4)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)},
|
|
|
|
sdk.DecCoin{testDenom2, sdk.NewDecWithPrec(333, 4)},
|
|
|
|
}, sdk.NewDec(10), sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(30, 0)},
|
|
|
|
sdk.DecCoin{testDenom2, sdk.NewDecWithPrec(3666, 3)},
|
|
|
|
}, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
s.T().Run(tc.name, func(t *testing.T) {
|
|
|
|
if tc.expectedPanic {
|
|
|
|
s.Require().Panics(func() { tc.coins.MulDecTruncate(tc.multiplier) }, "Test case #%d: %s", i, tc.name)
|
|
|
|
} else {
|
|
|
|
res := tc.coins.MulDecTruncate(tc.multiplier)
|
|
|
|
s.Require().Equal(tc.expectedResult, res, "Test case #%d: %s", i, tc.name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *decCoinTestSuite) TestDecCoins_QuoDec() {
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
coins sdk.DecCoins
|
|
|
|
input sdk.Dec
|
|
|
|
expectedResult sdk.DecCoins
|
|
|
|
panics bool
|
|
|
|
}{
|
|
|
|
{"No Coins", sdk.DecCoins{}, sdk.NewDec(1), sdk.DecCoins(nil), false},
|
|
|
|
|
|
|
|
{"Multiple coins - zero input", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(10)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(30)},
|
|
|
|
}, sdk.NewDec(0), sdk.DecCoins(nil), true},
|
|
|
|
|
|
|
|
{"Multiple coins - positive input", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(3)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(4)},
|
|
|
|
}, sdk.NewDec(2), sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(35, 1)},
|
|
|
|
}, false},
|
|
|
|
|
|
|
|
{"Multiple coins - negative input", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(3)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(4)},
|
|
|
|
}, sdk.NewDec(-2), sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(-35, 1)},
|
|
|
|
}, false},
|
|
|
|
|
|
|
|
{"Multiple coins - Different input", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(1)},
|
|
|
|
sdk.DecCoin{testDenom2, sdk.NewDec(2)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(3)},
|
|
|
|
sdk.DecCoin{testDenom2, sdk.NewDec(4)},
|
|
|
|
}, sdk.NewDec(2), sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(2)},
|
|
|
|
sdk.DecCoin{testDenom2, sdk.NewDec(3)},
|
|
|
|
}, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
s.T().Run(tc.name, func(t *testing.T) {
|
|
|
|
if tc.panics {
|
|
|
|
s.Require().Panics(func() { tc.coins.QuoDec(tc.input) }, "Test case #%d: %s", i, tc.name)
|
|
|
|
} else {
|
|
|
|
res := tc.coins.QuoDec(tc.input)
|
|
|
|
s.Require().Equal(tc.expectedResult, res, "Test case #%d: %s", i, tc.name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *decCoinTestSuite) TestDecCoin_IsEqual() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
coin sdk.DecCoin
|
|
|
|
otherCoin sdk.DecCoin
|
|
|
|
expectedResult bool
|
|
|
|
expectedPanic bool
|
|
|
|
}{
|
|
|
|
|
|
|
|
{"Different Denom Same Amount", sdk.DecCoin{testDenom1, sdk.NewDec(20)},
|
|
|
|
sdk.DecCoin{testDenom2, sdk.NewDec(20)},
|
|
|
|
false, true},
|
|
|
|
|
|
|
|
{"Different Denom Different Amount", sdk.DecCoin{testDenom1, sdk.NewDec(20)},
|
|
|
|
sdk.DecCoin{testDenom2, sdk.NewDec(10)},
|
|
|
|
false, true},
|
|
|
|
|
|
|
|
{"Same Denom Different Amount", sdk.DecCoin{testDenom1, sdk.NewDec(20)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(10)},
|
|
|
|
false, false},
|
|
|
|
|
|
|
|
{"Same Denom Same Amount", sdk.DecCoin{testDenom1, sdk.NewDec(20)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(20)},
|
|
|
|
true, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *decCoinTestSuite) TestDecCoins_IsEqual() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
coinsA sdk.DecCoins
|
|
|
|
coinsB sdk.DecCoins
|
|
|
|
expectedResult bool
|
|
|
|
expectedPanic bool
|
|
|
|
}{
|
|
|
|
{"Different length sets", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(3)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(4)},
|
|
|
|
}, sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(35)},
|
|
|
|
}, false, false},
|
|
|
|
|
|
|
|
{"Same length - different denoms", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(3)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(4)},
|
|
|
|
}, sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom2, sdk.NewDec(3)},
|
|
|
|
sdk.DecCoin{testDenom2, sdk.NewDec(4)},
|
|
|
|
}, false, true},
|
|
|
|
|
|
|
|
{"Same length - different amounts", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(3)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(4)},
|
|
|
|
}, sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(41)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(343)},
|
|
|
|
}, false, false},
|
|
|
|
|
|
|
|
{"Same length - same amounts", sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(33)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(344)},
|
|
|
|
}, sdk.DecCoins{
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(33)},
|
|
|
|
sdk.DecCoin{testDenom1, sdk.NewDec(344)},
|
|
|
|
}, true, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *decCoinTestSuite) TestDecCoin_Validate() {
|
|
|
|
var empty sdk.DecCoin
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
input sdk.DecCoin
|
|
|
|
expectedPass bool
|
|
|
|
}{
|
2021-09-14 07:43:12 -07:00
|
|
|
{"Uninitialized deccoin", empty, false},
|
2021-07-26 13:51:22 -07:00
|
|
|
|
|
|
|
{"Invalid denom string", sdk.DecCoin{"(){9**&})", sdk.NewDec(33)}, false},
|
|
|
|
|
|
|
|
{"Negative coin amount", sdk.DecCoin{testDenom1, sdk.NewDec(-33)}, false},
|
|
|
|
|
|
|
|
{"Valid coin", sdk.DecCoin{testDenom1, sdk.NewDec(33)}, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
s.T().Run(tc.name, func(t *testing.T) {
|
|
|
|
err := tc.input.Validate()
|
|
|
|
if tc.expectedPass {
|
|
|
|
s.Require().NoError(err, "unexpected result for test case #%d %s, input: %v", i, tc.name, tc.input)
|
|
|
|
} else {
|
|
|
|
s.Require().Error(err, "unexpected result for test case #%d %s, input: %v", i, tc.name, tc.input)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *decCoinTestSuite) TestDecCoin_ParseDecCoin() {
|
|
|
|
var empty sdk.DecCoin
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
input string
|
|
|
|
expectedResult sdk.DecCoin
|
|
|
|
expectedErr bool
|
|
|
|
}{
|
|
|
|
{"Empty input", "", empty, true},
|
|
|
|
|
|
|
|
{"Bad input", "✨🌟⭐", empty, true},
|
|
|
|
|
|
|
|
{"Invalid decimal coin", "9.3.0stake", empty, true},
|
|
|
|
|
|
|
|
{"Precision over limit", "9.11111111111111111111stake", empty, true},
|
|
|
|
|
2021-09-14 07:43:12 -07:00
|
|
|
{"Valid upper case denom", "9.3STAKE", sdk.DecCoin{"STAKE", sdk.NewDecWithPrec(93, 1)}, false},
|
2021-07-26 13:51:22 -07:00
|
|
|
|
2021-09-14 07:43:12 -07:00
|
|
|
{"Valid input - amount and denom separated by space", "9.3 stake", sdk.DecCoin{"stake", sdk.NewDecWithPrec(93, 1)}, false},
|
2021-07-26 13:51:22 -07:00
|
|
|
|
|
|
|
{"Valid input - amount and denom concatenated", "9.3stake", sdk.DecCoin{"stake", sdk.NewDecWithPrec(93, 1)}, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
s.T().Run(tc.name, func(t *testing.T) {
|
|
|
|
res, err := sdk.ParseDecCoin(tc.input)
|
|
|
|
if tc.expectedErr {
|
|
|
|
s.Require().Error(err, "expected error for test case #%d %s, input: %v", i, tc.name, tc.input)
|
|
|
|
} else {
|
|
|
|
s.Require().NoError(err, "unexpected error for test case #%d %s, input: %v", i, tc.name, tc.input)
|
|
|
|
s.Require().Equal(tc.expectedResult, res, "unexpected result for test case #%d %s, input: %v", i, tc.name, tc.input)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|