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