github.com/ethereum/go-ethereum@v1.14.3/accounts/abi/method_test.go (about)

     1  // Copyright 2018 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  	"strings"
    21  	"testing"
    22  )
    23  
    24  const methoddata = `
    25  [
    26  	{"type": "function", "name": "balance", "stateMutability": "view"},
    27  	{"type": "function", "name": "send", "inputs": [{ "name": "amount", "type": "uint256" }]},
    28  	{"type": "function", "name": "transfer", "inputs": [{"name": "from", "type": "address"}, {"name": "to", "type": "address"}, {"name": "value", "type": "uint256"}], "outputs": [{"name": "success", "type": "bool"}]},
    29  	{"constant":false,"inputs":[{"components":[{"name":"x","type":"uint256"},{"name":"y","type":"uint256"}],"name":"a","type":"tuple"}],"name":"tuple","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},
    30  	{"constant":false,"inputs":[{"components":[{"name":"x","type":"uint256"},{"name":"y","type":"uint256"}],"name":"a","type":"tuple[]"}],"name":"tupleSlice","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},
    31  	{"constant":false,"inputs":[{"components":[{"name":"x","type":"uint256"},{"name":"y","type":"uint256"}],"name":"a","type":"tuple[5]"}],"name":"tupleArray","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},
    32  	{"constant":false,"inputs":[{"components":[{"name":"x","type":"uint256"},{"name":"y","type":"uint256"}],"name":"a","type":"tuple[5][]"}],"name":"complexTuple","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},
    33  	{"stateMutability":"nonpayable","type":"fallback"},
    34  	{"stateMutability":"payable","type":"receive"}
    35  ]`
    36  
    37  func TestMethodString(t *testing.T) {
    38  	t.Parallel()
    39  	var table = []struct {
    40  		method      string
    41  		expectation string
    42  	}{
    43  		{
    44  			method:      "balance",
    45  			expectation: "function balance() view returns()",
    46  		},
    47  		{
    48  			method:      "send",
    49  			expectation: "function send(uint256 amount) returns()",
    50  		},
    51  		{
    52  			method:      "transfer",
    53  			expectation: "function transfer(address from, address to, uint256 value) returns(bool success)",
    54  		},
    55  		{
    56  			method:      "tuple",
    57  			expectation: "function tuple((uint256,uint256) a) returns()",
    58  		},
    59  		{
    60  			method:      "tupleArray",
    61  			expectation: "function tupleArray((uint256,uint256)[5] a) returns()",
    62  		},
    63  		{
    64  			method:      "tupleSlice",
    65  			expectation: "function tupleSlice((uint256,uint256)[] a) returns()",
    66  		},
    67  		{
    68  			method:      "complexTuple",
    69  			expectation: "function complexTuple((uint256,uint256)[5][] a) returns()",
    70  		},
    71  		{
    72  			method:      "fallback",
    73  			expectation: "fallback() returns()",
    74  		},
    75  		{
    76  			method:      "receive",
    77  			expectation: "receive() payable returns()",
    78  		},
    79  	}
    80  
    81  	abi, err := JSON(strings.NewReader(methoddata))
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  
    86  	for _, test := range table {
    87  		var got string
    88  		switch test.method {
    89  		case "fallback":
    90  			got = abi.Fallback.String()
    91  		case "receive":
    92  			got = abi.Receive.String()
    93  		default:
    94  			got = abi.Methods[test.method].String()
    95  		}
    96  		if got != test.expectation {
    97  			t.Errorf("expected string to be %s, got %s", test.expectation, got)
    98  		}
    99  	}
   100  }
   101  
   102  func TestMethodSig(t *testing.T) {
   103  	t.Parallel()
   104  	var cases = []struct {
   105  		method string
   106  		expect string
   107  	}{
   108  		{
   109  			method: "balance",
   110  			expect: "balance()",
   111  		},
   112  		{
   113  			method: "send",
   114  			expect: "send(uint256)",
   115  		},
   116  		{
   117  			method: "transfer",
   118  			expect: "transfer(address,address,uint256)",
   119  		},
   120  		{
   121  			method: "tuple",
   122  			expect: "tuple((uint256,uint256))",
   123  		},
   124  		{
   125  			method: "tupleArray",
   126  			expect: "tupleArray((uint256,uint256)[5])",
   127  		},
   128  		{
   129  			method: "tupleSlice",
   130  			expect: "tupleSlice((uint256,uint256)[])",
   131  		},
   132  		{
   133  			method: "complexTuple",
   134  			expect: "complexTuple((uint256,uint256)[5][])",
   135  		},
   136  	}
   137  	abi, err := JSON(strings.NewReader(methoddata))
   138  	if err != nil {
   139  		t.Fatal(err)
   140  	}
   141  
   142  	for _, test := range cases {
   143  		got := abi.Methods[test.method].Sig
   144  		if got != test.expect {
   145  			t.Errorf("expected string to be %s, got %s", test.expect, got)
   146  		}
   147  	}
   148  }