github.com/bloxroute-labs/bor@v0.1.4/accounts/abi/method_test.go (about) 1 // Copyright 2016 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", "constant": true }, 27 {"type": "function", "name": "send", "constant": false, "inputs": [{ "name": "amount", "type": "uint256" }]}, 28 {"type": "function", "name": "transfer", "constant": false, "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 ]` 34 35 func TestMethodString(t *testing.T) { 36 var table = []struct { 37 method string 38 expectation string 39 }{ 40 { 41 method: "balance", 42 expectation: "function balance() constant returns()", 43 }, 44 { 45 method: "send", 46 expectation: "function send(uint256 amount) returns()", 47 }, 48 { 49 method: "transfer", 50 expectation: "function transfer(address from, address to, uint256 value) returns(bool success)", 51 }, 52 { 53 method: "tuple", 54 expectation: "function tuple((uint256,uint256) a) returns()", 55 }, 56 { 57 method: "tupleArray", 58 expectation: "function tupleArray((uint256,uint256)[5] a) returns()", 59 }, 60 { 61 method: "tupleSlice", 62 expectation: "function tupleSlice((uint256,uint256)[] a) returns()", 63 }, 64 { 65 method: "complexTuple", 66 expectation: "function complexTuple((uint256,uint256)[5][] a) returns()", 67 }, 68 } 69 70 abi, err := JSON(strings.NewReader(methoddata)) 71 if err != nil { 72 t.Fatal(err) 73 } 74 75 for _, test := range table { 76 got := abi.Methods[test.method].String() 77 if got != test.expectation { 78 t.Errorf("expected string to be %s, got %s", test.expectation, got) 79 } 80 } 81 } 82 83 func TestMethodSig(t *testing.T) { 84 var cases = []struct { 85 method string 86 expect string 87 }{ 88 { 89 method: "balance", 90 expect: "balance()", 91 }, 92 { 93 method: "send", 94 expect: "send(uint256)", 95 }, 96 { 97 method: "transfer", 98 expect: "transfer(address,address,uint256)", 99 }, 100 { 101 method: "tuple", 102 expect: "tuple((uint256,uint256))", 103 }, 104 { 105 method: "tupleArray", 106 expect: "tupleArray((uint256,uint256)[5])", 107 }, 108 { 109 method: "tupleSlice", 110 expect: "tupleSlice((uint256,uint256)[])", 111 }, 112 { 113 method: "complexTuple", 114 expect: "complexTuple((uint256,uint256)[5][])", 115 }, 116 } 117 abi, err := JSON(strings.NewReader(methoddata)) 118 if err != nil { 119 t.Fatal(err) 120 } 121 122 for _, test := range cases { 123 got := abi.Methods[test.method].Sig() 124 if got != test.expect { 125 t.Errorf("expected string to be %s, got %s", test.expect, got) 126 } 127 } 128 }