github.com/benorgera/go-ethereum@v1.10.18-0.20220401011646-b3f57b1a73ba/accounts/abi/selector_parser_test.go (about)

     1  package abi
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  func TestParseSelector(t *testing.T) {
    11  	mkType := func(types ...interface{}) []ArgumentMarshaling {
    12  		var result []ArgumentMarshaling
    13  		for i, typeOrComponents := range types {
    14  			name := fmt.Sprintf("name%d", i)
    15  			if typeName, ok := typeOrComponents.(string); ok {
    16  				result = append(result, ArgumentMarshaling{name, typeName, typeName, nil, false})
    17  			} else if components, ok := typeOrComponents.([]ArgumentMarshaling); ok {
    18  				result = append(result, ArgumentMarshaling{name, "tuple", "tuple", components, false})
    19  			} else {
    20  				log.Fatalf("unexpected type %T", typeOrComponents)
    21  			}
    22  		}
    23  		return result
    24  	}
    25  	tests := []struct {
    26  		input string
    27  		name  string
    28  		args  []ArgumentMarshaling
    29  	}{
    30  		{"noargs()", "noargs", []ArgumentMarshaling{}},
    31  		{"simple(uint256,uint256,uint256)", "simple", mkType("uint256", "uint256", "uint256")},
    32  		{"other(uint256,address)", "other", mkType("uint256", "address")},
    33  		{"withArray(uint256[],address[2],uint8[4][][5])", "withArray", mkType("uint256[]", "address[2]", "uint8[4][][5]")},
    34  		{"singleNest(bytes32,uint8,(uint256,uint256),address)", "singleNest", mkType("bytes32", "uint8", mkType("uint256", "uint256"), "address")},
    35  		{"multiNest(address,(uint256[],uint256),((address,bytes32),uint256))", "multiNest",
    36  			mkType("address", mkType("uint256[]", "uint256"), mkType(mkType("address", "bytes32"), "uint256"))},
    37  	}
    38  	for i, tt := range tests {
    39  		selector, err := ParseSelector(tt.input)
    40  		if err != nil {
    41  			t.Errorf("test %d: failed to parse selector '%v': %v", i, tt.input, err)
    42  		}
    43  		if selector.Name != tt.name {
    44  			t.Errorf("test %d: unexpected function name: '%s' != '%s'", i, selector.Name, tt.name)
    45  		}
    46  
    47  		if selector.Type != "function" {
    48  			t.Errorf("test %d: unexpected type: '%s' != '%s'", i, selector.Type, "function")
    49  		}
    50  		if !reflect.DeepEqual(selector.Inputs, tt.args) {
    51  			t.Errorf("test %d: unexpected args: '%v' != '%v'", i, selector.Inputs, tt.args)
    52  		}
    53  	}
    54  }