github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/accounts/abi/type_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:26</date> 10 //</624342584088596480> 11 12 13 package abi 14 15 import ( 16 "math/big" 17 "reflect" 18 "testing" 19 20 "github.com/davecgh/go-spew/spew" 21 "github.com/ethereum/go-ethereum/common" 22 ) 23 24 //typewithoutStringer是类型类型类型的别名,该类型只是不实现 25 //Stringer接口允许在下面的测试中打印类型详细信息。 26 type typeWithoutStringer Type 27 28 //测试类型分析器识别所有允许的类型。 29 func TestTypeRegexp(t *testing.T) { 30 tests := []struct { 31 blob string 32 kind Type 33 }{ 34 {"bool", Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}}, 35 {"bool[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool(nil)), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}}, 36 {"bool[2]", Type{Size: 2, Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}}, 37 {"bool[2][]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}}, 38 {"bool[][]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}}, 39 {"bool[][2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}}, 40 {"bool[2][2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}}, 41 {"bool[2][][2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][][2]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}, stringKind: "bool[2][][2]"}}, 42 {"bool[2][2][2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}, stringKind: "bool[2][2][2]"}}, 43 {"bool[][][]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}, stringKind: "bool[][][]"}}, 44 {"bool[][2][]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][2][]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}, stringKind: "bool[][2][]"}}, 45 {"int8", Type{Kind: reflect.Int8, Type: int8T, Size: 8, T: IntTy, stringKind: "int8"}}, 46 {"int16", Type{Kind: reflect.Int16, Type: int16T, Size: 16, T: IntTy, stringKind: "int16"}}, 47 {"int32", Type{Kind: reflect.Int32, Type: int32T, Size: 32, T: IntTy, stringKind: "int32"}}, 48 {"int64", Type{Kind: reflect.Int64, Type: int64T, Size: 64, T: IntTy, stringKind: "int64"}}, 49 {"int256", Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: IntTy, stringKind: "int256"}}, 50 {"int8[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int8{}), Elem: &Type{Kind: reflect.Int8, Type: int8T, Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[]"}}, 51 {"int8[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int8{}), Elem: &Type{Kind: reflect.Int8, Type: int8T, Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[2]"}}, 52 {"int16[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int16{}), Elem: &Type{Kind: reflect.Int16, Type: int16T, Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[]"}}, 53 {"int16[2]", Type{Size: 2, Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]int16{}), Elem: &Type{Kind: reflect.Int16, Type: int16T, Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[2]"}}, 54 {"int32[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int32{}), Elem: &Type{Kind: reflect.Int32, Type: int32T, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[]"}}, 55 {"int32[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int32{}), Elem: &Type{Kind: reflect.Int32, Type: int32T, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[2]"}}, 56 {"int64[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int64{}), Elem: &Type{Kind: reflect.Int64, Type: int64T, Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[]"}}, 57 {"int64[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int64{}), Elem: &Type{Kind: reflect.Int64, Type: int64T, Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[2]"}}, 58 {"int256[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[]"}}, 59 {"int256[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[2]"}}, 60 {"uint8", Type{Kind: reflect.Uint8, Type: uint8T, Size: 8, T: UintTy, stringKind: "uint8"}}, 61 {"uint16", Type{Kind: reflect.Uint16, Type: uint16T, Size: 16, T: UintTy, stringKind: "uint16"}}, 62 {"uint32", Type{Kind: reflect.Uint32, Type: uint32T, Size: 32, T: UintTy, stringKind: "uint32"}}, 63 {"uint64", Type{Kind: reflect.Uint64, Type: uint64T, Size: 64, T: UintTy, stringKind: "uint64"}}, 64 {"uint256", Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: UintTy, stringKind: "uint256"}}, 65 {"uint8[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]uint8{}), Elem: &Type{Kind: reflect.Uint8, Type: uint8T, Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[]"}}, 66 {"uint8[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint8{}), Elem: &Type{Kind: reflect.Uint8, Type: uint8T, Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[2]"}}, 67 {"uint16[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint16{}), Elem: &Type{Kind: reflect.Uint16, Type: uint16T, Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[]"}}, 68 {"uint16[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint16{}), Elem: &Type{Kind: reflect.Uint16, Type: uint16T, Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[2]"}}, 69 {"uint32[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint32{}), Elem: &Type{Kind: reflect.Uint32, Type: uint32T, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[]"}}, 70 {"uint32[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint32{}), Elem: &Type{Kind: reflect.Uint32, Type: uint32T, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[2]"}}, 71 {"uint64[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint64{}), Elem: &Type{Kind: reflect.Uint64, Type: uint64T, Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[]"}}, 72 {"uint64[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint64{}), Elem: &Type{Kind: reflect.Uint64, Type: uint64T, Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[2]"}}, 73 {"uint256[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[]"}}, 74 {"uint256[2]", Type{Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]*big.Int{}), Size: 2, Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[2]"}}, 75 {"bytes32", Type{Kind: reflect.Array, T: FixedBytesTy, Size: 32, Type: reflect.TypeOf([32]byte{}), stringKind: "bytes32"}}, 76 {"bytes[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][]byte{}), Elem: &Type{Kind: reflect.Slice, Type: reflect.TypeOf([]byte{}), T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[]"}}, 77 {"bytes[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]byte{}), Elem: &Type{T: BytesTy, Type: reflect.TypeOf([]byte{}), Kind: reflect.Slice, stringKind: "bytes"}, stringKind: "bytes[2]"}}, 78 {"bytes32[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][32]byte{}), Elem: &Type{Kind: reflect.Array, Type: reflect.TypeOf([32]byte{}), T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[]"}}, 79 {"bytes32[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][32]byte{}), Elem: &Type{Kind: reflect.Array, T: FixedBytesTy, Size: 32, Type: reflect.TypeOf([32]byte{}), stringKind: "bytes32"}, stringKind: "bytes32[2]"}}, 80 {"string", Type{Kind: reflect.String, T: StringTy, Type: reflect.TypeOf(""), stringKind: "string"}}, 81 {"string[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]string{}), Elem: &Type{Kind: reflect.String, Type: reflect.TypeOf(""), T: StringTy, stringKind: "string"}, stringKind: "string[]"}}, 82 {"string[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]string{}), Elem: &Type{Kind: reflect.String, T: StringTy, Type: reflect.TypeOf(""), stringKind: "string"}, stringKind: "string[2]"}}, 83 {"address", Type{Kind: reflect.Array, Type: addressT, Size: 20, T: AddressTy, stringKind: "address"}}, 84 {"address[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]common.Address{}), Elem: &Type{Kind: reflect.Array, Type: addressT, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[]"}}, 85 {"address[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]common.Address{}), Elem: &Type{Kind: reflect.Array, Type: addressT, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[2]"}}, 86 //正确实现固定类型时的TODO 87 //“固定”,类型, 88 //“Fixed128x128”,类型, 89 //“Fixed[]”,类型, 90 //“Fixed[2]”,类型, 91 //“Fixed128x128[]”,类型, 92 //“Fixed128x128[2]”,类型, 93 } 94 95 for _, tt := range tests { 96 typ, err := NewType(tt.blob) 97 if err != nil { 98 t.Errorf("type %q: failed to parse type string: %v", tt.blob, err) 99 } 100 if !reflect.DeepEqual(typ, tt.kind) { 101 t.Errorf("type %q: parsed type mismatch:\nGOT %s\nWANT %s ", tt.blob, spew.Sdump(typeWithoutStringer(typ)), spew.Sdump(typeWithoutStringer(tt.kind))) 102 } 103 } 104 } 105 106 func TestTypeCheck(t *testing.T) { 107 for i, test := range []struct { 108 typ string 109 input interface{} 110 err string 111 }{ 112 {"uint", big.NewInt(1), "unsupported arg type: uint"}, 113 {"int", big.NewInt(1), "unsupported arg type: int"}, 114 {"uint256", big.NewInt(1), ""}, 115 {"uint256[][3][]", [][3][]*big.Int{{{}}}, ""}, 116 {"uint256[][][3]", [3][][]*big.Int{{{}}}, ""}, 117 {"uint256[3][][]", [][][3]*big.Int{{{}}}, ""}, 118 {"uint256[3][3][3]", [3][3][3]*big.Int{{{}}}, ""}, 119 {"uint8[][]", [][]uint8{}, ""}, 120 {"int256", big.NewInt(1), ""}, 121 {"uint8", uint8(1), ""}, 122 {"uint16", uint16(1), ""}, 123 {"uint32", uint32(1), ""}, 124 {"uint64", uint64(1), ""}, 125 {"int8", int8(1), ""}, 126 {"int16", int16(1), ""}, 127 {"int32", int32(1), ""}, 128 {"int64", int64(1), ""}, 129 {"uint24", big.NewInt(1), ""}, 130 {"uint40", big.NewInt(1), ""}, 131 {"uint48", big.NewInt(1), ""}, 132 {"uint56", big.NewInt(1), ""}, 133 {"uint72", big.NewInt(1), ""}, 134 {"uint80", big.NewInt(1), ""}, 135 {"uint88", big.NewInt(1), ""}, 136 {"uint96", big.NewInt(1), ""}, 137 {"uint104", big.NewInt(1), ""}, 138 {"uint112", big.NewInt(1), ""}, 139 {"uint120", big.NewInt(1), ""}, 140 {"uint128", big.NewInt(1), ""}, 141 {"uint136", big.NewInt(1), ""}, 142 {"uint144", big.NewInt(1), ""}, 143 {"uint152", big.NewInt(1), ""}, 144 {"uint160", big.NewInt(1), ""}, 145 {"uint168", big.NewInt(1), ""}, 146 {"uint176", big.NewInt(1), ""}, 147 {"uint184", big.NewInt(1), ""}, 148 {"uint192", big.NewInt(1), ""}, 149 {"uint200", big.NewInt(1), ""}, 150 {"uint208", big.NewInt(1), ""}, 151 {"uint216", big.NewInt(1), ""}, 152 {"uint224", big.NewInt(1), ""}, 153 {"uint232", big.NewInt(1), ""}, 154 {"uint240", big.NewInt(1), ""}, 155 {"uint248", big.NewInt(1), ""}, 156 {"int24", big.NewInt(1), ""}, 157 {"int40", big.NewInt(1), ""}, 158 {"int48", big.NewInt(1), ""}, 159 {"int56", big.NewInt(1), ""}, 160 {"int72", big.NewInt(1), ""}, 161 {"int80", big.NewInt(1), ""}, 162 {"int88", big.NewInt(1), ""}, 163 {"int96", big.NewInt(1), ""}, 164 {"int104", big.NewInt(1), ""}, 165 {"int112", big.NewInt(1), ""}, 166 {"int120", big.NewInt(1), ""}, 167 {"int128", big.NewInt(1), ""}, 168 {"int136", big.NewInt(1), ""}, 169 {"int144", big.NewInt(1), ""}, 170 {"int152", big.NewInt(1), ""}, 171 {"int160", big.NewInt(1), ""}, 172 {"int168", big.NewInt(1), ""}, 173 {"int176", big.NewInt(1), ""}, 174 {"int184", big.NewInt(1), ""}, 175 {"int192", big.NewInt(1), ""}, 176 {"int200", big.NewInt(1), ""}, 177 {"int208", big.NewInt(1), ""}, 178 {"int216", big.NewInt(1), ""}, 179 {"int224", big.NewInt(1), ""}, 180 {"int232", big.NewInt(1), ""}, 181 {"int240", big.NewInt(1), ""}, 182 {"int248", big.NewInt(1), ""}, 183 {"uint30", uint8(1), "abi: cannot use uint8 as type ptr as argument"}, 184 {"uint8", uint16(1), "abi: cannot use uint16 as type uint8 as argument"}, 185 {"uint8", uint32(1), "abi: cannot use uint32 as type uint8 as argument"}, 186 {"uint8", uint64(1), "abi: cannot use uint64 as type uint8 as argument"}, 187 {"uint8", int8(1), "abi: cannot use int8 as type uint8 as argument"}, 188 {"uint8", int16(1), "abi: cannot use int16 as type uint8 as argument"}, 189 {"uint8", int32(1), "abi: cannot use int32 as type uint8 as argument"}, 190 {"uint8", int64(1), "abi: cannot use int64 as type uint8 as argument"}, 191 {"uint16", uint16(1), ""}, 192 {"uint16", uint8(1), "abi: cannot use uint8 as type uint16 as argument"}, 193 {"uint16[]", []uint16{1, 2, 3}, ""}, 194 {"uint16[]", [3]uint16{1, 2, 3}, ""}, 195 {"uint16[]", []uint32{1, 2, 3}, "abi: cannot use []uint32 as type [0]uint16 as argument"}, 196 {"uint16[3]", [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"}, 197 {"uint16[3]", [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, 198 {"uint16[3]", []uint16{1, 2, 3}, ""}, 199 {"uint16[3]", []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, 200 {"address[]", []common.Address{{1}}, ""}, 201 {"address[1]", []common.Address{{1}}, ""}, 202 {"address[1]", [1]common.Address{{1}}, ""}, 203 {"address[2]", [1]common.Address{{1}}, "abi: cannot use [1]array as type [2]array as argument"}, 204 {"bytes32", [32]byte{}, ""}, 205 {"bytes31", [31]byte{}, ""}, 206 {"bytes30", [30]byte{}, ""}, 207 {"bytes29", [29]byte{}, ""}, 208 {"bytes28", [28]byte{}, ""}, 209 {"bytes27", [27]byte{}, ""}, 210 {"bytes26", [26]byte{}, ""}, 211 {"bytes25", [25]byte{}, ""}, 212 {"bytes24", [24]byte{}, ""}, 213 {"bytes23", [23]byte{}, ""}, 214 {"bytes22", [22]byte{}, ""}, 215 {"bytes21", [21]byte{}, ""}, 216 {"bytes20", [20]byte{}, ""}, 217 {"bytes19", [19]byte{}, ""}, 218 {"bytes18", [18]byte{}, ""}, 219 {"bytes17", [17]byte{}, ""}, 220 {"bytes16", [16]byte{}, ""}, 221 {"bytes15", [15]byte{}, ""}, 222 {"bytes14", [14]byte{}, ""}, 223 {"bytes13", [13]byte{}, ""}, 224 {"bytes12", [12]byte{}, ""}, 225 {"bytes11", [11]byte{}, ""}, 226 {"bytes10", [10]byte{}, ""}, 227 {"bytes9", [9]byte{}, ""}, 228 {"bytes8", [8]byte{}, ""}, 229 {"bytes7", [7]byte{}, ""}, 230 {"bytes6", [6]byte{}, ""}, 231 {"bytes5", [5]byte{}, ""}, 232 {"bytes4", [4]byte{}, ""}, 233 {"bytes3", [3]byte{}, ""}, 234 {"bytes2", [2]byte{}, ""}, 235 {"bytes1", [1]byte{}, ""}, 236 {"bytes32", [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"}, 237 {"bytes32", common.Hash{1}, ""}, 238 {"bytes31", common.Hash{1}, "abi: cannot use common.Hash as type [31]uint8 as argument"}, 239 {"bytes31", [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"}, 240 {"bytes", []byte{0, 1}, ""}, 241 {"bytes", [2]byte{0, 1}, "abi: cannot use array as type slice as argument"}, 242 {"bytes", common.Hash{1}, "abi: cannot use array as type slice as argument"}, 243 {"string", "hello world", ""}, 244 {"string", string(""), ""}, 245 {"string", []byte{}, "abi: cannot use slice as type string as argument"}, 246 {"bytes32[]", [][32]byte{{}}, ""}, 247 {"function", [24]byte{}, ""}, 248 {"bytes20", common.Address{}, ""}, 249 {"address", [20]byte{}, ""}, 250 {"address", common.Address{}, ""}, 251 {"bytes32[]]", "", "invalid arg type in abi"}, 252 {"invalidType", "", "unsupported arg type: invalidType"}, 253 {"invalidSlice[]", "", "unsupported arg type: invalidSlice"}, 254 } { 255 typ, err := NewType(test.typ) 256 if err != nil && len(test.err) == 0 { 257 t.Fatal("unexpected parse error:", err) 258 } else if err != nil && len(test.err) != 0 { 259 if err.Error() != test.err { 260 t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) 261 } 262 continue 263 } 264 265 err = typeCheck(typ, reflect.ValueOf(test.input)) 266 if err != nil && len(test.err) == 0 { 267 t.Errorf("%d failed. Expected no err but got: %v", i, err) 268 continue 269 } 270 if err == nil && len(test.err) != 0 { 271 t.Errorf("%d failed. Expected err: %v but got none", i, test.err) 272 continue 273 } 274 275 if err != nil && len(test.err) != 0 && err.Error() != test.err { 276 t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) 277 } 278 } 279 } 280