@@ -27,6 +27,7 @@ import (
2727 "testing"
2828
2929 "github.com/ethereum/go-ethereum/common"
30+ "github.com/stretchr/testify/require"
3031)
3132
3233type unpackTest struct {
@@ -286,56 +287,78 @@ func TestUnpack(t *testing.T) {
286287 }
287288}
288289
289- func TestMultiReturnWithStruct (t * testing.T ) {
290+ type methodMultiOutput struct {
291+ Int * big.Int
292+ String string
293+ }
294+
295+ func methodMultiReturn (require * require.Assertions ) (ABI , []byte , methodMultiOutput ) {
290296 const definition = `[
291297 { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
298+ var expected = methodMultiOutput {big .NewInt (1 ), "hello" }
292299
293300 abi , err := JSON (strings .NewReader (definition ))
294- if err != nil {
295- t .Fatal (err )
296- }
297-
301+ require .NoError (err )
298302 // using buff to make the code readable
299303 buff := new (bytes.Buffer )
300304 buff .Write (common .Hex2Bytes ("0000000000000000000000000000000000000000000000000000000000000001" ))
301305 buff .Write (common .Hex2Bytes ("0000000000000000000000000000000000000000000000000000000000000040" ))
302306 buff .Write (common .Hex2Bytes ("0000000000000000000000000000000000000000000000000000000000000005" ))
303- stringOut := "hello"
304- buff .Write (common .RightPadBytes ([]byte (stringOut ), 32 ))
305-
306- var inter struct {
307- Int * big.Int
308- String string
309- }
310- err = abi .Unpack (& inter , "multi" , buff .Bytes ())
311- if err != nil {
312- t .Error (err )
313- }
314-
315- if inter .Int == nil || inter .Int .Cmp (big .NewInt (1 )) != 0 {
316- t .Error ("expected Int to be 1 got" , inter .Int )
317- }
318-
319- if inter .String != stringOut {
320- t .Error ("expected String to be" , stringOut , "got" , inter .String )
321- }
307+ buff .Write (common .RightPadBytes ([]byte (expected .String ), 32 ))
308+ return abi , buff .Bytes (), expected
309+ }
322310
323- var reversed struct {
311+ func TestMethodMultiReturn (t * testing.T ) {
312+ type reversed struct {
324313 String string
325314 Int * big.Int
326315 }
327316
328- err = abi .Unpack (& reversed , "multi" , buff .Bytes ())
329- if err != nil {
330- t .Error (err )
331- }
332-
333- if reversed .Int == nil || reversed .Int .Cmp (big .NewInt (1 )) != 0 {
334- t .Error ("expected Int to be 1 got" , reversed .Int )
335- }
336-
337- if reversed .String != stringOut {
338- t .Error ("expected String to be" , stringOut , "got" , reversed .String )
317+ abi , data , expected := methodMultiReturn (require .New (t ))
318+ bigint := new (big.Int )
319+ var testCases = []struct {
320+ dest interface {}
321+ expected interface {}
322+ error string
323+ name string
324+ }{{
325+ & methodMultiOutput {},
326+ & expected ,
327+ "" ,
328+ "Can unpack into structure" ,
329+ }, {
330+ & reversed {},
331+ & reversed {expected .String , expected .Int },
332+ "" ,
333+ "Can unpack into reversed structure" ,
334+ }, {
335+ & []interface {}{& bigint , new (string )},
336+ & []interface {}{& expected .Int , & expected .String },
337+ "" ,
338+ "Can unpack into a slice" ,
339+ }, {
340+ & []interface {}{new (int ), new (int )},
341+ & []interface {}{& expected .Int , & expected .String },
342+ "abi: cannot unmarshal *big.Int in to int" ,
343+ "Can not unpack into a slice with wrong types" ,
344+ }, {
345+ & []interface {}{new (int )},
346+ & []interface {}{},
347+ "abi: insufficient number of elements in the list/array for unpack, want 2, got 1" ,
348+ "Can not unpack into a slice with wrong types" ,
349+ }}
350+ for _ , tc := range testCases {
351+ tc := tc
352+ t .Run (tc .name , func (t * testing.T ) {
353+ require := require .New (t )
354+ err := abi .Unpack (tc .dest , "multi" , data )
355+ if tc .error == "" {
356+ require .Nil (err , "Should be able to unpack method outputs." )
357+ require .Equal (tc .expected , tc .dest )
358+ } else {
359+ require .EqualError (err , tc .error )
360+ }
361+ })
339362 }
340363}
341364
0 commit comments