Skip to content

Commit

Permalink
utils: BigInt now uses big.Int as embedded struct. Validation unittest.
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed Feb 28, 2019
1 parent c939931 commit 5a4a67b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
15 changes: 12 additions & 3 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,21 @@ func AskPassword(question string) (password string, err error) {
}

// BigInt is used by swagger as a big.Int type with Validate() function
type BigInt big.Int
type BigInt struct {
*big.Int
}

// Validate only checks that the number is >=0
// Validate is an exported function that swagger uses.
// However, the implementation does not need 'formats', so it is broken
// out into validate().
func (b *BigInt) Validate(formats strfmt.Registry) error {
return b.validate()
}

// validate checks that the number is >=0
func (b *BigInt) validate() error {
var zero big.Int
var convertedCustomBigInt = big.Int(*b)
var convertedCustomBigInt = b

if convertedCustomBigInt.Cmp(&zero) != 1 {
return errors.New("swagger deserialization: Balance Validation failed")
Expand Down
31 changes: 31 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utils

import (
"errors"
"math"
"math/big"
"reflect"
"testing"
)

func TestValidate(t *testing.T) {
var amount = BigInt{&big.Int{}}
var tests = []struct {
input int64
expected error
}{
{math.MinInt64, errors.New("the error message is not important")},
{-1, errors.New("the error message is not important")},
{0, errors.New("the error message is not important")},
{1, nil},
{math.MaxInt64, nil},
}

for _, test := range tests {
amount.SetInt64(test.input)
err := amount.validate()
if reflect.TypeOf(err) != reflect.TypeOf(test.expected) {
t.Errorf("Test Failed: %v inputted, %v expected, %#v received", test.input, test.expected, err)
}
}
}

0 comments on commit 5a4a67b

Please sign in to comment.