github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/accounts/abi/method_test.go (about) 1 package abi 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 const methoddata = ` 9 [ 10 {"type": "function", "name": "balance", "constant": true }, 11 {"type": "function", "name": "send", "constant": false, "inputs": [{ "name": "amount", "type": "uint256" }]}, 12 {"type": "function", "name": "transfer", "constant": false, "inputs": [{"name": "from", "type": "address"}, {"name": "to", "type": "address"}, {"name": "value", "type": "uint256"}], "outputs": [{"name": "success", "type": "bool"}]}, 13 {"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"}, 14 {"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"}, 15 {"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"}, 16 {"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"} 17 ]` 18 19 func TestMethodString(t *testing.T) { 20 var table = []struct { 21 method string 22 expectation string 23 }{ 24 { 25 method: "balance", 26 expectation: "function balance() constant returns()", 27 }, 28 { 29 method: "send", 30 expectation: "function send(uint256 amount) returns()", 31 }, 32 { 33 method: "transfer", 34 expectation: "function transfer(address from, address to, uint256 value) returns(bool success)", 35 }, 36 { 37 method: "tuple", 38 expectation: "function tuple((uint256,uint256) a) returns()", 39 }, 40 { 41 method: "tupleArray", 42 expectation: "function tupleArray((uint256,uint256)[5] a) returns()", 43 }, 44 { 45 method: "tupleSlice", 46 expectation: "function tupleSlice((uint256,uint256)[] a) returns()", 47 }, 48 { 49 method: "complexTuple", 50 expectation: "function complexTuple((uint256,uint256)[5][] a) returns()", 51 }, 52 } 53 54 abi, err := JSON(strings.NewReader(methoddata)) 55 if err != nil { 56 t.Fatal(err) 57 } 58 59 for _, test := range table { 60 got := abi.Methods[test.method].String() 61 if got != test.expectation { 62 t.Errorf("expected string to be %s, got %s", test.expectation, got) 63 } 64 } 65 } 66 67 func TestMethodSig(t *testing.T) { 68 var cases = []struct { 69 method string 70 expect string 71 }{ 72 { 73 method: "balance", 74 expect: "balance()", 75 }, 76 { 77 method: "send", 78 expect: "send(uint256)", 79 }, 80 { 81 method: "transfer", 82 expect: "transfer(address,address,uint256)", 83 }, 84 { 85 method: "tuple", 86 expect: "tuple((uint256,uint256))", 87 }, 88 { 89 method: "tupleArray", 90 expect: "tupleArray((uint256,uint256)[5])", 91 }, 92 { 93 method: "tupleSlice", 94 expect: "tupleSlice((uint256,uint256)[])", 95 }, 96 { 97 method: "complexTuple", 98 expect: "complexTuple((uint256,uint256)[5][])", 99 }, 100 } 101 abi, err := JSON(strings.NewReader(methoddata)) 102 if err != nil { 103 t.Fatal(err) 104 } 105 106 for _, test := range cases { 107 got := abi.Methods[test.method].Sig() 108 if got != test.expect { 109 t.Errorf("expected string to be %s, got %s", test.expect, got) 110 } 111 } 112 }