aboutsummaryrefslogtreecommitdiffstats
path: root/accounts/abi/bind/topics_test.go
blob: ac865e5b4de8c10279ec9286786258befa6b5f3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2019 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package bind

import (
    "reflect"
    "testing"

    "github.com/ethereum/go-ethereum/accounts/abi"
    "github.com/ethereum/go-ethereum/common"
)

func TestMakeTopics(t *testing.T) {
    type args struct {
        query [][]interface{}
    }
    tests := []struct {
        name    string
        args    args
        want    [][]common.Hash
        wantErr bool
    }{
        {
            "support fixed byte types, right padded to 32 bytes",
            args{[][]interface{}{{[5]byte{1, 2, 3, 4, 5}}}},
            [][]common.Hash{{common.Hash{1, 2, 3, 4, 5}}},
            false,
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got, err := makeTopics(tt.args.query...)
            if (err != nil) != tt.wantErr {
                t.Errorf("makeTopics() error = %v, wantErr %v", err, tt.wantErr)
                return
            }
            if !reflect.DeepEqual(got, tt.want) {
                t.Errorf("makeTopics() = %v, want %v", got, tt.want)
            }
        })
    }
}

func TestParseTopics(t *testing.T) {
    type bytesStruct struct {
        StaticBytes [5]byte
    }
    bytesType, _ := abi.NewType("bytes5", nil)
    type args struct {
        createObj func() interface{}
        resultObj func() interface{}
        fields    abi.Arguments
        topics    []common.Hash
    }
    tests := []struct {
        name    string
        args    args
        wantErr bool
    }{
        {
            name: "support fixed byte types, right padded to 32 bytes",
            args: args{
                createObj: func() interface{} { return &bytesStruct{} },
                resultObj: func() interface{} { return &bytesStruct{StaticBytes: [5]byte{1, 2, 3, 4, 5}} },
                fields: abi.Arguments{abi.Argument{
                    Name:    "staticBytes",
                    Type:    bytesType,
                    Indexed: true,
                }},
                topics: []common.Hash{
                    {1, 2, 3, 4, 5},
                },
            },
            wantErr: false,
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            createObj := tt.args.createObj()
            if err := parseTopics(createObj, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr {
                t.Errorf("parseTopics() error = %v, wantErr %v", err, tt.wantErr)
            }
            resultObj := tt.args.resultObj()
            if !reflect.DeepEqual(createObj, resultObj) {
                t.Errorf("parseTopics() = %v, want %v", createObj, resultObj)
            }
        })
    }
}