From 9d81eaa8b2c919f0956a386f4d3658591169045e Mon Sep 17 00:00:00 2001 From: Jeff Wentworth Date: Tue, 12 Nov 2019 20:58:12 +0900 Subject: [PATCH] add ParseTopicsIntoMap() tests --- accounts/abi/bind/topics_test.go | 81 +++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 21 deletions(-) diff --git a/accounts/abi/bind/topics_test.go b/accounts/abi/bind/topics_test.go index f178574381..07eb73828f 100644 --- a/accounts/abi/bind/topics_test.go +++ b/accounts/abi/bind/topics_test.go @@ -56,35 +56,44 @@ func TestMakeTopics(t *testing.T) { } } -func TestParseTopics(t *testing.T) { - type bytesStruct struct { - StaticBytes [5]byte - } - type int8Struct struct { - Int8Value int8 - } - type int256Struct struct { - Int256Value *big.Int - } +type args struct { + createObj func() interface{} + resultObj func() interface{} + resultMap func() map[string]interface{} + fields abi.Arguments + topics []common.Hash +} + +type bytesStruct struct { + StaticBytes [5]byte +} +type int8Struct struct { + Int8Value int8 +} +type int256Struct struct { + Int256Value *big.Int +} + +type topicTest struct { + name string + args args + wantErr bool +} + +func setupTopicsTests() []topicTest { bytesType, _ := abi.NewType("bytes5", "", nil) int8Type, _ := abi.NewType("int8", "", nil) int256Type, _ := abi.NewType("int256", "", nil) - type args struct { - createObj func() interface{} - resultObj func() interface{} - fields abi.Arguments - topics []common.Hash - } - tests := []struct { - name string - args args - wantErr bool - }{ + + tests := []topicTest{ { name: "support fixed byte types, right padded to 32 bytes", args: args{ createObj: func() interface{} { return &bytesStruct{} }, resultObj: func() interface{} { return &bytesStruct{StaticBytes: [5]byte{1, 2, 3, 4, 5}} }, + resultMap: func() map[string]interface{} { + return map[string]interface{}{"staticBytes":[5]byte{1, 2, 3, 4, 5}} + }, fields: abi.Arguments{abi.Argument{ Name: "staticBytes", Type: bytesType, @@ -101,6 +110,9 @@ func TestParseTopics(t *testing.T) { args: args{ createObj: func() interface{} { return &int8Struct{} }, resultObj: func() interface{} { return &int8Struct{Int8Value: -1} }, + resultMap: func() map[string]interface{} { + return map[string]interface{}{"int8Value":int8(-1)} + }, fields: abi.Arguments{abi.Argument{ Name: "int8Value", Type: int8Type, @@ -118,6 +130,9 @@ func TestParseTopics(t *testing.T) { args: args{ createObj: func() interface{} { return &int256Struct{} }, resultObj: func() interface{} { return &int256Struct{Int256Value: big.NewInt(-1)} }, + resultMap: func() map[string]interface{} { + return map[string]interface{}{"int256Value":big.NewInt(-1)} + }, fields: abi.Arguments{abi.Argument{ Name: "int256Value", Type: int256Type, @@ -131,6 +146,13 @@ func TestParseTopics(t *testing.T) { wantErr: false, }, } + + return tests +} + +func TestParseTopics(t *testing.T) { + tests := setupTopicsTests() + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { createObj := tt.args.createObj() @@ -144,3 +166,20 @@ func TestParseTopics(t *testing.T) { }) } } + +func TestParseTopicsIntoMap(t *testing.T) { + tests := setupTopicsTests() + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + outMap := make(map[string]interface{}) + if err := parseTopicsIntoMap(outMap, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr { + t.Errorf("parseTopicsIntoMap() error = %v, wantErr %v", err, tt.wantErr) + } + resultMap := tt.args.resultMap() + if !reflect.DeepEqual(outMap, resultMap) { + t.Errorf("parseTopicsIntoMap() = %v, want %v", outMap, resultMap) + } + }) + } +}