github.com/ava-labs/subnet-evm@v0.6.4/accounts/abi/method_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2018 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package abi
    28  
    29  import (
    30  	"strings"
    31  	"testing"
    32  )
    33  
    34  const methoddata = `
    35  [
    36  	{"type": "function", "name": "balance", "stateMutability": "view"},
    37  	{"type": "function", "name": "send", "inputs": [{ "name": "amount", "type": "uint256" }]},
    38  	{"type": "function", "name": "transfer", "inputs": [{"name": "from", "type": "address"}, {"name": "to", "type": "address"}, {"name": "value", "type": "uint256"}], "outputs": [{"name": "success", "type": "bool"}]},
    39  	{"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"},
    40  	{"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"},
    41  	{"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"},
    42  	{"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"},
    43  	{"stateMutability":"nonpayable","type":"fallback"},
    44  	{"stateMutability":"payable","type":"receive"}
    45  ]`
    46  
    47  func TestMethodString(t *testing.T) {
    48  	var table = []struct {
    49  		method      string
    50  		expectation string
    51  	}{
    52  		{
    53  			method:      "balance",
    54  			expectation: "function balance() view returns()",
    55  		},
    56  		{
    57  			method:      "send",
    58  			expectation: "function send(uint256 amount) returns()",
    59  		},
    60  		{
    61  			method:      "transfer",
    62  			expectation: "function transfer(address from, address to, uint256 value) returns(bool success)",
    63  		},
    64  		{
    65  			method:      "tuple",
    66  			expectation: "function tuple((uint256,uint256) a) returns()",
    67  		},
    68  		{
    69  			method:      "tupleArray",
    70  			expectation: "function tupleArray((uint256,uint256)[5] a) returns()",
    71  		},
    72  		{
    73  			method:      "tupleSlice",
    74  			expectation: "function tupleSlice((uint256,uint256)[] a) returns()",
    75  		},
    76  		{
    77  			method:      "complexTuple",
    78  			expectation: "function complexTuple((uint256,uint256)[5][] a) returns()",
    79  		},
    80  		{
    81  			method:      "fallback",
    82  			expectation: "fallback() returns()",
    83  		},
    84  		{
    85  			method:      "receive",
    86  			expectation: "receive() payable returns()",
    87  		},
    88  	}
    89  
    90  	abi, err := JSON(strings.NewReader(methoddata))
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	for _, test := range table {
    96  		var got string
    97  		switch test.method {
    98  		case "fallback":
    99  			got = abi.Fallback.String()
   100  		case "receive":
   101  			got = abi.Receive.String()
   102  		default:
   103  			got = abi.Methods[test.method].String()
   104  		}
   105  		if got != test.expectation {
   106  			t.Errorf("expected string to be %s, got %s", test.expectation, got)
   107  		}
   108  	}
   109  }
   110  
   111  func TestMethodSig(t *testing.T) {
   112  	var cases = []struct {
   113  		method string
   114  		expect string
   115  	}{
   116  		{
   117  			method: "balance",
   118  			expect: "balance()",
   119  		},
   120  		{
   121  			method: "send",
   122  			expect: "send(uint256)",
   123  		},
   124  		{
   125  			method: "transfer",
   126  			expect: "transfer(address,address,uint256)",
   127  		},
   128  		{
   129  			method: "tuple",
   130  			expect: "tuple((uint256,uint256))",
   131  		},
   132  		{
   133  			method: "tupleArray",
   134  			expect: "tupleArray((uint256,uint256)[5])",
   135  		},
   136  		{
   137  			method: "tupleSlice",
   138  			expect: "tupleSlice((uint256,uint256)[])",
   139  		},
   140  		{
   141  			method: "complexTuple",
   142  			expect: "complexTuple((uint256,uint256)[5][])",
   143  		},
   144  	}
   145  	abi, err := JSON(strings.NewReader(methoddata))
   146  	if err != nil {
   147  		t.Fatal(err)
   148  	}
   149  
   150  	for _, test := range cases {
   151  		got := abi.Methods[test.method].Sig
   152  		if got != test.expect {
   153  			t.Errorf("expected string to be %s, got %s", test.expect, got)
   154  		}
   155  	}
   156  }