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 (
"bytes"
"reflect"
"encoding/hex"
"encoding/json"
"math/big"
"reflect"
"strings"
"testing"
@ -178,6 +178,16 @@ func TestEventTupleUnpack(t *testing.T) {
jsonEventPledge,
"",
"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,
&[]interface{}{new(int), 0, 0},

View file

@ -98,7 +98,7 @@ func (method Method) tupleUnpack(v interface{}, outputSlice []byte) error {
j := 0
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 {
return err
}

View file

@ -86,6 +86,27 @@ func set(dst, src reflect.Value, output Argument) error {
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.
func requireAssignable(dst, src reflect.Value) error {
if dst.Kind() != reflect.Ptr && dst.Kind() != reflect.Interface {

View file

@ -202,4 +202,4 @@ func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err
//fmt.Printf("LENGTH PREFIX INFO: \nsize: %v\noffset: %v\nstart: %v\n", length, offset, start)
return
}
}

View file

@ -336,6 +336,11 @@ func TestMethodMultiReturn(t *testing.T) {
&[]interface{}{&expected.Int, &expected.String},
"",
"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{}{&expected.Int, &expected.String},