Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miladz68 committed Feb 10, 2023
1 parent ac021ca commit a026e60
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
7 changes: 4 additions & 3 deletions x/asset/ft/types/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ var (

const (
denomSeparator = "-"
maxPrecision = 20
// MaxPrecision used when issuing a token.
MaxPrecision = 20
)

func init() {
Expand Down Expand Up @@ -95,8 +96,8 @@ func ValidateSubunit(subunit string) error {

// ValidatePrecision checks the provided precision is valid.
func ValidatePrecision(precision uint32) error {
if precision == 0 || precision > maxPrecision {
return sdkerrors.Wrapf(ErrInvalidInput, "precision must be between 1 and %d", maxPrecision)
if precision == 0 || precision > MaxPrecision {
return sdkerrors.Wrapf(ErrInvalidInput, "precision must be between 1 and %d", MaxPrecision)
}
return nil
}
Expand Down
27 changes: 27 additions & 0 deletions x/asset/ft/types/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ func TestBuildDenom(t *testing.T) {
require.Equal(t, "abc-devcore172rc5sz2uclpsy3vvx3y79ah5dk450z5ruq2r5", denom)
}

func TestValidatePrecision(t *testing.T) {
testCases := []struct {
precision uint32
expectError bool
}{
{precision: 1},
{precision: 3},
{precision: 10},
{precision: types.MaxPrecision},
{precision: 0, expectError: true},
{precision: types.MaxPrecision + 1, expectError: true},
{precision: 100_000, expectError: true},
}

for _, tc := range testCases {
tc := tc
t.Run(fmt.Sprint(tc), func(t *testing.T) {
err := types.ValidatePrecision(tc.precision)
if tc.expectError {
assert.ErrorIs(t, err, types.ErrInvalidInput)
} else {
assert.NoError(t, err)
}
})
}
}

func TestValidateSubunit(t *testing.T) {
requireT := require.New(t)
unacceptableSubunits := []string{
Expand Down

0 comments on commit a026e60

Please sign in to comment.