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

R4R: Added IsValid to Coin #4558

Merged
merged 8 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions .pending/improvements/sdk/4556-Added-IsValid-f
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4556 Added IsValid function to Coin
9 changes: 9 additions & 0 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ func (coin Coin) String() string {
return fmt.Sprintf("%v%v", coin.Amount, coin.Denom)
}

// IsValid asserts that the Coin has a positive amount and the Denom does not contain
// upper case characters and has a length of 3 ~ 16 characters.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would simply update this godoc to state:

// IsValid asserts that the Coin has a positive amount and the Denom is valid.

func (coin Coin) IsValid() bool {
if err := validateDenom(coin.Denom); err != nil {
return false
}
return coin.IsPositive()
}

// IsZero returns if this represents no money
func (coin Coin) IsZero() bool {
return coin.Amount.IsZero()
Expand Down
20 changes: 20 additions & 0 deletions types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ func TestIsEqualCoin(t *testing.T) {
}
}

func TestCoinIsValid(t *testing.T) {
cases := []struct {
coin Coin
expectPass bool
}{
{Coin{testDenom1, NewInt(-1)}, false},
{Coin{testDenom1, NewInt(0)}, false},
{Coin{testDenom1, NewInt(1)}, true},
{Coin{"Atom", NewInt(1)}, false},
{Coin{"a", NewInt(1)}, false},
{Coin{"a very long coin denom", NewInt(1)}, false},
{Coin{"atOm", NewInt(1)}, false},
{Coin{" ", NewInt(1)}, false},
}

for i, tc := range cases {
require.Equal(t, tc.expectPass, tc.coin.IsValid(), "unexpected result for IsValid, tc #%d", i)
}
}

func TestAddCoin(t *testing.T) {
cases := []struct {
inputOne Coin
Expand Down