github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/accounts/abi/bind/topics_test.go (about)

     1  package bind
     2  
     3  import (
     4  	"math/big"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/neatio-net/neatio/chain/accounts/abi"
     9  	"github.com/neatio-net/neatio/utilities/common"
    10  )
    11  
    12  func TestMakeTopics(t *testing.T) {
    13  	type args struct {
    14  		query [][]interface{}
    15  	}
    16  	tests := []struct {
    17  		name    string
    18  		args    args
    19  		want    [][]common.Hash
    20  		wantErr bool
    21  	}{
    22  		{
    23  			"support fixed byte types, right padded to 32 bytes",
    24  			args{[][]interface{}{{[5]byte{1, 2, 3, 4, 5}}}},
    25  			[][]common.Hash{{common.Hash{1, 2, 3, 4, 5}}},
    26  			false,
    27  		},
    28  	}
    29  	for _, tt := range tests {
    30  		t.Run(tt.name, func(t *testing.T) {
    31  			got, err := makeTopics(tt.args.query...)
    32  			if (err != nil) != tt.wantErr {
    33  				t.Errorf("makeTopics() error = %v, wantErr %v", err, tt.wantErr)
    34  				return
    35  			}
    36  			if !reflect.DeepEqual(got, tt.want) {
    37  				t.Errorf("makeTopics() = %v, want %v", got, tt.want)
    38  			}
    39  		})
    40  	}
    41  }
    42  
    43  type args struct {
    44  	createObj func() interface{}
    45  	resultObj func() interface{}
    46  	resultMap func() map[string]interface{}
    47  	fields    abi.Arguments
    48  	topics    []common.Hash
    49  }
    50  
    51  type bytesStruct struct {
    52  	StaticBytes [5]byte
    53  }
    54  type int8Struct struct {
    55  	Int8Value int8
    56  }
    57  type int256Struct struct {
    58  	Int256Value *big.Int
    59  }
    60  
    61  type topicTest struct {
    62  	name    string
    63  	args    args
    64  	wantErr bool
    65  }
    66  
    67  func setupTopicsTests() []topicTest {
    68  	bytesType, _ := abi.NewType("bytes5", "", nil)
    69  	int8Type, _ := abi.NewType("int8", "", nil)
    70  	int256Type, _ := abi.NewType("int256", "", nil)
    71  
    72  	tests := []topicTest{
    73  		{
    74  			name: "support fixed byte types, right padded to 32 bytes",
    75  			args: args{
    76  				createObj: func() interface{} { return &bytesStruct{} },
    77  				resultObj: func() interface{} { return &bytesStruct{StaticBytes: [5]byte{1, 2, 3, 4, 5}} },
    78  				resultMap: func() map[string]interface{} {
    79  					return map[string]interface{}{"staticBytes": [5]byte{1, 2, 3, 4, 5}}
    80  				},
    81  				fields: abi.Arguments{abi.Argument{
    82  					Name:    "staticBytes",
    83  					Type:    bytesType,
    84  					Indexed: true,
    85  				}},
    86  				topics: []common.Hash{
    87  					{1, 2, 3, 4, 5},
    88  				},
    89  			},
    90  			wantErr: false,
    91  		},
    92  		{
    93  			name: "int8 with negative value",
    94  			args: args{
    95  				createObj: func() interface{} { return &int8Struct{} },
    96  				resultObj: func() interface{} { return &int8Struct{Int8Value: -1} },
    97  				resultMap: func() map[string]interface{} {
    98  					return map[string]interface{}{"int8Value": int8(-1)}
    99  				},
   100  				fields: abi.Arguments{abi.Argument{
   101  					Name:    "int8Value",
   102  					Type:    int8Type,
   103  					Indexed: true,
   104  				}},
   105  				topics: []common.Hash{
   106  					{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   107  						255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
   108  				},
   109  			},
   110  			wantErr: false,
   111  		},
   112  		{
   113  			name: "int256 with negative value",
   114  			args: args{
   115  				createObj: func() interface{} { return &int256Struct{} },
   116  				resultObj: func() interface{} { return &int256Struct{Int256Value: big.NewInt(-1)} },
   117  				resultMap: func() map[string]interface{} {
   118  					return map[string]interface{}{"int256Value": big.NewInt(-1)}
   119  				},
   120  				fields: abi.Arguments{abi.Argument{
   121  					Name:    "int256Value",
   122  					Type:    int256Type,
   123  					Indexed: true,
   124  				}},
   125  				topics: []common.Hash{
   126  					{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   127  						255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
   128  				},
   129  			},
   130  			wantErr: false,
   131  		},
   132  	}
   133  
   134  	return tests
   135  }
   136  
   137  func TestParseTopics(t *testing.T) {
   138  	tests := setupTopicsTests()
   139  
   140  	for _, tt := range tests {
   141  		t.Run(tt.name, func(t *testing.T) {
   142  			createObj := tt.args.createObj()
   143  			if err := parseTopics(createObj, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr {
   144  				t.Errorf("parseTopics() error = %v, wantErr %v", err, tt.wantErr)
   145  			}
   146  			resultObj := tt.args.resultObj()
   147  			if !reflect.DeepEqual(createObj, resultObj) {
   148  				t.Errorf("parseTopics() = %v, want %v", createObj, resultObj)
   149  			}
   150  		})
   151  	}
   152  }
   153  
   154  func TestParseTopicsIntoMap(t *testing.T) {
   155  	tests := setupTopicsTests()
   156  
   157  	for _, tt := range tests {
   158  		t.Run(tt.name, func(t *testing.T) {
   159  			outMap := make(map[string]interface{})
   160  			if err := parseTopicsIntoMap(outMap, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr {
   161  				t.Errorf("parseTopicsIntoMap() error = %v, wantErr %v", err, tt.wantErr)
   162  			}
   163  			resultMap := tt.args.resultMap()
   164  			if !reflect.DeepEqual(outMap, resultMap) {
   165  				t.Errorf("parseTopicsIntoMap() = %v, want %v", outMap, resultMap)
   166  			}
   167  		})
   168  	}
   169  }