mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
add ParseTopicsIntoMap() tests
This commit is contained in:
parent
3c7aa59dc7
commit
9d81eaa8b2
1 changed files with 60 additions and 21 deletions
|
|
@ -56,7 +56,14 @@ func TestMakeTopics(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseTopics(t *testing.T) {
|
type args struct {
|
||||||
|
createObj func() interface{}
|
||||||
|
resultObj func() interface{}
|
||||||
|
resultMap func() map[string]interface{}
|
||||||
|
fields abi.Arguments
|
||||||
|
topics []common.Hash
|
||||||
|
}
|
||||||
|
|
||||||
type bytesStruct struct {
|
type bytesStruct struct {
|
||||||
StaticBytes [5]byte
|
StaticBytes [5]byte
|
||||||
}
|
}
|
||||||
|
|
@ -66,25 +73,27 @@ func TestParseTopics(t *testing.T) {
|
||||||
type int256Struct struct {
|
type int256Struct struct {
|
||||||
Int256Value *big.Int
|
Int256Value *big.Int
|
||||||
}
|
}
|
||||||
bytesType, _ := abi.NewType("bytes5", "", nil)
|
|
||||||
int8Type, _ := abi.NewType("int8", "", nil)
|
type topicTest struct {
|
||||||
int256Type, _ := abi.NewType("int256", "", nil)
|
|
||||||
type args struct {
|
|
||||||
createObj func() interface{}
|
|
||||||
resultObj func() interface{}
|
|
||||||
fields abi.Arguments
|
|
||||||
topics []common.Hash
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
name string
|
||||||
args args
|
args args
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}
|
||||||
|
|
||||||
|
func setupTopicsTests() []topicTest {
|
||||||
|
bytesType, _ := abi.NewType("bytes5", "", nil)
|
||||||
|
int8Type, _ := abi.NewType("int8", "", nil)
|
||||||
|
int256Type, _ := abi.NewType("int256", "", nil)
|
||||||
|
|
||||||
|
tests := []topicTest{
|
||||||
{
|
{
|
||||||
name: "support fixed byte types, right padded to 32 bytes",
|
name: "support fixed byte types, right padded to 32 bytes",
|
||||||
args: args{
|
args: args{
|
||||||
createObj: func() interface{} { return &bytesStruct{} },
|
createObj: func() interface{} { return &bytesStruct{} },
|
||||||
resultObj: func() interface{} { return &bytesStruct{StaticBytes: [5]byte{1, 2, 3, 4, 5}} },
|
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{
|
fields: abi.Arguments{abi.Argument{
|
||||||
Name: "staticBytes",
|
Name: "staticBytes",
|
||||||
Type: bytesType,
|
Type: bytesType,
|
||||||
|
|
@ -101,6 +110,9 @@ func TestParseTopics(t *testing.T) {
|
||||||
args: args{
|
args: args{
|
||||||
createObj: func() interface{} { return &int8Struct{} },
|
createObj: func() interface{} { return &int8Struct{} },
|
||||||
resultObj: func() interface{} { return &int8Struct{Int8Value: -1} },
|
resultObj: func() interface{} { return &int8Struct{Int8Value: -1} },
|
||||||
|
resultMap: func() map[string]interface{} {
|
||||||
|
return map[string]interface{}{"int8Value":int8(-1)}
|
||||||
|
},
|
||||||
fields: abi.Arguments{abi.Argument{
|
fields: abi.Arguments{abi.Argument{
|
||||||
Name: "int8Value",
|
Name: "int8Value",
|
||||||
Type: int8Type,
|
Type: int8Type,
|
||||||
|
|
@ -118,6 +130,9 @@ func TestParseTopics(t *testing.T) {
|
||||||
args: args{
|
args: args{
|
||||||
createObj: func() interface{} { return &int256Struct{} },
|
createObj: func() interface{} { return &int256Struct{} },
|
||||||
resultObj: func() interface{} { return &int256Struct{Int256Value: big.NewInt(-1)} },
|
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{
|
fields: abi.Arguments{abi.Argument{
|
||||||
Name: "int256Value",
|
Name: "int256Value",
|
||||||
Type: int256Type,
|
Type: int256Type,
|
||||||
|
|
@ -131,6 +146,13 @@ func TestParseTopics(t *testing.T) {
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return tests
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseTopics(t *testing.T) {
|
||||||
|
tests := setupTopicsTests()
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
createObj := tt.args.createObj()
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue