table driven testing

squash
This commit is contained in:
rigelrozanski 2017-02-22 17:30:50 -05:00
parent 5c3550acce
commit e6579cf9e9
7 changed files with 246 additions and 159 deletions

View File

@ -15,13 +15,24 @@ func TestHex(t *testing.T) {
hexWPrefix := "0x" + hexNoPrefix
str := "foobar"
strWPrefix := "0xfoobar"
assert.True(t, isHex(hexWPrefix), "isHex not identifying hex with 0x prefix")
assert.True(t, !isHex(hexNoPrefix), "isHex shouldn't identify hex without 0x prefix")
assert.True(t, !isHex(str), "isHex shouldn't identify non-hex string")
assert.True(t, !isHex(strWPrefix), "isHex shouldn't identify non-hex string with 0x prefix")
//test strip hex
assert.True(t, StripHex(hexWPrefix) == hexNoPrefix, "StripHex doesn't remove first two characters")
//define the list of coin tests
var testList = []struct {
testPass bool
errMsg string
}{
{isHex(hexWPrefix), "isHex not identifying hex with 0x prefix"},
{!isHex(hexNoPrefix), "isHex shouldn't identify hex without 0x prefix"},
{!isHex(str), "isHex shouldn't identify non-hex string"},
{!isHex(strWPrefix), "isHex shouldn't identify non-hex string with 0x prefix"},
{StripHex(hexWPrefix) == hexNoPrefix, "StripHex doesn't remove first two characters"},
}
//execute the tests
for _, tl := range testList {
assert.True(t, tl.testPass, tl.errMsg)
}
}
//Test the parse coin and parse coins functionality
@ -43,15 +54,27 @@ func TestParse(t *testing.T) {
return coin
}
//testing ParseCoin Function
assert.True(t, types.Coin{} == makeCoin(""), "parseCoin makes bad empty coin")
assert.True(t, types.Coin{"fooCoin", 1} == makeCoin("1fooCoin"), "parseCoin makes bad coins")
assert.True(t, types.Coin{"barCoin", 10} == makeCoin("10 barCoin"), "parseCoin makes bad coins")
//define the list of coin tests
var testList = []struct {
testPass bool
errMsg string
}{
//testing ParseCoin Function
{types.Coin{} == makeCoin(""), "parseCoin makes bad empty coin"},
{types.Coin{"fooCoin", 1} == makeCoin("1fooCoin"), "parseCoin makes bad coins"},
{types.Coin{"barCoin", 10} == makeCoin("10 barCoin"), "parseCoin makes bad coins"},
//testing ParseCoins Function
assert.True(t, types.Coins{{"fooCoin", 1}}.IsEqual(makeCoins("1fooCoin")), "parseCoins doesn't parse a single coin")
assert.True(t, types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99barCoin,1fooCoin")),
"parseCoins doesn't properly parse two coins")
assert.True(t, types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99 barCoin, 1 fooCoin")),
"parseCoins doesn't properly parse two coins which use spaces")
//testing ParseCoins Function
{types.Coins{{"fooCoin", 1}}.IsEqual(makeCoins("1fooCoin")),
"parseCoins doesn't parse a single coin"},
{types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99barCoin,1fooCoin")),
"parseCoins doesn't properly parse two coins"},
{types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99 barCoin, 1 fooCoin")),
"parseCoins doesn't properly parse two coins which use spaces"},
}
//execute the tests
for _, tl := range testList {
assert.True(t, tl.testPass, tl.errMsg)
}
}

View File

@ -12,21 +12,13 @@ import (
func TestState(t *testing.T) {
s := NewState(types.NewMemKVStore())
//States and Stores for tests
store := types.NewMemKVStore()
state := NewState(store)
cache := state.CacheWrap()
eyesCli := eyes.NewLocalClient("", 0)
s.SetChainID("testchain")
assert.True(t, s.GetChainID() == "testchain", "ChainID is improperly stored")
setRecords := func(kv types.KVStore) {
kv.Set([]byte("foo"), []byte("snake"))
kv.Set([]byte("bar"), []byte("mouse"))
}
setRecords(s)
assert.True(t, bytes.Equal(s.Get([]byte("foo")), []byte("snake")), "state doesn't retrieve after Set")
assert.True(t, bytes.Equal(s.Get([]byte("bar")), []byte("mouse")), "state doesn't retrieve after Set")
// Test account retrieve
//Account and address for tests
dumAddr := []byte("dummyAddress")
acc := &types.Account{
@ -35,34 +27,83 @@ func TestState(t *testing.T) {
Balance: nil,
}
s.SetAccount(dumAddr, acc)
assert.True(t, s.GetAccount(dumAddr).Sequence == 1, "GetAccount not retrieving")
//reset the store/state/cache
reset := func() {
store = types.NewMemKVStore()
state = NewState(store)
cache = state.CacheWrap()
}
//Test CacheWrap with local mem store
store := types.NewMemKVStore()
s = NewState(store)
cache := s.CacheWrap()
setRecords(cache)
assert.True(t, !bytes.Equal(store.Get([]byte("foo")), []byte("snake")), "store retrieving before Commit")
assert.True(t, !bytes.Equal(store.Get([]byte("bar")), []byte("mouse")), "store retrieving before Commit")
cache.CacheSync()
assert.True(t, bytes.Equal(store.Get([]byte("foo")), []byte("snake")), "store doesn't retrieve after Commit")
assert.True(t, bytes.Equal(store.Get([]byte("bar")), []byte("mouse")), "store doesn't retrieve after Commit")
//set the state to using the eyesCli instead of MemKVStore
useEyesCli := func() {
state = NewState(eyesCli)
cache = state.CacheWrap()
}
//Test Commit on state with non-merkle store
assert.True(t, !s.Commit().IsOK(), "Commit shouldn't work with non-merkle store")
//key value pairs to be tested within the system
keyvalue := []struct {
key string
value string
}{
{"foo", "snake"},
{"bar", "mouse"},
}
//Test CacheWrap with merkleeyes client store
eyesCli := eyes.NewLocalClient("", 0)
s = NewState(eyesCli)
//set the kvc to have all the key value pairs
setRecords := func(kv types.KVStore) {
for _, n := range keyvalue {
kv.Set([]byte(n.key), []byte(n.value))
}
}
cache = s.CacheWrap()
setRecords(cache)
assert.True(t, !bytes.Equal(eyesCli.Get([]byte("foo")), []byte("snake")), "store retrieving before Commit")
assert.True(t, !bytes.Equal(eyesCli.Get([]byte("bar")), []byte("mouse")), "store retrieving before Commit")
cache.CacheSync()
assert.True(t, s.Commit().IsOK(), "Bad Commit")
assert.True(t, bytes.Equal(eyesCli.Get([]byte("foo")), []byte("snake")), "store doesn't retrieve after Commit")
assert.True(t, bytes.Equal(eyesCli.Get([]byte("bar")), []byte("mouse")), "store doesn't retrieve after Commit")
//store has all the key value pairs
storeHasAll := func(kv types.KVStore) bool {
for _, n := range keyvalue {
if !bytes.Equal(kv.Get([]byte(n.key)), []byte(n.value)) {
return false
}
}
return true
}
//define the test list
testList := []struct {
testPass func() bool
errMsg string
}{
//test chainID
{func() bool { state.SetChainID("testchain"); return state.GetChainID() == "testchain" },
"ChainID is improperly stored"},
//test basic retrieve
{func() bool { setRecords(state); return storeHasAll(state) },
"state doesn't retrieve after Set"},
// Test account retrieve
{func() bool { state.SetAccount(dumAddr, acc); return state.GetAccount(dumAddr).Sequence == 1 },
"GetAccount not retrieving"},
//Test CacheWrap with local mem store
{func() bool { reset(); setRecords(cache); return !storeHasAll(store) },
"store retrieving before CacheSync"},
{func() bool { cache.CacheSync(); return storeHasAll(store) },
"store doesn't retrieve after CacheSync"},
//Test Commit on state with non-merkle store
{func() bool { return !state.Commit().IsOK() },
"Commit shouldn't work with non-merkle store"},
//Test CacheWrap with merkleeyes client store
{func() bool { useEyesCli(); setRecords(cache); return !storeHasAll(eyesCli) },
"eyesCli retrieving before Commit"},
{func() bool { cache.CacheSync(); return state.Commit().IsOK() },
"Bad Commit"},
{func() bool { return storeHasAll(eyesCli) },
"eyesCli doesn't retrieve after Commit"},
}
//execute the tests
for _, tl := range testList {
assert.True(t, tl.testPass(), tl.errMsg)
}
}

View File

@ -16,10 +16,7 @@ func TestAccount(t *testing.T) {
//test Copy
accCopy := acc.Copy()
accCopy.Sequence = 1
t.Log(acc.Sequence)
t.Log(accCopy.Sequence)
assert.True(t, acc.Sequence != accCopy.Sequence, "Account Copy Error")
assert.True(t, &acc != accCopy, "Account Copy Error")
//test sending nils for panic
var nilAcc *Account

View File

@ -2,84 +2,64 @@ package types
import (
"testing"
cmn "github.com/tendermint/go-common"
"github.com/stretchr/testify/assert"
)
func TestCoins(t *testing.T) {
coins := Coins{
//Define the coins to be used in tests
good := Coins{
Coin{"GAS", 1},
Coin{"MINERAL", 1},
Coin{"TREE", 1},
}
if !coins.IsValid() {
t.Fatal("Coins are valid")
neg := good.Negative()
sum := good.Plus(neg)
empty := Coins{
Coin{"GOLD", 0},
}
if !coins.IsPositive() {
t.Fatalf("Expected coins to be positive: %v", coins)
}
emptyCoins := Coins{Coin{"GOLD", 0}}
if !coins.IsGTE(emptyCoins) {
t.Fatalf("Expected %v to be >= %v", coins, emptyCoins)
}
negCoins := coins.Negative()
if negCoins.IsPositive() {
t.Fatalf("Expected neg coins to not be positive: %v", negCoins)
}
sumCoins := coins.Plus(negCoins)
if len(sumCoins) != 0 {
t.Fatal("Expected 0 coins")
}
}
func TestCoinsBadSort(t *testing.T) {
coins := Coins{
badSort1 := Coins{
Coin{"TREE", 1},
Coin{"GAS", 1},
Coin{"MINERAL", 1},
}
if coins.IsValid() {
t.Fatal("Coins are not sorted")
}
}
func TestCoinsBadSort2(t *testing.T) {
// both are after the first one, but the second and third are in the wrong order
coins := Coins{
badSort2 := Coins{ // both are after the first one, but the second and third are in the wrong order
Coin{"GAS", 1},
Coin{"TREE", 1},
Coin{"MINERAL", 1},
}
if coins.IsValid() {
t.Fatal("Coins are not sorted")
}
}
func TestCoinsBadAmount(t *testing.T) {
coins := Coins{
badAmt := Coins{
Coin{"GAS", 1},
Coin{"TREE", 0},
Coin{"MINERAL", 1},
}
if coins.IsValid() {
t.Fatal("Coins cannot include 0 amounts")
}
}
func TestCoinsDuplicate(t *testing.T) {
coins := Coins{
dup := Coins{
Coin{"GAS", 1},
Coin{"GAS", 1},
Coin{"MINERAL", 1},
}
if coins.IsValid() {
t.Fatal("Duplicate coin")
//define the list of coin tests
var testList = []struct {
testPass bool
errMsg string
}{
{good.IsValid(), "Coins are valid"},
{good.IsPositive(), cmn.Fmt("Expected coins to be positive: %v", good)},
{good.IsGTE(empty), cmn.Fmt("Expected %v to be >= %v", good, empty)},
{!neg.IsPositive(), cmn.Fmt("Expected neg coins to not be positive: %v", neg)},
{len(sum) == 0, "Expected 0 coins"},
{!badSort1.IsValid(), "Coins are not sorted"},
{!badSort2.IsValid(), "Coins are not sorted"},
{!badAmt.IsValid(), "Coins cannot include 0 amounts"},
{!dup.IsValid(), "Duplicate coin"},
}
//execute the tests
for _, tl := range testList {
assert.True(t, tl.testPass, tl.errMsg)
}
}

View File

@ -2,58 +2,78 @@ package types
import (
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMemKVStore(t *testing.T) {
func TestKVStore(t *testing.T) {
//stores to be tested
ms := NewMemKVStore()
ms.Set([]byte("foo"), []byte("snake"))
ms.Set([]byte("bar"), []byte("mouse"))
assert.True(t, bytes.Equal(ms.Get([]byte("foo")), []byte("snake")), "MemKVStore doesn't retrieve after Set")
assert.True(t, bytes.Equal(ms.Get([]byte("bar")), []byte("mouse")), "MemKVStore doesn't retrieve after Set")
}
func TestKVCache(t *testing.T) {
store := NewMemKVStore()
kvc := NewKVCache(store)
setRecords := func() {
kvc.Set([]byte("foo"), []byte("snake"))
kvc.Set([]byte("bar"), []byte("mouse"))
//key value pairs to be tested within the system
var keyvalue = []struct {
key string
value string
}{
{"foo", "snake"},
{"bar", "mouse"},
}
//test read/write
setRecords()
assert.True(t, bytes.Equal(kvc.Get([]byte("foo")), []byte("snake")), "KVCache doesn't retrieve after Set")
assert.True(t, bytes.Equal(kvc.Get([]byte("bar")), []byte("mouse")), "KVCache doesn't retrieve after Set")
//set the kvc to have all the key value pairs
setRecords := func(kv KVStore) {
for _, n := range keyvalue {
kv.Set([]byte(n.key), []byte(n.value))
}
}
//test reset
kvc.Reset()
assert.True(t, !bytes.Equal(kvc.Get([]byte("foo")), []byte("snake")), "KVCache retrieving after reset")
assert.True(t, !bytes.Equal(kvc.Get([]byte("bar")), []byte("mouse")), "KVCache retrieving after reset")
//store has all the key value pairs
storeHasAll := func(kv KVStore) bool {
for _, n := range keyvalue {
if !bytes.Equal(kv.Get([]byte(n.key)), []byte(n.value)) {
return false
}
}
return true
}
//test sync
setRecords()
assert.True(t, !bytes.Equal(store.Get([]byte("foo")), []byte("snake")), "store retrieving before synced")
assert.True(t, !bytes.Equal(store.Get([]byte("bar")), []byte("mouse")), "store retrieving before synced")
kvc.Sync()
assert.True(t, bytes.Equal(store.Get([]byte("foo")), []byte("snake")), "store isn't retrieving after synced")
assert.True(t, bytes.Equal(store.Get([]byte("bar")), []byte("mouse")), "store isn't retrieving after synced")
//define the test list
var testList = []struct {
testPass func() bool
errMsg string
}{
//test read/write for MemKVStore
{func() bool { setRecords(ms); return storeHasAll(ms) },
"MemKVStore doesn't retrieve after Set"},
//test logging
assert.True(t, len(kvc.GetLogLines()) == 0, "logging events existed before using SetLogging")
fmt.Println(len(kvc.GetLogLines()))
//test read/write for KVCache
{func() bool { setRecords(kvc); return storeHasAll(kvc) },
"KVCache doesn't retrieve after Set"},
kvc.SetLogging()
setRecords()
assert.True(t, len(kvc.GetLogLines()) == 2, "incorrect number of logging events recorded")
//test reset
{func() bool { kvc.Reset(); return !storeHasAll(kvc) },
"KVCache retrieving after reset"},
kvc.ClearLogLines()
assert.True(t, len(kvc.GetLogLines()) == 0, "logging events still exists after ClearLogLines")
//test sync
{func() bool { setRecords(kvc); return !storeHasAll(store) },
"store retrieving before synced"},
{func() bool { kvc.Sync(); return storeHasAll(store) },
"store isn't retrieving after synced"},
//test logging
{func() bool { return len(kvc.GetLogLines()) == 0 },
"logging events existed before using SetLogging"},
{func() bool { kvc.SetLogging(); setRecords(kvc); return len(kvc.GetLogLines()) == 2 },
"incorrect number of logging events recorded"},
{func() bool { kvc.ClearLogLines(); return len(kvc.GetLogLines()) == 0 },
"logging events still exists after ClearLogLines"},
}
//execute the tests
for _, tl := range testList {
assert.True(t, tl.testPass(), tl.errMsg)
}
}

View File

@ -31,7 +31,7 @@ func (d *Dummy) EndBlock(store KVStore, height uint64) (res abci.ResponseEndBloc
//----------------------------------
func TestPlugin(t *testing.T) {
/*func TestPlugin(t *testing.T) {
plugins := NewPlugins()
assert.True(t, len(plugins.GetList()) == 0, "plugins object init with a objects")
@ -39,4 +39,28 @@ func TestPlugin(t *testing.T) {
assert.True(t, len(plugins.GetList()) == 1, "plugin wasn't added to plist after registered")
dum := plugins.GetByName("dummy")
assert.True(t, dum.Name() == "dummy", "plugin wasn't retrieved properly with GetByName")
}*/
func TestPlugin(t *testing.T) {
plugins := NewPlugins()
//define the test list
var testList = []struct {
testPass func() bool
errMsg string
}{
{func() bool { return (len(plugins.GetList()) == 0) },
"plugins object init with a objects"},
{func() bool { plugins.RegisterPlugin(&Dummy{}); return (len(plugins.GetList()) == 1) },
"plugin wasn't added to plist after registered"},
{func() bool { return (plugins.GetByName("dummy").Name() == "dummy") },
"plugin wasn't retrieved properly with GetByName"},
}
//execute the tests
for _, tl := range testList {
assert.True(t, tl.testPass(), tl.errMsg)
}
}

View File

@ -3,7 +3,9 @@ package types
import (
"testing"
. "github.com/tendermint/go-common"
cmn "github.com/tendermint/go-common"
"github.com/stretchr/testify/assert"
)
var chainID string = "test_chain"
@ -36,11 +38,11 @@ func TestSendTxSignable(t *testing.T) {
},
}
signBytes := sendTx.SignBytes(chainID)
signBytesHex := Fmt("%X", signBytes)
signBytesHex := cmn.Fmt("%X", signBytes)
expected := "010A746573745F636861696E0100000000000000DE00000000000000006F01020106696E7075743101010000000000000030390301093200000106696E70757432010100000000000000006F01DE0000010201076F757470757431010100000000000000014D01076F75747075743201010000000000000001BC"
if signBytesHex != expected {
t.Errorf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex)
}
assert.True(t, signBytesHex == expected,
cmn.Fmt("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex))
}
func TestAppTxSignable(t *testing.T) {
@ -56,9 +58,9 @@ func TestAppTxSignable(t *testing.T) {
Data: []byte("data1"),
}
signBytes := callTx.SignBytes(chainID)
signBytesHex := Fmt("%X", signBytes)
signBytesHex := cmn.Fmt("%X", signBytes)
expected := "010A746573745F636861696E0100000000000000DE00000000000000006F0101580106696E70757431010100000000000000303903010932000001056461746131"
if signBytesHex != expected {
t.Errorf("Got unexpected sign string for AppTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex)
}
assert.True(t, signBytesHex == expected,
cmn.Fmt("Got unexpected sign string for AppTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex))
}