github.com/Cleverse/go-ethereum@v0.0.0-20220927095127-45113064e7f2/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  	var table = []struct {
    39  		method      string
    40  		expectation string
    41  	}{
    42  		{
    43  			method:      "balance",
    44  			expectation: "function balance() view returns()",
    45  		},
    46  		{
    47  			method:      "send",
    48  			expectation: "function send(uint256 amount) returns()",
    49  		},
    50  		{
    51  			method:      "transfer",
    52  			expectation: "function transfer(address from, address to, uint256 value) returns(bool success)",
    53  		},
    54  		{
    55  			method:      "tuple",
    56  			expectation: "function tuple((uint256,uint256) a) returns()",
    57  		},
    58  		{
    59  			method:      "tupleArray",
    60  			expectation: "function tupleArray((uint256,uint256)[5] a) returns()",
    61  		},
    62  		{
    63  			method:      "tupleSlice",
    64  			expectation: "function tupleSlice((uint256,uint256)[] a) returns()",
    65  		},
    66  		{
    67  			method:      "complexTuple",
    68  			expectation: "function complexTuple((uint256,uint256)[5][] a) returns()",
    69  		},
    70  		{
    71  			method:      "fallback",
    72  			expectation: "fallback() returns()",
    73  		},
    74  		{
    75  			method:      "receive",
    76  			expectation: "receive() payable returns()",
    77  		},
    78  	}
    79  
    80  	abi, err := JSON(strings.NewReader(methoddata))
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	for _, test := range table {
    86  		var got string
    87  		if test.method == "fallback" {
    88  			got = abi.Fallback.String()
    89  		} else if test.method == "receive" {
    90  			got = abi.Receive.String()
    91  		} else {
    92  			got = abi.Methods[test.method].String()
    93  		}
    94  		if got != test.expectation {
    95  			t.Errorf("expected string to be %s, got %s", test.expectation, got)
    96  		}
    97  	}
    98  }
    99  
   100  func TestMethodSig(t *testing.T) {
   101  	var cases = []struct {
   102  		method string
   103  		expect string
   104  	}{
   105  		{
   106  			method: "balance",
   107  			expect: "balance()",
   108  		},
   109  		{
   110  			method: "send",
   111  			expect: "send(uint256)",
   112  		},
   113  		{
   114  			method: "transfer",
   115  			expect: "transfer(address,address,uint256)",
   116  		},
   117  		{
   118  			method: "tuple",
   119  			expect: "tuple((uint256,uint256))",
   120  		},
   121  		{
   122  			method: "tupleArray",
   123  			expect: "tupleArray((uint256,uint256)[5])",
   124  		},
   125  		{
   126  			method: "tupleSlice",
   127  			expect: "tupleSlice((uint256,uint256)[])",
   128  		},
   129  		{
   130  			method: "complexTuple",
   131  			expect: "complexTuple((uint256,uint256)[5][])",
   132  		},
   133  	}
   134  	abi, err := JSON(strings.NewReader(methoddata))
   135  	if err != nil {
   136  		t.Fatal(err)
   137  	}
   138  
   139  	for _, test := range cases {
   140  		got := abi.Methods[test.method].Sig
   141  		if got != test.expect {
   142  			t.Errorf("expected string to be %s, got %s", test.expect, got)
   143  		}
   144  	}
   145  }