method_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2018 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. package abi
  17. import (
  18. "strings"
  19. "testing"
  20. )
  21. const methoddata = `
  22. [
  23. {"type": "function", "name": "balance", "stateMutability": "view"},
  24. {"type": "function", "name": "send", "inputs": [{ "name": "amount", "type": "uint256" }]},
  25. {"type": "function", "name": "transfer", "inputs": [{"name": "from", "type": "address"}, {"name": "to", "type": "address"}, {"name": "value", "type": "uint256"}], "outputs": [{"name": "success", "type": "bool"}]},
  26. {"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"},
  27. {"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"},
  28. {"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"},
  29. {"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"},
  30. {"stateMutability":"nonpayable","type":"fallback"},
  31. {"stateMutability":"payable","type":"receive"}
  32. ]`
  33. func TestMethodString(t *testing.T) {
  34. var table = []struct {
  35. method string
  36. expectation string
  37. }{
  38. {
  39. method: "balance",
  40. expectation: "function balance() view returns()",
  41. },
  42. {
  43. method: "send",
  44. expectation: "function send(uint256 amount) returns()",
  45. },
  46. {
  47. method: "transfer",
  48. expectation: "function transfer(address from, address to, uint256 value) returns(bool success)",
  49. },
  50. {
  51. method: "tuple",
  52. expectation: "function tuple((uint256,uint256) a) returns()",
  53. },
  54. {
  55. method: "tupleArray",
  56. expectation: "function tupleArray((uint256,uint256)[5] a) returns()",
  57. },
  58. {
  59. method: "tupleSlice",
  60. expectation: "function tupleSlice((uint256,uint256)[] a) returns()",
  61. },
  62. {
  63. method: "complexTuple",
  64. expectation: "function complexTuple((uint256,uint256)[5][] a) returns()",
  65. },
  66. {
  67. method: "fallback",
  68. expectation: "fallback() returns()",
  69. },
  70. {
  71. method: "receive",
  72. expectation: "receive() payable returns()",
  73. },
  74. }
  75. abi, err := JSON(strings.NewReader(methoddata))
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. for _, test := range table {
  80. var got string
  81. if test.method == "fallback" {
  82. got = abi.Fallback.String()
  83. } else if test.method == "receive" {
  84. got = abi.Receive.String()
  85. } else {
  86. got = abi.Methods[test.method].String()
  87. }
  88. if got != test.expectation {
  89. t.Errorf("expected string to be %s, got %s", test.expectation, got)
  90. }
  91. }
  92. }
  93. func TestMethodSig(t *testing.T) {
  94. var cases = []struct {
  95. method string
  96. expect string
  97. }{
  98. {
  99. method: "balance",
  100. expect: "balance()",
  101. },
  102. {
  103. method: "send",
  104. expect: "send(uint256)",
  105. },
  106. {
  107. method: "transfer",
  108. expect: "transfer(address,address,uint256)",
  109. },
  110. {
  111. method: "tuple",
  112. expect: "tuple((uint256,uint256))",
  113. },
  114. {
  115. method: "tupleArray",
  116. expect: "tupleArray((uint256,uint256)[5])",
  117. },
  118. {
  119. method: "tupleSlice",
  120. expect: "tupleSlice((uint256,uint256)[])",
  121. },
  122. {
  123. method: "complexTuple",
  124. expect: "complexTuple((uint256,uint256)[5][])",
  125. },
  126. }
  127. abi, err := JSON(strings.NewReader(methoddata))
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. for _, test := range cases {
  132. got := abi.Methods[test.method].Sig
  133. if got != test.expect {
  134. t.Errorf("expected string to be %s, got %s", test.expect, got)
  135. }
  136. }
  137. }