github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/accounts/abi/bind/topics_test.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package bind
    19  
    20  import (
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/AigarNetwork/aigar/accounts/abi"
    25  	"github.com/AigarNetwork/aigar/common"
    26  )
    27  
    28  func TestMakeTopics(t *testing.T) {
    29  	type args struct {
    30  		query [][]interface{}
    31  	}
    32  	tests := []struct {
    33  		name    string
    34  		args    args
    35  		want    [][]common.Hash
    36  		wantErr bool
    37  	}{
    38  		{
    39  			"support fixed byte types, right padded to 32 bytes",
    40  			args{[][]interface{}{{[5]byte{1, 2, 3, 4, 5}}}},
    41  			[][]common.Hash{{common.Hash{1, 2, 3, 4, 5}}},
    42  			false,
    43  		},
    44  	}
    45  	for _, tt := range tests {
    46  		t.Run(tt.name, func(t *testing.T) {
    47  			got, err := makeTopics(tt.args.query...)
    48  			if (err != nil) != tt.wantErr {
    49  				t.Errorf("makeTopics() error = %v, wantErr %v", err, tt.wantErr)
    50  				return
    51  			}
    52  			if !reflect.DeepEqual(got, tt.want) {
    53  				t.Errorf("makeTopics() = %v, want %v", got, tt.want)
    54  			}
    55  		})
    56  	}
    57  }
    58  
    59  func TestParseTopics(t *testing.T) {
    60  	type bytesStruct struct {
    61  		StaticBytes [5]byte
    62  	}
    63  	bytesType, _ := abi.NewType("bytes5", "", nil)
    64  	type args struct {
    65  		createObj func() interface{}
    66  		resultObj func() interface{}
    67  		fields    abi.Arguments
    68  		topics    []common.Hash
    69  	}
    70  	tests := []struct {
    71  		name    string
    72  		args    args
    73  		wantErr bool
    74  	}{
    75  		{
    76  			name: "support fixed byte types, right padded to 32 bytes",
    77  			args: args{
    78  				createObj: func() interface{} { return &bytesStruct{} },
    79  				resultObj: func() interface{} { return &bytesStruct{StaticBytes: [5]byte{1, 2, 3, 4, 5}} },
    80  				fields: abi.Arguments{abi.Argument{
    81  					Name:    "staticBytes",
    82  					Type:    bytesType,
    83  					Indexed: true,
    84  				}},
    85  				topics: []common.Hash{
    86  					{1, 2, 3, 4, 5},
    87  				},
    88  			},
    89  			wantErr: false,
    90  		},
    91  	}
    92  	for _, tt := range tests {
    93  		t.Run(tt.name, func(t *testing.T) {
    94  			createObj := tt.args.createObj()
    95  			if err := parseTopics(createObj, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr {
    96  				t.Errorf("parseTopics() error = %v, wantErr %v", err, tt.wantErr)
    97  			}
    98  			resultObj := tt.args.resultObj()
    99  			if !reflect.DeepEqual(createObj, resultObj) {
   100  				t.Errorf("parseTopics() = %v, want %v", createObj, resultObj)
   101  			}
   102  		})
   103  	}
   104  }