Skip to content

Commit

Permalink
add new checkers helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Dec 27, 2023
1 parent 8977012 commit cd484a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
16 changes: 16 additions & 0 deletions util/checkers/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,19 @@ func DecInZeroOne(a sdk.Dec, name string, oneInclusive bool) error {
}
return nil
}

// DecNotNegative checks if a is defined and a >= 0
func DecNotNegative(a sdk.Dec, paramName string, errs []error) []error {
if a.IsNil() || a.IsNegative() {
return append(errs, fmt.Errorf("%s can't be negative", paramName))
}
return errs
}

// DecPositive checks if a is defined and a > 0
func DecPositive(a sdk.Dec, paramName string, errs []error) []error {
if a.IsNil() || !a.IsPositive() {
return append(errs, fmt.Errorf("%s must be positive", paramName))
}
return errs
}
9 changes: 9 additions & 0 deletions util/checkers/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package checkers
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/umee-network/umee/v6/tests/tsdk"
)
Expand Down Expand Up @@ -81,3 +82,11 @@ func TestDecInZeroOne(t *testing.T) {
assert.Error(t, DecInZeroOne(tsdk.DecF(213812), "", true))
assert.Error(t, DecInZeroOne(tsdk.DecF(-1), "", true))
}

func TestDecNotNegative(t *testing.T) {
assert.NotNil(t, DecNotNegative(tsdk.DecF(-1), "", nil))
assert.NotNil(t, DecNotNegative(sdk.Dec{}, "", nil))

assert.Nil(t, DecNotNegative(sdk.ZeroDec(), "", nil))
assert.Nil(t, DecNotNegative(tsdk.DecF(5), "", nil))
}

0 comments on commit cd484a3

Please sign in to comment.