Make coins denoms case insensitive (#3092)
This commit is contained in:
committed by
Jack Zampolin
parent
eac7d6939d
commit
500fa2b694
+16
-2
@@ -30,6 +30,9 @@ func NewCoin(denom string, amount Int) Coin {
|
||||
if amount.LT(ZeroInt()) {
|
||||
panic(fmt.Sprintf("negative coin amount: %v\n", amount))
|
||||
}
|
||||
if strings.ToLower(denom) != denom {
|
||||
panic(fmt.Sprintf("denom cannot contain upper case characters: %s\n", denom))
|
||||
}
|
||||
|
||||
return Coin{
|
||||
Denom: denom,
|
||||
@@ -132,7 +135,8 @@ func (coins Coins) String() string {
|
||||
return out[:len(out)-1]
|
||||
}
|
||||
|
||||
// IsValid asserts the Coins are sorted and have positive amounts.
|
||||
// IsValid asserts the Coins are sorted, have positive amount,
|
||||
// and Denom does not contain upper case characters.
|
||||
func (coins Coins) IsValid() bool {
|
||||
switch len(coins) {
|
||||
case 0:
|
||||
@@ -143,6 +147,9 @@ func (coins Coins) IsValid() bool {
|
||||
lowDenom := coins[0].Denom
|
||||
|
||||
for _, coin := range coins[1:] {
|
||||
if strings.ToLower(coin.Denom) != coin.Denom {
|
||||
return false
|
||||
}
|
||||
if coin.Denom <= lowDenom {
|
||||
return false
|
||||
}
|
||||
@@ -320,6 +327,9 @@ func (coins Coins) Empty() bool {
|
||||
|
||||
// Returns the amount of a denom from coins
|
||||
func (coins Coins) AmountOf(denom string) Int {
|
||||
if strings.ToLower(denom) != denom {
|
||||
panic(fmt.Sprintf("denom cannot contain upper case characters: %s\n", denom))
|
||||
}
|
||||
switch len(coins) {
|
||||
case 0:
|
||||
return ZeroInt()
|
||||
@@ -457,7 +467,11 @@ func ParseCoin(coinStr string) (coin Coin, err error) {
|
||||
return Coin{}, fmt.Errorf("failed to parse coin amount: %s", amountStr)
|
||||
}
|
||||
|
||||
return Coin{denomStr, amount}, nil
|
||||
if denomStr != strings.ToLower(denomStr) {
|
||||
return Coin{}, fmt.Errorf("denom cannot contain upper case characters: %s", denomStr)
|
||||
}
|
||||
|
||||
return NewCoin(denomStr, amount), nil
|
||||
}
|
||||
|
||||
// ParseCoins will parse out a list of coins separated by commas.
|
||||
|
||||
+119
-111
@@ -11,10 +11,12 @@ import (
|
||||
// Coin tests
|
||||
|
||||
func TestCoin(t *testing.T) {
|
||||
require.Panics(t, func() { NewInt64Coin("A", -1) })
|
||||
require.Panics(t, func() { NewCoin("A", NewInt(-1)) })
|
||||
require.Equal(t, NewInt(5), NewInt64Coin("A", 5).Amount)
|
||||
require.Equal(t, NewInt(5), NewCoin("A", NewInt(5)).Amount)
|
||||
require.Panics(t, func() { NewInt64Coin("a", -1) })
|
||||
require.Panics(t, func() { NewCoin("a", NewInt(-1)) })
|
||||
require.Panics(t, func() { NewInt64Coin("Atom", 10) })
|
||||
require.Panics(t, func() { NewCoin("Atom", NewInt(10)) })
|
||||
require.Equal(t, NewInt(5), NewInt64Coin("a", 5).Amount)
|
||||
require.Equal(t, NewInt(5), NewCoin("a", NewInt(5)).Amount)
|
||||
}
|
||||
|
||||
func TestSameDenomAsCoin(t *testing.T) {
|
||||
@@ -23,8 +25,7 @@ func TestSameDenomAsCoin(t *testing.T) {
|
||||
inputTwo Coin
|
||||
expected bool
|
||||
}{
|
||||
{NewInt64Coin("A", 1), NewInt64Coin("A", 1), true},
|
||||
{NewInt64Coin("A", 1), NewInt64Coin("a", 1), false},
|
||||
{NewInt64Coin("a", 1), NewInt64Coin("a", 1), true},
|
||||
{NewInt64Coin("a", 1), NewInt64Coin("b", 1), false},
|
||||
{NewInt64Coin("steak", 1), NewInt64Coin("steak", 10), true},
|
||||
}
|
||||
@@ -41,8 +42,7 @@ func TestIsEqualCoin(t *testing.T) {
|
||||
inputTwo Coin
|
||||
expected bool
|
||||
}{
|
||||
{NewInt64Coin("A", 1), NewInt64Coin("A", 1), true},
|
||||
{NewInt64Coin("A", 1), NewInt64Coin("a", 1), false},
|
||||
{NewInt64Coin("a", 1), NewInt64Coin("a", 1), true},
|
||||
{NewInt64Coin("a", 1), NewInt64Coin("b", 1), false},
|
||||
{NewInt64Coin("steak", 1), NewInt64Coin("steak", 10), false},
|
||||
}
|
||||
@@ -60,9 +60,9 @@ func TestPlusCoin(t *testing.T) {
|
||||
expected Coin
|
||||
shouldPanic bool
|
||||
}{
|
||||
{NewInt64Coin("A", 1), NewInt64Coin("A", 1), NewInt64Coin("A", 2), false},
|
||||
{NewInt64Coin("A", 1), NewInt64Coin("A", 0), NewInt64Coin("A", 1), false},
|
||||
{NewInt64Coin("A", 1), NewInt64Coin("B", 1), NewInt64Coin("A", 1), true},
|
||||
{NewInt64Coin("a", 1), NewInt64Coin("a", 1), NewInt64Coin("a", 2), false},
|
||||
{NewInt64Coin("a", 1), NewInt64Coin("a", 0), NewInt64Coin("a", 1), false},
|
||||
{NewInt64Coin("a", 1), NewInt64Coin("b", 1), NewInt64Coin("a", 1), true},
|
||||
}
|
||||
|
||||
for tcIndex, tc := range cases {
|
||||
@@ -82,11 +82,11 @@ func TestMinusCoin(t *testing.T) {
|
||||
expected Coin
|
||||
shouldPanic bool
|
||||
}{
|
||||
{NewInt64Coin("A", 1), NewInt64Coin("B", 1), NewInt64Coin("A", 1), true},
|
||||
{NewInt64Coin("A", 10), NewInt64Coin("A", 1), NewInt64Coin("A", 9), false},
|
||||
{NewInt64Coin("A", 5), NewInt64Coin("A", 3), NewInt64Coin("A", 2), false},
|
||||
{NewInt64Coin("A", 5), NewInt64Coin("A", 0), NewInt64Coin("A", 5), false},
|
||||
{NewInt64Coin("A", 1), NewInt64Coin("A", 5), Coin{}, true},
|
||||
{NewInt64Coin("a", 1), NewInt64Coin("b", 1), NewInt64Coin("a", 1), true},
|
||||
{NewInt64Coin("a", 10), NewInt64Coin("a", 1), NewInt64Coin("a", 9), false},
|
||||
{NewInt64Coin("a", 5), NewInt64Coin("a", 3), NewInt64Coin("a", 2), false},
|
||||
{NewInt64Coin("a", 5), NewInt64Coin("a", 0), NewInt64Coin("a", 5), false},
|
||||
{NewInt64Coin("a", 1), NewInt64Coin("a", 5), Coin{}, true},
|
||||
}
|
||||
|
||||
for tcIndex, tc := range cases {
|
||||
@@ -102,7 +102,7 @@ func TestMinusCoin(t *testing.T) {
|
||||
inputOne Coin
|
||||
inputTwo Coin
|
||||
expected int64
|
||||
}{NewInt64Coin("A", 1), NewInt64Coin("A", 1), 0}
|
||||
}{NewInt64Coin("a", 1), NewInt64Coin("a", 1), 0}
|
||||
res := tc.inputOne.Minus(tc.inputTwo)
|
||||
require.Equal(t, tc.expected, res.Amount.Int64())
|
||||
}
|
||||
@@ -113,9 +113,9 @@ func TestIsGTECoin(t *testing.T) {
|
||||
inputTwo Coin
|
||||
expected bool
|
||||
}{
|
||||
{NewInt64Coin("A", 1), NewInt64Coin("A", 1), true},
|
||||
{NewInt64Coin("A", 2), NewInt64Coin("A", 1), true},
|
||||
{NewInt64Coin("A", 1), NewInt64Coin("B", 1), false},
|
||||
{NewInt64Coin("a", 1), NewInt64Coin("a", 1), true},
|
||||
{NewInt64Coin("a", 2), NewInt64Coin("a", 1), true},
|
||||
{NewInt64Coin("a", 1), NewInt64Coin("b", 1), false},
|
||||
}
|
||||
|
||||
for tcIndex, tc := range cases {
|
||||
@@ -130,8 +130,8 @@ func TestIsLTCoin(t *testing.T) {
|
||||
inputTwo Coin
|
||||
expected bool
|
||||
}{
|
||||
{NewInt64Coin("A", 1), NewInt64Coin("A", 1), false},
|
||||
{NewInt64Coin("A", 2), NewInt64Coin("A", 1), false},
|
||||
{NewInt64Coin("a", 1), NewInt64Coin("a", 1), false},
|
||||
{NewInt64Coin("a", 2), NewInt64Coin("a", 1), false},
|
||||
{NewInt64Coin("a", 0), NewInt64Coin("b", 1), false},
|
||||
{NewInt64Coin("a", 1), NewInt64Coin("b", 1), false},
|
||||
{NewInt64Coin("a", 1), NewInt64Coin("a", 1), false},
|
||||
@@ -145,11 +145,11 @@ func TestIsLTCoin(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCoinIsZero(t *testing.T) {
|
||||
coin := NewInt64Coin("A", 0)
|
||||
coin := NewInt64Coin("a", 0)
|
||||
res := coin.IsZero()
|
||||
require.True(t, res)
|
||||
|
||||
coin = NewInt64Coin("A", 1)
|
||||
coin = NewInt64Coin("a", 1)
|
||||
res = coin.IsZero()
|
||||
require.False(t, res)
|
||||
}
|
||||
@@ -163,10 +163,10 @@ func TestIsZeroCoins(t *testing.T) {
|
||||
expected bool
|
||||
}{
|
||||
{Coins{}, true},
|
||||
{Coins{NewInt64Coin("A", 0)}, true},
|
||||
{Coins{NewInt64Coin("A", 0), NewInt64Coin("B", 0)}, true},
|
||||
{Coins{NewInt64Coin("A", 1)}, false},
|
||||
{Coins{NewInt64Coin("A", 0), NewInt64Coin("B", 1)}, false},
|
||||
{Coins{NewInt64Coin("a", 0)}, true},
|
||||
{Coins{NewInt64Coin("a", 0), NewInt64Coin("b", 0)}, true},
|
||||
{Coins{NewInt64Coin("a", 1)}, false},
|
||||
{Coins{NewInt64Coin("a", 0), NewInt64Coin("b", 1)}, false},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
@@ -182,12 +182,12 @@ func TestEqualCoins(t *testing.T) {
|
||||
expected bool
|
||||
}{
|
||||
{Coins{}, Coins{}, true},
|
||||
{Coins{NewInt64Coin("A", 0)}, Coins{NewInt64Coin("A", 0)}, true},
|
||||
{Coins{NewInt64Coin("A", 0), NewInt64Coin("B", 1)}, Coins{NewInt64Coin("A", 0), NewInt64Coin("B", 1)}, true},
|
||||
{Coins{NewInt64Coin("A", 0)}, Coins{NewInt64Coin("B", 0)}, false},
|
||||
{Coins{NewInt64Coin("A", 0)}, Coins{NewInt64Coin("A", 1)}, false},
|
||||
{Coins{NewInt64Coin("A", 0)}, Coins{NewInt64Coin("A", 0), NewInt64Coin("B", 1)}, false},
|
||||
{Coins{NewInt64Coin("A", 0), NewInt64Coin("B", 1)}, Coins{NewInt64Coin("B", 1), NewInt64Coin("A", 0)}, true},
|
||||
{Coins{NewInt64Coin("a", 0)}, Coins{NewInt64Coin("a", 0)}, true},
|
||||
{Coins{NewInt64Coin("a", 0), NewInt64Coin("b", 1)}, Coins{NewInt64Coin("a", 0), NewInt64Coin("b", 1)}, true},
|
||||
{Coins{NewInt64Coin("a", 0)}, Coins{NewInt64Coin("b", 0)}, false},
|
||||
{Coins{NewInt64Coin("a", 0)}, Coins{NewInt64Coin("a", 1)}, false},
|
||||
{Coins{NewInt64Coin("a", 0)}, Coins{NewInt64Coin("a", 0), NewInt64Coin("b", 1)}, false},
|
||||
{Coins{NewInt64Coin("a", 0), NewInt64Coin("b", 1)}, Coins{NewInt64Coin("b", 1), NewInt64Coin("a", 0)}, true},
|
||||
}
|
||||
|
||||
for tcnum, tc := range cases {
|
||||
@@ -206,11 +206,11 @@ func TestPlusCoins(t *testing.T) {
|
||||
inputTwo Coins
|
||||
expected Coins
|
||||
}{
|
||||
{Coins{{"A", one}, {"B", one}}, Coins{{"A", one}, {"B", one}}, Coins{{"A", two}, {"B", two}}},
|
||||
{Coins{{"A", zero}, {"B", one}}, Coins{{"A", zero}, {"B", zero}}, Coins{{"B", one}}},
|
||||
{Coins{{"A", two}}, Coins{{"B", zero}}, Coins{{"A", two}}},
|
||||
{Coins{{"A", one}}, Coins{{"A", one}, {"B", two}}, Coins{{"A", two}, {"B", two}}},
|
||||
{Coins{{"A", zero}, {"B", zero}}, Coins{{"A", zero}, {"B", zero}}, Coins(nil)},
|
||||
{Coins{{"a", one}, {"b", one}}, Coins{{"a", one}, {"b", one}}, Coins{{"a", two}, {"b", two}}},
|
||||
{Coins{{"a", zero}, {"b", one}}, Coins{{"a", zero}, {"b", zero}}, Coins{{"b", one}}},
|
||||
{Coins{{"a", two}}, Coins{{"b", zero}}, Coins{{"a", two}}},
|
||||
{Coins{{"a", one}}, Coins{{"a", one}, {"b", two}}, Coins{{"a", two}, {"b", two}}},
|
||||
{Coins{{"a", zero}, {"b", zero}}, Coins{{"a", zero}, {"b", zero}}, Coins(nil)},
|
||||
}
|
||||
|
||||
for tcIndex, tc := range cases {
|
||||
@@ -231,11 +231,11 @@ func TestMinusCoins(t *testing.T) {
|
||||
expected Coins
|
||||
shouldPanic bool
|
||||
}{
|
||||
{Coins{{"A", two}}, Coins{{"A", one}, {"B", two}}, Coins{{"A", one}, {"B", two}}, true},
|
||||
{Coins{{"A", two}}, Coins{{"B", zero}}, Coins{{"A", two}}, false},
|
||||
{Coins{{"A", one}}, Coins{{"B", zero}}, Coins{{"A", one}}, false},
|
||||
{Coins{{"A", one}, {"B", one}}, Coins{{"A", one}}, Coins{{"B", one}}, false},
|
||||
{Coins{{"A", one}, {"B", one}}, Coins{{"A", two}}, Coins{}, true},
|
||||
{Coins{{"a", two}}, Coins{{"a", one}, {"b", two}}, Coins{{"a", one}, {"b", two}}, true},
|
||||
{Coins{{"a", two}}, Coins{{"b", zero}}, Coins{{"a", two}}, false},
|
||||
{Coins{{"a", one}}, Coins{{"b", zero}}, Coins{{"a", one}}, false},
|
||||
{Coins{{"a", one}, {"b", one}}, Coins{{"a", one}}, Coins{{"b", one}}, false},
|
||||
{Coins{{"a", one}, {"b", one}}, Coins{{"a", two}}, Coins{}, true},
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
@@ -251,38 +251,44 @@ func TestMinusCoins(t *testing.T) {
|
||||
|
||||
func TestCoins(t *testing.T) {
|
||||
good := Coins{
|
||||
{"GAS", NewInt(1)},
|
||||
{"MINERAL", NewInt(1)},
|
||||
{"gas", NewInt(1)},
|
||||
{"mineral", NewInt(1)},
|
||||
{"tree", NewInt(1)},
|
||||
}
|
||||
mixedCase := Coins{
|
||||
{"gAs", NewInt(1)},
|
||||
{"MineraL", NewInt(1)},
|
||||
{"TREE", NewInt(1)},
|
||||
}
|
||||
empty := Coins{
|
||||
{"GOLD", NewInt(0)},
|
||||
{"gold", NewInt(0)},
|
||||
}
|
||||
null := Coins{}
|
||||
badSort1 := Coins{
|
||||
{"TREE", NewInt(1)},
|
||||
{"GAS", NewInt(1)},
|
||||
{"MINERAL", NewInt(1)},
|
||||
{"tree", NewInt(1)},
|
||||
{"gas", NewInt(1)},
|
||||
{"mineral", NewInt(1)},
|
||||
}
|
||||
|
||||
// both are after the first one, but the second and third are in the wrong order
|
||||
badSort2 := Coins{
|
||||
{"GAS", NewInt(1)},
|
||||
{"TREE", NewInt(1)},
|
||||
{"MINERAL", NewInt(1)},
|
||||
{"gas", NewInt(1)},
|
||||
{"tree", NewInt(1)},
|
||||
{"mineral", NewInt(1)},
|
||||
}
|
||||
badAmt := Coins{
|
||||
{"GAS", NewInt(1)},
|
||||
{"TREE", NewInt(0)},
|
||||
{"MINERAL", NewInt(1)},
|
||||
{"gas", NewInt(1)},
|
||||
{"tree", NewInt(0)},
|
||||
{"mineral", NewInt(1)},
|
||||
}
|
||||
dup := Coins{
|
||||
{"GAS", NewInt(1)},
|
||||
{"GAS", NewInt(1)},
|
||||
{"MINERAL", NewInt(1)},
|
||||
{"gas", NewInt(1)},
|
||||
{"gas", NewInt(1)},
|
||||
{"mineral", NewInt(1)},
|
||||
}
|
||||
|
||||
assert.True(t, good.IsValid(), "Coins are valid")
|
||||
assert.False(t, mixedCase.IsValid(), "Coins denoms contain upper case characters")
|
||||
assert.True(t, good.IsPositive(), "Expected coins to be positive: %v", good)
|
||||
assert.False(t, null.IsPositive(), "Expected coins to not be positive: %v", null)
|
||||
assert.True(t, good.IsAllGTE(empty), "Expected %v to be >= %v", good, empty)
|
||||
@@ -299,11 +305,11 @@ func TestCoinsGT(t *testing.T) {
|
||||
two := NewInt(2)
|
||||
|
||||
assert.False(t, Coins{}.IsAllGT(Coins{}))
|
||||
assert.True(t, Coins{{"A", one}}.IsAllGT(Coins{}))
|
||||
assert.False(t, Coins{{"A", one}}.IsAllGT(Coins{{"A", one}}))
|
||||
assert.False(t, Coins{{"A", one}}.IsAllGT(Coins{{"B", one}}))
|
||||
assert.True(t, Coins{{"A", one}, {"B", one}}.IsAllGT(Coins{{"B", one}}))
|
||||
assert.False(t, Coins{{"A", one}, {"B", one}}.IsAllGT(Coins{{"B", two}}))
|
||||
assert.True(t, Coins{{"a", one}}.IsAllGT(Coins{}))
|
||||
assert.False(t, Coins{{"a", one}}.IsAllGT(Coins{{"a", one}}))
|
||||
assert.False(t, Coins{{"a", one}}.IsAllGT(Coins{{"b", one}}))
|
||||
assert.True(t, Coins{{"a", one}, {"b", one}}.IsAllGT(Coins{{"b", one}}))
|
||||
assert.False(t, Coins{{"a", one}, {"b", one}}.IsAllGT(Coins{{"b", two}}))
|
||||
}
|
||||
|
||||
func TestCoinsGTE(t *testing.T) {
|
||||
@@ -311,11 +317,11 @@ func TestCoinsGTE(t *testing.T) {
|
||||
two := NewInt(2)
|
||||
|
||||
assert.True(t, Coins{}.IsAllGTE(Coins{}))
|
||||
assert.True(t, Coins{{"A", one}}.IsAllGTE(Coins{}))
|
||||
assert.True(t, Coins{{"A", one}}.IsAllGTE(Coins{{"A", one}}))
|
||||
assert.False(t, Coins{{"A", one}}.IsAllGTE(Coins{{"B", one}}))
|
||||
assert.True(t, Coins{{"A", one}, {"B", one}}.IsAllGTE(Coins{{"B", one}}))
|
||||
assert.False(t, Coins{{"A", one}, {"B", one}}.IsAllGTE(Coins{{"B", two}}))
|
||||
assert.True(t, Coins{{"a", one}}.IsAllGTE(Coins{}))
|
||||
assert.True(t, Coins{{"a", one}}.IsAllGTE(Coins{{"a", one}}))
|
||||
assert.False(t, Coins{{"a", one}}.IsAllGTE(Coins{{"b", one}}))
|
||||
assert.True(t, Coins{{"a", one}, {"b", one}}.IsAllGTE(Coins{{"b", one}}))
|
||||
assert.False(t, Coins{{"a", one}, {"b", one}}.IsAllGTE(Coins{{"b", two}}))
|
||||
}
|
||||
|
||||
func TestCoinsLT(t *testing.T) {
|
||||
@@ -323,14 +329,14 @@ func TestCoinsLT(t *testing.T) {
|
||||
two := NewInt(2)
|
||||
|
||||
assert.False(t, Coins{}.IsAllLT(Coins{}))
|
||||
assert.False(t, Coins{{"A", one}}.IsAllLT(Coins{}))
|
||||
assert.False(t, Coins{{"A", one}}.IsAllLT(Coins{{"A", one}}))
|
||||
assert.False(t, Coins{{"A", one}}.IsAllLT(Coins{{"B", one}}))
|
||||
assert.False(t, Coins{{"A", one}, {"B", one}}.IsAllLT(Coins{{"B", one}}))
|
||||
assert.False(t, Coins{{"A", one}, {"B", one}}.IsAllLT(Coins{{"B", two}}))
|
||||
assert.False(t, Coins{{"A", one}, {"B", one}}.IsAllLT(Coins{{"A", one}, {"B", one}}))
|
||||
assert.True(t, Coins{{"A", one}, {"B", one}}.IsAllLT(Coins{{"A", one}, {"B", two}}))
|
||||
assert.True(t, Coins{}.IsAllLT(Coins{{"A", one}}))
|
||||
assert.False(t, Coins{{"a", one}}.IsAllLT(Coins{}))
|
||||
assert.False(t, Coins{{"a", one}}.IsAllLT(Coins{{"a", one}}))
|
||||
assert.False(t, Coins{{"a", one}}.IsAllLT(Coins{{"b", one}}))
|
||||
assert.False(t, Coins{{"a", one}, {"b", one}}.IsAllLT(Coins{{"b", one}}))
|
||||
assert.False(t, Coins{{"a", one}, {"b", one}}.IsAllLT(Coins{{"b", two}}))
|
||||
assert.False(t, Coins{{"a", one}, {"b", one}}.IsAllLT(Coins{{"a", one}, {"b", one}}))
|
||||
assert.True(t, Coins{{"a", one}, {"b", one}}.IsAllLT(Coins{{"a", one}, {"b", two}}))
|
||||
assert.True(t, Coins{}.IsAllLT(Coins{{"a", one}}))
|
||||
}
|
||||
|
||||
func TestCoinsLTE(t *testing.T) {
|
||||
@@ -338,14 +344,14 @@ func TestCoinsLTE(t *testing.T) {
|
||||
two := NewInt(2)
|
||||
|
||||
assert.True(t, Coins{}.IsAllLTE(Coins{}))
|
||||
assert.False(t, Coins{{"A", one}}.IsAllLTE(Coins{}))
|
||||
assert.True(t, Coins{{"A", one}}.IsAllLTE(Coins{{"A", one}}))
|
||||
assert.False(t, Coins{{"A", one}}.IsAllLTE(Coins{{"B", one}}))
|
||||
assert.False(t, Coins{{"A", one}, {"B", one}}.IsAllLTE(Coins{{"B", one}}))
|
||||
assert.False(t, Coins{{"A", one}, {"B", one}}.IsAllLTE(Coins{{"B", two}}))
|
||||
assert.True(t, Coins{{"A", one}, {"B", one}}.IsAllLTE(Coins{{"A", one}, {"B", one}}))
|
||||
assert.True(t, Coins{{"A", one}, {"B", one}}.IsAllLTE(Coins{{"A", one}, {"B", two}}))
|
||||
assert.True(t, Coins{}.IsAllLTE(Coins{{"A", one}}))
|
||||
assert.False(t, Coins{{"a", one}}.IsAllLTE(Coins{}))
|
||||
assert.True(t, Coins{{"a", one}}.IsAllLTE(Coins{{"a", one}}))
|
||||
assert.False(t, Coins{{"a", one}}.IsAllLTE(Coins{{"b", one}}))
|
||||
assert.False(t, Coins{{"a", one}, {"b", one}}.IsAllLTE(Coins{{"b", one}}))
|
||||
assert.False(t, Coins{{"a", one}, {"b", one}}.IsAllLTE(Coins{{"b", two}}))
|
||||
assert.True(t, Coins{{"a", one}, {"b", one}}.IsAllLTE(Coins{{"a", one}, {"b", one}}))
|
||||
assert.True(t, Coins{{"a", one}, {"b", one}}.IsAllLTE(Coins{{"a", one}, {"b", two}}))
|
||||
assert.True(t, Coins{}.IsAllLTE(Coins{{"a", one}}))
|
||||
}
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
@@ -382,32 +388,32 @@ func TestParse(t *testing.T) {
|
||||
|
||||
func TestSortCoins(t *testing.T) {
|
||||
good := Coins{
|
||||
NewInt64Coin("GAS", 1),
|
||||
NewInt64Coin("MINERAL", 1),
|
||||
NewInt64Coin("TREE", 1),
|
||||
NewInt64Coin("gas", 1),
|
||||
NewInt64Coin("mineral", 1),
|
||||
NewInt64Coin("tree", 1),
|
||||
}
|
||||
empty := Coins{
|
||||
NewInt64Coin("GOLD", 0),
|
||||
NewInt64Coin("gold", 0),
|
||||
}
|
||||
badSort1 := Coins{
|
||||
NewInt64Coin("TREE", 1),
|
||||
NewInt64Coin("GAS", 1),
|
||||
NewInt64Coin("MINERAL", 1),
|
||||
NewInt64Coin("tree", 1),
|
||||
NewInt64Coin("gas", 1),
|
||||
NewInt64Coin("mineral", 1),
|
||||
}
|
||||
badSort2 := Coins{ // both are after the first one, but the second and third are in the wrong order
|
||||
NewInt64Coin("GAS", 1),
|
||||
NewInt64Coin("TREE", 1),
|
||||
NewInt64Coin("MINERAL", 1),
|
||||
NewInt64Coin("gas", 1),
|
||||
NewInt64Coin("tree", 1),
|
||||
NewInt64Coin("mineral", 1),
|
||||
}
|
||||
badAmt := Coins{
|
||||
NewInt64Coin("GAS", 1),
|
||||
NewInt64Coin("TREE", 0),
|
||||
NewInt64Coin("MINERAL", 1),
|
||||
NewInt64Coin("gas", 1),
|
||||
NewInt64Coin("tree", 0),
|
||||
NewInt64Coin("mineral", 1),
|
||||
}
|
||||
dup := Coins{
|
||||
NewInt64Coin("GAS", 1),
|
||||
NewInt64Coin("GAS", 1),
|
||||
NewInt64Coin("MINERAL", 1),
|
||||
NewInt64Coin("gas", 1),
|
||||
NewInt64Coin("gas", 1),
|
||||
NewInt64Coin("mineral", 1),
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
@@ -438,16 +444,16 @@ func TestAmountOf(t *testing.T) {
|
||||
NewInt64Coin(" ", 0),
|
||||
}
|
||||
case3 := Coins{
|
||||
NewInt64Coin("GOLD", 0),
|
||||
NewInt64Coin("gold", 0),
|
||||
}
|
||||
case4 := Coins{
|
||||
NewInt64Coin("GAS", 1),
|
||||
NewInt64Coin("MINERAL", 1),
|
||||
NewInt64Coin("TREE", 1),
|
||||
NewInt64Coin("gas", 1),
|
||||
NewInt64Coin("mineral", 1),
|
||||
NewInt64Coin("tree", 1),
|
||||
}
|
||||
case5 := Coins{
|
||||
NewInt64Coin("MINERAL", 1),
|
||||
NewInt64Coin("TREE", 1),
|
||||
NewInt64Coin("mineral", 1),
|
||||
NewInt64Coin("tree", 1),
|
||||
}
|
||||
case6 := Coins{
|
||||
NewInt64Coin("", 6),
|
||||
@@ -456,7 +462,7 @@ func TestAmountOf(t *testing.T) {
|
||||
NewInt64Coin(" ", 7),
|
||||
}
|
||||
case8 := Coins{
|
||||
NewInt64Coin("GAS", 8),
|
||||
NewInt64Coin("gas", 8),
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
@@ -481,8 +487,10 @@ func TestAmountOf(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
assert.Equal(t, NewInt(tc.amountOf), tc.coins.AmountOf(""))
|
||||
assert.Equal(t, NewInt(tc.amountOfSpace), tc.coins.AmountOf(" "))
|
||||
assert.Equal(t, NewInt(tc.amountOfGAS), tc.coins.AmountOf("GAS"))
|
||||
assert.Equal(t, NewInt(tc.amountOfMINERAL), tc.coins.AmountOf("MINERAL"))
|
||||
assert.Equal(t, NewInt(tc.amountOfTREE), tc.coins.AmountOf("TREE"))
|
||||
assert.Equal(t, NewInt(tc.amountOfGAS), tc.coins.AmountOf("gas"))
|
||||
assert.Equal(t, NewInt(tc.amountOfMINERAL), tc.coins.AmountOf("mineral"))
|
||||
assert.Equal(t, NewInt(tc.amountOfTREE), tc.coins.AmountOf("tree"))
|
||||
}
|
||||
|
||||
assert.Panics(t, func() { cases[0].coins.AmountOf("Invalid") })
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ func TestContextWithCustom(t *testing.T) {
|
||||
logger := NewMockLogger()
|
||||
voteinfos := []abci.VoteInfo{{}}
|
||||
meter := types.NewGasMeter(10000)
|
||||
minFees := types.Coins{types.NewInt64Coin("feeCoin", 1)}
|
||||
minFees := types.Coins{types.NewInt64Coin("feetoken", 1)}
|
||||
|
||||
ctx = types.NewContext(nil, header, ischeck, logger)
|
||||
require.Equal(t, header, ctx.BlockHeader())
|
||||
@@ -182,5 +182,5 @@ func TestContextWithCustom(t *testing.T) {
|
||||
require.Equal(t, logger, ctx.Logger())
|
||||
require.Equal(t, voteinfos, ctx.VoteInfos())
|
||||
require.Equal(t, meter, ctx.GasMeter())
|
||||
require.Equal(t, minFees, types.Coins{types.NewInt64Coin("feeCoin", 1)})
|
||||
require.Equal(t, minFees, types.Coins{types.NewInt64Coin("feetoken", 1)})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user