github.com/theQRL/go-zond@v0.1.1/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  		switch test.method {
    88  		case "fallback":
    89  			got = abi.Fallback.String()
    90  		case "receive":
    91  			got = abi.Receive.String()
    92  		default:
    93  			got = abi.Methods[test.method].String()
    94  		}
    95  		if got != test.expectation {
    96  			t.Errorf("expected string to be %s, got %s", test.expectation, got)
    97  		}
    98  	}
    99  }
   100  
   101  func TestMethodSig(t *testing.T) {
   102  	var cases = []struct {
   103  		method string
   104  		expect string
   105  	}{
   106  		{
   107  			method: "balance",
   108  			expect: "balance()",
   109  		},
   110  		{
   111  			method: "send",
   112  			expect: "send(uint256)",
   113  		},
   114  		{
   115  			method: "transfer",
   116  			expect: "transfer(address,address,uint256)",
   117  		},
   118  		{
   119  			method: "tuple",
   120  			expect: "tuple((uint256,uint256))",
   121  		},
   122  		{
   123  			method: "tupleArray",
   124  			expect: "tupleArray((uint256,uint256)[5])",
   125  		},
   126  		{
   127  			method: "tupleSlice",
   128  			expect: "tupleSlice((uint256,uint256)[])",
   129  		},
   130  		{
   131  			method: "complexTuple",
   132  			expect: "complexTuple((uint256,uint256)[5][])",
   133  		},
   134  	}
   135  	abi, err := JSON(strings.NewReader(methoddata))
   136  	if err != nil {
   137  		t.Fatal(err)
   138  	}
   139  
   140  	for _, test := range cases {
   141  		got := abi.Methods[test.method].Sig
   142  		if got != test.expect {
   143  			t.Errorf("expected string to be %s, got %s", test.expect, got)
   144  		}
   145  	}
   146  }