selector_parser_test.go 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2022 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. "fmt"
  19. "log"
  20. "reflect"
  21. "testing"
  22. )
  23. func TestParseSelector(t *testing.T) {
  24. mkType := func(types ...interface{}) []ArgumentMarshaling {
  25. var result []ArgumentMarshaling
  26. for i, typeOrComponents := range types {
  27. name := fmt.Sprintf("name%d", i)
  28. if typeName, ok := typeOrComponents.(string); ok {
  29. result = append(result, ArgumentMarshaling{name, typeName, typeName, nil, false})
  30. } else if components, ok := typeOrComponents.([]ArgumentMarshaling); ok {
  31. result = append(result, ArgumentMarshaling{name, "tuple", "tuple", components, false})
  32. } else if components, ok := typeOrComponents.([][]ArgumentMarshaling); ok {
  33. result = append(result, ArgumentMarshaling{name, "tuple[]", "tuple[]", components[0], false})
  34. } else {
  35. log.Fatalf("unexpected type %T", typeOrComponents)
  36. }
  37. }
  38. return result
  39. }
  40. tests := []struct {
  41. input string
  42. name string
  43. args []ArgumentMarshaling
  44. }{
  45. {"noargs()", "noargs", []ArgumentMarshaling{}},
  46. {"simple(uint256,uint256,uint256)", "simple", mkType("uint256", "uint256", "uint256")},
  47. {"other(uint256,address)", "other", mkType("uint256", "address")},
  48. {"withArray(uint256[],address[2],uint8[4][][5])", "withArray", mkType("uint256[]", "address[2]", "uint8[4][][5]")},
  49. {"singleNest(bytes32,uint8,(uint256,uint256),address)", "singleNest", mkType("bytes32", "uint8", mkType("uint256", "uint256"), "address")},
  50. {"multiNest(address,(uint256[],uint256),((address,bytes32),uint256))", "multiNest",
  51. mkType("address", mkType("uint256[]", "uint256"), mkType(mkType("address", "bytes32"), "uint256"))},
  52. {"arrayNest((uint256,uint256)[],bytes32)", "arrayNest", mkType([][]ArgumentMarshaling{mkType("uint256", "uint256")}, "bytes32")},
  53. {"multiArrayNest((uint256,uint256)[],(uint256,uint256)[])", "multiArrayNest",
  54. mkType([][]ArgumentMarshaling{mkType("uint256", "uint256")}, [][]ArgumentMarshaling{mkType("uint256", "uint256")})},
  55. {"singleArrayNestAndArray((uint256,uint256)[],bytes32[])", "singleArrayNestAndArray",
  56. mkType([][]ArgumentMarshaling{mkType("uint256", "uint256")}, "bytes32[]")},
  57. {"singleArrayNestWithArrayAndArray((uint256[],address[2],uint8[4][][5])[],bytes32[])", "singleArrayNestWithArrayAndArray",
  58. mkType([][]ArgumentMarshaling{mkType("uint256[]", "address[2]", "uint8[4][][5]")}, "bytes32[]")},
  59. }
  60. for i, tt := range tests {
  61. selector, err := ParseSelector(tt.input)
  62. if err != nil {
  63. t.Errorf("test %d: failed to parse selector '%v': %v", i, tt.input, err)
  64. }
  65. if selector.Name != tt.name {
  66. t.Errorf("test %d: unexpected function name: '%s' != '%s'", i, selector.Name, tt.name)
  67. }
  68. if selector.Type != "function" {
  69. t.Errorf("test %d: unexpected type: '%s' != '%s'", i, selector.Type, "function")
  70. }
  71. if !reflect.DeepEqual(selector.Inputs, tt.args) {
  72. t.Errorf("test %d: unexpected args: '%v' != '%v'", i, selector.Inputs, tt.args)
  73. }
  74. }
  75. }