forked from cerc-io/plugeth
Improved test coverage for rpc types
This commit is contained in:
parent
71e62eb620
commit
435378e953
18
rpc/types.go
18
rpc/types.go
@ -43,16 +43,11 @@ func (d *hexdata) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(d.String())
|
||||
}
|
||||
|
||||
func (d *hexdata) UnmarshalJSON(b []byte) (err error) {
|
||||
d.data = common.FromHex(string(b))
|
||||
return nil
|
||||
}
|
||||
|
||||
func newHexData(input interface{}) *hexdata {
|
||||
d := new(hexdata)
|
||||
|
||||
if input == nil {
|
||||
d.data = nil
|
||||
d.isNil = true
|
||||
return d
|
||||
}
|
||||
switch input := input.(type) {
|
||||
@ -105,19 +100,19 @@ func newHexData(input interface{}) *hexdata {
|
||||
case int16:
|
||||
d.data = big.NewInt(int64(input)).Bytes()
|
||||
case uint16:
|
||||
buff := make([]byte, 8)
|
||||
buff := make([]byte, 2)
|
||||
binary.BigEndian.PutUint16(buff, input)
|
||||
d.data = buff
|
||||
case int32:
|
||||
d.data = big.NewInt(int64(input)).Bytes()
|
||||
case uint32:
|
||||
buff := make([]byte, 8)
|
||||
buff := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(buff, input)
|
||||
d.data = buff
|
||||
case string: // hexstring
|
||||
d.data = common.Big(input).Bytes()
|
||||
default:
|
||||
d.data = nil
|
||||
d.isNil = true
|
||||
}
|
||||
|
||||
return d
|
||||
@ -147,11 +142,6 @@ func (d *hexnum) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(d.String())
|
||||
}
|
||||
|
||||
func (d *hexnum) UnmarshalJSON(b []byte) (err error) {
|
||||
d.data = common.FromHex(string(b))
|
||||
return nil
|
||||
}
|
||||
|
||||
func newHexNum(input interface{}) *hexnum {
|
||||
d := new(hexnum)
|
||||
|
||||
|
@ -1,7 +1,13 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
)
|
||||
|
||||
func TestInvalidTypeError(t *testing.T) {
|
||||
@ -48,3 +54,151 @@ func TestValidationError(t *testing.T) {
|
||||
t.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataMarshalNil(t *testing.T) {
|
||||
hd := newHexData([]byte{})
|
||||
hd.isNil = true
|
||||
v, _ := json.Marshal(hd)
|
||||
if string(v) != "null" {
|
||||
t.Errorf("Expected null, got %s", v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexnumMarshalNil(t *testing.T) {
|
||||
hn := newHexNum([]byte{})
|
||||
hn.isNil = true
|
||||
v, _ := json.Marshal(hn)
|
||||
if string(v) != "null" {
|
||||
t.Errorf("Expected null, got %s", v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataNil(t *testing.T) {
|
||||
v := newHexData(nil)
|
||||
if v.isNil != true {
|
||||
t.Errorf("Expected isNil to be true, but is %v", v.isNil)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataPtrHash(t *testing.T) {
|
||||
in := common.Hash{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}
|
||||
v := newHexData(&in)
|
||||
if bytes.Compare(in.Bytes(), v.data) != 0 {
|
||||
t.Errorf("Got % x expected % x", in, v.data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataPtrHashNil(t *testing.T) {
|
||||
var in *common.Hash
|
||||
in = nil
|
||||
v := newHexData(in)
|
||||
if !v.isNil {
|
||||
t.Errorf("Expect isNil to be true, but is %v", v.isNil)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataPtrAddress(t *testing.T) {
|
||||
in := common.Address{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
|
||||
v := newHexData(&in)
|
||||
if bytes.Compare(in.Bytes(), v.data) != 0 {
|
||||
t.Errorf("Got % x expected % x", in, v.data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataPtrAddressNil(t *testing.T) {
|
||||
var in *common.Address
|
||||
in = nil
|
||||
v := newHexData(in)
|
||||
if !v.isNil {
|
||||
t.Errorf("Expect isNil to be true, but is %v", v.isNil)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataPtrBloom(t *testing.T) {
|
||||
in := types.Bloom{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
|
||||
v := newHexData(&in)
|
||||
if bytes.Compare(in.Bytes(), v.data) != 0 {
|
||||
t.Errorf("Got % x expected % x", in, v.data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataPtrBloomNil(t *testing.T) {
|
||||
var in *types.Bloom
|
||||
in = nil
|
||||
v := newHexData(in)
|
||||
if !v.isNil {
|
||||
t.Errorf("Expect isNil to be true, but is %v", v.isNil)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataBigintNil(t *testing.T) {
|
||||
var in *big.Int
|
||||
in = nil
|
||||
v := newHexData(in)
|
||||
if !v.isNil {
|
||||
t.Errorf("Expect isNil to be true, but is %v", v.isNil)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataUint(t *testing.T) {
|
||||
var in = uint(16)
|
||||
var expected = []byte{0x10}
|
||||
v := newHexData(in)
|
||||
if bytes.Compare(expected, v.data) != 0 {
|
||||
t.Errorf("Expected % x got % x", expected, v.data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataInt8(t *testing.T) {
|
||||
var in = int8(16)
|
||||
var expected = []byte{0x10}
|
||||
v := newHexData(in)
|
||||
if bytes.Compare(expected, v.data) != 0 {
|
||||
t.Errorf("Expected % x got % x", expected, v.data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataUint8(t *testing.T) {
|
||||
var in = uint8(16)
|
||||
var expected = []byte{0x10}
|
||||
v := newHexData(in)
|
||||
if bytes.Compare(expected, v.data) != 0 {
|
||||
t.Errorf("Expected % x got % x", expected, v.data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataInt16(t *testing.T) {
|
||||
var in = int16(16)
|
||||
var expected = []byte{0x10}
|
||||
v := newHexData(in)
|
||||
if bytes.Compare(expected, v.data) != 0 {
|
||||
t.Errorf("Expected % x got % x", expected, v.data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataUint16(t *testing.T) {
|
||||
var in = uint16(16)
|
||||
var expected = []byte{0x0, 0x10}
|
||||
v := newHexData(in)
|
||||
if bytes.Compare(expected, v.data) != 0 {
|
||||
t.Errorf("Expected % x got % x", expected, v.data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataInt32(t *testing.T) {
|
||||
var in = int32(16)
|
||||
var expected = []byte{0x10}
|
||||
v := newHexData(in)
|
||||
if bytes.Compare(expected, v.data) != 0 {
|
||||
t.Errorf("Expected % x got % x", expected, v.data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexdataUint32(t *testing.T) {
|
||||
var in = uint32(16)
|
||||
var expected = []byte{0x0, 0x0, 0x0, 0x10}
|
||||
v := newHexData(in)
|
||||
if bytes.Compare(expected, v.data) != 0 {
|
||||
t.Errorf("Expected % x got % x", expected, v.data)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user