github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/accounts/abi/bind/topics_test.go (about) 1 // Copyright 2019 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package bind 18 19 import ( 20 "math/big" 21 "reflect" 22 "testing" 23 24 "github.com/intfoundation/intchain/accounts/abi" 25 "github.com/intfoundation/intchain/common" 26 ) 27 28 func TestMakeTopics(t *testing.T) { 29 type args struct { 30 query [][]interface{} 31 } 32 tests := []struct { 33 name string 34 args args 35 want [][]common.Hash 36 wantErr bool 37 }{ 38 { 39 "support fixed byte types, right padded to 32 bytes", 40 args{[][]interface{}{{[5]byte{1, 2, 3, 4, 5}}}}, 41 [][]common.Hash{{common.Hash{1, 2, 3, 4, 5}}}, 42 false, 43 }, 44 } 45 for _, tt := range tests { 46 t.Run(tt.name, func(t *testing.T) { 47 got, err := makeTopics(tt.args.query...) 48 if (err != nil) != tt.wantErr { 49 t.Errorf("makeTopics() error = %v, wantErr %v", err, tt.wantErr) 50 return 51 } 52 if !reflect.DeepEqual(got, tt.want) { 53 t.Errorf("makeTopics() = %v, want %v", got, tt.want) 54 } 55 }) 56 } 57 } 58 59 type args struct { 60 createObj func() interface{} 61 resultObj func() interface{} 62 resultMap func() map[string]interface{} 63 fields abi.Arguments 64 topics []common.Hash 65 } 66 67 type bytesStruct struct { 68 StaticBytes [5]byte 69 } 70 type int8Struct struct { 71 Int8Value int8 72 } 73 type int256Struct struct { 74 Int256Value *big.Int 75 } 76 77 type topicTest struct { 78 name string 79 args args 80 wantErr bool 81 } 82 83 func setupTopicsTests() []topicTest { 84 bytesType, _ := abi.NewType("bytes5", "", nil) 85 int8Type, _ := abi.NewType("int8", "", nil) 86 int256Type, _ := abi.NewType("int256", "", nil) 87 88 tests := []topicTest{ 89 { 90 name: "support fixed byte types, right padded to 32 bytes", 91 args: args{ 92 createObj: func() interface{} { return &bytesStruct{} }, 93 resultObj: func() interface{} { return &bytesStruct{StaticBytes: [5]byte{1, 2, 3, 4, 5}} }, 94 resultMap: func() map[string]interface{} { 95 return map[string]interface{}{"staticBytes": [5]byte{1, 2, 3, 4, 5}} 96 }, 97 fields: abi.Arguments{abi.Argument{ 98 Name: "staticBytes", 99 Type: bytesType, 100 Indexed: true, 101 }}, 102 topics: []common.Hash{ 103 {1, 2, 3, 4, 5}, 104 }, 105 }, 106 wantErr: false, 107 }, 108 { 109 name: "int8 with negative value", 110 args: args{ 111 createObj: func() interface{} { return &int8Struct{} }, 112 resultObj: func() interface{} { return &int8Struct{Int8Value: -1} }, 113 resultMap: func() map[string]interface{} { 114 return map[string]interface{}{"int8Value": int8(-1)} 115 }, 116 fields: abi.Arguments{abi.Argument{ 117 Name: "int8Value", 118 Type: int8Type, 119 Indexed: true, 120 }}, 121 topics: []common.Hash{ 122 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 123 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 124 }, 125 }, 126 wantErr: false, 127 }, 128 { 129 name: "int256 with negative value", 130 args: args{ 131 createObj: func() interface{} { return &int256Struct{} }, 132 resultObj: func() interface{} { return &int256Struct{Int256Value: big.NewInt(-1)} }, 133 resultMap: func() map[string]interface{} { 134 return map[string]interface{}{"int256Value": big.NewInt(-1)} 135 }, 136 fields: abi.Arguments{abi.Argument{ 137 Name: "int256Value", 138 Type: int256Type, 139 Indexed: true, 140 }}, 141 topics: []common.Hash{ 142 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 143 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 144 }, 145 }, 146 wantErr: false, 147 }, 148 } 149 150 return tests 151 } 152 153 func TestParseTopics(t *testing.T) { 154 tests := setupTopicsTests() 155 156 for _, tt := range tests { 157 t.Run(tt.name, func(t *testing.T) { 158 createObj := tt.args.createObj() 159 if err := parseTopics(createObj, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr { 160 t.Errorf("parseTopics() error = %v, wantErr %v", err, tt.wantErr) 161 } 162 resultObj := tt.args.resultObj() 163 if !reflect.DeepEqual(createObj, resultObj) { 164 t.Errorf("parseTopics() = %v, want %v", createObj, resultObj) 165 } 166 }) 167 } 168 } 169 170 func TestParseTopicsIntoMap(t *testing.T) { 171 tests := setupTopicsTests() 172 173 for _, tt := range tests { 174 t.Run(tt.name, func(t *testing.T) { 175 outMap := make(map[string]interface{}) 176 if err := parseTopicsIntoMap(outMap, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr { 177 t.Errorf("parseTopicsIntoMap() error = %v, wantErr %v", err, tt.wantErr) 178 } 179 resultMap := tt.args.resultMap() 180 if !reflect.DeepEqual(outMap, resultMap) { 181 t.Errorf("parseTopicsIntoMap() = %v, want %v", outMap, resultMap) 182 } 183 }) 184 } 185 }