github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/accounts/abi/method_test.go (about)

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