@@ -22,6 +22,7 @@ import (
2222 "fmt"
2323 "math/big"
2424 "reflect"
25+ "strconv"
2526 "strings"
2627 "testing"
2728
@@ -261,25 +262,27 @@ var unpackTests = []unpackTest{
261262
262263func TestUnpack (t * testing.T ) {
263264 for i , test := range unpackTests {
264- def := fmt .Sprintf (`[{ "name" : "method", "outputs": %s}]` , test .def )
265- abi , err := JSON (strings .NewReader (def ))
266- if err != nil {
267- t .Fatalf ("invalid ABI definition %s: %v" , def , err )
268- }
269- encb , err := hex .DecodeString (test .enc )
270- if err != nil {
271- t .Fatalf ("invalid hex: %s" + test .enc )
272- }
273- outptr := reflect .New (reflect .TypeOf (test .want ))
274- err = abi .Unpack (outptr .Interface (), "method" , encb )
275- if err := test .checkError (err ); err != nil {
276- t .Errorf ("test %d (%v) failed: %v" , i , test .def , err )
277- continue
278- }
279- out := outptr .Elem ().Interface ()
280- if ! reflect .DeepEqual (test .want , out ) {
281- t .Errorf ("test %d (%v) failed: expected %v, got %v" , i , test .def , test .want , out )
282- }
265+ t .Run (strconv .Itoa (i ), func (t * testing.T ) {
266+ def := fmt .Sprintf (`[{ "name" : "method", "outputs": %s}]` , test .def )
267+ abi , err := JSON (strings .NewReader (def ))
268+ if err != nil {
269+ t .Fatalf ("invalid ABI definition %s: %v" , def , err )
270+ }
271+ encb , err := hex .DecodeString (test .enc )
272+ if err != nil {
273+ t .Fatalf ("invalid hex: %s" + test .enc )
274+ }
275+ outptr := reflect .New (reflect .TypeOf (test .want ))
276+ err = abi .Unpack (outptr .Interface (), "method" , encb )
277+ if err := test .checkError (err ); err != nil {
278+ t .Errorf ("test %d (%v) failed: %v" , i , test .def , err )
279+ return
280+ }
281+ out := outptr .Elem ().Interface ()
282+ if ! reflect .DeepEqual (test .want , out ) {
283+ t .Errorf ("test %d (%v) failed: expected %v, got %v" , i , test .def , test .want , out )
284+ }
285+ })
283286 }
284287}
285288
@@ -336,6 +339,29 @@ func TestMultiReturnWithStruct(t *testing.T) {
336339 }
337340}
338341
342+ func TestMultiReturnWithArray (t * testing.T ) {
343+ const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
344+ abi , err := JSON (strings .NewReader (definition ))
345+ if err != nil {
346+ t .Fatal (err )
347+ }
348+ buff := new (bytes.Buffer )
349+ buff .Write (common .Hex2Bytes ("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009" ))
350+ buff .Write (common .Hex2Bytes ("0000000000000000000000000000000000000000000000000000000000000008" ))
351+
352+ ret1 , ret1Exp := new ([3 ]uint64 ), [3 ]uint64 {9 , 9 , 9 }
353+ ret2 , ret2Exp := new (uint64 ), uint64 (8 )
354+ if err := abi .Unpack (& []interface {}{ret1 , ret2 }, "multi" , buff .Bytes ()); err != nil {
355+ t .Fatal (err )
356+ }
357+ if ! reflect .DeepEqual (* ret1 , ret1Exp ) {
358+ t .Error ("array result" , * ret1 , "!= Expected" , ret1Exp )
359+ }
360+ if * ret2 != ret2Exp {
361+ t .Error ("int result" , * ret2 , "!= Expected" , ret2Exp )
362+ }
363+ }
364+
339365func TestUnmarshal (t * testing.T ) {
340366 const definition = `[
341367 { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
0 commit comments