github.com/googgoog/go-ethereum@v1.9.7/accounts/abi/bind/topics_test.go (about)

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