accounts/abi: add unpack into array test

This commit is contained in:
Robert Zaremba 2017-12-01 22:32:04 +01:00 committed by Guillaume Ballet
parent 882a32a5b7
commit 2aeb2555fa
5 changed files with 39 additions and 3 deletions

View file

@ -18,10 +18,10 @@ package abi
import ( import (
"bytes" "bytes"
"reflect"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"math/big" "math/big"
"reflect"
"strings" "strings"
"testing" "testing"
@ -178,6 +178,16 @@ func TestEventTupleUnpack(t *testing.T) {
jsonEventPledge, jsonEventPledge,
"", "",
"Can unpack Pledge event into slice", "Can unpack Pledge event into slice",
}, {
pledgeData1,
&[3]interface{}{&common.Address{}, &bigint, &[3]byte{}},
&[3]interface{}{
&addr,
&bigintExpected2,
&[3]byte{'u', 's', 'd'}},
jsonEventPledge,
"",
"Can unpack Pledge event into an array",
}, { }, {
pledgeData1, pledgeData1,
&[]interface{}{new(int), 0, 0}, &[]interface{}{new(int), 0, 0},

View file

@ -98,7 +98,7 @@ func (method Method) tupleUnpack(v interface{}, outputSlice []byte) error {
j := 0 j := 0
for i, output := range method.Outputs { for i, output := range method.Outputs {
marshalledValue, err := toGoType((i+j)*32, ouptut.Type, outputSlice) marshalledValue, err := toGoType((i+j)*32, output.Type, outputSlice)
if err != nil { if err != nil {
return err return err
} }

View file

@ -86,6 +86,27 @@ func set(dst, src reflect.Value, output Argument) error {
return nil return nil
} }
// requireUnpackKind verifies preconditions for unpacking `args` into `kind`
func requireUnpackKind(v reflect.Value, t reflect.Type, k reflect.Kind,
args []Argument, ignoreIndexed bool) error {
switch k {
case reflect.Struct:
case reflect.Slice, reflect.Array:
minLen := len(args)
if ignoreIndexed {
minLen = countNonIndexedArguments(args)
}
if v.Len() < minLen {
return fmt.Errorf("abi: insufficient number of elements in the list/array for unpack, want %d, got %d",
minLen, v.Len())
}
default:
return fmt.Errorf("abi: cannot unmarshal tuple into %v", t)
}
return nil
}
// requireAssignable assures that `dest` is a pointer and it's not an interface. // requireAssignable assures that `dest` is a pointer and it's not an interface.
func requireAssignable(dst, src reflect.Value) error { func requireAssignable(dst, src reflect.Value) error {
if dst.Kind() != reflect.Ptr && dst.Kind() != reflect.Interface { if dst.Kind() != reflect.Ptr && dst.Kind() != reflect.Interface {

View file

@ -336,6 +336,11 @@ func TestMethodMultiReturn(t *testing.T) {
&[]interface{}{&expected.Int, &expected.String}, &[]interface{}{&expected.Int, &expected.String},
"", "",
"Can unpack into a slice", "Can unpack into a slice",
}, {
&[2]interface{}{&bigint, new(string)},
&[2]interface{}{&expected.Int, &expected.String},
"",
"Can unpack into an array",
}, { }, {
&[]interface{}{new(int), new(int)}, &[]interface{}{new(int), new(int)},
&[]interface{}{&expected.Int, &expected.String}, &[]interface{}{&expected.Int, &expected.String},