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(types/coin): add unit tests for the coins #20253

Merged
Merged
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
103 changes: 103 additions & 0 deletions types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,21 @@ func TestCoinsAddCoalescesDuplicateDenominations(t *testing.T) {
}
}

func TestCoinsAddUnorderedDenominations(t *testing.T) {
A := sdk.Coins{
{"aaa", math.NewInt(2)},
{"bbb", math.NewInt(3)},
}
B := sdk.Coins{
{"bbb", math.NewInt(3)},
{"aaa", math.NewInt(2)},
}

require.Panics(t, func() {
A.Add(B...)
})
}

func (s *coinTestSuite) TestSubCoins() {
cA0M0 := sdk.Coins{s.ca0, s.cm0}
cA0M1 := sdk.Coins{s.ca0, s.cm1}
Expand Down Expand Up @@ -930,6 +945,13 @@ func (s *coinTestSuite) TestMinMax() {
sdk.Coins{{testDenom1, one}, {testDenom2, one}},
sdk.Coins{{testDenom1, two}, {testDenom2, two}},
},
{
"one-one",
sdk.Coins{{testDenom2, one}},
sdk.Coins{{testDenom1, one}},
sdk.Coins{},
sdk.Coins{{testDenom1, one}, {testDenom2, one}},
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -1385,3 +1407,84 @@ func (s *coinTestSuite) TestCoinAminoEncoding() {
s.Require().Equal(bz1, bz3)
s.Require().Equal(bz2[1:], bz3)
}

func (s *coinTestSuite) TestGetDenomByIndex() {
twoAtom := sdk.NewInt64Coin("atom", 2)
threeEth := sdk.NewInt64Coin("eth", 3)

coins := sdk.Coins{twoAtom, threeEth}

actual := coins.GetDenomByIndex(0)
s.Require().Equal("atom", actual)
actual = coins.GetDenomByIndex(1)
s.Require().Equal("eth", actual)
}

func (s *coinTestSuite) TestIsAllPositive() {
negative := sdk.Coin{
Denom: "atom",
Amount: math.NewInt(-2),
}

cases := map[string]struct {
coins sdk.Coins
expected bool
}{
"Coins with zero length": {
coins: sdk.Coins{},
expected: false,
},
"Coins with negative value": {
coins: sdk.Coins{negative, sdk.NewInt64Coin("atom", 2)},
expected: false,
},
"Coins only with positive values": {
coins: sdk.Coins{sdk.NewInt64Coin("atom", 2), sdk.NewInt64Coin("eth", 10)},
expected: true,
},
}

for name, tc := range cases {
c := tc
s.T().Run(name, func(t *testing.T) {
s.Require().Equal(c.expected, c.coins.IsAllPositive())
})
}
}

func (s *coinTestSuite) TestParseCoin() {
one := math.OneInt()

cases := []struct {
input string
valid bool // if false, we expect an error on parse
expected sdk.Coin // if valid is true, make sure this is returned
}{
{"", false, sdk.Coin{}},
{"0stake", true, sdk.Coin{Denom: "stake", Amount: math.NewInt(0)}}, // remove zero coins
{"1foo", true, sdk.Coin{Denom: "foo", Amount: one}},
{"10btc", true, sdk.Coin{Denom: "btc", Amount: math.NewInt(10)}},
{"10bar", true, sdk.Coin{Denom: "bar", Amount: math.NewInt(10)}},
{"98 bar", true, sdk.Coin{Denom: "bar", Amount: math.NewInt(98)}},
{" 55\t \t bling\n", true, sdk.Coin{Denom: "bling", Amount: math.NewInt(55)}},
{"2foo", true, sdk.Coin{Denom: "foo", Amount: math.NewInt(2)}},
{"5 mycoin,", false, sdk.Coin{}}, // no empty coins in a list
{"2 3foo", false, sdk.Coin{}}, // 3foo is invalid coin name
{"11me coin", false, sdk.Coin{}}, // no spaces in coin names
{"1.2btc", true, sdk.Coin{Denom: "btc", Amount: math.NewInt(1)}}, // amount can be decimal, will get truncated
{"5foo:bar", true, sdk.Coin{Denom: "foo:bar", Amount: math.NewInt(5)}},
{"10atom10", true, sdk.Coin{Denom: "atom10", Amount: math.NewInt(10)}},
{"200transfer/channelToA/uatom", true, sdk.Coin{Denom: "transfer/channelToA/uatom", Amount: math.NewInt(200)}},
{"50ibc/7F1D3FCF4AE79E1554D670D1AD949A9BA4E4A3C76C63093E17E446A46061A7A2", true, sdk.Coin{Denom: "ibc/7F1D3FCF4AE79E1554D670D1AD949A9BA4E4A3C76C63093E17E446A46061A7A2", Amount: math.NewInt(50)}},
{"120000000000000000000000000000000000000000000000000000000000000000000000000000btc", false, sdk.Coin{}},
}

for tcIndex, tc := range cases {
res, err := sdk.ParseCoinNormalized(tc.input)
if !tc.valid {
s.Require().Error(err, "%s: %#v. tc #%d", tc.input, res, tcIndex)
} else if s.Assert().Nil(err) {
s.Require().Equal(tc.expected, res, "coin parsing was incorrect, tc #%d", tcIndex)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s.Require().Equal(tc.expected, res, "coin parsing was incorrect, tc #%d", tcIndex)
s.Require().Equal(tc.expected, res, "coin parsing was correct, tc #%d", tcIndex)

Correct me if im wrong, we in scope err = nil right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message coin parsing was incorrect, tc <index> will be printed only when expected and res are not equal

}
}
}
Loading