@@ -22,6 +22,7 @@ import (
22
22
"fmt"
23
23
"math/big"
24
24
"reflect"
25
+ "strconv"
25
26
"strings"
26
27
"testing"
27
28
@@ -261,25 +262,27 @@ var unpackTests = []unpackTest{
261
262
262
263
func TestUnpack (t * testing.T ) {
263
264
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
+ })
283
286
}
284
287
}
285
288
@@ -336,6 +339,29 @@ func TestMultiReturnWithStruct(t *testing.T) {
336
339
}
337
340
}
338
341
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
+
339
365
func TestUnmarshal (t * testing.T ) {
340
366
const definition = `[
341
367
{ "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
0 commit comments