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