github.com/ethereum/go-ethereum@v1.14.3/accounts/abi/selector_parser_test.go (about) 1 // Copyright 2022 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 abi 18 19 import ( 20 "fmt" 21 "log" 22 "reflect" 23 "testing" 24 ) 25 26 func TestParseSelector(t *testing.T) { 27 t.Parallel() 28 mkType := func(types ...interface{}) []ArgumentMarshaling { 29 var result []ArgumentMarshaling 30 for i, typeOrComponents := range types { 31 name := fmt.Sprintf("name%d", i) 32 if typeName, ok := typeOrComponents.(string); ok { 33 result = append(result, ArgumentMarshaling{name, typeName, typeName, nil, false}) 34 } else if components, ok := typeOrComponents.([]ArgumentMarshaling); ok { 35 result = append(result, ArgumentMarshaling{name, "tuple", "tuple", components, false}) 36 } else if components, ok := typeOrComponents.([][]ArgumentMarshaling); ok { 37 result = append(result, ArgumentMarshaling{name, "tuple[]", "tuple[]", components[0], false}) 38 } else { 39 log.Fatalf("unexpected type %T", typeOrComponents) 40 } 41 } 42 return result 43 } 44 tests := []struct { 45 input string 46 name string 47 args []ArgumentMarshaling 48 }{ 49 {"noargs()", "noargs", []ArgumentMarshaling{}}, 50 {"simple(uint256,uint256,uint256)", "simple", mkType("uint256", "uint256", "uint256")}, 51 {"other(uint256,address)", "other", mkType("uint256", "address")}, 52 {"withArray(uint256[],address[2],uint8[4][][5])", "withArray", mkType("uint256[]", "address[2]", "uint8[4][][5]")}, 53 {"singleNest(bytes32,uint8,(uint256,uint256),address)", "singleNest", mkType("bytes32", "uint8", mkType("uint256", "uint256"), "address")}, 54 {"multiNest(address,(uint256[],uint256),((address,bytes32),uint256))", "multiNest", 55 mkType("address", mkType("uint256[]", "uint256"), mkType(mkType("address", "bytes32"), "uint256"))}, 56 {"arrayNest((uint256,uint256)[],bytes32)", "arrayNest", mkType([][]ArgumentMarshaling{mkType("uint256", "uint256")}, "bytes32")}, 57 {"multiArrayNest((uint256,uint256)[],(uint256,uint256)[])", "multiArrayNest", 58 mkType([][]ArgumentMarshaling{mkType("uint256", "uint256")}, [][]ArgumentMarshaling{mkType("uint256", "uint256")})}, 59 {"singleArrayNestAndArray((uint256,uint256)[],bytes32[])", "singleArrayNestAndArray", 60 mkType([][]ArgumentMarshaling{mkType("uint256", "uint256")}, "bytes32[]")}, 61 {"singleArrayNestWithArrayAndArray((uint256[],address[2],uint8[4][][5])[],bytes32[])", "singleArrayNestWithArrayAndArray", 62 mkType([][]ArgumentMarshaling{mkType("uint256[]", "address[2]", "uint8[4][][5]")}, "bytes32[]")}, 63 } 64 for i, tt := range tests { 65 selector, err := ParseSelector(tt.input) 66 if err != nil { 67 t.Errorf("test %d: failed to parse selector '%v': %v", i, tt.input, err) 68 } 69 if selector.Name != tt.name { 70 t.Errorf("test %d: unexpected function name: '%s' != '%s'", i, selector.Name, tt.name) 71 } 72 73 if selector.Type != "function" { 74 t.Errorf("test %d: unexpected type: '%s' != '%s'", i, selector.Type, "function") 75 } 76 if !reflect.DeepEqual(selector.Inputs, tt.args) { 77 t.Errorf("test %d: unexpected args: '%v' != '%v'", i, selector.Inputs, tt.args) 78 } 79 } 80 }