review changes

int

int
This commit is contained in:
rigelrozanski
2017-04-13 23:31:52 -04:00
committed by Rigel Rozanski
parent 468b35f28d
commit d19f52c893
10 changed files with 365 additions and 400 deletions
+4 -4
View File
@@ -92,11 +92,11 @@ func cmdSendTx(c *cli.Context) error {
}
//parse the fee and amounts into coin types
feeCoin, err := ParseCoin(fee)
feeCoin, err := types.ParseCoin(fee)
if err != nil {
return err
}
amountCoins, err := ParseCoins(amount)
amountCoins, err := types.ParseCoins(amount)
if err != nil {
return err
}
@@ -153,11 +153,11 @@ func AppTx(c *cli.Context, name string, data []byte) error {
}
//parse the fee and amounts into coin types
feeCoin, err := ParseCoin(fee)
feeCoin, err := types.ParseCoin(fee)
if err != nil {
return err
}
amountCoins, err := ParseCoins(amount)
amountCoins, err := types.ParseCoins(amount)
if err != nil {
return err
}
-41
View File
@@ -4,9 +4,6 @@ import (
"encoding/hex"
"errors"
"os"
"regexp"
"strconv"
"strings"
"github.com/urfave/cli"
@@ -50,44 +47,6 @@ func StripHex(s string) string {
return s
}
//regex codes for extracting coins from CLI input
var reDenom = regexp.MustCompile("([^\\d\\W]+)")
var reAmt = regexp.MustCompile("(\\d+)")
func ParseCoin(str string) (types.Coin, error) {
var coin types.Coin
if len(str) > 0 {
amt, err := strconv.Atoi(reAmt.FindString(str))
if err != nil {
return coin, err
}
denom := reDenom.FindString(str)
coin = types.Coin{denom, int64(amt)}
}
return coin, nil
}
func ParseCoins(str string) (types.Coins, error) {
split := strings.Split(str, ",")
var coins []types.Coin
for _, el := range split {
if len(el) > 0 {
coin, err := ParseCoin(el)
if err != nil {
return coins, err
}
coins = append(coins, coin)
}
}
return coins, nil
}
func Query(tmAddr string, key []byte) (*abci.ResponseQuery, error) {
uriClient := client.NewURIClient(tmAddr)
tmResult := new(ctypes.TMResult)
-36
View File
@@ -5,7 +5,6 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/tendermint/basecoin/types"
)
func TestHex(t *testing.T) {
@@ -22,39 +21,4 @@ func TestHex(t *testing.T) {
assert.False(isHex(str), "isHex shouldn't identify non-hex string")
assert.False(isHex(strWPrefix), "isHex shouldn't identify non-hex string with 0x prefix")
assert.True(StripHex(hexWPrefix) == hexNoPrefix, "StripHex doesn't remove first two characters")
}
//Test the parse coin and parse coins functionality
func TestParse(t *testing.T) {
assert := assert.New(t)
makeCoin := func(str string) types.Coin {
coin, err := ParseCoin(str)
if err != nil {
panic(err.Error())
}
return coin
}
makeCoins := func(str string) types.Coins {
coin, err := ParseCoins(str)
if err != nil {
panic(err.Error())
}
return coin
}
//testing ParseCoin Function
assert.Equal(types.Coin{}, makeCoin(""), "parseCoin makes bad empty coin")
assert.Equal(types.Coin{"fooCoin", 1}, makeCoin("1fooCoin"), "parseCoin makes bad coins")
assert.Equal(types.Coin{"barCoin", 10}, makeCoin("10 barCoin"), "parseCoin makes bad coins")
//testing ParseCoins Function
assert.True(types.Coins{{"fooCoin", 1}}.IsEqual(makeCoins("1fooCoin")),
"parseCoins doesn't parse a single coin")
assert.True(types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99barCoin,1fooCoin")),
"parseCoins doesn't properly parse two coins")
assert.True(types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99 barCoin, 1 fooCoin")),
"parseCoins doesn't properly parse two coins which use spaces")
}