Improved response tests

Actually verifies output as by regex
This commit is contained in:
Taylor Gerring 2015-04-01 11:38:06 +02:00
parent 25998cfc45
commit 7b7392826d
2 changed files with 117 additions and 17 deletions

View File

@ -50,8 +50,12 @@ func newHexData(input interface{}) *hexdata {
d.data = input.([]byte) d.data = input.([]byte)
case common.Hash: case common.Hash:
d.data = input.(common.Hash).Bytes() d.data = input.(common.Hash).Bytes()
case *common.Hash:
d.data = input.(*common.Hash).Bytes()
case common.Address: case common.Address:
d.data = input.(common.Address).Bytes() d.data = input.(common.Address).Bytes()
case *common.Address:
d.data = input.(*common.Address).Bytes()
case *big.Int: case *big.Int:
d.data = input.(*big.Int).Bytes() d.data = input.(*big.Int).Bytes()
case int64: case int64:
@ -62,7 +66,7 @@ func newHexData(input interface{}) *hexdata {
d.data = big.NewInt(int64(input.(int))).Bytes() d.data = big.NewInt(int64(input.(int))).Bytes()
case uint: case uint:
d.data = big.NewInt(int64(input.(uint))).Bytes() d.data = big.NewInt(int64(input.(uint))).Bytes()
case string: case string: // hexstring
d.data = common.Big(input.(string)).Bytes() d.data = common.Big(input.(string)).Bytes()
default: default:
d.data = nil d.data = nil

View File

@ -1,7 +1,10 @@
package rpc package rpc
import ( import (
"encoding/json"
"fmt"
"math/big" "math/big"
"regexp"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -9,6 +12,15 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
) )
const (
reHash = `"0x[0-9a-f]{64}"` // 32 bytes
reHashOpt = `"(0x[0-9a-f]{64})"|null` // 32 bytes or null
reAddress = `"0x[0-9a-f]{40}"` // 20 bytes
reAddressOpt = `"0x[0-9a-f]{40}"|null` // 20 bytes or null
reNum = `"0x([1-9a-f][0-9a-f]{1,15})|0"` // must not have left-padded zeros
reData = `"0x[0-9a-f]*"` // can be "empty"
)
func TestNewBlockRes(t *testing.T) { func TestNewBlockRes(t *testing.T) {
parentHash := common.HexToHash("0x01") parentHash := common.HexToHash("0x01")
coinbase := common.HexToAddress("0x01") coinbase := common.HexToAddress("0x01")
@ -17,8 +29,35 @@ func TestNewBlockRes(t *testing.T) {
nonce := uint64(1) nonce := uint64(1)
extra := "" extra := ""
block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, extra) block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, extra)
tests := map[string]string{
"number": reNum,
"hash": reHash,
"parentHash": reHash,
"nonce": reNum,
"sha3Uncles": reHash,
"logsBloom": reData,
"transactionRoot": reHash,
"stateRoot": reHash,
"miner": reAddress,
"difficulty": `"0x1"`,
"totalDifficulty": reNum,
"size": reNum,
"extraData": reData,
"gasLimit": reNum,
// "minGasPrice": "0x",
"gasUsed": reNum,
"timestamp": reNum,
}
_ = NewBlockRes(block) v := NewBlockRes(block)
j, _ := json.Marshal(v)
for k, re := range tests {
match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
if !match {
t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j))
}
}
} }
func TestNewTransactionRes(t *testing.T) { func TestNewTransactionRes(t *testing.T) {
@ -29,10 +68,80 @@ func TestNewTransactionRes(t *testing.T) {
data := []byte{1, 2, 3} data := []byte{1, 2, 3}
tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data) tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data)
_ = NewTransactionRes(tx) tests := map[string]string{
"hash": reHash,
"nonce": reNum,
"blockHash": reHash,
"blockNum": reNum,
"transactionIndex": reNum,
"from": reAddress,
"to": reAddressOpt,
"value": reNum,
"gas": reNum,
"gasPrice": reNum,
"input": reData,
}
v := NewTransactionRes(tx)
v.BlockHash = newHexData(common.HexToHash("0x030201"))
v.BlockNumber = newHexNum(5)
v.TxIndex = newHexNum(0)
j, _ := json.Marshal(v)
for k, re := range tests {
match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
if !match {
t.Error(fmt.Sprintf("`%s` output json does not match format %s. Source %s", k, re, j))
}
}
} }
func MakeStateLog(num int) state.Log { func TestNewLogRes(t *testing.T) {
log := makeStateLog(0)
tests := map[string]string{
"address": reAddress,
// "topics": "[.*]"
"data": reData,
"blockNumber": reNum,
// "hash": reHash,
// "logIndex": reNum,
// "blockHash": reHash,
// "transactionHash": reHash,
"transactionIndex": reNum,
}
v := NewLogRes(log)
j, _ := json.Marshal(v)
for k, re := range tests {
match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
if !match {
t.Error(fmt.Sprintf("`%s` output json does not match format %s. Got %s", k, re, j))
}
}
}
func TestNewLogsRes(t *testing.T) {
logs := make([]state.Log, 3)
logs[0] = makeStateLog(1)
logs[1] = makeStateLog(2)
logs[2] = makeStateLog(3)
tests := map[string]string{}
v := NewLogsRes(logs)
j, _ := json.Marshal(v)
for k, re := range tests {
match, _ := regexp.MatchString(fmt.Sprintf(`[{.*"%s":%s.*}]`, k, re), string(j))
if !match {
t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j))
}
}
}
func makeStateLog(num int) state.Log {
address := common.HexToAddress("0x0") address := common.HexToAddress("0x0")
data := []byte{1, 2, 3} data := []byte{1, 2, 3}
number := uint64(num) number := uint64(num)
@ -43,16 +152,3 @@ func MakeStateLog(num int) state.Log {
log := state.NewLog(address, topics, data, number) log := state.NewLog(address, topics, data, number)
return log return log
} }
func TestNewLogRes(t *testing.T) {
log := MakeStateLog(0)
_ = NewLogRes(log)
}
func TestNewLogsRes(t *testing.T) {
logs := make([]state.Log, 3)
logs[0] = MakeStateLog(1)
logs[1] = MakeStateLog(2)
logs[2] = MakeStateLog(3)
_ = NewLogsRes(logs)
}