Skip to content

Commit

Permalink
feat(checkers): coin checker functions
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Mar 26, 2024
1 parent d42cf1c commit 820a6f2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
20 changes: 20 additions & 0 deletions util/checkers/coin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package checkers

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// PositiveCoins checks if all coins are valida and amount is positive.
func PositiveCoins(note string, coins ...sdk.Coin) []error {
var errs []error
for i := range coins {
if err := coins[i].Validate(); err != nil {
errs = append(errs, fmt.Errorf("%s coin[%d]: %w", note, i, err))
} else if !coins[i].Amount.IsPositive() {
errs = append(errs, fmt.Errorf("%s coin[%d] amount must be positive", note, i))
}
}
return errs
}
30 changes: 30 additions & 0 deletions util/checkers/coin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package checkers

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
)

func TestCoinPositive(t *testing.T) {
assert := assert.New(t)

c0 := sdk.NewInt64Coin("abc", 0)
cNeg := sdk.NewInt64Coin("abc", 0)
cPos := sdk.NewInt64Coin("abc", 1)

assert.Empty(PositiveCoins(""))
assert.Empty(PositiveCoins("", cPos))
assert.Empty(PositiveCoins("", cPos, cPos))

assert.Contains(errsToStr(PositiveCoins("", c0)), "coin[0]")
assert.Contains(errsToStr(PositiveCoins("", cNeg)), "coin[0]")
assert.Contains(errsToStr(PositiveCoins("", cPos, c0)), "coin[1]")
assert.Contains(errsToStr(PositiveCoins("", cPos, cNeg)), "coin[1]")
assert.NotContains(errsToStr(PositiveCoins("", cPos, cNeg)), "coin[0]")

assert.Contains(errsToStr(PositiveCoins("", cPos, c0, cNeg)), "coin[1]")
assert.Contains(errsToStr(PositiveCoins("", cPos, c0, cNeg)), "coin[2]")
assert.NotContains(errsToStr(PositiveCoins("", cPos, c0, cNeg)), "coin[0]")
}
13 changes: 13 additions & 0 deletions util/checkers/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package checkers

import (
"strings"
)

func errsToStr(errs []error) string {
strs := make([]string, len(errs))
for i := range errs {
strs[i] = errs[i].Error()
}
return strings.Join(strs, " ")
}

0 comments on commit 820a6f2

Please sign in to comment.