Skip to content

Commit e6579cf

Browse files
committed
table driven testing
squash
1 parent 5c3550a commit e6579cf

File tree

7 files changed

+249
-162
lines changed

7 files changed

+249
-162
lines changed

cmd/commands/utils_test.go

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,24 @@ func TestHex(t *testing.T) {
1515
hexWPrefix := "0x" + hexNoPrefix
1616
str := "foobar"
1717
strWPrefix := "0xfoobar"
18-
assert.True(t, isHex(hexWPrefix), "isHex not identifying hex with 0x prefix")
19-
assert.True(t, !isHex(hexNoPrefix), "isHex shouldn't identify hex without 0x prefix")
20-
assert.True(t, !isHex(str), "isHex shouldn't identify non-hex string")
21-
assert.True(t, !isHex(strWPrefix), "isHex shouldn't identify non-hex string with 0x prefix")
2218

23-
//test strip hex
24-
assert.True(t, StripHex(hexWPrefix) == hexNoPrefix, "StripHex doesn't remove first two characters")
19+
//define the list of coin tests
20+
var testList = []struct {
21+
testPass bool
22+
errMsg string
23+
}{
24+
{isHex(hexWPrefix), "isHex not identifying hex with 0x prefix"},
25+
{!isHex(hexNoPrefix), "isHex shouldn't identify hex without 0x prefix"},
26+
{!isHex(str), "isHex shouldn't identify non-hex string"},
27+
{!isHex(strWPrefix), "isHex shouldn't identify non-hex string with 0x prefix"},
28+
{StripHex(hexWPrefix) == hexNoPrefix, "StripHex doesn't remove first two characters"},
29+
}
30+
31+
//execute the tests
32+
for _, tl := range testList {
33+
assert.True(t, tl.testPass, tl.errMsg)
34+
}
35+
2536
}
2637

2738
//Test the parse coin and parse coins functionality
@@ -43,15 +54,27 @@ func TestParse(t *testing.T) {
4354
return coin
4455
}
4556

46-
//testing ParseCoin Function
47-
assert.True(t, types.Coin{} == makeCoin(""), "parseCoin makes bad empty coin")
48-
assert.True(t, types.Coin{"fooCoin", 1} == makeCoin("1fooCoin"), "parseCoin makes bad coins")
49-
assert.True(t, types.Coin{"barCoin", 10} == makeCoin("10 barCoin"), "parseCoin makes bad coins")
50-
51-
//testing ParseCoins Function
52-
assert.True(t, types.Coins{{"fooCoin", 1}}.IsEqual(makeCoins("1fooCoin")), "parseCoins doesn't parse a single coin")
53-
assert.True(t, types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99barCoin,1fooCoin")),
54-
"parseCoins doesn't properly parse two coins")
55-
assert.True(t, types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99 barCoin, 1 fooCoin")),
56-
"parseCoins doesn't properly parse two coins which use spaces")
57+
//define the list of coin tests
58+
var testList = []struct {
59+
testPass bool
60+
errMsg string
61+
}{
62+
//testing ParseCoin Function
63+
{types.Coin{} == makeCoin(""), "parseCoin makes bad empty coin"},
64+
{types.Coin{"fooCoin", 1} == makeCoin("1fooCoin"), "parseCoin makes bad coins"},
65+
{types.Coin{"barCoin", 10} == makeCoin("10 barCoin"), "parseCoin makes bad coins"},
66+
67+
//testing ParseCoins Function
68+
{types.Coins{{"fooCoin", 1}}.IsEqual(makeCoins("1fooCoin")),
69+
"parseCoins doesn't parse a single coin"},
70+
{types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99barCoin,1fooCoin")),
71+
"parseCoins doesn't properly parse two coins"},
72+
{types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99 barCoin, 1 fooCoin")),
73+
"parseCoins doesn't properly parse two coins which use spaces"},
74+
}
75+
76+
//execute the tests
77+
for _, tl := range testList {
78+
assert.True(t, tl.testPass, tl.errMsg)
79+
}
5780
}

state/state_test.go

Lines changed: 82 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,13 @@ import (
1212

1313
func TestState(t *testing.T) {
1414

15-
s := NewState(types.NewMemKVStore())
16-
17-
s.SetChainID("testchain")
18-
assert.True(t, s.GetChainID() == "testchain", "ChainID is improperly stored")
19-
20-
setRecords := func(kv types.KVStore) {
21-
kv.Set([]byte("foo"), []byte("snake"))
22-
kv.Set([]byte("bar"), []byte("mouse"))
23-
}
24-
25-
setRecords(s)
26-
assert.True(t, bytes.Equal(s.Get([]byte("foo")), []byte("snake")), "state doesn't retrieve after Set")
27-
assert.True(t, bytes.Equal(s.Get([]byte("bar")), []byte("mouse")), "state doesn't retrieve after Set")
15+
//States and Stores for tests
16+
store := types.NewMemKVStore()
17+
state := NewState(store)
18+
cache := state.CacheWrap()
19+
eyesCli := eyes.NewLocalClient("", 0)
2820

29-
// Test account retrieve
21+
//Account and address for tests
3022
dumAddr := []byte("dummyAddress")
3123

3224
acc := &types.Account{
@@ -35,34 +27,83 @@ func TestState(t *testing.T) {
3527
Balance: nil,
3628
}
3729

38-
s.SetAccount(dumAddr, acc)
39-
assert.True(t, s.GetAccount(dumAddr).Sequence == 1, "GetAccount not retrieving")
30+
//reset the store/state/cache
31+
reset := func() {
32+
store = types.NewMemKVStore()
33+
state = NewState(store)
34+
cache = state.CacheWrap()
35+
}
4036

41-
//Test CacheWrap with local mem store
42-
store := types.NewMemKVStore()
43-
s = NewState(store)
44-
cache := s.CacheWrap()
45-
setRecords(cache)
46-
assert.True(t, !bytes.Equal(store.Get([]byte("foo")), []byte("snake")), "store retrieving before Commit")
47-
assert.True(t, !bytes.Equal(store.Get([]byte("bar")), []byte("mouse")), "store retrieving before Commit")
48-
cache.CacheSync()
49-
assert.True(t, bytes.Equal(store.Get([]byte("foo")), []byte("snake")), "store doesn't retrieve after Commit")
50-
assert.True(t, bytes.Equal(store.Get([]byte("bar")), []byte("mouse")), "store doesn't retrieve after Commit")
51-
52-
//Test Commit on state with non-merkle store
53-
assert.True(t, !s.Commit().IsOK(), "Commit shouldn't work with non-merkle store")
54-
55-
//Test CacheWrap with merkleeyes client store
56-
eyesCli := eyes.NewLocalClient("", 0)
57-
s = NewState(eyesCli)
37+
//set the state to using the eyesCli instead of MemKVStore
38+
useEyesCli := func() {
39+
state = NewState(eyesCli)
40+
cache = state.CacheWrap()
41+
}
42+
43+
//key value pairs to be tested within the system
44+
keyvalue := []struct {
45+
key string
46+
value string
47+
}{
48+
{"foo", "snake"},
49+
{"bar", "mouse"},
50+
}
51+
52+
//set the kvc to have all the key value pairs
53+
setRecords := func(kv types.KVStore) {
54+
for _, n := range keyvalue {
55+
kv.Set([]byte(n.key), []byte(n.value))
56+
}
57+
}
5858

59-
cache = s.CacheWrap()
60-
setRecords(cache)
61-
assert.True(t, !bytes.Equal(eyesCli.Get([]byte("foo")), []byte("snake")), "store retrieving before Commit")
62-
assert.True(t, !bytes.Equal(eyesCli.Get([]byte("bar")), []byte("mouse")), "store retrieving before Commit")
63-
cache.CacheSync()
64-
assert.True(t, s.Commit().IsOK(), "Bad Commit")
65-
assert.True(t, bytes.Equal(eyesCli.Get([]byte("foo")), []byte("snake")), "store doesn't retrieve after Commit")
66-
assert.True(t, bytes.Equal(eyesCli.Get([]byte("bar")), []byte("mouse")), "store doesn't retrieve after Commit")
59+
//store has all the key value pairs
60+
storeHasAll := func(kv types.KVStore) bool {
61+
for _, n := range keyvalue {
62+
if !bytes.Equal(kv.Get([]byte(n.key)), []byte(n.value)) {
63+
return false
64+
}
65+
}
66+
return true
67+
}
6768

69+
//define the test list
70+
testList := []struct {
71+
testPass func() bool
72+
errMsg string
73+
}{
74+
//test chainID
75+
{func() bool { state.SetChainID("testchain"); return state.GetChainID() == "testchain" },
76+
"ChainID is improperly stored"},
77+
78+
//test basic retrieve
79+
{func() bool { setRecords(state); return storeHasAll(state) },
80+
"state doesn't retrieve after Set"},
81+
82+
// Test account retrieve
83+
{func() bool { state.SetAccount(dumAddr, acc); return state.GetAccount(dumAddr).Sequence == 1 },
84+
"GetAccount not retrieving"},
85+
86+
//Test CacheWrap with local mem store
87+
{func() bool { reset(); setRecords(cache); return !storeHasAll(store) },
88+
"store retrieving before CacheSync"},
89+
{func() bool { cache.CacheSync(); return storeHasAll(store) },
90+
"store doesn't retrieve after CacheSync"},
91+
92+
//Test Commit on state with non-merkle store
93+
{func() bool { return !state.Commit().IsOK() },
94+
"Commit shouldn't work with non-merkle store"},
95+
96+
//Test CacheWrap with merkleeyes client store
97+
{func() bool { useEyesCli(); setRecords(cache); return !storeHasAll(eyesCli) },
98+
"eyesCli retrieving before Commit"},
99+
{func() bool { cache.CacheSync(); return state.Commit().IsOK() },
100+
"Bad Commit"},
101+
{func() bool { return storeHasAll(eyesCli) },
102+
"eyesCli doesn't retrieve after Commit"},
103+
}
104+
105+
//execute the tests
106+
for _, tl := range testList {
107+
assert.True(t, tl.testPass(), tl.errMsg)
108+
}
68109
}

types/account_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ func TestAccount(t *testing.T) {
1616

1717
//test Copy
1818
accCopy := acc.Copy()
19-
accCopy.Sequence = 1
20-
t.Log(acc.Sequence)
21-
t.Log(accCopy.Sequence)
22-
assert.True(t, acc.Sequence != accCopy.Sequence, "Account Copy Error")
19+
assert.True(t, &acc != accCopy, "Account Copy Error")
2320

2421
//test sending nils for panic
2522
var nilAcc *Account

types/coin_test.go

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,84 +2,64 @@ package types
22

33
import (
44
"testing"
5+
6+
cmn "github.com/tendermint/go-common"
7+
8+
"github.com/stretchr/testify/assert"
59
)
610

711
func TestCoins(t *testing.T) {
8-
coins := Coins{
12+
13+
//Define the coins to be used in tests
14+
good := Coins{
915
Coin{"GAS", 1},
1016
Coin{"MINERAL", 1},
1117
Coin{"TREE", 1},
1218
}
13-
14-
if !coins.IsValid() {
15-
t.Fatal("Coins are valid")
16-
}
17-
18-
if !coins.IsPositive() {
19-
t.Fatalf("Expected coins to be positive: %v", coins)
20-
}
21-
22-
emptyCoins := Coins{Coin{"GOLD", 0}}
23-
if !coins.IsGTE(emptyCoins) {
24-
t.Fatalf("Expected %v to be >= %v", coins, emptyCoins)
25-
}
26-
27-
negCoins := coins.Negative()
28-
if negCoins.IsPositive() {
29-
t.Fatalf("Expected neg coins to not be positive: %v", negCoins)
19+
neg := good.Negative()
20+
sum := good.Plus(neg)
21+
empty := Coins{
22+
Coin{"GOLD", 0},
3023
}
31-
32-
sumCoins := coins.Plus(negCoins)
33-
if len(sumCoins) != 0 {
34-
t.Fatal("Expected 0 coins")
35-
}
36-
}
37-
38-
func TestCoinsBadSort(t *testing.T) {
39-
coins := Coins{
24+
badSort1 := Coins{
4025
Coin{"TREE", 1},
4126
Coin{"GAS", 1},
4227
Coin{"MINERAL", 1},
4328
}
44-
45-
if coins.IsValid() {
46-
t.Fatal("Coins are not sorted")
47-
}
48-
}
49-
50-
func TestCoinsBadSort2(t *testing.T) {
51-
// both are after the first one, but the second and third are in the wrong order
52-
coins := Coins{
29+
badSort2 := Coins{ // both are after the first one, but the second and third are in the wrong order
5330
Coin{"GAS", 1},
5431
Coin{"TREE", 1},
5532
Coin{"MINERAL", 1},
5633
}
57-
58-
if coins.IsValid() {
59-
t.Fatal("Coins are not sorted")
60-
}
61-
}
62-
63-
func TestCoinsBadAmount(t *testing.T) {
64-
coins := Coins{
34+
badAmt := Coins{
6535
Coin{"GAS", 1},
6636
Coin{"TREE", 0},
6737
Coin{"MINERAL", 1},
6838
}
69-
70-
if coins.IsValid() {
71-
t.Fatal("Coins cannot include 0 amounts")
72-
}
73-
}
74-
75-
func TestCoinsDuplicate(t *testing.T) {
76-
coins := Coins{
39+
dup := Coins{
7740
Coin{"GAS", 1},
7841
Coin{"GAS", 1},
7942
Coin{"MINERAL", 1},
8043
}
8144

82-
if coins.IsValid() {
83-
t.Fatal("Duplicate coin")
45+
//define the list of coin tests
46+
var testList = []struct {
47+
testPass bool
48+
errMsg string
49+
}{
50+
{good.IsValid(), "Coins are valid"},
51+
{good.IsPositive(), cmn.Fmt("Expected coins to be positive: %v", good)},
52+
{good.IsGTE(empty), cmn.Fmt("Expected %v to be >= %v", good, empty)},
53+
{!neg.IsPositive(), cmn.Fmt("Expected neg coins to not be positive: %v", neg)},
54+
{len(sum) == 0, "Expected 0 coins"},
55+
{!badSort1.IsValid(), "Coins are not sorted"},
56+
{!badSort2.IsValid(), "Coins are not sorted"},
57+
{!badAmt.IsValid(), "Coins cannot include 0 amounts"},
58+
{!dup.IsValid(), "Duplicate coin"},
59+
}
60+
61+
//execute the tests
62+
for _, tl := range testList {
63+
assert.True(t, tl.testPass, tl.errMsg)
8464
}
8565
}

0 commit comments

Comments
 (0)