topics_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2019 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 bind
  17. import (
  18. "reflect"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/accounts/abi"
  21. "github.com/ethereum/go-ethereum/common"
  22. )
  23. func TestMakeTopics(t *testing.T) {
  24. type args struct {
  25. query [][]interface{}
  26. }
  27. tests := []struct {
  28. name string
  29. args args
  30. want [][]common.Hash
  31. wantErr bool
  32. }{
  33. {
  34. "support fixed byte types, right padded to 32 bytes",
  35. args{[][]interface{}{{[5]byte{1, 2, 3, 4, 5}}}},
  36. [][]common.Hash{{common.Hash{1, 2, 3, 4, 5}}},
  37. false,
  38. },
  39. }
  40. for _, tt := range tests {
  41. t.Run(tt.name, func(t *testing.T) {
  42. got, err := makeTopics(tt.args.query...)
  43. if (err != nil) != tt.wantErr {
  44. t.Errorf("makeTopics() error = %v, wantErr %v", err, tt.wantErr)
  45. return
  46. }
  47. if !reflect.DeepEqual(got, tt.want) {
  48. t.Errorf("makeTopics() = %v, want %v", got, tt.want)
  49. }
  50. })
  51. }
  52. }
  53. func TestParseTopics(t *testing.T) {
  54. type bytesStruct struct {
  55. StaticBytes [5]byte
  56. }
  57. bytesType, _ := abi.NewType("bytes5", nil)
  58. type args struct {
  59. createObj func() interface{}
  60. resultObj func() interface{}
  61. fields abi.Arguments
  62. topics []common.Hash
  63. }
  64. tests := []struct {
  65. name string
  66. args args
  67. wantErr bool
  68. }{
  69. {
  70. name: "support fixed byte types, right padded to 32 bytes",
  71. args: args{
  72. createObj: func() interface{} { return &bytesStruct{} },
  73. resultObj: func() interface{} { return &bytesStruct{StaticBytes: [5]byte{1, 2, 3, 4, 5}} },
  74. fields: abi.Arguments{abi.Argument{
  75. Name: "staticBytes",
  76. Type: bytesType,
  77. Indexed: true,
  78. }},
  79. topics: []common.Hash{
  80. {1, 2, 3, 4, 5},
  81. },
  82. },
  83. wantErr: false,
  84. },
  85. }
  86. for _, tt := range tests {
  87. t.Run(tt.name, func(t *testing.T) {
  88. createObj := tt.args.createObj()
  89. if err := parseTopics(createObj, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr {
  90. t.Errorf("parseTopics() error = %v, wantErr %v", err, tt.wantErr)
  91. }
  92. resultObj := tt.args.resultObj()
  93. if !reflect.DeepEqual(createObj, resultObj) {
  94. t.Errorf("parseTopics() = %v, want %v", createObj, resultObj)
  95. }
  96. })
  97. }
  98. }