cosmos-sdk/types/coin_test.go

86 lines
2.3 KiB
Go
Raw Normal View History

2016-04-01 15:19:07 -07:00
package types
import (
"testing"
2017-02-22 14:30:50 -08:00
"github.com/stretchr/testify/assert"
2017-03-28 13:32:55 -07:00
"github.com/stretchr/testify/require"
2016-04-01 15:19:07 -07:00
)
func TestCoins(t *testing.T) {
2017-03-23 17:51:50 -07:00
assert := assert.New(t)
2017-02-22 14:30:50 -08:00
//Define the coins to be used in tests
good := Coins{
2016-04-01 15:19:07 -07:00
Coin{"GAS", 1},
Coin{"MINERAL", 1},
Coin{"TREE", 1},
}
2017-02-22 14:30:50 -08:00
neg := good.Negative()
sum := good.Plus(neg)
empty := Coins{
Coin{"GOLD", 0},
2016-04-01 15:19:07 -07:00
}
2017-02-22 14:30:50 -08:00
badSort1 := Coins{
2016-04-01 15:19:07 -07:00
Coin{"TREE", 1},
Coin{"GAS", 1},
Coin{"MINERAL", 1},
}
2017-02-22 14:30:50 -08:00
badSort2 := Coins{ // both are after the first one, but the second and third are in the wrong order
2017-01-31 03:24:49 -08:00
Coin{"GAS", 1},
Coin{"TREE", 1},
Coin{"MINERAL", 1},
}
2017-02-22 14:30:50 -08:00
badAmt := Coins{
2016-04-01 15:19:07 -07:00
Coin{"GAS", 1},
Coin{"TREE", 0},
Coin{"MINERAL", 1},
}
2017-02-22 14:30:50 -08:00
dup := Coins{
2016-04-01 15:19:07 -07:00
Coin{"GAS", 1},
Coin{"GAS", 1},
Coin{"MINERAL", 1},
}
2017-03-23 17:51:50 -07:00
assert.True(good.IsValid(), "Coins are valid")
2017-04-17 16:53:06 -07:00
assert.True(good.IsPositive(), "Expected coins to be positive: %v", good)
assert.True(good.IsGTE(empty), "Expected %v to be >= %v", good, empty)
assert.False(neg.IsPositive(), "Expected neg coins to not be positive: %v", neg)
2017-03-23 17:51:50 -07:00
assert.Zero(len(sum), "Expected 0 coins")
assert.False(badSort1.IsValid(), "Coins are not sorted")
assert.False(badSort2.IsValid(), "Coins are not sorted")
assert.False(badAmt.IsValid(), "Coins cannot include 0 amounts")
assert.False(dup.IsValid(), "Duplicate coin")
2017-02-22 14:30:50 -08:00
2016-04-01 15:19:07 -07:00
}
2017-03-28 13:32:55 -07:00
//Test the parse coin and parse coins functionality
func TestParse(t *testing.T) {
assert, require := assert.New(t), require.New(t)
makeCoin := func(str string) Coin {
coin, err := ParseCoin(str)
require.Nil(err)
return coin
}
makeCoins := func(str string) Coins {
coin, err := ParseCoins(str)
require.Nil(err)
return coin
}
//testing ParseCoin Function
assert.Equal(Coin{}, makeCoin(""), "ParseCoin makes bad empty coin")
assert.Equal(Coin{"fooCoin", 1}, makeCoin("1fooCoin"), "ParseCoin makes bad coins")
assert.Equal(Coin{"barCoin", 10}, makeCoin("10 barCoin"), "ParseCoin makes bad coins")
//testing ParseCoins Function
assert.True(Coins{{"fooCoin", 1}}.IsEqual(makeCoins("1fooCoin")),
"ParseCoins doesn't parse a single coin")
assert.True(Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99barCoin,1fooCoin")),
"ParseCoins doesn't properly parse two coins")
assert.True(Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99 barCoin, 1 fooCoin")),
"ParseCoins doesn't properly parse two coins which use spaces")
}