Add tests for types

This commit is contained in:
Adrian Brink 2018-03-20 16:04:08 +01:00
parent 75674a9ec3
commit f837c16760
No known key found for this signature in database
GPG Key ID: F61053D3FBD06353
3 changed files with 40 additions and 0 deletions

View File

@ -8,6 +8,7 @@ const (
// Staking errors reserve 300 - 399.
CodeEmptyValidator sdk.CodeType = 300
CodeInvalidUnbond sdk.CodeType = 301
CodeEmptyStake sdk.CodeType = 302
)
func ErrEmptyValidator() sdk.Error {
@ -18,6 +19,10 @@ func ErrInvalidUnbond() sdk.Error {
return newError(CodeInvalidUnbond, "")
}
func ErrEmptyStake() sdk.Error {
return newError(CodeEmptyStake, "")
}
// -----------------------------
// Helpers

View File

@ -30,6 +30,10 @@ func (msg BondMsg) Type() string {
}
func (msg BondMsg) ValidateBasic() sdk.Error {
if msg.Stake.IsZero() {
return ErrEmptyStake()
}
return nil
}

31
x/staking/types_test.go Normal file
View File

@ -0,0 +1,31 @@
package staking
import (
"testing"
"github.com/stretchr/testify/assert"
crypto "github.com/tendermint/go-crypto"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func TestBondMsgValidation(t *testing.T) {
privKey := crypto.GenPrivKeyEd25519()
cases := []struct {
valid bool
bondMsg BondMsg
}{
{true, NewBondMsg(sdk.Address{}, sdk.Coin{"mycoin", 5}, privKey.PubKey())},
{false, NewBondMsg(sdk.Address{}, sdk.Coin{"mycoin", 0}, privKey.PubKey())},
}
for i, tc := range cases {
err := tc.bondMsg.ValidateBasic()
if tc.valid {
assert.Nil(t, err, "%d: %+v", i, err)
} else {
assert.NotNil(t, err, "%d", i)
}
}
}